test: add test for chart

This commit is contained in:
vicanso 2022-02-12 16:12:02 +08:00
parent 56709e22b7
commit 11fdd9121a
4 changed files with 304 additions and 16 deletions

271
chart_test.go Normal file

File diff suppressed because one or more lines are too long

View file

@ -460,7 +460,9 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
484, 484,
300, 300,
}, charts.PieSeriesOption{ }, charts.PieSeriesOption{
LabelShow: true, Label: charts.SeriesLabel{
Show: true,
},
Radius: "35%", Radius: "35%",
}), }),
}, },
@ -551,7 +553,9 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
285.9, 285.9,
204.5, 204.5,
}, charts.PieSeriesOption{ }, charts.PieSeriesOption{
LabelShow: true, Label: charts.SeriesLabel{
Show: true,
},
Radius: "35%", Radius: "35%",
}), }),
}, },

View file

@ -47,6 +47,22 @@ type pieChartOption struct {
SeriesList SeriesList SeriesList SeriesList
} }
func getPieRadius(diameter float64, radiusValue string) float64 {
var radius float64
if len(radiusValue) != 0 {
v := convertPercent(radiusValue)
if v != -1 {
radius = float64(diameter) * v
} else {
radius, _ = strconv.ParseFloat(radiusValue, 64)
}
}
if radius <= 0 {
radius = float64(diameter) * defaultRadiusPercent
}
return radius
}
func pieChartRender(opt pieChartOption, result *basicRenderResult) error { func pieChartRender(opt pieChartOption, result *basicRenderResult) error {
d, err := NewDraw(DrawOption{ d, err := NewDraw(DrawOption{
Parent: result.d, Parent: result.d,
@ -79,19 +95,8 @@ func pieChartRender(opt pieChartOption, result *basicRenderResult) error {
cy := box.Height() >> 1 cy := box.Height() >> 1
diameter := chart.MinInt(box.Width(), box.Height()) diameter := chart.MinInt(box.Width(), box.Height())
radius := getPieRadius(float64(diameter), radiusValue)
var radius float64
if len(radiusValue) != 0 {
v := convertPercent(radiusValue)
if v != -1 {
radius = float64(diameter) * v
} else {
radius, _ = strconv.ParseFloat(radiusValue, 64)
}
}
if radius <= 0 {
radius = float64(diameter) * defaultRadiusPercent
}
labelLineWidth := 15 labelLineWidth := 15
if radius < 50 { if radius < 50 {
labelLineWidth = 10 labelLineWidth = 10

View file

@ -30,6 +30,14 @@ import (
"github.com/wcharczuk/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2/drawing"
) )
func TestGetPieRadius(t *testing.T) {
assert := assert.New(t)
assert.Equal(50.0, getPieRadius(100, "50%"))
assert.Equal(30.0, getPieRadius(100, "30"))
assert.Equal(40.0, getPieRadius(100, ""))
}
func TestPieChartRender(t *testing.T) { func TestPieChartRender(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)