This commit is contained in:
Will Charczuk 2016-08-05 20:08:24 -07:00
parent 7bb3fbf810
commit 95d0bcca4b
5 changed files with 161 additions and 15 deletions

View File

@ -35,11 +35,8 @@ type BarChart struct {
} }
// GetDPI returns the dpi for the chart. // GetDPI returns the dpi for the chart.
func (bc BarChart) GetDPI(defaults ...float64) float64 { func (bc BarChart) GetDPI() float64 {
if bc.DPI == 0 { if bc.DPI == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return DefaultDPI return DefaultDPI
} }
return bc.DPI return bc.DPI
@ -64,7 +61,7 @@ func (bc BarChart) GetWidth() int {
// GetHeight returns the chart height or the default value. // GetHeight returns the chart height or the default value.
func (bc BarChart) GetHeight() int { func (bc BarChart) GetHeight() int {
if bc.Height == 0 { if bc.Height == 0 {
return DefaultChartWidth return DefaultChartHeight
} }
return bc.Height return bc.Height
} }
@ -72,7 +69,7 @@ func (bc BarChart) GetHeight() int {
// GetBarSpacing returns the spacing between bars. // GetBarSpacing returns the spacing between bars.
func (bc BarChart) GetBarSpacing() int { func (bc BarChart) GetBarSpacing() int {
if bc.BarSpacing == 0 { if bc.BarSpacing == 0 {
return 100 return DefaultBarSpacing
} }
return bc.BarSpacing return bc.BarSpacing
} }
@ -80,7 +77,7 @@ func (bc BarChart) GetBarSpacing() int {
// GetBarWidth returns the default bar width. // GetBarWidth returns the default bar width.
func (bc BarChart) GetBarWidth() int { func (bc BarChart) GetBarWidth() int {
if bc.BarWidth == 0 { if bc.BarWidth == 0 {
return 40 return DefaultBarWidth
} }
return bc.BarWidth return bc.BarWidth
} }
@ -103,7 +100,7 @@ func (bc BarChart) Render(rp RendererProvider, w io.Writer) error {
} }
bc.defaultFont = defaultFont bc.defaultFont = defaultFont
} }
r.SetDPI(bc.GetDPI(DefaultDPI)) r.SetDPI(bc.GetDPI())
bc.drawBackground(r) bc.drawBackground(r)

144
bar_chart_test.go Normal file
View File

@ -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())
}

View File

@ -66,6 +66,11 @@ const (
DefaultFloatFormat = "%.2f" DefaultFloatFormat = "%.2f"
// DefaultPercentValueFormat is the default percent format. // DefaultPercentValueFormat is the default percent format.
DefaultPercentValueFormat = "%0.2f%%" 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 ( var (

View File

@ -14,20 +14,20 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
Height: 512, Height: 512,
BarWidth: 60, BarWidth: 60,
XAxis: chart.Style{ XAxis: chart.Style{
Show: true, Show: false,
}, },
YAxis: chart.YAxis{ YAxis: chart.YAxis{
Style: chart.Style{ Style: chart.Style{
Show: true, Show: false,
}, },
}, },
Bars: []chart.Value{ Bars: []chart.Value{
{Value: 5, Label: "Blue"}, {Value: 5.25, Label: "Blue"},
{Value: 5, Label: "Green"}, {Value: 4.88, Label: "Green"},
{Value: 4, Label: "Gray"}, {Value: 4.74, Label: "Gray"},
{Value: 3, Label: "Orange"}, {Value: 3.22, Label: "Orange"},
{Value: 3, Label: "Test"}, {Value: 3, Label: "Test"},
{Value: 2, Label: "??"}, {Value: 2.27, Label: "??"},
{Value: 1, Label: "!!"}, {Value: 1, Label: "!!"},
}, },
} }

BIN
images/bar_chart.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB