From e07cb9060705963d5001cbbdbc442a8cdbd831a5 Mon Sep 17 00:00:00 2001 From: vicanso Date: Sun, 6 Feb 2022 11:31:56 +0800 Subject: [PATCH] test: add test for x axis --- axis.go | 4 ++- xaxis.go | 16 ++++++--- xaxis_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 xaxis_test.go diff --git a/axis.go b/axis.go index 4b9444c..e6488cd 100644 --- a/axis.go +++ b/axis.go @@ -31,6 +31,8 @@ import ( ) type AxisOption struct { + // The boundary gap on both sides of a coordinate axis. + // Nil or *true means the center part of two axis ticks BoundaryGap *bool // The flag for show axis, set this to *false will hide axis Show *bool @@ -379,7 +381,7 @@ func (a *axis) axisMeasureTextMaxWidthHeight() (int, int) { return measureTextMaxWidthHeight(data.TextList(), r) } -// measureAxis return the measurement of axis. +// measureAxis returns the measurement of axis. // If the position is left or right, it will be textMaxWidth + labelMargin + tickLength. // If the position is top or bottom, it will be textMaxHeight + labelMargin + tickLength. func (a *axis) measureAxis() int { diff --git a/xaxis.go b/xaxis.go index 0383c73..60c7c23 100644 --- a/xaxis.go +++ b/xaxis.go @@ -25,16 +25,24 @@ package charts import "github.com/wcharczuk/go-chart/v2" type XAxisOption struct { + // The boundary gap on both sides of a coordinate axis. + // Nil or *true means the center part of two axis ticks BoundaryGap *bool - Data []string - Theme string - Hidden bool + // The data value of x axis + Data []string + // The theme of chart + Theme string + // Hidden x axis + Hidden bool + // Number of segments that the axis is split into. Note that this number serves only as a recommendation. SplitNumber int - // TODO split number } // drawXAxis draws x axis, and returns the height, range of if. func drawXAxis(p *Draw, opt *XAxisOption) (int, *Range, error) { + if opt.Hidden { + return 0, nil, nil + } dXAxis, err := NewDraw( DrawOption{ Parent: p, diff --git a/xaxis_test.go b/xaxis_test.go new file mode 100644 index 0000000..bce3863 --- /dev/null +++ b/xaxis_test.go @@ -0,0 +1,91 @@ +// 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" +) + +func TestDrawXAxis(t *testing.T) { + assert := assert.New(t) + + newDraw := func() *Draw { + d, _ := NewDraw(DrawOption{ + Width: 400, + Height: 300, + }) + return d + } + + tests := []struct { + newDraw func() *Draw + newOption func() *XAxisOption + result string + }{ + { + newDraw: newDraw, + newOption: func() *XAxisOption { + return &XAxisOption{ + BoundaryGap: FalseFlag(), + Data: []string{ + "Mon", + "Tue", + }, + } + }, + result: "\\nMonTue", + }, + { + newDraw: newDraw, + newOption: func() *XAxisOption { + return &XAxisOption{ + Data: []string{ + "01-01", + "01-02", + "01-03", + "01-04", + "01-05", + "01-06", + "01-07", + "01-08", + "01-09", + }, + SplitNumber: 3, + } + }, + result: "\\n01-0201-0501-08", + }, + } + + for _, tt := range tests { + d := tt.newDraw() + height, _, err := drawXAxis(d, tt.newOption()) + assert.Nil(err) + assert.Equal(25, height) + data, err := d.Bytes() + assert.Nil(err) + assert.Equal(tt.result, string(data)) + } +}