refactor: return error if sum value of pie is not gt 0
This commit is contained in:
parent
981e5a0d27
commit
1be8d43405
2 changed files with 7 additions and 1 deletions
|
|
@ -23,6 +23,7 @@
|
||||||
package charts
|
package charts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
|
|
@ -68,6 +69,9 @@ func pieChartRender(opt pieChartOption, result *basicRenderResult) error {
|
||||||
values[index] = value
|
values[index] = value
|
||||||
total += value
|
total += value
|
||||||
}
|
}
|
||||||
|
if total <= 0 {
|
||||||
|
return errors.New("The sum value of pie chart should gt 0")
|
||||||
|
}
|
||||||
r := d.Render
|
r := d.Render
|
||||||
theme := NewTheme(opt.Theme)
|
theme := NewTheme(opt.Theme)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,10 +46,12 @@ func TestPieChartRender(t *testing.T) {
|
||||||
SeriesList: NewPieSeriesList([]float64{
|
SeriesList: NewPieSeriesList([]float64{
|
||||||
5,
|
5,
|
||||||
10,
|
10,
|
||||||
|
0,
|
||||||
}, PieSeriesOption{
|
}, PieSeriesOption{
|
||||||
Names: []string{
|
Names: []string{
|
||||||
"a",
|
"a",
|
||||||
"b",
|
"b",
|
||||||
|
"c",
|
||||||
},
|
},
|
||||||
Label: SeriesLabel{
|
Label: SeriesLabel{
|
||||||
Show: true,
|
Show: true,
|
||||||
|
|
@ -63,5 +65,5 @@ func TestPieChartRender(t *testing.T) {
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
data, err := d.Bytes()
|
data, err := d.Bytes()
|
||||||
assert.Nil(err)
|
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))
|
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><path d=\"M 125 75\nL 125 45\nA 30 30 0.00 0 1 125 45\nL 125 75\nZ\" style=\"stroke-width:1;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><path d=\"M 125 45\nL 125 35\nM 125 35\nL 135 35\" style=\"stroke-width:1;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><text x=\"138\" y=\"40\" style=\"stroke-width:0;stroke:none;fill:rgba(255,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">c: 0%</text></svg>", string(data))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue