// 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)) }