test: add test for legend and draw

This commit is contained in:
vicanso 2022-02-06 10:40:05 +08:00
parent c01f4001f1
commit 54f0195c53
4 changed files with 67 additions and 14 deletions

14
axis.go
View file

@ -32,19 +32,32 @@ import (
type AxisOption struct {
BoundaryGap *bool
// The flag for show axis, set this to *false will hide axis
Show *bool
// The position of axis, it can be 'left', 'top', 'right' or 'bottom'
Position string
// Number of segments that the axis is split into. Note that this number serves only as a recommendation.
SplitNumber int
ClassName string
// The line color of axis
StrokeColor drawing.Color
// The line width
StrokeWidth float64
// The length of the axis tick
TickLength int
// The flag for show axis tick, set this to *false will hide axis tick
TickShow *bool
// The margin value of label
LabelMargin int
// The font size of label
FontSize float64
// The font of label
Font *truetype.Font
// The color of label
FontColor drawing.Color
// The flag for show axis split line, set this to true will show axis split line
SplitLineShow bool
// The color of split line
SplitLineColor drawing.Color
}
@ -380,6 +393,7 @@ func (a *axis) measureAxis() int {
return textMaxHeight + value
}
// Render renders the axis for chart
func (a *axis) Render() {
style := a.style
if isFalse(style.Show) {

1
bar.go
View file

@ -43,6 +43,7 @@ func (bs *BarStyle) Style() chart.Style {
}
}
// Bar renders bar for chart
func (d *Draw) Bar(b chart.Box, style BarStyle) {
s := style.Style()

View file

@ -23,6 +23,8 @@
package charts
import (
"fmt"
"math"
"testing"
"github.com/stretchr/testify/assert"
@ -224,6 +226,20 @@ func TestDraw(t *testing.T) {
},
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 0 0\nL 400 0\nL 400 300\nL 0 300\nL 0 0\" style=\"stroke-width:0;stroke:none;fill:rgba(255,255,255,1.0)\"/></svg>",
},
// arcTo
{
fn: func(d *Draw) {
chart.Style{
StrokeWidth: 1,
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlue,
}.WriteToRenderer(d.Render)
d.arcTo(100, 100, 100, 100, 0, math.Pi/2)
d.Render.Close()
d.Render.FillStroke()
},
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 205 110\nA 100 100 90.00 0 1 105 210\nZ\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,255,1.0)\"/></svg>",
},
}
for _, tt := range tests {
d, err := NewDraw(DrawOption{
@ -237,6 +253,7 @@ func TestDraw(t *testing.T) {
tt.fn(d)
data, err := d.Bytes()
assert.Nil(err)
fmt.Println(string(data))
assert.Equal(tt.result, string(data))
}
}

View file

@ -133,6 +133,27 @@ func TestLegendRender(t *testing.T) {
Bottom: 70,
},
},
{
newDraw: newDraw,
newLegend: func(d *Draw) *legend {
return NewLegend(d, LegendOption{
Top: "10",
Left: "10%",
Data: []string{
"Mon",
"Tue",
"Wed",
},
Style: style,
Orient: OrientVertical,
})
},
box: chart.Box{
Right: 101,
Bottom: 70,
},
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 10\nL 70 10\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><circle cx=\"55\" cy=\"10\" r=\"5\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"75\" y=\"13\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><path d=\"M 40 30\nL 70 30\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><circle cx=\"55\" cy=\"30\" r=\"5\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><text x=\"75\" y=\"33\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><path d=\"M 40 50\nL 70 50\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><circle cx=\"55\" cy=\"50\" r=\"5\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><text x=\"75\" y=\"53\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text></svg>",
},
}
for _, tt := range tests {