test: add test for mark point

This commit is contained in:
vicanso 2022-02-12 14:11:40 +08:00
parent 63d4b0e229
commit bdf7bff313
5 changed files with 113 additions and 11 deletions

View file

@ -29,9 +29,9 @@ import (
) )
func NewMarkLine(markLineTypes ...string) SeriesMarkLine { func NewMarkLine(markLineTypes ...string) SeriesMarkLine {
data := make([]SeriesMarkLineData, len(markLineTypes)) data := make([]SeriesMarkData, len(markLineTypes))
for index, t := range markLineTypes { for index, t := range markLineTypes {
data[index] = SeriesMarkLineData{ data[index] = SeriesMarkData{
Type: t, Type: t,
} }
} }

View file

@ -40,7 +40,7 @@ func TestNewMarkLine(t *testing.T) {
) )
assert.Equal(SeriesMarkLine{ assert.Equal(SeriesMarkLine{
Data: []SeriesMarkLineData{ Data: []SeriesMarkData{
{ {
Type: SeriesMarkDataTypeMax, Type: SeriesMarkDataTypeMax,
}, },

View file

@ -29,9 +29,9 @@ import (
) )
func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint { func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint {
data := make([]SeriesMarkPointData, len(markPointTypes)) data := make([]SeriesMarkData, len(markPointTypes))
for index, t := range markPointTypes { for index, t := range markPointTypes {
data[index] = SeriesMarkPointData{ data[index] = SeriesMarkData{
Type: t, Type: t,
} }
} }

105
mark_point_test.go Normal file
View file

@ -0,0 +1,105 @@
// 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 (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)
func TestNewMarkPoint(t *testing.T) {
assert := assert.New(t)
markPoint := NewMarkPoint(
SeriesMarkDataTypeMax,
SeriesMarkDataTypeMin,
SeriesMarkDataTypeAverage,
)
assert.Equal(SeriesMarkPoint{
Data: []SeriesMarkData{
{
Type: SeriesMarkDataTypeMax,
},
{
Type: SeriesMarkDataTypeMin,
},
{
Type: SeriesMarkDataTypeAverage,
},
},
}, markPoint)
}
func TestMarkPointRender(t *testing.T) {
assert := assert.New(t)
d, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
}, PaddingOption(chart.Box{
Left: 20,
Right: 20,
}))
assert.Nil(err)
f, _ := chart.GetDefaultFont()
markPointRender(&markPointRenderOption{
Draw: d,
FillColor: drawing.ColorBlack,
Font: f,
Series: &Series{
MarkPoint: NewMarkPoint(
SeriesMarkDataTypeMax,
SeriesMarkDataTypeMin,
),
Data: NewSeriesDataFromValues([]float64{
1,
3,
5,
}),
},
Points: []Point{
{
X: 1,
Y: 50,
},
{
X: 100,
Y: 100,
},
{
X: 200,
Y: 200,
},
},
})
data, err := d.Bytes()
assert.Nil(err)
fmt.Println(string(data))
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 217 192\nA 15 15 330.00 1 1 223 192\nL 220 178\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><path d=\"M 205 178\nQ220,215 235,178\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><text x=\"216\" y=\"183\" style=\"stroke-width:0;stroke:none;fill:rgba(238,238,238,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">5</text><path d=\"M 18 42\nA 15 15 330.00 1 1 24 42\nL 21 28\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><path d=\"M 6 28\nQ21,65 36,28\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><text x=\"17\" y=\"33\" style=\"stroke-width:0;stroke:none;fill:rgba(238,238,238,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">1</text></svg>", string(data))
}

View file

@ -68,18 +68,15 @@ const (
SeriesMarkDataTypeAverage = "average" SeriesMarkDataTypeAverage = "average"
) )
type SeriesMarkPointData struct { type SeriesMarkData struct {
Type string Type string
} }
type SeriesMarkPoint struct { type SeriesMarkPoint struct {
SymbolSize int SymbolSize int
Data []SeriesMarkPointData Data []SeriesMarkData
}
type SeriesMarkLineData struct {
Type string
} }
type SeriesMarkLine struct { type SeriesMarkLine struct {
Data []SeriesMarkLineData Data []SeriesMarkData
} }
type Series struct { type Series struct {
index int index int