test: add test for chart
This commit is contained in:
parent
56709e22b7
commit
11fdd9121a
4 changed files with 304 additions and 16 deletions
271
chart_test.go
Normal file
271
chart_test.go
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -460,8 +460,10 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
|
|||
484,
|
||||
300,
|
||||
}, charts.PieSeriesOption{
|
||||
LabelShow: true,
|
||||
Radius: "35%",
|
||||
Label: charts.SeriesLabel{
|
||||
Show: true,
|
||||
},
|
||||
Radius: "35%",
|
||||
}),
|
||||
},
|
||||
// 多图展示
|
||||
|
|
@ -551,8 +553,10 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
|
|||
285.9,
|
||||
204.5,
|
||||
}, charts.PieSeriesOption{
|
||||
LabelShow: true,
|
||||
Radius: "35%",
|
||||
Label: charts.SeriesLabel{
|
||||
Show: true,
|
||||
},
|
||||
Radius: "35%",
|
||||
}),
|
||||
},
|
||||
},
|
||||
|
|
|
|||
29
pie_chart.go
29
pie_chart.go
|
|
@ -47,6 +47,22 @@ type pieChartOption struct {
|
|||
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 {
|
||||
d, err := NewDraw(DrawOption{
|
||||
Parent: result.d,
|
||||
|
|
@ -79,19 +95,8 @@ func pieChartRender(opt pieChartOption, result *basicRenderResult) error {
|
|||
cy := box.Height() >> 1
|
||||
|
||||
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
|
||||
if radius < 50 {
|
||||
labelLineWidth = 10
|
||||
|
|
|
|||
|
|
@ -30,6 +30,14 @@ import (
|
|||
"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) {
|
||||
assert := assert.New(t)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue