go-chart/bar_chart_test.go

311 lines
7 KiB
Go
Raw Normal View History

2016-08-05 23:08:24 -04:00
package chart
import (
"bytes"
"math"
"testing"
2024-10-27 22:52:38 -04:00
"git.smarteching.com/zeni/go-chart/v2/testutil"
2016-08-05 23:08:24 -04:00
)
func TestBarChartRender(t *testing.T) {
// replaced new assertions helper
bc := BarChart{
2019-02-13 21:55:13 -05:00
Width: 1024,
Title: "Test Title",
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"},
},
}
buf := bytes.NewBuffer([]byte{})
err := bc.Render(PNG, buf)
testutil.AssertNil(t, err)
testutil.AssertNotZero(t, buf.Len())
}
2017-01-13 17:37:08 -05:00
func TestBarChartRenderZero(t *testing.T) {
// replaced new assertions helper
2017-01-13 17:37:08 -05:00
bc := BarChart{
2019-02-13 21:55:13 -05:00
Width: 1024,
Title: "Test Title",
2017-01-13 17:37:08 -05:00
Bars: []Value{
{Value: 0.0, Label: "One"},
{Value: 0.0, Label: "Two"},
},
}
buf := bytes.NewBuffer([]byte{})
err := bc.Render(PNG, buf)
testutil.AssertNotNil(t, err)
2017-01-13 17:37:08 -05:00
}
2016-08-05 23:08:24 -04:00
func TestBarChartProps(t *testing.T) {
// replaced new assertions helper
2016-08-05 23:08:24 -04:00
bc := BarChart{}
testutil.AssertEqual(t, DefaultDPI, bc.GetDPI())
2016-08-05 23:08:24 -04:00
bc.DPI = 100
testutil.AssertEqual(t, 100, bc.GetDPI())
2016-08-05 23:08:24 -04:00
testutil.AssertNil(t, bc.GetFont())
2016-08-05 23:08:24 -04:00
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
2016-08-05 23:08:24 -04:00
bc.Font = f
testutil.AssertNotNil(t, bc.GetFont())
2016-08-05 23:08:24 -04:00
testutil.AssertEqual(t, DefaultChartWidth, bc.GetWidth())
2016-08-05 23:08:24 -04:00
bc.Width = DefaultChartWidth - 1
testutil.AssertEqual(t, DefaultChartWidth-1, bc.GetWidth())
2016-08-05 23:08:24 -04:00
testutil.AssertEqual(t, DefaultChartHeight, bc.GetHeight())
2016-08-05 23:08:24 -04:00
bc.Height = DefaultChartHeight - 1
testutil.AssertEqual(t, DefaultChartHeight-1, bc.GetHeight())
2016-08-05 23:08:24 -04:00
testutil.AssertEqual(t, DefaultBarSpacing, bc.GetBarSpacing())
2016-08-05 23:08:24 -04:00
bc.BarSpacing = 150
testutil.AssertEqual(t, 150, bc.GetBarSpacing())
2016-08-05 23:08:24 -04:00
testutil.AssertEqual(t, DefaultBarWidth, bc.GetBarWidth())
2016-08-05 23:08:24 -04:00
bc.BarWidth = 75
testutil.AssertEqual(t, 75, bc.GetBarWidth())
2016-08-05 23:08:24 -04:00
}
func TestBarChartRenderNoBars(t *testing.T) {
// replaced new assertions helper
2016-08-05 23:08:24 -04:00
bc := BarChart{}
err := bc.Render(PNG, bytes.NewBuffer([]byte{}))
testutil.AssertNotNil(t, err)
2016-08-05 23:08:24 -04:00
}
func TestBarChartGetRanges(t *testing.T) {
// replaced new assertions helper
2016-08-05 23:08:24 -04:00
bc := BarChart{}
yr := bc.getRanges()
testutil.AssertNotNil(t, yr)
testutil.AssertFalse(t, yr.IsZero())
2016-08-05 23:08:24 -04:00
testutil.AssertEqual(t, -math.MaxFloat64, yr.GetMax())
testutil.AssertEqual(t, math.MaxFloat64, yr.GetMin())
2016-08-05 23:08:24 -04:00
}
func TestBarChartGetRangesBarsMinMax(t *testing.T) {
// replaced new assertions helper
2016-08-05 23:08:24 -04:00
bc := BarChart{
Bars: []Value{
{Value: 1.0},
{Value: 10.0},
},
}
yr := bc.getRanges()
testutil.AssertNotNil(t, yr)
testutil.AssertFalse(t, yr.IsZero())
2016-08-05 23:08:24 -04:00
testutil.AssertEqual(t, 10, yr.GetMax())
testutil.AssertEqual(t, 1, yr.GetMin())
2016-08-05 23:08:24 -04:00
}
func TestBarChartGetRangesMinMax(t *testing.T) {
// replaced new assertions helper
2016-08-05 23:08:24 -04:00
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()
testutil.AssertNotNil(t, yr)
testutil.AssertFalse(t, yr.IsZero())
2016-08-05 23:08:24 -04:00
testutil.AssertEqual(t, 15, yr.GetMax())
testutil.AssertEqual(t, 5, yr.GetMin())
2016-08-05 23:08:24 -04:00
}
func TestBarChartGetRangesTicksMinMax(t *testing.T) {
// replaced new assertions helper
2016-08-05 23:08:24 -04:00
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()
testutil.AssertNotNil(t, yr)
testutil.AssertFalse(t, yr.IsZero())
2016-08-05 23:08:24 -04:00
testutil.AssertEqual(t, 11, yr.GetMax())
testutil.AssertEqual(t, 7, yr.GetMin())
2016-08-05 23:08:24 -04:00
}
func TestBarChartHasAxes(t *testing.T) {
// replaced new assertions helper
2016-08-05 23:08:24 -04:00
bc := BarChart{}
testutil.AssertTrue(t, bc.hasAxes())
2016-08-05 23:08:24 -04:00
bc.YAxis = YAxis{
2019-02-13 21:55:13 -05:00
Style: Hidden(),
2016-08-05 23:08:24 -04:00
}
testutil.AssertFalse(t, bc.hasAxes())
2016-08-05 23:08:24 -04:00
}
func TestBarChartGetDefaultCanvasBox(t *testing.T) {
// replaced new assertions helper
bc := BarChart{}
b := bc.getDefaultCanvasBox()
testutil.AssertFalse(t, b.IsZero())
}
func TestBarChartSetRangeDomains(t *testing.T) {
// replaced new assertions helper
bc := BarChart{}
cb := bc.box()
yr := bc.getRanges()
yr2 := bc.setRangeDomains(cb, yr)
testutil.AssertNotZero(t, yr2.GetDomain())
}
func TestBarChartGetValueFormatters(t *testing.T) {
// replaced new assertions helper
bc := BarChart{}
vf := bc.getValueFormatters()
testutil.AssertNotNil(t, vf)
testutil.AssertEqual(t, "1234.00", vf(1234.0))
bc.YAxis.ValueFormatter = func(_ interface{}) string { return "test" }
testutil.AssertEqual(t, "test", bc.getValueFormatters()(1234))
}
2016-08-06 00:25:56 -04:00
func TestBarChartGetAxesTicks(t *testing.T) {
// replaced new assertions helper
2016-08-06 00:25:56 -04:00
bc := BarChart{
Bars: []Value{
{Value: 1.0},
{Value: 2.0},
{Value: 3.0},
},
}
r, err := PNG(128, 128)
testutil.AssertNil(t, err)
2016-08-06 00:25:56 -04:00
yr := bc.getRanges()
yf := bc.getValueFormatters()
2019-02-13 21:55:13 -05:00
bc.YAxis.Style.Hidden = true
2016-08-06 00:25:56 -04:00
ticks := bc.getAxesTicks(r, yr, yf)
testutil.AssertEmpty(t, ticks)
2016-08-06 00:25:56 -04:00
2019-02-13 21:55:13 -05:00
bc.YAxis.Style.Hidden = false
2016-08-06 00:25:56 -04:00
ticks = bc.getAxesTicks(r, yr, yf)
testutil.AssertLen(t, ticks, 2)
2016-08-06 00:25:56 -04:00
}
func TestBarChartCalculateEffectiveBarSpacing(t *testing.T) {
// replaced new assertions helper
2016-08-06 00:25:56 -04:00
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())
testutil.AssertNotZero(t, spacing)
2016-08-06 00:25:56 -04:00
bc.BarWidth = 250
spacing = bc.calculateEffectiveBarSpacing(bc.box())
testutil.AssertZero(t, spacing)
2016-08-06 00:25:56 -04:00
}
func TestBarChartCalculateEffectiveBarWidth(t *testing.T) {
// replaced new assertions helper
2016-08-06 00:25:56 -04:00
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"},
},
}
2016-08-06 00:31:41 -04:00
cb := bc.box()
2016-08-06 00:25:56 -04:00
spacing := bc.calculateEffectiveBarSpacing(bc.box())
testutil.AssertNotZero(t, spacing)
2016-08-06 00:25:56 -04:00
barWidth := bc.calculateEffectiveBarWidth(bc.box(), spacing)
testutil.AssertEqual(t, 10, barWidth)
2016-08-06 00:25:56 -04:00
bc.BarWidth = 250
spacing = bc.calculateEffectiveBarSpacing(bc.box())
testutil.AssertZero(t, spacing)
2016-08-06 00:25:56 -04:00
barWidth = bc.calculateEffectiveBarWidth(bc.box(), spacing)
testutil.AssertEqual(t, 199, barWidth)
2016-08-06 00:25:56 -04:00
testutil.AssertEqual(t, cb.Width()+1, bc.calculateTotalBarWidth(barWidth, spacing))
2016-08-06 00:31:41 -04:00
bw, bs, total := bc.calculateScaledTotalWidth(cb)
testutil.AssertEqual(t, spacing, bs)
testutil.AssertEqual(t, barWidth, bw)
testutil.AssertEqual(t, cb.Width()+1, total)
}
2016-08-06 00:31:41 -04:00
func TestBarChatGetTitleFontSize(t *testing.T) {
// replaced new assertions helper
size := BarChart{Width: 2049, Height: 2049}.getTitleFontSize()
testutil.AssertEqual(t, 48, size)
size = BarChart{Width: 1025, Height: 1025}.getTitleFontSize()
testutil.AssertEqual(t, 24, size)
size = BarChart{Width: 513, Height: 513}.getTitleFontSize()
testutil.AssertEqual(t, 18, size)
size = BarChart{Width: 257, Height: 257}.getTitleFontSize()
testutil.AssertEqual(t, 12, size)
size = BarChart{Width: 128, Height: 128}.getTitleFontSize()
testutil.AssertEqual(t, 10, size)
2016-08-06 00:25:56 -04:00
}