diff --git a/pie_chart_test.go b/pie_chart_test.go new file mode 100644 index 0000000..7cb9fde --- /dev/null +++ b/pie_chart_test.go @@ -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("\\na: 33.33%b: 66.66%", string(data)) +} diff --git a/series.go b/series.go index 7876579..f986c54 100644 --- a/series.go +++ b/series.go @@ -94,8 +94,9 @@ type Series struct { type SeriesList []Series type PieSeriesOption struct { - Radius string - LabelShow bool + Radius string + Label SeriesLabel + Names []string } func NewPieSeriesList(values []float64, opts ...PieSeriesOption) []Series { @@ -105,6 +106,10 @@ func NewPieSeriesList(values []float64, opts ...PieSeriesOption) []Series { opt = opts[0] } for index, v := range values { + name := "" + if index < len(opt.Names) { + name = opt.Names[index] + } s := Series{ Type: ChartTypePie, Data: []SeriesData{ @@ -113,9 +118,8 @@ func NewPieSeriesList(values []float64, opts ...PieSeriesOption) []Series { }, }, Radius: opt.Radius, - Label: SeriesLabel{ - Show: opt.LabelShow, - }, + Label: opt.Label, + Name: name, } result[index] = s } diff --git a/series_test.go b/series_test.go index cab14c3..aae83de 100644 --- a/series_test.go +++ b/series_test.go @@ -69,6 +69,7 @@ func TestNewPieSeriesList(t *testing.T) { assert.Equal([]Series{ { Type: ChartTypePie, + Name: "a", Label: SeriesLabel{ Show: true, }, @@ -81,6 +82,7 @@ func TestNewPieSeriesList(t *testing.T) { }, { Type: ChartTypePie, + Name: "b", Label: SeriesLabel{ Show: true, }, @@ -95,8 +97,14 @@ func TestNewPieSeriesList(t *testing.T) { 1, 2, }, PieSeriesOption{ - Radius: "30%", - LabelShow: true, + Radius: "30%", + Label: SeriesLabel{ + Show: true, + }, + Names: []string{ + "a", + "b", + }, })) }