test: add test for x axis

This commit is contained in:
vicanso 2022-02-06 11:31:56 +08:00
parent cc6a1832fe
commit e07cb90607
3 changed files with 106 additions and 5 deletions

View file

@ -31,6 +31,8 @@ import (
) )
type AxisOption struct { 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 BoundaryGap *bool
// The flag for show axis, set this to *false will hide axis // The flag for show axis, set this to *false will hide axis
Show *bool Show *bool
@ -379,7 +381,7 @@ func (a *axis) axisMeasureTextMaxWidthHeight() (int, int) {
return measureTextMaxWidthHeight(data.TextList(), r) 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 left or right, it will be textMaxWidth + labelMargin + tickLength.
// If the position is top or bottom, it will be textMaxHeight + labelMargin + tickLength. // If the position is top or bottom, it will be textMaxHeight + labelMargin + tickLength.
func (a *axis) measureAxis() int { func (a *axis) measureAxis() int {

View file

@ -25,16 +25,24 @@ package charts
import "github.com/wcharczuk/go-chart/v2" import "github.com/wcharczuk/go-chart/v2"
type XAxisOption struct { 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 BoundaryGap *bool
// The data value of x axis
Data []string Data []string
// The theme of chart
Theme string Theme string
// Hidden x axis
Hidden bool Hidden bool
// Number of segments that the axis is split into. Note that this number serves only as a recommendation.
SplitNumber int SplitNumber int
// TODO split number
} }
// drawXAxis draws x axis, and returns the height, range of if. // drawXAxis draws x axis, and returns the height, range of if.
func drawXAxis(p *Draw, opt *XAxisOption) (int, *Range, error) { func drawXAxis(p *Draw, opt *XAxisOption) (int, *Range, error) {
if opt.Hidden {
return 0, nil, nil
}
dXAxis, err := NewDraw( dXAxis, err := NewDraw(
DrawOption{ DrawOption{
Parent: p, Parent: p,

91
xaxis_test.go Normal file
View file

@ -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: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 40 275\nL 400 275\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 40 275\nL 40 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 400 275\nL 400 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"27\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"389\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text></svg>",
},
{
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: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 40 275\nL 400 275\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 40 275\nL 40 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 160 275\nL 160 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 280 275\nL 280 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 400 275\nL 400 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"83\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-02</text><text x=\"203\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-05</text><text x=\"323\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-08</text></svg>",
},
}
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))
}
}