go-chart/chart_test.go

595 lines
14 KiB
Go
Raw Permalink Normal View History

2016-07-06 21:54:00 -04:00
package chart
import (
"bytes"
"image"
"image/png"
2016-07-13 14:50:22 -04:00
"math"
2016-07-06 21:54:00 -04:00
"testing"
"time"
"github.com/wcharczuk/go-chart/v2/drawing"
"github.com/wcharczuk/go-chart/v2/testutil"
2016-07-06 21:54:00 -04:00
)
2016-07-12 23:34:59 -04:00
func TestChartGetDPI(t *testing.T) {
// replaced new assertions helper
2016-07-12 23:34:59 -04:00
unset := Chart{}
testutil.AssertEqual(t, DefaultDPI, unset.GetDPI())
testutil.AssertEqual(t, 192, unset.GetDPI(192))
2016-07-12 23:34:59 -04:00
set := Chart{DPI: 128}
testutil.AssertEqual(t, 128, set.GetDPI())
testutil.AssertEqual(t, 128, set.GetDPI(192))
2016-07-12 23:34:59 -04:00
}
func TestChartGetFont(t *testing.T) {
// replaced new assertions helper
2016-07-12 23:34:59 -04:00
f, err := GetDefaultFont()
testutil.AssertNil(t, err)
2016-07-12 23:34:59 -04:00
unset := Chart{}
testutil.AssertNil(t, unset.GetFont())
2016-07-12 23:34:59 -04:00
set := Chart{Font: f}
testutil.AssertNotNil(t, set.GetFont())
2016-07-12 23:34:59 -04:00
}
func TestChartGetWidth(t *testing.T) {
// replaced new assertions helper
2016-07-12 23:34:59 -04:00
unset := Chart{}
testutil.AssertEqual(t, DefaultChartWidth, unset.GetWidth())
2016-07-12 23:34:59 -04:00
set := Chart{Width: DefaultChartWidth + 10}
testutil.AssertEqual(t, DefaultChartWidth+10, set.GetWidth())
2016-07-12 23:34:59 -04:00
}
func TestChartGetHeight(t *testing.T) {
// replaced new assertions helper
2016-07-12 23:34:59 -04:00
unset := Chart{}
testutil.AssertEqual(t, DefaultChartHeight, unset.GetHeight())
2016-07-12 23:34:59 -04:00
set := Chart{Height: DefaultChartHeight + 10}
testutil.AssertEqual(t, DefaultChartHeight+10, set.GetHeight())
2016-07-12 23:34:59 -04:00
}
func TestChartGetRanges(t *testing.T) {
// replaced new assertions helper
2016-07-12 23:34:59 -04:00
c := Chart{
Series: []Series{
ContinuousSeries{
XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
},
ContinuousSeries{
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{-2.1, -1.0, 0, 1.0, 2.0},
},
ContinuousSeries{
YAxis: YAxisSecondary,
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{10.0, 11.0, 12.0, 13.0, 14.0},
},
},
}
xrange, yrange, yrangeAlt := c.getRanges()
testutil.AssertEqual(t, -2.0, xrange.GetMin())
testutil.AssertEqual(t, 5.0, xrange.GetMax())
2016-07-12 23:34:59 -04:00
testutil.AssertEqual(t, -2.1, yrange.GetMin())
testutil.AssertEqual(t, 4.5, yrange.GetMax())
2016-07-12 23:34:59 -04:00
testutil.AssertEqual(t, 10.0, yrangeAlt.GetMin())
testutil.AssertEqual(t, 14.0, yrangeAlt.GetMax())
2016-07-12 23:34:59 -04:00
cSet := Chart{
XAxis: XAxis{
Range: &ContinuousRange{Min: 9.8, Max: 19.8},
2016-07-12 23:34:59 -04:00
},
YAxis: YAxis{
Range: &ContinuousRange{Min: 9.9, Max: 19.9},
2016-07-12 23:34:59 -04:00
},
YAxisSecondary: YAxis{
Range: &ContinuousRange{Min: 9.7, Max: 19.7},
2016-07-12 23:34:59 -04:00
},
Series: []Series{
ContinuousSeries{
XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
},
ContinuousSeries{
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{-2.1, -1.0, 0, 1.0, 2.0},
},
ContinuousSeries{
YAxis: YAxisSecondary,
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{10.0, 11.0, 12.0, 13.0, 14.0},
},
},
}
xr2, yr2, yra2 := cSet.getRanges()
testutil.AssertEqual(t, 9.8, xr2.GetMin())
testutil.AssertEqual(t, 19.8, xr2.GetMax())
2016-07-12 23:34:59 -04:00
testutil.AssertEqual(t, 9.9, yr2.GetMin())
testutil.AssertEqual(t, 19.9, yr2.GetMax())
2016-07-12 23:34:59 -04:00
testutil.AssertEqual(t, 9.7, yra2.GetMin())
testutil.AssertEqual(t, 19.7, yra2.GetMax())
2016-07-12 23:34:59 -04:00
}
func TestChartGetRangesUseTicks(t *testing.T) {
// replaced new assertions helper
// this test asserts that ticks should supercede manual ranges when generating the overall ranges.
c := Chart{
YAxis: YAxis{
Ticks: []Tick{
{0.0, "Zero"},
{1.0, "1.0"},
{2.0, "2.0"},
{3.0, "3.0"},
{4.0, "4.0"},
{5.0, "Five"},
},
Range: &ContinuousRange{
Min: -5.0,
Max: 5.0,
},
},
Series: []Series{
ContinuousSeries{
XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
},
},
}
xr, yr, yar := c.getRanges()
testutil.AssertEqual(t, -2.0, xr.GetMin())
testutil.AssertEqual(t, 2.0, xr.GetMax())
testutil.AssertEqual(t, 0.0, yr.GetMin())
testutil.AssertEqual(t, 5.0, yr.GetMax())
testutil.AssertTrue(t, yar.IsZero(), yar.String())
}
func TestChartGetRangesUseUserRanges(t *testing.T) {
// replaced new assertions helper
c := Chart{
YAxis: YAxis{
Range: &ContinuousRange{
Min: -5.0,
Max: 5.0,
},
},
Series: []Series{
ContinuousSeries{
XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
},
},
}
xr, yr, yar := c.getRanges()
testutil.AssertEqual(t, -2.0, xr.GetMin())
testutil.AssertEqual(t, 2.0, xr.GetMax())
testutil.AssertEqual(t, -5.0, yr.GetMin())
testutil.AssertEqual(t, 5.0, yr.GetMax())
testutil.AssertTrue(t, yar.IsZero(), yar.String())
}
func TestChartGetBackgroundStyle(t *testing.T) {
// replaced new assertions helper
c := Chart{
Background: Style{
FillColor: drawing.ColorBlack,
},
}
bs := c.getBackgroundStyle()
testutil.AssertEqual(t, bs.FillColor.String(), drawing.ColorBlack.String())
}
func TestChartGetCanvasStyle(t *testing.T) {
// replaced new assertions helper
c := Chart{
Canvas: Style{
FillColor: drawing.ColorBlack,
},
}
bs := c.getCanvasStyle()
testutil.AssertEqual(t, bs.FillColor.String(), drawing.ColorBlack.String())
}
2016-07-13 01:04:30 -04:00
func TestChartGetDefaultCanvasBox(t *testing.T) {
// replaced new assertions helper
2016-07-13 01:04:30 -04:00
c := Chart{}
canvasBoxDefault := c.getDefaultCanvasBox()
testutil.AssertFalse(t, canvasBoxDefault.IsZero())
testutil.AssertEqual(t, DefaultBackgroundPadding.Top, canvasBoxDefault.Top)
testutil.AssertEqual(t, DefaultBackgroundPadding.Left, canvasBoxDefault.Left)
testutil.AssertEqual(t, c.GetWidth()-DefaultBackgroundPadding.Right, canvasBoxDefault.Right)
testutil.AssertEqual(t, c.GetHeight()-DefaultBackgroundPadding.Bottom, canvasBoxDefault.Bottom)
2016-07-13 01:04:30 -04:00
custom := Chart{
Background: Style{
Padding: Box{
Top: DefaultBackgroundPadding.Top + 1,
Left: DefaultBackgroundPadding.Left + 1,
Right: DefaultBackgroundPadding.Right + 1,
Bottom: DefaultBackgroundPadding.Bottom + 1,
},
},
}
canvasBoxCustom := custom.getDefaultCanvasBox()
testutil.AssertFalse(t, canvasBoxCustom.IsZero())
testutil.AssertEqual(t, DefaultBackgroundPadding.Top+1, canvasBoxCustom.Top)
testutil.AssertEqual(t, DefaultBackgroundPadding.Left+1, canvasBoxCustom.Left)
testutil.AssertEqual(t, c.GetWidth()-(DefaultBackgroundPadding.Right+1), canvasBoxCustom.Right)
testutil.AssertEqual(t, c.GetHeight()-(DefaultBackgroundPadding.Bottom+1), canvasBoxCustom.Bottom)
2016-07-13 01:04:30 -04:00
}
func TestChartGetValueFormatters(t *testing.T) {
// replaced new assertions helper
2016-07-13 01:04:30 -04:00
c := Chart{
Series: []Series{
ContinuousSeries{
XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
},
ContinuousSeries{
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{-2.1, -1.0, 0, 1.0, 2.0},
},
ContinuousSeries{
YAxis: YAxisSecondary,
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{10.0, 11.0, 12.0, 13.0, 14.0},
},
},
}
dxf, dyf, dyaf := c.getValueFormatters()
testutil.AssertNotNil(t, dxf)
testutil.AssertNotNil(t, dyf)
testutil.AssertNotNil(t, dyaf)
2016-07-13 01:04:30 -04:00
}
func TestChartHasAxes(t *testing.T) {
// replaced new assertions helper
2016-07-13 01:04:30 -04:00
testutil.AssertTrue(t, Chart{}.hasAxes())
testutil.AssertFalse(t, Chart{XAxis: XAxis{Style: Hidden()}, YAxis: YAxis{Style: Hidden()}, YAxisSecondary: YAxis{Style: Hidden()}}.hasAxes())
2016-07-13 01:04:30 -04:00
x := Chart{
XAxis: XAxis{
2019-02-13 21:55:13 -05:00
Style: Hidden(),
},
YAxis: YAxis{
Style: Shown(),
},
YAxisSecondary: YAxis{
Style: Hidden(),
2016-07-13 01:04:30 -04:00
},
}
testutil.AssertTrue(t, x.hasAxes())
2016-07-13 01:04:30 -04:00
y := Chart{
2019-02-13 21:55:13 -05:00
XAxis: XAxis{
Style: Shown(),
},
2016-07-13 01:04:30 -04:00
YAxis: YAxis{
2019-02-13 21:55:13 -05:00
Style: Hidden(),
},
YAxisSecondary: YAxis{
Style: Hidden(),
2016-07-13 01:04:30 -04:00
},
}
testutil.AssertTrue(t, y.hasAxes())
2016-07-13 01:04:30 -04:00
ya := Chart{
2019-02-13 21:55:13 -05:00
XAxis: XAxis{
Style: Hidden(),
},
YAxis: YAxis{
Style: Hidden(),
},
2016-07-13 01:04:30 -04:00
YAxisSecondary: YAxis{
2019-02-13 21:55:13 -05:00
Style: Shown(),
2016-07-13 01:04:30 -04:00
},
}
testutil.AssertTrue(t, ya.hasAxes())
2016-07-13 01:04:30 -04:00
}
func TestChartGetAxesTicks(t *testing.T) {
// replaced new assertions helper
2016-07-13 01:04:30 -04:00
r, err := PNG(1024, 1024)
testutil.AssertNil(t, err)
2016-07-13 01:04:30 -04:00
c := Chart{
XAxis: XAxis{
Range: &ContinuousRange{Min: 9.8, Max: 19.8},
2016-07-13 01:04:30 -04:00
},
YAxis: YAxis{
Range: &ContinuousRange{Min: 9.9, Max: 19.9},
2016-07-13 01:04:30 -04:00
},
YAxisSecondary: YAxis{
Range: &ContinuousRange{Min: 9.7, Max: 19.7},
2016-07-13 01:04:30 -04:00
},
}
xr, yr, yar := c.getRanges()
xt, yt, yat := c.getAxesTicks(r, xr, yr, yar, FloatValueFormatter, FloatValueFormatter, FloatValueFormatter)
testutil.AssertNotEmpty(t, xt)
testutil.AssertNotEmpty(t, yt)
testutil.AssertNotEmpty(t, yat)
2016-07-13 01:04:30 -04:00
}
2016-07-06 21:54:00 -04:00
func TestChartSingleSeries(t *testing.T) {
// replaced new assertions helper
2016-07-06 21:54:00 -04:00
now := time.Now()
c := Chart{
2019-02-13 21:55:13 -05:00
Title: "Hello!",
Width: 1024,
Height: 400,
2016-07-10 14:27:12 -04:00
YAxis: YAxis{
Range: &ContinuousRange{
2016-07-10 14:27:12 -04:00
Min: 0.0,
Max: 4.0,
},
2016-07-07 17:44:03 -04:00
},
2016-07-06 21:54:00 -04:00
Series: []Series{
TimeSeries{
2016-07-07 17:44:03 -04:00
Name: "goog",
2016-07-06 21:54:00 -04:00
XValues: []time.Time{now.AddDate(0, 0, -3), now.AddDate(0, 0, -2), now.AddDate(0, 0, -1)},
YValues: []float64{1.0, 2.0, 3.0},
},
},
}
buffer := bytes.NewBuffer([]byte{})
c.Render(PNG, buffer)
testutil.AssertNotEmpty(t, buffer.Bytes())
2016-07-06 21:54:00 -04:00
}
2016-07-13 14:50:22 -04:00
func TestChartRegressionBadRanges(t *testing.T) {
// replaced new assertions helper
2016-07-13 14:50:22 -04:00
c := Chart{
Series: []Series{
ContinuousSeries{
XValues: []float64{math.Inf(1), math.Inf(1), math.Inf(1), math.Inf(1), math.Inf(1)},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5},
},
},
}
buffer := bytes.NewBuffer([]byte{})
c.Render(PNG, buffer)
testutil.AssertTrue(t, true, "Render needs to finish.")
2016-07-13 14:50:22 -04:00
}
func TestChartRegressionBadRangesByUser(t *testing.T) {
// replaced new assertions helper
2016-07-13 14:50:22 -04:00
c := Chart{
YAxis: YAxis{
Range: &ContinuousRange{
2016-07-13 14:50:22 -04:00
Min: math.Inf(-1),
Max: math.Inf(1), // this could really happen? eh.
},
},
Series: []Series{
ContinuousSeries{
2019-02-13 21:55:13 -05:00
XValues: LinearRange(1.0, 10.0),
YValues: LinearRange(1.0, 10.0),
2016-07-13 14:50:22 -04:00
},
},
}
buffer := bytes.NewBuffer([]byte{})
c.Render(PNG, buffer)
testutil.AssertTrue(t, true, "Render needs to finish.")
2016-07-13 14:50:22 -04:00
}
2017-02-03 14:27:58 -05:00
func TestChartValidatesSeries(t *testing.T) {
// replaced new assertions helper
2017-02-03 14:27:58 -05:00
c := Chart{
Series: []Series{
ContinuousSeries{
2019-02-13 21:55:13 -05:00
XValues: LinearRange(1.0, 10.0),
YValues: LinearRange(1.0, 10.0),
2017-02-03 14:27:58 -05:00
},
},
}
testutil.AssertNil(t, c.validateSeries())
2017-02-03 14:27:58 -05:00
c = Chart{
Series: []Series{
ContinuousSeries{
2019-02-13 21:55:13 -05:00
XValues: LinearRange(1.0, 10.0),
2017-02-03 14:27:58 -05:00
},
},
}
testutil.AssertNotNil(t, c.validateSeries())
2017-03-02 17:39:32 -05:00
}
func TestChartCheckRanges(t *testing.T) {
// replaced new assertions helper
2017-03-02 17:39:32 -05:00
c := Chart{
Series: []Series{
ContinuousSeries{
XValues: []float64{1.0, 2.0},
YValues: []float64{3.10, 3.14},
},
},
}
xr, yr, yra := c.getRanges()
testutil.AssertNil(t, c.checkRanges(xr, yr, yra))
2017-03-02 17:39:32 -05:00
}
func TestChartCheckRangesWithRanges(t *testing.T) {
// replaced new assertions helper
2017-03-02 17:39:32 -05:00
c := Chart{
XAxis: XAxis{
Range: &ContinuousRange{
Min: 0,
Max: 10,
},
},
YAxis: YAxis{
Range: &ContinuousRange{
Min: 0,
Max: 5,
},
},
Series: []Series{
ContinuousSeries{
XValues: []float64{1.0, 2.0},
YValues: []float64{3.14, 3.14},
},
},
}
2017-02-03 14:27:58 -05:00
2017-03-02 17:39:32 -05:00
xr, yr, yra := c.getRanges()
testutil.AssertNil(t, c.checkRanges(xr, yr, yra))
2017-02-03 14:27:58 -05:00
}
func at(i image.Image, x, y int) drawing.Color {
return drawing.ColorFromAlphaMixedRGBA(i.At(x, y).RGBA())
}
func TestChartE2ELine(t *testing.T) {
// replaced new assertions helper
c := Chart{
2019-02-16 14:17:39 -05:00
Height: 50,
Width: 50,
TitleStyle: Hidden(),
XAxis: HideXAxis(),
YAxis: HideYAxis(),
YAxisSecondary: HideYAxis(),
Canvas: Style{
Padding: BoxZero,
},
Background: Style{
Padding: BoxZero,
},
Series: []Series{
ContinuousSeries{
2019-02-13 21:55:13 -05:00
XValues: LinearRangeWithStep(0, 4, 1),
YValues: LinearRangeWithStep(0, 4, 1),
},
},
}
var buffer = &bytes.Buffer{}
err := c.Render(PNG, buffer)
testutil.AssertNil(t, err)
// do color tests ...
i, err := png.Decode(buffer)
testutil.AssertNil(t, err)
// test the bottom and top of the line
testutil.AssertEqual(t, drawing.ColorWhite, at(i, 0, 0))
testutil.AssertEqual(t, drawing.ColorWhite, at(i, 49, 49))
// test a line mid point
defaultSeriesColor := GetDefaultColor(0)
testutil.AssertEqual(t, defaultSeriesColor, at(i, 0, 49))
testutil.AssertEqual(t, defaultSeriesColor, at(i, 49, 0))
testutil.AssertEqual(t, drawing.ColorFromHex("bddbf6"), at(i, 24, 24))
}
func TestChartE2ELineWithFill(t *testing.T) {
// replaced new assertions helper
2019-09-09 22:57:56 -04:00
logBuffer := new(bytes.Buffer)
c := Chart{
Height: 50,
Width: 50,
Canvas: Style{
Padding: BoxZero,
},
Background: Style{
Padding: BoxZero,
},
2019-02-16 14:17:39 -05:00
TitleStyle: Hidden(),
XAxis: HideXAxis(),
YAxis: HideYAxis(),
YAxisSecondary: HideYAxis(),
Series: []Series{
ContinuousSeries{
Style: Style{
StrokeColor: drawing.ColorBlue,
FillColor: drawing.ColorRed,
},
2019-02-13 21:55:13 -05:00
XValues: LinearRangeWithStep(0, 4, 1),
YValues: LinearRangeWithStep(0, 4, 1),
},
},
2019-09-09 22:57:56 -04:00
Log: NewLogger(OptLoggerStdout(logBuffer), OptLoggerStderr(logBuffer)),
}
testutil.AssertEqual(t, 5, len(c.Series[0].(ContinuousSeries).XValues))
testutil.AssertEqual(t, 5, len(c.Series[0].(ContinuousSeries).YValues))
2019-02-13 21:55:13 -05:00
var buffer = &bytes.Buffer{}
err := c.Render(PNG, buffer)
testutil.AssertNil(t, err)
i, err := png.Decode(buffer)
testutil.AssertNil(t, err)
// test the bottom and top of the line
testutil.AssertEqual(t, drawing.ColorWhite, at(i, 0, 0))
testutil.AssertEqual(t, drawing.ColorRed, at(i, 49, 49))
// test a line mid point
defaultSeriesColor := drawing.ColorBlue
testutil.AssertEqual(t, defaultSeriesColor, at(i, 0, 49))
testutil.AssertEqual(t, defaultSeriesColor, at(i, 49, 0))
}
2024-08-23 11:41:44 -04:00
func Test_Chart_cve(t *testing.T) {
poc := StackedBarChart{
Title: "poc",
Bars: []StackedBar{
{
Name: "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
Values: []Value{
{Value: 1, Label: "infinite"},
{Value: 1, Label: "loop"},
},
},
},
}
var imgContent bytes.Buffer
err := poc.Render(PNG, &imgContent)
testutil.AssertNotNil(t, err)
}