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
|
|
|
)
|
|
|
|
|
2016-08-06 03:26:17 -04:00
|
|
|
func TestBarChartRender(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-08-06 03:26:17 -04:00
|
|
|
|
|
|
|
bc := BarChart{
|
2019-02-13 21:55:13 -05:00
|
|
|
Width: 1024,
|
|
|
|
Title: "Test Title",
|
2016-08-06 03:26:17 -04:00
|
|
|
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)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNil(t, err)
|
|
|
|
testutil.AssertNotZero(t, buf.Len())
|
2016-08-06 03:26:17 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:37:08 -05:00
|
|
|
func TestBarChartRenderZero(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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)
|
2020-11-22 19:45:10 -05:00
|
|
|
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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-08-05 23:08:24 -04:00
|
|
|
|
|
|
|
bc := BarChart{}
|
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, DefaultDPI, bc.GetDPI())
|
2016-08-05 23:08:24 -04:00
|
|
|
bc.DPI = 100
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 100, bc.GetDPI())
|
2016-08-05 23:08:24 -04:00
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNil(t, bc.GetFont())
|
2016-08-05 23:08:24 -04:00
|
|
|
f, err := GetDefaultFont()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNil(t, err)
|
2016-08-05 23:08:24 -04:00
|
|
|
bc.Font = f
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotNil(t, bc.GetFont())
|
2016-08-05 23:08:24 -04:00
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, DefaultChartWidth, bc.GetWidth())
|
2016-08-05 23:08:24 -04:00
|
|
|
bc.Width = DefaultChartWidth - 1
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, DefaultChartWidth-1, bc.GetWidth())
|
2016-08-05 23:08:24 -04:00
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, DefaultChartHeight, bc.GetHeight())
|
2016-08-05 23:08:24 -04:00
|
|
|
bc.Height = DefaultChartHeight - 1
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, DefaultChartHeight-1, bc.GetHeight())
|
2016-08-05 23:08:24 -04:00
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, DefaultBarSpacing, bc.GetBarSpacing())
|
2016-08-05 23:08:24 -04:00
|
|
|
bc.BarSpacing = 150
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 150, bc.GetBarSpacing())
|
2016-08-05 23:08:24 -04:00
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, DefaultBarWidth, bc.GetBarWidth())
|
2016-08-05 23:08:24 -04:00
|
|
|
bc.BarWidth = 75
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 75, bc.GetBarWidth())
|
2016-08-05 23:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBarChartRenderNoBars(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-08-05 23:08:24 -04:00
|
|
|
|
|
|
|
bc := BarChart{}
|
|
|
|
err := bc.Render(PNG, bytes.NewBuffer([]byte{}))
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotNil(t, err)
|
2016-08-05 23:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBarChartGetRanges(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-08-05 23:08:24 -04:00
|
|
|
|
|
|
|
bc := BarChart{}
|
|
|
|
|
|
|
|
yr := bc.getRanges()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotNil(t, yr)
|
|
|
|
testutil.AssertFalse(t, yr.IsZero())
|
2016-08-05 23:08:24 -04:00
|
|
|
|
2020-11-22 19:45:10 -05: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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotNil(t, yr)
|
|
|
|
testutil.AssertFalse(t, yr.IsZero())
|
2016-08-05 23:08:24 -04:00
|
|
|
|
2020-11-22 19:45:10 -05: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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotNil(t, yr)
|
|
|
|
testutil.AssertFalse(t, yr.IsZero())
|
2016-08-05 23:08:24 -04:00
|
|
|
|
2020-11-22 19:45:10 -05: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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotNil(t, yr)
|
|
|
|
testutil.AssertFalse(t, yr.IsZero())
|
2016-08-05 23:08:24 -04:00
|
|
|
|
2020-11-22 19:45:10 -05: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) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-08-05 23:08:24 -04:00
|
|
|
|
|
|
|
bc := BarChart{}
|
2020-11-22 19:45:10 -05:00
|
|
|
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
|
|
|
}
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertFalse(t, bc.hasAxes())
|
2016-08-05 23:08:24 -04:00
|
|
|
}
|
2016-08-05 23:44:40 -04:00
|
|
|
|
|
|
|
func TestBarChartGetDefaultCanvasBox(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-08-05 23:44:40 -04:00
|
|
|
|
|
|
|
bc := BarChart{}
|
|
|
|
b := bc.getDefaultCanvasBox()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertFalse(t, b.IsZero())
|
2016-08-05 23:44:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBarChartSetRangeDomains(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-08-05 23:44:40 -04:00
|
|
|
|
|
|
|
bc := BarChart{}
|
|
|
|
cb := bc.box()
|
|
|
|
yr := bc.getRanges()
|
|
|
|
yr2 := bc.setRangeDomains(cb, yr)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotZero(t, yr2.GetDomain())
|
2016-08-05 23:44:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBarChartGetValueFormatters(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-08-05 23:44:40 -04:00
|
|
|
|
|
|
|
bc := BarChart{}
|
|
|
|
vf := bc.getValueFormatters()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotNil(t, vf)
|
|
|
|
testutil.AssertEqual(t, "1234.00", vf(1234.0))
|
2016-08-05 23:44:40 -04:00
|
|
|
|
|
|
|
bc.YAxis.ValueFormatter = func(_ interface{}) string { return "test" }
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, "test", bc.getValueFormatters()(1234))
|
2016-08-05 23:44:40 -04:00
|
|
|
}
|
2016-08-06 00:25:56 -04:00
|
|
|
|
|
|
|
func TestBarChartGetAxesTicks(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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)
|
2020-11-22 19:45:10 -05:00
|
|
|
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)
|
2020-11-22 19:45:10 -05:00
|
|
|
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)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertLen(t, ticks, 2)
|
2016-08-06 00:25:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBarChartCalculateEffectiveBarSpacing(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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())
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotZero(t, spacing)
|
2016-08-06 00:25:56 -04:00
|
|
|
|
|
|
|
bc.BarWidth = 250
|
|
|
|
spacing = bc.calculateEffectiveBarSpacing(bc.box())
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertZero(t, spacing)
|
2016-08-06 00:25:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBarChartCalculateEffectiveBarWidth(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// 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())
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotZero(t, spacing)
|
2016-08-06 00:25:56 -04:00
|
|
|
|
|
|
|
barWidth := bc.calculateEffectiveBarWidth(bc.box(), spacing)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 10, barWidth)
|
2016-08-06 00:25:56 -04:00
|
|
|
|
|
|
|
bc.BarWidth = 250
|
|
|
|
spacing = bc.calculateEffectiveBarSpacing(bc.box())
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertZero(t, spacing)
|
2016-08-06 00:25:56 -04:00
|
|
|
barWidth = bc.calculateEffectiveBarWidth(bc.box(), spacing)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 199, barWidth)
|
2016-08-06 00:25:56 -04:00
|
|
|
|
2020-11-22 19:45:10 -05: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)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, spacing, bs)
|
|
|
|
testutil.AssertEqual(t, barWidth, bw)
|
|
|
|
testutil.AssertEqual(t, cb.Width()+1, total)
|
2016-08-06 03:26:17 -04:00
|
|
|
}
|
2016-08-06 00:31:41 -04:00
|
|
|
|
2016-08-06 03:26:17 -04:00
|
|
|
func TestBarChatGetTitleFontSize(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-08-06 03:26:17 -04:00
|
|
|
size := BarChart{Width: 2049, Height: 2049}.getTitleFontSize()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 48, size)
|
2016-08-06 03:26:17 -04:00
|
|
|
size = BarChart{Width: 1025, Height: 1025}.getTitleFontSize()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 24, size)
|
2016-08-06 03:26:17 -04:00
|
|
|
size = BarChart{Width: 513, Height: 513}.getTitleFontSize()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 18, size)
|
2016-08-06 03:26:17 -04:00
|
|
|
size = BarChart{Width: 257, Height: 257}.getTitleFontSize()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 12, size)
|
2016-08-06 03:26:17 -04:00
|
|
|
size = BarChart{Width: 128, Height: 128}.getTitleFontSize()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 10, size)
|
2016-08-06 00:25:56 -04:00
|
|
|
}
|