test: add test for charts
This commit is contained in:
parent
a6b92f1d47
commit
212a51083f
11 changed files with 963 additions and 16 deletions
|
|
@ -201,17 +201,7 @@ func ChildOptionFunc(child ...ChartOption) OptionFunc {
|
||||||
// RadarIndicatorOptionFunc set radar indicator of chart
|
// RadarIndicatorOptionFunc set radar indicator of chart
|
||||||
func RadarIndicatorOptionFunc(names []string, values []float64) OptionFunc {
|
func RadarIndicatorOptionFunc(names []string, values []float64) OptionFunc {
|
||||||
return func(opt *ChartOption) {
|
return func(opt *ChartOption) {
|
||||||
if len(names) != len(values) {
|
opt.RadarIndicators = NewRadarIndicators(names, values)
|
||||||
return
|
|
||||||
}
|
|
||||||
indicators := make([]RadarIndicator, len(names))
|
|
||||||
for index, name := range names {
|
|
||||||
indicators[index] = RadarIndicator{
|
|
||||||
Name: name,
|
|
||||||
Max: values[index],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
opt.RadarIndicators = indicators
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
263
echarts_test.go
Normal file
263
echarts_test.go
Normal file
|
|
@ -0,0 +1,263 @@
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
// Copyright (c) 2022 Tree Xie
|
||||||
|
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
package charts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConvertToArray(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
assert.Equal([]byte(`[1]`), convertToArray([]byte("1")))
|
||||||
|
assert.Equal([]byte(`[1]`), convertToArray([]byte("[1]")))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEChartsPosition(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
var p EChartsPosition
|
||||||
|
err := p.UnmarshalJSON([]byte("1"))
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(EChartsPosition("1"), p)
|
||||||
|
err = p.UnmarshalJSON([]byte(`"left"`))
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(EChartsPosition("left"), p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEChartsSeriesDataValue(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
es := EChartsSeriesDataValue{}
|
||||||
|
err := es.UnmarshalJSON([]byte(`[1, 2]`))
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(EChartsSeriesDataValue{
|
||||||
|
values: []float64{
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
},
|
||||||
|
}, es)
|
||||||
|
assert.Equal(1.0, es.First())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEChartsSeriesData(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
es := EChartsSeriesData{}
|
||||||
|
err := es.UnmarshalJSON([]byte("1.1"))
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(EChartsSeriesDataValue{
|
||||||
|
values: []float64{
|
||||||
|
1.1,
|
||||||
|
},
|
||||||
|
}, es.Value)
|
||||||
|
|
||||||
|
err = es.UnmarshalJSON([]byte(`{"value":200,"itemStyle":{"color":"#a90000"}}`))
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(EChartsSeriesData{
|
||||||
|
Value: EChartsSeriesDataValue{
|
||||||
|
values: []float64{
|
||||||
|
200.0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ItemStyle: EChartStyle{
|
||||||
|
Color: "#a90000",
|
||||||
|
},
|
||||||
|
}, es)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEChartsXAxis(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
ex := EChartsXAxis{}
|
||||||
|
err := ex.UnmarshalJSON([]byte(`{"boundaryGap": true, "splitNumber": 5, "data": ["a", "b"], "type": "value"}`))
|
||||||
|
assert.Nil(err)
|
||||||
|
|
||||||
|
assert.Equal(EChartsXAxis{
|
||||||
|
Data: []EChartsXAxisData{
|
||||||
|
{
|
||||||
|
BoundaryGap: TrueFlag(),
|
||||||
|
SplitNumber: 5,
|
||||||
|
Data: []string{
|
||||||
|
"a",
|
||||||
|
"b",
|
||||||
|
},
|
||||||
|
Type: "value",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, ex)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEChartsOption(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
opt := EChartsOption{}
|
||||||
|
err := json.Unmarshal([]byte(`{
|
||||||
|
"title": {
|
||||||
|
"text": "Rainfall vs Evaporation",
|
||||||
|
"subtext": "Fake Data"
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"trigger": "axis"
|
||||||
|
},
|
||||||
|
"legend": {
|
||||||
|
"data": [
|
||||||
|
"Rainfall",
|
||||||
|
"Evaporation"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"toolbox": {
|
||||||
|
"show": true,
|
||||||
|
"feature": {
|
||||||
|
"dataView": {
|
||||||
|
"show": true,
|
||||||
|
"readOnly": false
|
||||||
|
},
|
||||||
|
"magicType": {
|
||||||
|
"show": true,
|
||||||
|
"type": [
|
||||||
|
"line",
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"restore": {
|
||||||
|
"show": true
|
||||||
|
},
|
||||||
|
"saveAsImage": {
|
||||||
|
"show": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"calculable": true,
|
||||||
|
"xAxis": [
|
||||||
|
{
|
||||||
|
"type": "category",
|
||||||
|
"data": [
|
||||||
|
"Jan",
|
||||||
|
"Feb",
|
||||||
|
"Mar",
|
||||||
|
"Apr",
|
||||||
|
"May",
|
||||||
|
"Jun",
|
||||||
|
"Jul",
|
||||||
|
"Aug",
|
||||||
|
"Sep",
|
||||||
|
"Oct",
|
||||||
|
"Nov",
|
||||||
|
"Dec"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"yAxis": [
|
||||||
|
{
|
||||||
|
"type": "value"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"series": [
|
||||||
|
{
|
||||||
|
"name": "Rainfall",
|
||||||
|
"type": "bar",
|
||||||
|
"data": [
|
||||||
|
2,
|
||||||
|
4.9,
|
||||||
|
7,
|
||||||
|
23.2,
|
||||||
|
25.6,
|
||||||
|
76.7,
|
||||||
|
135.6,
|
||||||
|
162.2,
|
||||||
|
32.6,
|
||||||
|
20,
|
||||||
|
6.4,
|
||||||
|
3.3
|
||||||
|
],
|
||||||
|
"markPoint": {
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"type": "max",
|
||||||
|
"name": "Max"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "min",
|
||||||
|
"name": "Min"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"markLine": {
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"type": "average",
|
||||||
|
"name": "Avg"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Evaporation",
|
||||||
|
"type": "bar",
|
||||||
|
"data": [
|
||||||
|
2.6,
|
||||||
|
5.9,
|
||||||
|
9,
|
||||||
|
26.4,
|
||||||
|
28.7,
|
||||||
|
70.7,
|
||||||
|
175.6,
|
||||||
|
182.2,
|
||||||
|
48.7,
|
||||||
|
18.8,
|
||||||
|
6,
|
||||||
|
2.3
|
||||||
|
],
|
||||||
|
"markPoint": {
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"name": "Max",
|
||||||
|
"value": 182.2,
|
||||||
|
"xAxis": 7,
|
||||||
|
"yAxis": 183
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Min",
|
||||||
|
"value": 2.3,
|
||||||
|
"xAxis": 11,
|
||||||
|
"yAxis": 3
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"markLine": {
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"type": "average",
|
||||||
|
"name": "Avg"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`), &opt)
|
||||||
|
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.NotEmpty(opt.Series)
|
||||||
|
}
|
||||||
69
grid_test.go
Normal file
69
grid_test.go
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
// Copyright (c) 2022 Tree Xie
|
||||||
|
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
package charts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/wcharczuk/go-chart/v2/drawing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGrid(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
render func(*Painter) ([]byte, error)
|
||||||
|
result string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
render: func(p *Painter) ([]byte, error) {
|
||||||
|
_, err := NewGridPainter(p, GridPainterOption{
|
||||||
|
StrokeColor: drawing.ColorBlack,
|
||||||
|
Column: 6,
|
||||||
|
Row: 6,
|
||||||
|
IgnoreFirstRow: true,
|
||||||
|
IgnoreLastRow: true,
|
||||||
|
IgnoreFirstColumn: true,
|
||||||
|
IgnoreLastColumn: true,
|
||||||
|
}).Render()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p.Bytes()
|
||||||
|
},
|
||||||
|
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 100 0\nL 100 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 200 0\nL 200 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 300 0\nL 300 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 400 0\nL 400 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 500 0\nL 500 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 66\nL 600 66\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 133\nL 600 133\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 200\nL 600 200\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 266\nL 600 266\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 333\nL 600 333\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/></svg>",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
p, err := NewPainter(PainterOptions{
|
||||||
|
Type: ChartOutputSVG,
|
||||||
|
Width: 600,
|
||||||
|
Height: 400,
|
||||||
|
}, PainterThemeOption(defaultTheme))
|
||||||
|
assert.Nil(err)
|
||||||
|
data, err := tt.render(p)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(tt.result, string(data))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -89,10 +89,7 @@ func TestNewLegend(t *testing.T) {
|
||||||
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 60 3\nL 90 3\nL 90 16\nL 60 16\nL 60 3\" style=\"stroke-width:0;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"92\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">One</text><path d=\"M 60 23\nL 90 23\nL 90 36\nL 60 36\nL 60 23\" style=\"stroke-width:0;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><text x=\"92\" y=\"35\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Two</text><path d=\"M 60 43\nL 90 43\nL 90 56\nL 60 56\nL 60 43\" style=\"stroke-width:0;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><text x=\"92\" y=\"55\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Three</text></svg>",
|
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 60 3\nL 90 3\nL 90 16\nL 60 16\nL 60 3\" style=\"stroke-width:0;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"92\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">One</text><path d=\"M 60 23\nL 90 23\nL 90 36\nL 60 36\nL 60 23\" style=\"stroke-width:0;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><text x=\"92\" y=\"35\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Two</text><path d=\"M 60 43\nL 90 43\nL 90 56\nL 60 56\nL 60 43\" style=\"stroke-width:0;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><text x=\"92\" y=\"55\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Three</text></svg>",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for index, tt := range tests {
|
for _, tt := range tests {
|
||||||
if index != 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
p, err := NewPainter(PainterOptions{
|
p, err := NewPainter(PainterOptions{
|
||||||
Type: ChartOutputSVG,
|
Type: ChartOutputSVG,
|
||||||
Width: 600,
|
Width: 600,
|
||||||
|
|
|
||||||
219
line_chart_test.go
Normal file
219
line_chart_test.go
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -24,6 +24,7 @@ package charts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
|
"github.com/wcharczuk/go-chart/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewMarkLine(markLineTypes ...string) SeriesMarkLine {
|
func NewMarkLine(markLineTypes ...string) SeriesMarkLine {
|
||||||
|
|
@ -70,6 +71,10 @@ func (m *markLinePainter) Render() (Box, error) {
|
||||||
if len(s.MarkLine.Data) == 0 {
|
if len(s.MarkLine.Data) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
font := opt.Font
|
||||||
|
if font == nil {
|
||||||
|
font, _ = chart.GetDefaultFont()
|
||||||
|
}
|
||||||
summary := s.Summary()
|
summary := s.Summary()
|
||||||
for _, markLine := range s.MarkLine.Data {
|
for _, markLine := range s.MarkLine.Data {
|
||||||
// 由于mark line会修改style,因此每次重新设置
|
// 由于mark line会修改style,因此每次重新设置
|
||||||
|
|
@ -82,7 +87,7 @@ func (m *markLinePainter) Render() (Box, error) {
|
||||||
2,
|
2,
|
||||||
},
|
},
|
||||||
}).OverrideTextStyle(Style{
|
}).OverrideTextStyle(Style{
|
||||||
Font: opt.Font,
|
Font: font,
|
||||||
FontColor: opt.FontColor,
|
FontColor: opt.FontColor,
|
||||||
FontSize: labelFontSize,
|
FontSize: labelFontSize,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
89
mark_line_test.go
Normal file
89
mark_line_test.go
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
// Copyright (c) 2022 Tree Xie
|
||||||
|
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
package charts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/wcharczuk/go-chart/v2/drawing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMarkLine(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
render func(*Painter) ([]byte, error)
|
||||||
|
result string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
render: func(p *Painter) ([]byte, error) {
|
||||||
|
markLine := NewMarkLinePainter(p)
|
||||||
|
series := NewSeriesFromValues([]float64{
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
})
|
||||||
|
series.MarkLine = NewMarkLine(
|
||||||
|
SeriesMarkDataTypeMax,
|
||||||
|
SeriesMarkDataTypeAverage,
|
||||||
|
SeriesMarkDataTypeMin,
|
||||||
|
)
|
||||||
|
markLine.Add(markLineRenderOption{
|
||||||
|
FillColor: drawing.ColorBlack,
|
||||||
|
FontColor: drawing.ColorBlack,
|
||||||
|
StrokeColor: drawing.ColorBlack,
|
||||||
|
Series: series,
|
||||||
|
Range: NewRange(AxisRangeOption{
|
||||||
|
Min: 0,
|
||||||
|
Max: 5,
|
||||||
|
Size: p.Height(),
|
||||||
|
DivideCount: 6,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
_, err := markLine.Render()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p.Bytes()
|
||||||
|
},
|
||||||
|
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<circle cx=\"23\" cy=\"290\" r=\"3\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 29 290\nL 562 290\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 562 285\nL 578 290\nL 562 295\nL 567 290\nL 562 285\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><text x=\"580\" y=\"294\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">3</text><circle cx=\"23\" cy=\"320\" r=\"3\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 29 320\nL 562 320\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 562 315\nL 578 320\nL 562 325\nL 567 320\nL 562 315\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><text x=\"580\" y=\"324\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">2</text><circle cx=\"23\" cy=\"350\" r=\"3\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 29 350\nL 562 350\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 562 345\nL 578 350\nL 562 355\nL 567 350\nL 562 345\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><text x=\"580\" y=\"354\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">1</text></svg>",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
p, err := NewPainter(PainterOptions{
|
||||||
|
Type: ChartOutputSVG,
|
||||||
|
Width: 600,
|
||||||
|
Height: 400,
|
||||||
|
}, PainterThemeOption(defaultTheme))
|
||||||
|
assert.Nil(err)
|
||||||
|
data, err := tt.render(p.Child(PainterPaddingOption(Box{
|
||||||
|
Left: 20,
|
||||||
|
Top: 20,
|
||||||
|
Right: 20,
|
||||||
|
Bottom: 20,
|
||||||
|
})))
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(tt.result, string(data))
|
||||||
|
}
|
||||||
|
}
|
||||||
92
mark_point_test.go
Normal file
92
mark_point_test.go
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
// Copyright (c) 2022 Tree Xie
|
||||||
|
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
package charts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/wcharczuk/go-chart/v2/drawing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMarkPoint(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
render func(*Painter) ([]byte, error)
|
||||||
|
result string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
render: func(p *Painter) ([]byte, error) {
|
||||||
|
series := NewSeriesFromValues([]float64{
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
})
|
||||||
|
series.MarkPoint = NewMarkPoint(SeriesMarkDataTypeMax)
|
||||||
|
markPoint := NewMarkPointPainter(p)
|
||||||
|
markPoint.Add(markPointRenderOption{
|
||||||
|
FillColor: drawing.ColorBlack,
|
||||||
|
Series: series,
|
||||||
|
Points: []Point{
|
||||||
|
{
|
||||||
|
X: 10,
|
||||||
|
Y: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
X: 30,
|
||||||
|
Y: 30,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
X: 50,
|
||||||
|
Y: 50,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
_, err := markPoint.Render()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p.Bytes()
|
||||||
|
},
|
||||||
|
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 67 62\nA 15 15 330.00 1 1 73 62\nL 70 48\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><path d=\"M 55 48\nQ70,85 85,48\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><text x=\"66\" y=\"53\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">3</text></svg>",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
p, err := NewPainter(PainterOptions{
|
||||||
|
Type: ChartOutputSVG,
|
||||||
|
Width: 600,
|
||||||
|
Height: 400,
|
||||||
|
}, PainterThemeOption(defaultTheme))
|
||||||
|
assert.Nil(err)
|
||||||
|
data, err := tt.render(p.Child(PainterPaddingOption(Box{
|
||||||
|
Left: 20,
|
||||||
|
Top: 20,
|
||||||
|
Right: 20,
|
||||||
|
Bottom: 20,
|
||||||
|
})))
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(tt.result, string(data))
|
||||||
|
}
|
||||||
|
}
|
||||||
100
pie_chart_test.go
Normal file
100
pie_chart_test.go
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -62,6 +62,20 @@ type RadarChartOption struct {
|
||||||
backgroundIsFilled bool
|
backgroundIsFilled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewRadarIndicators(names []string, values []float64) []RadarIndicator {
|
||||||
|
if len(names) != len(values) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
indicators := make([]RadarIndicator, len(names))
|
||||||
|
for index, name := range names {
|
||||||
|
indicators[index] = RadarIndicator{
|
||||||
|
Name: name,
|
||||||
|
Max: values[index],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return indicators
|
||||||
|
}
|
||||||
|
|
||||||
func NewRadarChart(p *Painter, opt RadarChartOption) *radarChart {
|
func NewRadarChart(p *Painter, opt RadarChartOption) *radarChart {
|
||||||
if opt.Theme == nil {
|
if opt.Theme == nil {
|
||||||
opt.Theme = defaultTheme
|
opt.Theme = defaultTheme
|
||||||
|
|
|
||||||
109
radar_chart_test.go
Normal file
109
radar_chart_test.go
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue