test: add test for pie chart
This commit is contained in:
parent
cae55c3163
commit
56709e22b7
3 changed files with 86 additions and 7 deletions
67
pie_chart_test.go
Normal file
67
pie_chart_test.go
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
// 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"
|
||||||
|
"github.com/wcharczuk/go-chart/v2/drawing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPieChartRender(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
d, err := NewDraw(DrawOption{
|
||||||
|
Width: 250,
|
||||||
|
Height: 150,
|
||||||
|
})
|
||||||
|
assert.Nil(err)
|
||||||
|
|
||||||
|
f, _ := chart.GetDefaultFont()
|
||||||
|
|
||||||
|
err = pieChartRender(pieChartOption{
|
||||||
|
Font: f,
|
||||||
|
SeriesList: NewPieSeriesList([]float64{
|
||||||
|
5,
|
||||||
|
10,
|
||||||
|
}, PieSeriesOption{
|
||||||
|
Names: []string{
|
||||||
|
"a",
|
||||||
|
"b",
|
||||||
|
},
|
||||||
|
Label: SeriesLabel{
|
||||||
|
Show: true,
|
||||||
|
Color: drawing.ColorRed,
|
||||||
|
},
|
||||||
|
Radius: "20%",
|
||||||
|
}),
|
||||||
|
}, &basicRenderResult{
|
||||||
|
d: d,
|
||||||
|
})
|
||||||
|
assert.Nil(err)
|
||||||
|
data, err := d.Bytes()
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"250\" height=\"150\">\\n<path d=\"M 125 75\nL 125 45\nA 30 30 120.00 0 1 150 89\nL 125 75\nZ\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path d=\"M 150 60\nL 159 55\nM 159 55\nL 169 55\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"172\" y=\"60\" style=\"stroke-width:0;stroke:none;fill:rgba(255,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">a: 33.33%</text><path d=\"M 125 75\nL 150 89\nA 30 30 240.00 1 1 125 45\nL 125 75\nZ\" style=\"stroke-width:1;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><path d=\"M 100 90\nL 91 95\nM 91 95\nL 81 95\" style=\"stroke-width:1;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><text x=\"22\" y=\"100\" style=\"stroke-width:0;stroke:none;fill:rgba(255,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">b: 66.66%</text></svg>", string(data))
|
||||||
|
}
|
||||||
12
series.go
12
series.go
|
|
@ -95,7 +95,8 @@ type SeriesList []Series
|
||||||
|
|
||||||
type PieSeriesOption struct {
|
type PieSeriesOption struct {
|
||||||
Radius string
|
Radius string
|
||||||
LabelShow bool
|
Label SeriesLabel
|
||||||
|
Names []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPieSeriesList(values []float64, opts ...PieSeriesOption) []Series {
|
func NewPieSeriesList(values []float64, opts ...PieSeriesOption) []Series {
|
||||||
|
|
@ -105,6 +106,10 @@ func NewPieSeriesList(values []float64, opts ...PieSeriesOption) []Series {
|
||||||
opt = opts[0]
|
opt = opts[0]
|
||||||
}
|
}
|
||||||
for index, v := range values {
|
for index, v := range values {
|
||||||
|
name := ""
|
||||||
|
if index < len(opt.Names) {
|
||||||
|
name = opt.Names[index]
|
||||||
|
}
|
||||||
s := Series{
|
s := Series{
|
||||||
Type: ChartTypePie,
|
Type: ChartTypePie,
|
||||||
Data: []SeriesData{
|
Data: []SeriesData{
|
||||||
|
|
@ -113,9 +118,8 @@ func NewPieSeriesList(values []float64, opts ...PieSeriesOption) []Series {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Radius: opt.Radius,
|
Radius: opt.Radius,
|
||||||
Label: SeriesLabel{
|
Label: opt.Label,
|
||||||
Show: opt.LabelShow,
|
Name: name,
|
||||||
},
|
|
||||||
}
|
}
|
||||||
result[index] = s
|
result[index] = s
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ func TestNewPieSeriesList(t *testing.T) {
|
||||||
assert.Equal([]Series{
|
assert.Equal([]Series{
|
||||||
{
|
{
|
||||||
Type: ChartTypePie,
|
Type: ChartTypePie,
|
||||||
|
Name: "a",
|
||||||
Label: SeriesLabel{
|
Label: SeriesLabel{
|
||||||
Show: true,
|
Show: true,
|
||||||
},
|
},
|
||||||
|
|
@ -81,6 +82,7 @@ func TestNewPieSeriesList(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Type: ChartTypePie,
|
Type: ChartTypePie,
|
||||||
|
Name: "b",
|
||||||
Label: SeriesLabel{
|
Label: SeriesLabel{
|
||||||
Show: true,
|
Show: true,
|
||||||
},
|
},
|
||||||
|
|
@ -96,7 +98,13 @@ func TestNewPieSeriesList(t *testing.T) {
|
||||||
2,
|
2,
|
||||||
}, PieSeriesOption{
|
}, PieSeriesOption{
|
||||||
Radius: "30%",
|
Radius: "30%",
|
||||||
LabelShow: true,
|
Label: SeriesLabel{
|
||||||
|
Show: true,
|
||||||
|
},
|
||||||
|
Names: []string{
|
||||||
|
"a",
|
||||||
|
"b",
|
||||||
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue