From 11fdd9121a9bd8aa08aa162c694166e852a29cd2 Mon Sep 17 00:00:00 2001 From: vicanso Date: Sat, 12 Feb 2022 16:12:02 +0800 Subject: [PATCH] test: add test for chart --- chart_test.go | 271 ++++++++++++++++++++++++++++++++++++++++++ examples/demo/main.go | 12 +- pie_chart.go | 29 +++-- pie_chart_test.go | 8 ++ 4 files changed, 304 insertions(+), 16 deletions(-) create mode 100644 chart_test.go diff --git a/chart_test.go b/chart_test.go new file mode 100644 index 0000000..06370c8 --- /dev/null +++ b/chart_test.go @@ -0,0 +1,271 @@ +// 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 TestChartFillDefault(t *testing.T) { + assert := assert.New(t) + // default value + opt := ChartOption{} + opt.FillDefault("") + // padding + assert.Equal(chart.Box{ + Top: 20, + Right: 10, + Bottom: 10, + Left: 10, + }, opt.Padding) + // background color + assert.Equal(drawing.ColorWhite, opt.BackgroundColor) + // title font color + assert.Equal(drawing.Color{ + R: 70, + G: 70, + B: 70, + A: 255, + }, opt.Title.Style.FontColor) + // title font size + assert.Equal(float64(14), opt.Title.Style.FontSize) + // title padding + assert.Equal(chart.Box{ + Left: 5, + Top: 5, + Right: 5, + Bottom: 5, + }, opt.Title.Style.Padding) + // sub title font color + assert.Equal(drawing.Color{ + R: 70, + G: 70, + B: 70, + A: 180, + }, opt.Title.SubtextStyle.FontColor) + // sub title font size + assert.Equal(float64(10), opt.Title.SubtextStyle.FontSize) + // legend font size + assert.Equal(float64(10), opt.Legend.Style.FontSize) + // legend position + assert.Equal("center", opt.Legend.Left) + assert.Equal(drawing.Color{ + R: 70, + G: 70, + B: 70, + A: 255, + }, opt.Legend.Style.FontColor) + + // y axis + opt = ChartOption{ + SeriesList: SeriesList{ + { + YAxisIndex: 1, + }, + }, + } + opt.FillDefault("") + assert.Equal([]YAxisOption{ + {}, + {}, + }, opt.YAxisList) + opt = ChartOption{} + opt.FillDefault("") + assert.Equal([]YAxisOption{ + {}, + }, opt.YAxisList) + + // legend get from series's name + + opt = ChartOption{ + SeriesList: SeriesList{ + { + Name: "a", + }, + { + Name: "b", + }, + }, + } + opt.FillDefault("") + assert.Equal([]string{ + "a", + "b", + }, opt.Legend.Data) + // series name set by legend + opt = ChartOption{ + Legend: LegendOption{ + Data: []string{ + "a", + "b", + }, + }, + SeriesList: SeriesList{ + {}, + {}, + }, + } + opt.FillDefault("") + assert.Equal("a", opt.SeriesList[0].Name) + assert.Equal("b", opt.SeriesList[1].Name) +} + +func TestChartGetWidthHeight(t *testing.T) { + assert := assert.New(t) + + opt := ChartOption{ + Width: 10, + } + assert.Equal(10, opt.getWidth()) + opt.Width = 0 + assert.Equal(600, opt.getWidth()) + opt.Parent = &Draw{ + Box: chart.Box{ + Left: 10, + Right: 50, + }, + } + assert.Equal(40, opt.getWidth()) + + opt = ChartOption{ + Height: 20, + } + assert.Equal(20, opt.getHeight()) + opt.Height = 0 + assert.Equal(400, opt.getHeight()) + opt.Parent = &Draw{ + Box: chart.Box{ + Top: 20, + Bottom: 80, + }, + } + assert.Equal(60, opt.getHeight()) +} + +func TestChartRender(t *testing.T) { + assert := assert.New(t) + + d, err := Render(ChartOption{ + Width: 800, + Height: 600, + Legend: LegendOption{ + Top: "-90", + Data: []string{ + "Milk Tea", + "Matcha Latte", + "Cheese Cocoa", + "Walnut Brownie", + }, + }, + Padding: chart.Box{ + Top: 100, + }, + XAxis: NewXAxisOption([]string{ + "2012", + "2013", + "2014", + "2015", + "2016", + "2017", + }), + YAxisList: []YAxisOption{ + { + + Min: NewFloatPoint(0), + Max: NewFloatPoint(90), + }, + }, + SeriesList: []Series{ + NewSeriesFromValues([]float64{ + 56.5, + 82.1, + 88.7, + 70.1, + 53.4, + 85.1, + }), + NewSeriesFromValues([]float64{ + 51.1, + 51.4, + 55.1, + 53.3, + 73.8, + 68.7, + }), + NewSeriesFromValues([]float64{ + 40.1, + 62.2, + 69.5, + 36.4, + 45.2, + 32.5, + }, ChartTypeBar), + NewSeriesFromValues([]float64{ + 25.2, + 37.1, + 41.2, + 18, + 33.9, + 49.1, + }, ChartTypeBar), + }, + Children: []ChartOption{ + { + Legend: LegendOption{ + Show: FalseFlag(), + Data: []string{ + "Milk Tea", + "Matcha Latte", + "Cheese Cocoa", + "Walnut Brownie", + }, + }, + Box: chart.Box{ + Top: 20, + Left: 400, + Right: 500, + Bottom: 120, + }, + SeriesList: NewPieSeriesList([]float64{ + 435.9, + 354.3, + 285.9, + 204.5, + }, PieSeriesOption{ + Label: SeriesLabel{ + Show: true, + }, + Radius: "35%", + }), + }, + }, + }) + assert.Nil(err) + data, err := d.Bytes() + assert.Nil(err) + assert.Equal("\\n2012201320142015201620170153045607590Milk TeaMatcha LatteCheese CocoaWalnut BrownieMilk Tea: 34.03%Matcha Latte: 27.66%Cheese Cocoa: 22.32%Walnut Brownie: 15.96%", string(data)) +} diff --git a/examples/demo/main.go b/examples/demo/main.go index 1f37e25..5d58cda 100644 --- a/examples/demo/main.go +++ b/examples/demo/main.go @@ -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%", }), }, }, diff --git a/pie_chart.go b/pie_chart.go index 7c62e8f..f581273 100644 --- a/pie_chart.go +++ b/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 diff --git a/pie_chart_test.go b/pie_chart_test.go index 7cb9fde..92ef6d0 100644 --- a/pie_chart_test.go +++ b/pie_chart_test.go @@ -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)