// 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 ( "encoding/json" "testing" "github.com/stretchr/testify/assert" "github.com/wcharczuk/go-chart/v2/drawing" ) func TestConvertToArray(t *testing.T) { assert := assert.New(t) assert.Equal([]byte(`[1]`), convertToArray([]byte("1"))) assert.Equal([]byte(`[1]`), convertToArray([]byte("[1]"))) } func TestEChartsPosition(t *testing.T) { assert := assert.New(t) var p EChartsPosition err := p.UnmarshalJSON([]byte("1")) assert.Nil(err) assert.Equal(EChartsPosition("1"), p) err = p.UnmarshalJSON([]byte(`"left"`)) assert.Nil(err) assert.Equal(EChartsPosition("left"), p) } func TestEChartsSeriesDataValue(t *testing.T) { assert := assert.New(t) es := EChartsSeriesDataValue{} err := es.UnmarshalJSON([]byte(`[1, 2]`)) assert.Nil(err) assert.Equal(EChartsSeriesDataValue{ values: []float64{ 1, 2, }, }, es) assert.Equal(NewEChartsSeriesDataValue(1, 2), es) assert.Equal(1.0, es.First()) } func TestEChartsSeriesData(t *testing.T) { assert := assert.New(t) es := EChartsSeriesData{} err := es.UnmarshalJSON([]byte("1.1")) assert.Nil(err) assert.Equal(EChartsSeriesDataValue{ values: []float64{ 1.1, }, }, es.Value) err = es.UnmarshalJSON([]byte(`{"value":200,"itemStyle":{"color":"#a90000"}}`)) assert.Nil(err) assert.Nil(err) assert.Equal(EChartsSeriesData{ Value: EChartsSeriesDataValue{ values: []float64{ 200.0, }, }, ItemStyle: EChartStyle{ Color: "#a90000", }, }, es) } func TestEChartsXAxis(t *testing.T) { assert := assert.New(t) ex := EChartsXAxis{} err := ex.UnmarshalJSON([]byte(`{"boundaryGap": true, "splitNumber": 5, "data": ["a", "b"], "type": "value"}`)) assert.Nil(err) assert.Equal(EChartsXAxis{ Data: []EChartsXAxisData{ { BoundaryGap: TrueFlag(), SplitNumber: 5, Data: []string{ "a", "b", }, Type: "value", }, }, }, ex) } func TestEChartStyle(t *testing.T) { assert := assert.New(t) es := EChartStyle{ Color: "#999", } color := drawing.Color{ R: 153, G: 153, B: 153, A: 255, } assert.Equal(Style{ FillColor: color, FontColor: color, StrokeColor: color, }, es.ToStyle()) } func TestEChartsPadding(t *testing.T) { assert := assert.New(t) eb := EChartsPadding{} err := eb.UnmarshalJSON([]byte(`1`)) assert.Nil(err) assert.Equal(Box{ Left: 1, Top: 1, Right: 1, Bottom: 1, }, eb.Box) err = eb.UnmarshalJSON([]byte(`[2, 3]`)) assert.Nil(err) assert.Equal(Box{ Left: 3, Top: 2, Right: 3, Bottom: 2, }, eb.Box) err = eb.UnmarshalJSON([]byte(`[4, 5, 6]`)) assert.Nil(err) assert.Equal(Box{ Left: 5, Top: 4, Right: 5, Bottom: 6, }, eb.Box) err = eb.UnmarshalJSON([]byte(`[4, 5, 6, 7]`)) assert.Nil(err) assert.Equal(Box{ Left: 7, Top: 4, Right: 5, Bottom: 6, }, eb.Box) } func TestEChartsMarkPoint(t *testing.T) { assert := assert.New(t) emp := EChartsMarkPoint{ SymbolSize: 30, Data: []EChartsMarkData{ { Type: "test", }, }, } assert.Equal(SeriesMarkPoint{ SymbolSize: 30, Data: []SeriesMarkData{ { Type: "test", }, }, }, emp.ToSeriesMarkPoint()) } func TestEChartsMarkLine(t *testing.T) { assert := assert.New(t) eml := EChartsMarkLine{ Data: []EChartsMarkData{ { Type: "min", }, { Type: "max", }, }, } assert.Equal(SeriesMarkLine{ Data: []SeriesMarkData{ { Type: "min", }, { Type: "max", }, }, }, eml.ToSeriesMarkLine()) } func TestEChartsOption(t *testing.T) { assert := assert.New(t) tests := []struct { option string }{ { option: `{ "xAxis": { "type": "category", "data": [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" ] }, "yAxis": { "type": "value" }, "series": [ { "data": [ 120, { "value": 200, "itemStyle": { "color": "#a90000" } }, 150, 80, 70, 110, 130 ], "type": "bar" } ] }`, }, { option: `{ "title": { "text": "Referer of a Website", "subtext": "Fake Data", "left": "center" }, "tooltip": { "trigger": "item" }, "legend": { "orient": "vertical", "left": "left" }, "series": [ { "name": "Access From", "type": "pie", "radius": "50%", "data": [ { "value": 1048, "name": "Search Engine" }, { "value": 735, "name": "Direct" }, { "value": 580, "name": "Email" }, { "value": 484, "name": "Union Ads" }, { "value": 300, "name": "Video Ads" } ], "emphasis": { "itemStyle": { "shadowBlur": 10, "shadowOffsetX": 0, "shadowColor": "rgba(0, 0, 0, 0.5)" } } } ] }`, }, { option: `{ "title": { "text": "Rainfall vs Evaporation", "subtext": "Fake Data" }, "tooltip": { "trigger": "axis" }, "legend": { "data": [ "Rainfall", "Evaporation" ] }, "toolbox": { "show": true, "feature": { "dataView": { "show": true, "readOnly": false }, "magicType": { "show": true, "type": [ "line", "bar" ] }, "restore": { "show": true }, "saveAsImage": { "show": true } } }, "calculable": true, "xAxis": [ { "type": "category", "data": [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] } ], "yAxis": [ { "type": "value" } ], "series": [ { "name": "Rainfall", "type": "bar", "data": [ 2, 4.9, 7, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20, 6.4, 3.3 ], "markPoint": { "data": [ { "type": "max", "name": "Max" }, { "type": "min", "name": "Min" } ] }, "markLine": { "data": [ { "type": "average", "name": "Avg" } ] } }, { "name": "Evaporation", "type": "bar", "data": [ 2.6, 5.9, 9, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6, 2.3 ], "markPoint": { "data": [ { "name": "Max", "value": 182.2, "xAxis": 7, "yAxis": 183 }, { "name": "Min", "value": 2.3, "xAxis": 11, "yAxis": 3 } ] }, "markLine": { "data": [ { "type": "average", "name": "Avg" } ] } } ] }`, }, } for _, tt := range tests { opt := EChartsOption{} err := json.Unmarshal([]byte(tt.option), &opt) assert.Nil(err) assert.NotEmpty(opt.Series) assert.NotEmpty(opt.ToOption().SeriesList) } } func TestRenderEChartsToSVG(t *testing.T) { assert := assert.New(t) data, err := RenderEChartsToSVG(`{ "title": { "text": "Rainfall vs Evaporation", "subtext": "Fake Data" }, "legend": { "data": [ "Rainfall", "Evaporation" ] }, "padding": [10, 30, 10, 10], "xAxis": [ { "type": "category", "data": [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] } ], "series": [ { "name": "Rainfall", "type": "bar", "data": [ 2, 4.9, 7, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20, 6.4, 3.3 ], "markPoint": { "data": [ { "type": "max" }, { "type": "min" } ] }, "markLine": { "data": [ { "type": "average" } ] } }, { "name": "Evaporation", "type": "bar", "data": [ 2.6, 5.9, 9, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6, 2.3 ], "markPoint": { "data": [ { "type": "max" }, { "type": "min" } ] }, "markLine": { "data": [ { "type": "average" } ] } } ] }`) assert.Nil(err) assert.Equal("\\nRainfallEvaporationRainfall vs EvaporationFake Data24020016012080400JanFebMarAprMayJunJulAugSepOctNovDec162.22182.22.341.6248.07", string(data)) }