test: add test for echart
This commit is contained in:
parent
519c8a492e
commit
c15fec21ad
10 changed files with 1516 additions and 47 deletions
|
|
@ -54,7 +54,10 @@ func barChartRender(opt barChartOption, result *basicRenderResult) ([]markPointR
|
|||
margin := 10
|
||||
// 每一个bar之间的margin
|
||||
barMargin := 5
|
||||
if width < 50 {
|
||||
if width < 20 {
|
||||
margin = 2
|
||||
barMargin = 2
|
||||
} else if width < 50 {
|
||||
margin = 5
|
||||
barMargin = 3
|
||||
}
|
||||
|
|
@ -95,6 +98,9 @@ func barChartRender(opt barChartOption, result *basicRenderResult) ([]markPointR
|
|||
})
|
||||
divideValues := xRange.AutoDivide()
|
||||
for j, item := range series.Data {
|
||||
if j >= len(divideValues) {
|
||||
continue
|
||||
}
|
||||
x := divideValues[j]
|
||||
x += margin
|
||||
if i != 0 {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@
|
|||
package charts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -128,6 +127,5 @@ func TestBarChartRender(t *testing.T) {
|
|||
|
||||
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<circle cx=\"40\" cy=\"240\" r=\"3\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 45 240\nL 382 240\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path d=\"M 382 235\nL 398 240\nL 382 245\nL 387 240\nL 382 235\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"400\" y=\"244\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">20</text><path d=\"M 50 240\nL 87 240\nL 87 299\nL 50 299\nL 50 240\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"61\" y=\"235\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">20</text><path d=\"M 150 120\nL 187 120\nL 187 299\nL 150 299\nL 150 120\" style=\"stroke-width:1;stroke:rgba(255,0,0,1.0);fill:rgba(255,0,0,1.0)\"/><text x=\"161\" y=\"115\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">60</text><path d=\"M 250 30\nL 287 30\nL 287 299\nL 250 299\nL 250 30\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"261\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">90</text><path d=\"M 92 60\nL 129 60\nL 129 299\nL 92 299\nL 92 60\" style=\"stroke-width:1;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><path d=\"M 192 210\nL 229 210\nL 229 299\nL 192 299\nL 192 210\" style=\"stroke-width:1;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><path d=\"M 292 90\nL 329 90\nL 329 299\nL 292 299\nL 292 90\" style=\"stroke-width:1;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/></svg>", string(data))
|
||||
}
|
||||
|
|
|
|||
5
chart.go
5
chart.go
|
|
@ -38,6 +38,11 @@ const (
|
|||
ChartTypePie = "pie"
|
||||
)
|
||||
|
||||
const (
|
||||
ChartOutputSVG = "svg"
|
||||
ChartOutputPNG = "png"
|
||||
)
|
||||
|
||||
type Point struct {
|
||||
X int
|
||||
Y int
|
||||
|
|
|
|||
226
chart_test.go
226
chart_test.go
File diff suppressed because one or more lines are too long
2
draw.go
2
draw.go
|
|
@ -114,7 +114,7 @@ func NewDraw(opt DrawOption, opts ...Option) (*Draw, error) {
|
|||
// 创建render
|
||||
if d.parent == nil {
|
||||
fn := chart.SVG
|
||||
if opt.Type == "png" {
|
||||
if opt.Type == ChartOutputPNG {
|
||||
fn = chart.PNG
|
||||
}
|
||||
r, err := fn(d.Box.Right, d.Box.Bottom)
|
||||
|
|
|
|||
60
echarts.go
60
echarts.go
|
|
@ -76,12 +76,14 @@ type EChartsSeriesData struct {
|
|||
}
|
||||
type _EChartsSeriesData EChartsSeriesData
|
||||
|
||||
var numericRep = regexp.MustCompile("^[-+]?[0-9]+(?:\\.[0-9]+)?$")
|
||||
|
||||
func (es *EChartsSeriesData) UnmarshalJSON(data []byte) error {
|
||||
data = bytes.TrimSpace(data)
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
if regexp.MustCompile(`^\d+`).Match(data) {
|
||||
if numericRep.Match(data) {
|
||||
v, err := strconv.ParseFloat(string(data), 64)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -215,16 +217,20 @@ type EChartsMarkPoint struct {
|
|||
}
|
||||
|
||||
func (emp *EChartsMarkPoint) ToSeriesMarkPoint() SeriesMarkPoint {
|
||||
sp := SeriesMarkPoint{
|
||||
SymbolSize: emp.SymbolSize,
|
||||
}
|
||||
if len(emp.Data) == 0 {
|
||||
return sp
|
||||
}
|
||||
data := make([]SeriesMarkData, len(emp.Data))
|
||||
for index, item := range emp.Data {
|
||||
data[index] = SeriesMarkData{
|
||||
Type: item.Type,
|
||||
}
|
||||
}
|
||||
return SeriesMarkPoint{
|
||||
Data: data,
|
||||
SymbolSize: emp.SymbolSize,
|
||||
}
|
||||
sp.Data = data
|
||||
return sp
|
||||
}
|
||||
|
||||
type EChartsMarkLine struct {
|
||||
|
|
@ -234,15 +240,18 @@ type EChartsMarkLine struct {
|
|||
}
|
||||
|
||||
func (eml *EChartsMarkLine) ToSeriesMarkLine() SeriesMarkLine {
|
||||
sl := SeriesMarkLine{}
|
||||
if len(eml.Data) == 0 {
|
||||
return sl
|
||||
}
|
||||
data := make([]SeriesMarkData, len(eml.Data))
|
||||
for index, item := range eml.Data {
|
||||
data[index] = SeriesMarkData{
|
||||
Type: item.Type,
|
||||
}
|
||||
}
|
||||
return SeriesMarkLine{
|
||||
Data: data,
|
||||
}
|
||||
sl.Data = data
|
||||
return sl
|
||||
}
|
||||
|
||||
type EChartsSeries struct {
|
||||
|
|
@ -260,8 +269,27 @@ type EChartsSeries struct {
|
|||
type EChartsSeriesList []EChartsSeries
|
||||
|
||||
func (esList EChartsSeriesList) ToSeriesList() SeriesList {
|
||||
seriesList := make(SeriesList, len(esList))
|
||||
for index, item := range esList {
|
||||
seriesList := make(SeriesList, 0, len(esList))
|
||||
for _, item := range esList {
|
||||
// 如果是pie,则每个子荐生成一个series
|
||||
if item.Type == ChartTypePie {
|
||||
for _, dataItem := range item.Data {
|
||||
seriesList = append(seriesList, Series{
|
||||
Type: ChartTypePie,
|
||||
Name: dataItem.Name,
|
||||
Label: SeriesLabel{
|
||||
Show: true,
|
||||
},
|
||||
Radius: item.Radius,
|
||||
Data: []SeriesData{
|
||||
{
|
||||
Value: dataItem.Value,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
continue
|
||||
}
|
||||
data := make([]SeriesData, len(item.Data))
|
||||
for j, dataItem := range item.Data {
|
||||
data[j] = SeriesData{
|
||||
|
|
@ -269,7 +297,7 @@ func (esList EChartsSeriesList) ToSeriesList() SeriesList {
|
|||
Style: dataItem.ItemStyle.ToStyle(),
|
||||
}
|
||||
}
|
||||
seriesList[index] = Series{
|
||||
seriesList = append(seriesList, Series{
|
||||
Type: item.Type,
|
||||
Data: data,
|
||||
YAxisIndex: item.YAxisIndex,
|
||||
|
|
@ -280,10 +308,9 @@ func (esList EChartsSeriesList) ToSeriesList() SeriesList {
|
|||
Distance: item.Label.Distance,
|
||||
},
|
||||
Name: item.Name,
|
||||
Radius: item.Radius,
|
||||
MarkPoint: item.MarkPoint.ToSeriesMarkPoint(),
|
||||
MarkLine: item.MarkLine.ToSeriesMarkLine(),
|
||||
}
|
||||
})
|
||||
}
|
||||
return seriesList
|
||||
}
|
||||
|
|
@ -295,10 +322,14 @@ type EChartsTextStyle struct {
|
|||
}
|
||||
|
||||
func (et *EChartsTextStyle) ToStyle() chart.Style {
|
||||
return chart.Style{
|
||||
s := chart.Style{
|
||||
FontSize: et.FontSize,
|
||||
FontColor: parseColor(et.Color),
|
||||
}
|
||||
if et.FontFamily != "" {
|
||||
s.Font, _ = GetFont(et.FontFamily)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
type EChartsOption struct {
|
||||
|
|
@ -373,6 +404,7 @@ func (eo *EChartsOption) ToOption() ChartOption {
|
|||
Color: parseColor(item.AxisLine.LineStyle.Color),
|
||||
}
|
||||
}
|
||||
o.YAxisList = yAxisOptions
|
||||
|
||||
if len(eo.Children) != 0 {
|
||||
o.Children = make([]ChartOption, len(eo.Children))
|
||||
|
|
|
|||
258
echarts_test.go
258
echarts_test.go
|
|
@ -28,6 +28,7 @@ import (
|
|||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/wcharczuk/go-chart/v2"
|
||||
"github.com/wcharczuk/go-chart/v2/drawing"
|
||||
)
|
||||
|
||||
func TestEChartsPosition(t *testing.T) {
|
||||
|
|
@ -42,6 +43,24 @@ func TestEChartsPosition(t *testing.T) {
|
|||
assert.Nil(err)
|
||||
assert.Equal("12%", string(p))
|
||||
}
|
||||
func TestEChartStyle(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
s := EChartStyle{
|
||||
Color: "#aaa",
|
||||
}
|
||||
r := drawing.Color{
|
||||
R: 170,
|
||||
G: 170,
|
||||
B: 170,
|
||||
A: 255,
|
||||
}
|
||||
assert.Equal(chart.Style{
|
||||
FillColor: r,
|
||||
FontColor: r,
|
||||
StrokeColor: r,
|
||||
}, s.ToStyle())
|
||||
}
|
||||
|
||||
func TestEChartsXAxis(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
|
@ -299,3 +318,242 @@ func TestEChartsSeries(t *testing.T) {
|
|||
},
|
||||
}, esList)
|
||||
}
|
||||
|
||||
func TestEChartsMarkPoint(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
p := EChartsMarkPoint{}
|
||||
|
||||
err := json.Unmarshal([]byte(`{
|
||||
"symbolSize": 30,
|
||||
"data": [
|
||||
{
|
||||
"type": "max"
|
||||
},
|
||||
{
|
||||
"type": "min"
|
||||
}
|
||||
]
|
||||
}`), &p)
|
||||
assert.Nil(err)
|
||||
assert.Equal(SeriesMarkPoint{
|
||||
SymbolSize: 30,
|
||||
Data: []SeriesMarkData{
|
||||
{
|
||||
Type: "max",
|
||||
},
|
||||
{
|
||||
Type: "min",
|
||||
},
|
||||
},
|
||||
}, p.ToSeriesMarkPoint())
|
||||
}
|
||||
|
||||
func TestEChartsMarkLine(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
l := EChartsMarkLine{}
|
||||
|
||||
err := json.Unmarshal([]byte(`{
|
||||
"data": [
|
||||
{
|
||||
"type": "max"
|
||||
},
|
||||
{
|
||||
"type": "min"
|
||||
}
|
||||
]
|
||||
}`), &l)
|
||||
assert.Nil(err)
|
||||
assert.Equal(SeriesMarkLine{
|
||||
Data: []SeriesMarkData{
|
||||
{
|
||||
Type: "max",
|
||||
},
|
||||
{
|
||||
Type: "min",
|
||||
},
|
||||
},
|
||||
}, l.ToSeriesMarkLine())
|
||||
}
|
||||
|
||||
func TestEChartsTextStyle(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
s := EChartsTextStyle{
|
||||
Color: "#aaa",
|
||||
FontFamily: "test",
|
||||
FontSize: 14,
|
||||
}
|
||||
assert.Equal(chart.Style{
|
||||
FontColor: drawing.Color{
|
||||
R: 170,
|
||||
G: 170,
|
||||
B: 170,
|
||||
A: 255,
|
||||
},
|
||||
FontSize: 14,
|
||||
}, s.ToStyle())
|
||||
}
|
||||
|
||||
func TestEChartsSeriesList(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// pie
|
||||
es := EChartsSeriesList{
|
||||
{
|
||||
Type: ChartTypePie,
|
||||
Radius: "30%",
|
||||
Data: []EChartsSeriesData{
|
||||
{
|
||||
Name: "1",
|
||||
Value: 1,
|
||||
},
|
||||
{
|
||||
Name: "2",
|
||||
Value: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.Equal(SeriesList{
|
||||
{
|
||||
Type: ChartTypePie,
|
||||
Name: "1",
|
||||
Label: SeriesLabel{
|
||||
Show: true,
|
||||
},
|
||||
Radius: "30%",
|
||||
Data: []SeriesData{
|
||||
{
|
||||
Value: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: ChartTypePie,
|
||||
Name: "2",
|
||||
Label: SeriesLabel{
|
||||
Show: true,
|
||||
},
|
||||
Radius: "30%",
|
||||
Data: []SeriesData{
|
||||
{
|
||||
Value: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, es.ToSeriesList())
|
||||
|
||||
es = EChartsSeriesList{
|
||||
{
|
||||
Type: ChartTypeBar,
|
||||
Data: []EChartsSeriesData{
|
||||
{
|
||||
Value: 1,
|
||||
ItemStyle: EChartStyle{
|
||||
Color: "#aaa",
|
||||
},
|
||||
},
|
||||
{
|
||||
Value: 2,
|
||||
},
|
||||
},
|
||||
YAxisIndex: 1,
|
||||
},
|
||||
{
|
||||
Data: []EChartsSeriesData{
|
||||
{
|
||||
Value: 3,
|
||||
},
|
||||
{
|
||||
Value: 4,
|
||||
},
|
||||
},
|
||||
ItemStyle: EChartStyle{
|
||||
Color: "#ccc",
|
||||
},
|
||||
Label: EChartsLabelOption{
|
||||
Color: "#ddd",
|
||||
Show: true,
|
||||
Distance: 5,
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.Equal(SeriesList{
|
||||
{
|
||||
Type: ChartTypeBar,
|
||||
Data: []SeriesData{
|
||||
{
|
||||
Value: 1,
|
||||
Style: chart.Style{
|
||||
FontColor: drawing.Color{
|
||||
R: 170,
|
||||
G: 170,
|
||||
B: 170,
|
||||
A: 255,
|
||||
},
|
||||
StrokeColor: drawing.Color{
|
||||
R: 170,
|
||||
G: 170,
|
||||
B: 170,
|
||||
A: 255,
|
||||
},
|
||||
FillColor: drawing.Color{
|
||||
R: 170,
|
||||
G: 170,
|
||||
B: 170,
|
||||
A: 255,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Value: 2,
|
||||
},
|
||||
},
|
||||
YAxisIndex: 1,
|
||||
},
|
||||
{
|
||||
Data: []SeriesData{
|
||||
{
|
||||
Value: 3,
|
||||
},
|
||||
{
|
||||
Value: 4,
|
||||
},
|
||||
},
|
||||
Style: chart.Style{
|
||||
FontColor: drawing.Color{
|
||||
R: 204,
|
||||
G: 204,
|
||||
B: 204,
|
||||
A: 255,
|
||||
},
|
||||
StrokeColor: drawing.Color{
|
||||
R: 204,
|
||||
G: 204,
|
||||
B: 204,
|
||||
A: 255,
|
||||
},
|
||||
FillColor: drawing.Color{
|
||||
R: 204,
|
||||
G: 204,
|
||||
B: 204,
|
||||
A: 255,
|
||||
},
|
||||
},
|
||||
Label: SeriesLabel{
|
||||
Color: drawing.Color{
|
||||
R: 221,
|
||||
G: 221,
|
||||
B: 221,
|
||||
A: 255,
|
||||
},
|
||||
Show: true,
|
||||
Distance: 5,
|
||||
},
|
||||
MarkPoint: SeriesMarkPoint{},
|
||||
MarkLine: SeriesMarkLine{},
|
||||
},
|
||||
}, es.ToSeriesList())
|
||||
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -23,7 +23,6 @@
|
|||
package charts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -179,7 +178,6 @@ func TestLegendRender(t *testing.T) {
|
|||
assert.Nil(err)
|
||||
assert.Equal(tt.box, b)
|
||||
data, err := d.Bytes()
|
||||
fmt.Println(string(data))
|
||||
assert.Nil(err)
|
||||
assert.NotEmpty(data)
|
||||
assert.Equal(tt.result, string(data))
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@
|
|||
package charts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -100,6 +99,5 @@ func TestMarkPointRender(t *testing.T) {
|
|||
})
|
||||
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))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue