From 129cbad0faeb7d46b52e7df19a0e23f692753dd2 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Fri, 5 Aug 2016 21:25:56 -0700 Subject: [PATCH] tests. --- bar_chart.go | 6 ++-- bar_chart_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/bar_chart.go b/bar_chart.go index 86bb61f..6a74083 100644 --- a/bar_chart.go +++ b/bar_chart.go @@ -309,7 +309,7 @@ func (bc BarChart) calculateEffectiveBarSpacing(canvasBox Box) int { if totalWithBaseSpacing > canvasBox.Width() { lessBarWidths := canvasBox.Width() - (len(bc.Bars) * bc.GetBarWidth()) if lessBarWidths > 0 { - return int(math.Floor(float64(lessBarWidths) / float64(len(bc.Bars)))) + return int(math.Ceil(float64(lessBarWidths) / float64(len(bc.Bars)))) } return 0 } @@ -321,7 +321,7 @@ func (bc BarChart) calculateEffectiveBarWidth(canvasBox Box, spacing int) int { if totalWithBaseWidth > canvasBox.Width() { totalLessBarSpacings := canvasBox.Width() - (len(bc.Bars) * spacing) if totalLessBarSpacings > 0 { - return int(math.Floor(float64(totalLessBarSpacings) / float64(len(bc.Bars)))) + return int(math.Ceil(float64(totalLessBarSpacings) / float64(len(bc.Bars)))) } return 0 } @@ -329,7 +329,7 @@ func (bc BarChart) calculateEffectiveBarWidth(canvasBox Box, spacing int) int { } func (bc BarChart) calculateTotalBarWidth(barWidth, spacing int) int { - return len(bc.Bars) * (bc.GetBarWidth() + spacing) + return len(bc.Bars) * (barWidth + spacing) } func (bc BarChart) calculateScaledTotalWidth(canvasBox Box) (width, spacing, total int) { diff --git a/bar_chart_test.go b/bar_chart_test.go index a2ae9c1..aea719c 100644 --- a/bar_chart_test.go +++ b/bar_chart_test.go @@ -172,3 +172,79 @@ func TestBarChartGetValueFormatters(t *testing.T) { bc.YAxis.ValueFormatter = func(_ interface{}) string { return "test" } assert.Equal("test", bc.getValueFormatters()(1234)) } + +func TestBarChartGetAxesTicks(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{ + Bars: []Value{ + {Value: 1.0}, + {Value: 2.0}, + {Value: 3.0}, + }, + } + + r, err := PNG(128, 128) + assert.Nil(err) + yr := bc.getRanges() + yf := bc.getValueFormatters() + + ticks := bc.getAxesTicks(r, yr, yf) + assert.Empty(ticks) + + bc.YAxis.Style.Show = true + ticks = bc.getAxesTicks(r, yr, yf) + assert.Len(ticks, 2) +} + +func TestBarChartCalculateEffectiveBarSpacing(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{ + Width: 1024, + BarWidth: 10, + Bars: []Value{ + {Value: 1.0, Label: "One"}, + {Value: 2.0, Label: "Two"}, + {Value: 3.0, Label: "Three"}, + {Value: 4.0, Label: "Four"}, + {Value: 5.0, Label: "Five"}, + }, + } + + spacing := bc.calculateEffectiveBarSpacing(bc.box()) + assert.NotZero(spacing) + + bc.BarWidth = 250 + spacing = bc.calculateEffectiveBarSpacing(bc.box()) + assert.Zero(spacing) +} + +func TestBarChartCalculateEffectiveBarWidth(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{ + Width: 1024, + BarWidth: 10, + Bars: []Value{ + {Value: 1.0, Label: "One"}, + {Value: 2.0, Label: "Two"}, + {Value: 3.0, Label: "Three"}, + {Value: 4.0, Label: "Four"}, + {Value: 5.0, Label: "Five"}, + }, + } + + spacing := bc.calculateEffectiveBarSpacing(bc.box()) + assert.NotZero(spacing) + + barWidth := bc.calculateEffectiveBarWidth(bc.box(), spacing) + assert.Equal(10, barWidth) + + bc.BarWidth = 250 + spacing = bc.calculateEffectiveBarSpacing(bc.box()) + barWidth = bc.calculateEffectiveBarWidth(bc.box(), spacing) + assert.Equal(199, barWidth) + + assert.Equal(1024, bc.calculateTotalBarWidth(barWidth, spacing)) +}