2016-07-06 21:54:00 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-07-13 14:50:22 -04:00
|
|
|
"math"
|
2016-07-06 21:54:00 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/blendlabs/go-assert"
|
2016-07-17 01:56:12 -04:00
|
|
|
"github.com/wcharczuk/go-chart/drawing"
|
2016-07-06 21:54:00 -04:00
|
|
|
)
|
|
|
|
|
2016-07-12 23:34:59 -04:00
|
|
|
func TestChartGetDPI(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
unset := Chart{}
|
|
|
|
assert.Equal(DefaultDPI, unset.GetDPI())
|
|
|
|
assert.Equal(192, unset.GetDPI(192))
|
|
|
|
|
|
|
|
set := Chart{DPI: 128}
|
|
|
|
assert.Equal(128, set.GetDPI())
|
|
|
|
assert.Equal(128, set.GetDPI(192))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartGetFont(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
f, err := GetDefaultFont()
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
unset := Chart{}
|
|
|
|
assert.Nil(unset.GetFont())
|
|
|
|
|
|
|
|
set := Chart{Font: f}
|
|
|
|
assert.NotNil(set.GetFont())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartGetWidth(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
unset := Chart{}
|
|
|
|
assert.Equal(DefaultChartWidth, unset.GetWidth())
|
|
|
|
|
|
|
|
set := Chart{Width: DefaultChartWidth + 10}
|
|
|
|
assert.Equal(DefaultChartWidth+10, set.GetWidth())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartGetHeight(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
unset := Chart{}
|
|
|
|
assert.Equal(DefaultChartHeight, unset.GetHeight())
|
|
|
|
|
|
|
|
set := Chart{Height: DefaultChartHeight + 10}
|
|
|
|
assert.Equal(DefaultChartHeight+10, set.GetHeight())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartGetRanges(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
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()
|
2016-07-21 17:11:27 -04:00
|
|
|
assert.Equal(-2.0, xrange.GetMin())
|
|
|
|
assert.Equal(5.0, xrange.GetMax())
|
2016-07-12 23:34:59 -04:00
|
|
|
|
2016-07-21 17:11:27 -04:00
|
|
|
assert.Equal(-2.1, yrange.GetMin())
|
|
|
|
assert.Equal(4.5, yrange.GetMax())
|
2016-07-12 23:34:59 -04:00
|
|
|
|
2016-07-21 17:11:27 -04:00
|
|
|
assert.Equal(10.0, yrangeAlt.GetMin())
|
|
|
|
assert.Equal(14.0, yrangeAlt.GetMax())
|
2016-07-12 23:34:59 -04:00
|
|
|
|
|
|
|
cSet := Chart{
|
|
|
|
XAxis: XAxis{
|
2016-07-21 17:11:27 -04:00
|
|
|
Range: &ContinuousRange{Min: 9.8, Max: 19.8},
|
2016-07-12 23:34:59 -04:00
|
|
|
},
|
|
|
|
YAxis: YAxis{
|
2016-07-21 17:11:27 -04:00
|
|
|
Range: &ContinuousRange{Min: 9.9, Max: 19.9},
|
2016-07-12 23:34:59 -04:00
|
|
|
},
|
|
|
|
YAxisSecondary: YAxis{
|
2016-07-21 17:11:27 -04:00
|
|
|
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()
|
2016-07-21 17:11:27 -04:00
|
|
|
assert.Equal(9.8, xr2.GetMin())
|
|
|
|
assert.Equal(19.8, xr2.GetMax())
|
2016-07-12 23:34:59 -04:00
|
|
|
|
2016-07-21 17:11:27 -04:00
|
|
|
assert.Equal(9.9, yr2.GetMin())
|
|
|
|
assert.Equal(19.9, yr2.GetMax())
|
2016-07-12 23:34:59 -04:00
|
|
|
|
2016-07-21 17:11:27 -04:00
|
|
|
assert.Equal(9.7, yra2.GetMin())
|
|
|
|
assert.Equal(19.7, yra2.GetMax())
|
2016-07-12 23:34:59 -04:00
|
|
|
}
|
|
|
|
|
2016-07-17 01:56:12 -04:00
|
|
|
func TestChartGetRangesUseTicks(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
// 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"},
|
|
|
|
},
|
2016-07-21 17:11:27 -04:00
|
|
|
Range: &ContinuousRange{
|
2016-07-17 01:56:12 -04:00
|
|
|
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()
|
2016-07-21 17:11:27 -04:00
|
|
|
assert.Equal(-2.0, xr.GetMin())
|
|
|
|
assert.Equal(2.0, xr.GetMax())
|
|
|
|
assert.Equal(0.0, yr.GetMin())
|
|
|
|
assert.Equal(5.0, yr.GetMax())
|
2016-07-17 01:56:12 -04:00
|
|
|
assert.True(yar.IsZero(), yar.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartGetRangesUseUserRanges(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
c := Chart{
|
|
|
|
YAxis: YAxis{
|
2016-07-21 17:11:27 -04:00
|
|
|
Range: &ContinuousRange{
|
2016-07-17 01:56:12 -04:00
|
|
|
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()
|
2016-07-21 17:11:27 -04:00
|
|
|
assert.Equal(-2.0, xr.GetMin())
|
|
|
|
assert.Equal(2.0, xr.GetMax())
|
|
|
|
assert.Equal(-5.0, yr.GetMin())
|
|
|
|
assert.Equal(5.0, yr.GetMax())
|
2016-07-17 01:56:12 -04:00
|
|
|
assert.True(yar.IsZero(), yar.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartGetBackgroundStyle(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
c := Chart{
|
|
|
|
Background: Style{
|
|
|
|
FillColor: drawing.ColorBlack,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := c.getBackgroundStyle()
|
|
|
|
assert.Equal(bs.FillColor.String(), drawing.ColorBlack.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartGetCanvasStyle(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
c := Chart{
|
|
|
|
Canvas: Style{
|
|
|
|
FillColor: drawing.ColorBlack,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := c.getCanvasStyle()
|
|
|
|
assert.Equal(bs.FillColor.String(), drawing.ColorBlack.String())
|
|
|
|
}
|
|
|
|
|
2016-07-13 01:04:30 -04:00
|
|
|
func TestChartGetDefaultCanvasBox(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
c := Chart{}
|
|
|
|
canvasBoxDefault := c.getDefaultCanvasBox()
|
|
|
|
assert.False(canvasBoxDefault.IsZero())
|
|
|
|
assert.Equal(DefaultBackgroundPadding.Top, canvasBoxDefault.Top)
|
|
|
|
assert.Equal(DefaultBackgroundPadding.Left, canvasBoxDefault.Left)
|
|
|
|
assert.Equal(c.GetWidth()-DefaultBackgroundPadding.Right, canvasBoxDefault.Right)
|
|
|
|
assert.Equal(c.GetHeight()-DefaultBackgroundPadding.Bottom, canvasBoxDefault.Bottom)
|
|
|
|
|
|
|
|
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()
|
|
|
|
assert.False(canvasBoxCustom.IsZero())
|
|
|
|
assert.Equal(DefaultBackgroundPadding.Top+1, canvasBoxCustom.Top)
|
|
|
|
assert.Equal(DefaultBackgroundPadding.Left+1, canvasBoxCustom.Left)
|
|
|
|
assert.Equal(c.GetWidth()-(DefaultBackgroundPadding.Right+1), canvasBoxCustom.Right)
|
|
|
|
assert.Equal(c.GetHeight()-(DefaultBackgroundPadding.Bottom+1), canvasBoxCustom.Bottom)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartGetValueFormatters(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
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()
|
|
|
|
assert.NotNil(dxf)
|
|
|
|
assert.NotNil(dyf)
|
|
|
|
assert.NotNil(dyaf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartHasAxes(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
assert.False(Chart{}.hasAxes())
|
|
|
|
|
|
|
|
x := Chart{
|
|
|
|
XAxis: XAxis{
|
|
|
|
Style: Style{
|
|
|
|
Show: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.True(x.hasAxes())
|
|
|
|
|
|
|
|
y := Chart{
|
|
|
|
YAxis: YAxis{
|
|
|
|
Style: Style{
|
|
|
|
Show: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.True(y.hasAxes())
|
|
|
|
|
|
|
|
ya := Chart{
|
|
|
|
YAxisSecondary: YAxis{
|
|
|
|
Style: Style{
|
|
|
|
Show: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.True(ya.hasAxes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartGetAxesTicks(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
r, err := PNG(1024, 1024)
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
c := Chart{
|
|
|
|
XAxis: XAxis{
|
|
|
|
Style: Style{Show: true},
|
2016-07-21 17:11:27 -04:00
|
|
|
Range: &ContinuousRange{Min: 9.8, Max: 19.8},
|
2016-07-13 01:04:30 -04:00
|
|
|
},
|
|
|
|
YAxis: YAxis{
|
|
|
|
Style: Style{Show: true},
|
2016-07-21 17:11:27 -04:00
|
|
|
Range: &ContinuousRange{Min: 9.9, Max: 19.9},
|
2016-07-13 01:04:30 -04:00
|
|
|
},
|
|
|
|
YAxisSecondary: YAxis{
|
|
|
|
Style: Style{Show: true},
|
2016-07-21 17:11:27 -04:00
|
|
|
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)
|
|
|
|
assert.NotEmpty(xt)
|
|
|
|
assert.NotEmpty(yt)
|
|
|
|
assert.NotEmpty(yat)
|
|
|
|
}
|
|
|
|
|
2016-07-06 21:54:00 -04:00
|
|
|
func TestChartSingleSeries(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
now := time.Now()
|
|
|
|
c := Chart{
|
|
|
|
Title: "Hello!",
|
|
|
|
Width: 1024,
|
|
|
|
Height: 400,
|
2016-07-10 14:27:12 -04:00
|
|
|
YAxis: YAxis{
|
2016-07-21 17:11:27 -04:00
|
|
|
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)
|
|
|
|
assert.NotEmpty(buffer.Bytes())
|
|
|
|
}
|
2016-07-13 14:50:22 -04:00
|
|
|
|
|
|
|
func TestChartRegressionBadRanges(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
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)
|
|
|
|
assert.True(true, "Render needs to finish.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChartRegressionBadRangesByUser(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
c := Chart{
|
|
|
|
YAxis: YAxis{
|
2016-07-21 17:11:27 -04:00
|
|
|
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{
|
2016-07-29 21:24:25 -04:00
|
|
|
XValues: Sequence.Float64(1.0, 10.0),
|
|
|
|
YValues: Sequence.Float64(1.0, 10.0),
|
2016-07-13 14:50:22 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buffer := bytes.NewBuffer([]byte{})
|
|
|
|
c.Render(PNG, buffer)
|
|
|
|
assert.True(true, "Render needs to finish.")
|
|
|
|
}
|