From d3c6649cd9e152daf3f6a7ea0780825db2695c17 Mon Sep 17 00:00:00 2001 From: vicanso Date: Sat, 18 Jun 2022 10:38:46 +0800 Subject: [PATCH] test: add test for axis --- README.md | 2 +- README_zh.md | 2 +- axis.go | 7 ++- axis_test.go | 129 +++++++++++++++++++++++++++++++++++++++ examples/chinese/main.go | 5 +- examples/painter/main.go | 6 +- legend.go | 2 +- line_chart.go | 2 +- mark_line.go | 10 --- painter.go | 4 +- range.go | 10 --- series.go | 6 -- 12 files changed, 145 insertions(+), 40 deletions(-) create mode 100644 axis_test.go diff --git a/README.md b/README.md index a58adb4..8183871 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ import ( ) func main() { -values := [][]float64{ + values := [][]float64{ { 120, 132, diff --git a/README_zh.md b/README_zh.md index 9c0be5b..fed2d61 100644 --- a/README_zh.md +++ b/README_zh.md @@ -32,7 +32,7 @@ import ( ) func main() { -values := [][]float64{ + values := [][]float64{ { 120, 132, diff --git a/axis.go b/axis.go index aa7cf7d..00a3332 100644 --- a/axis.go +++ b/axis.go @@ -81,7 +81,10 @@ func (a *axisPainter) Render() (Box, error) { opt := a.opt top := a.p theme := opt.Theme - if opt.Show != nil && !*opt.Show { + if theme == nil { + theme = top.theme + } + if isFalse(opt.Show) { return BoxZero, nil } @@ -121,7 +124,7 @@ func (a *axisPainter) Render() (Box, error) { tickCount := dataCount boundaryGap := true - if opt.BoundaryGap != nil && !*opt.BoundaryGap { + if isFalse(opt.BoundaryGap) { boundaryGap = false } isVertical := opt.Position == PositionLeft || diff --git a/axis_test.go b/axis_test.go new file mode 100644 index 0000000..fe7f874 --- /dev/null +++ b/axis_test.go @@ -0,0 +1,129 @@ +// 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/drawing" +) + +func TestAxis(t *testing.T) { + assert := assert.New(t) + tests := []struct { + render func(*Painter) ([]byte, error) + result string + }{ + // 底部x轴 + { + render: func(p *Painter) ([]byte, error) { + _, _ = NewAxisPainter(p, AxisOption{ + Data: []string{ + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + "Sun", + }, + }).Render() + return p.Bytes() + }, + result: "\\nMonTueWedThuFriSatSun", + }, + // 底部x轴文本居左 + { + render: func(p *Painter) ([]byte, error) { + _, _ = NewAxisPainter(p, AxisOption{ + Data: []string{ + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + "Sun", + }, + BoundaryGap: FalseFlag(), + }).Render() + return p.Bytes() + }, + result: "\\nMonTueWedThuFriSatSun", + }, + // 左侧y轴 + { + render: func(p *Painter) ([]byte, error) { + _, _ = NewAxisPainter(p, AxisOption{ + Data: []string{ + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + "Sun", + }, + Position: PositionLeft, + }).Render() + return p.Bytes() + }, + result: "\\nMonTueWedThuFriSatSun", + }, + // 左侧y轴居中 + { + render: func(p *Painter) ([]byte, error) { + _, _ = NewAxisPainter(p, AxisOption{ + Data: []string{ + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + "Sun", + }, + Position: PositionLeft, + BoundaryGap: FalseFlag(), + SplitLineShow: true, + SplitLineColor: drawing.ColorBlack, + }).Render() + return p.Bytes() + }, + result: "\\nMonTueWedThuFriSatSun", + }, + } + + for _, tt := range tests { + p, err := NewPainter(PainterOptions{ + Type: ChartOutputSVG, + Width: 600, + Height: 400, + }, PainterThemeOption(defaultTheme)) + assert.Nil(err) + data, err := tt.render(p) + assert.Nil(err) + assert.Equal(tt.result, string(data)) + } +} diff --git a/examples/chinese/main.go b/examples/chinese/main.go index 9c2d6a5..bb7cc00 100644 --- a/examples/chinese/main.go +++ b/examples/chinese/main.go @@ -114,6 +114,5 @@ func main() { err = writeFile(buf) if err != nil { panic(err) - } -) - + } +} diff --git a/examples/painter/main.go b/examples/painter/main.go index 304361d..3c31ce4 100644 --- a/examples/painter/main.go +++ b/examples/painter/main.go @@ -430,7 +430,7 @@ func main() { Left: 1, Right: p.Width() - 1, Bottom: top + 30, - })), charts.LegendPainterOption{ + })), charts.LegendOption{ Left: "10", Data: []string{ "Email", @@ -449,7 +449,7 @@ func main() { Left: 1, Right: p.Width() - 1, Bottom: top + 30, - })), charts.LegendPainterOption{ + })), charts.LegendOption{ Left: charts.PositionRight, Data: []string{ "Email", @@ -470,7 +470,7 @@ func main() { Left: 1, Right: p.Width() - 1, Bottom: top + 100, - })), charts.LegendPainterOption{ + })), charts.LegendOption{ Top: "10", Data: []string{ "Email", diff --git a/legend.go b/legend.go index cf8d417..65db102 100644 --- a/legend.go +++ b/legend.go @@ -92,7 +92,7 @@ func (l *legendPainter) Render() (Box, error) { opt := l.opt theme := opt.Theme if opt.IsEmpty() || - (opt.Show != nil && !*opt.Show) { + isFalse(opt.Show) { return BoxZero, nil } if theme == nil { diff --git a/line_chart.go b/line_chart.go index 0dc0fd8..f171813 100644 --- a/line_chart.go +++ b/line_chart.go @@ -66,7 +66,7 @@ func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) ( p := l.p opt := l.opt boundaryGap := true - if opt.XAxis.BoundaryGap != nil && !*opt.XAxis.BoundaryGap { + if isFalse(opt.XAxis.BoundaryGap) { boundaryGap = false } diff --git a/mark_line.go b/mark_line.go index bb1b602..00a37f2 100644 --- a/mark_line.go +++ b/mark_line.go @@ -105,13 +105,3 @@ func (m *markLinePainter) Render() (Box, error) { } return BoxZero, nil } - -func markLineRender(opt markLineRenderOption) { - // d := opt.Draw - // s := opt.Series - // if len(s.MarkLine.Data) == 0 { - // return - // } - // r := d.Render - -} diff --git a/painter.go b/painter.go index 0bacd3c..da07007 100644 --- a/painter.go +++ b/painter.go @@ -41,7 +41,7 @@ type Painter struct { } type PainterOptions struct { - // Draw type, "svg" or "png", default type is "svg" + // Draw type, "svg" or "png", default type is "png" Type string // The width of draw painter Width int @@ -628,7 +628,7 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter { values = autoDivide(width, count) } for index, text := range opt.TextList { - if index%opt.Unit != 0 { + if opt.Unit != 0 && index%opt.Unit != 0 { continue } box := p.MeasureText(text) diff --git a/range.go b/range.go index 399c449..d5a9ef7 100644 --- a/range.go +++ b/range.go @@ -115,13 +115,3 @@ func (r *axisRange) GetRange(index int) (float64, float64) { func (r *axisRange) AutoDivide() []int { return autoDivide(r.size, r.divideCount) } - -func (r *axisRange) getWidth(value float64) int { - v := value / (r.max - r.min) - // 移至居中 - if r.boundary && - r.divideCount != 0 { - v += 1 / float64(r.divideCount*2) - } - return int(v * float64(r.size)) -} diff --git a/series.go b/series.go index 905c140..44c4749 100644 --- a/series.go +++ b/series.go @@ -140,12 +140,6 @@ func (sl SeriesList) init() { } } -func (sl SeriesList) reverse() { - for i, j := 0, len(sl)-1; i < j; i, j = i+1, j-1 { - sl[i], sl[j] = sl[j], sl[i] - } -} - func (sl SeriesList) Filter(chartType string) SeriesList { arr := make(SeriesList, 0) for index, item := range sl {