test: add test for x axis
This commit is contained in:
parent
cc6a1832fe
commit
e07cb90607
3 changed files with 106 additions and 5 deletions
4
axis.go
4
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 {
|
||||
|
|
|
|||
16
xaxis.go
16
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,
|
||||
|
|
|
|||
91
xaxis_test.go
Normal file
91
xaxis_test.go
Normal 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))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue