diff --git a/bar_chart.go b/bar_chart.go index 9dc59a3..86bb61f 100644 --- a/bar_chart.go +++ b/bar_chart.go @@ -35,11 +35,8 @@ type BarChart struct { } // GetDPI returns the dpi for the chart. -func (bc BarChart) GetDPI(defaults ...float64) float64 { +func (bc BarChart) GetDPI() float64 { if bc.DPI == 0 { - if len(defaults) > 0 { - return defaults[0] - } return DefaultDPI } return bc.DPI @@ -64,7 +61,7 @@ func (bc BarChart) GetWidth() int { // GetHeight returns the chart height or the default value. func (bc BarChart) GetHeight() int { if bc.Height == 0 { - return DefaultChartWidth + return DefaultChartHeight } return bc.Height } @@ -72,7 +69,7 @@ func (bc BarChart) GetHeight() int { // GetBarSpacing returns the spacing between bars. func (bc BarChart) GetBarSpacing() int { if bc.BarSpacing == 0 { - return 100 + return DefaultBarSpacing } return bc.BarSpacing } @@ -80,7 +77,7 @@ func (bc BarChart) GetBarSpacing() int { // GetBarWidth returns the default bar width. func (bc BarChart) GetBarWidth() int { if bc.BarWidth == 0 { - return 40 + return DefaultBarWidth } return bc.BarWidth } @@ -103,7 +100,7 @@ func (bc BarChart) Render(rp RendererProvider, w io.Writer) error { } bc.defaultFont = defaultFont } - r.SetDPI(bc.GetDPI(DefaultDPI)) + r.SetDPI(bc.GetDPI()) bc.drawBackground(r) diff --git a/bar_chart_test.go b/bar_chart_test.go new file mode 100644 index 0000000..40e4c98 --- /dev/null +++ b/bar_chart_test.go @@ -0,0 +1,144 @@ +package chart + +import ( + "bytes" + "math" + "testing" + + assert "github.com/blendlabs/go-assert" +) + +func TestBarChartProps(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{} + + assert.Equal(DefaultDPI, bc.GetDPI()) + bc.DPI = 100 + assert.Equal(100, bc.GetDPI()) + + assert.Nil(bc.GetFont()) + f, err := GetDefaultFont() + assert.Nil(err) + bc.Font = f + assert.NotNil(bc.GetFont()) + + assert.Equal(DefaultChartWidth, bc.GetWidth()) + bc.Width = DefaultChartWidth - 1 + assert.Equal(DefaultChartWidth-1, bc.GetWidth()) + + assert.Equal(DefaultChartHeight, bc.GetHeight()) + bc.Height = DefaultChartHeight - 1 + assert.Equal(DefaultChartHeight-1, bc.GetHeight()) + + assert.Equal(DefaultBarSpacing, bc.GetBarSpacing()) + bc.BarSpacing = 150 + assert.Equal(150, bc.GetBarSpacing()) + + assert.Equal(DefaultBarWidth, bc.GetBarWidth()) + bc.BarWidth = 75 + assert.Equal(75, bc.GetBarWidth()) +} + +func TestBarChartRenderNoBars(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{} + err := bc.Render(PNG, bytes.NewBuffer([]byte{})) + assert.NotNil(err) +} + +func TestBarChartGetRanges(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{} + + yr := bc.getRanges() + assert.NotNil(yr) + assert.False(yr.IsZero()) + + assert.Equal(-math.MaxFloat64, yr.GetMax()) + assert.Equal(math.MaxFloat64, yr.GetMin()) +} + +func TestBarChartGetRangesBarsMinMax(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{ + Bars: []Value{ + {Value: 1.0}, + {Value: 10.0}, + }, + } + + yr := bc.getRanges() + assert.NotNil(yr) + assert.False(yr.IsZero()) + + assert.Equal(10, yr.GetMax()) + assert.Equal(1, yr.GetMin()) +} + +func TestBarChartGetRangesMinMax(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{ + YAxis: YAxis{ + Range: &ContinuousRange{ + Min: 5.0, + Max: 15.0, + }, + Ticks: []Tick{ + {Value: 7.0, Label: "Foo"}, + {Value: 11.0, Label: "Foo2"}, + }, + }, + Bars: []Value{ + {Value: 1.0}, + {Value: 10.0}, + }, + } + + yr := bc.getRanges() + assert.NotNil(yr) + assert.False(yr.IsZero()) + + assert.Equal(15, yr.GetMax()) + assert.Equal(5, yr.GetMin()) +} + +func TestBarChartGetRangesTicksMinMax(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{ + YAxis: YAxis{ + Ticks: []Tick{ + {Value: 7.0, Label: "Foo"}, + {Value: 11.0, Label: "Foo2"}, + }, + }, + Bars: []Value{ + {Value: 1.0}, + {Value: 10.0}, + }, + } + + yr := bc.getRanges() + assert.NotNil(yr) + assert.False(yr.IsZero()) + + assert.Equal(11, yr.GetMax()) + assert.Equal(7, yr.GetMin()) +} + +func TestBarChartHasAxes(t *testing.T) { + assert := assert.New(t) + + bc := BarChart{} + assert.False(bc.hasAxes()) + bc.YAxis = YAxis{ + Style: StyleShow(), + } + + assert.True(bc.hasAxes()) +} diff --git a/defaults.go b/defaults.go index 4c9fd1f..482d489 100644 --- a/defaults.go +++ b/defaults.go @@ -66,6 +66,11 @@ const ( DefaultFloatFormat = "%.2f" // DefaultPercentValueFormat is the default percent format. DefaultPercentValueFormat = "%0.2f%%" + + // DefaultBarSpacing is the default pixel spacing between bars. + DefaultBarSpacing = 100 + // DefaultBarWidth is the default pixel width of bars in a bar chart. + DefaultBarWidth = 50 ) var ( diff --git a/examples/bar_chart/main.go b/examples/bar_chart/main.go index 613deac..54619bb 100644 --- a/examples/bar_chart/main.go +++ b/examples/bar_chart/main.go @@ -14,20 +14,20 @@ func drawChart(res http.ResponseWriter, req *http.Request) { Height: 512, BarWidth: 60, XAxis: chart.Style{ - Show: true, + Show: false, }, YAxis: chart.YAxis{ Style: chart.Style{ - Show: true, + Show: false, }, }, Bars: []chart.Value{ - {Value: 5, Label: "Blue"}, - {Value: 5, Label: "Green"}, - {Value: 4, Label: "Gray"}, - {Value: 3, Label: "Orange"}, + {Value: 5.25, Label: "Blue"}, + {Value: 4.88, Label: "Green"}, + {Value: 4.74, Label: "Gray"}, + {Value: 3.22, Label: "Orange"}, {Value: 3, Label: "Test"}, - {Value: 2, Label: "??"}, + {Value: 2.27, Label: "??"}, {Value: 1, Label: "!!"}, }, } diff --git a/images/bar_chart.png b/images/bar_chart.png new file mode 100644 index 0000000..21c8fdd Binary files /dev/null and b/images/bar_chart.png differ