// MIT License // Copyright (c) 2021 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 ( "bytes" "testing" "github.com/stretchr/testify/assert" "github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2/drawing" ) func TestBarSeries(t *testing.T) { assert := assert.New(t) customStyle := chart.Style{ StrokeColor: drawing.ColorBlue, } bs := BarSeries{ CustomStyles: []BarSeriesCustomStyle{ { PointIndex: 1, Style: customStyle, }, }, } assert.Equal(customStyle, bs.GetBarStyle(0, 1)) assert.True(bs.GetBarStyle(1, 0).IsZero()) } func TestBarSeriesGetWidthValues(t *testing.T) { assert := assert.New(t) bs := BarSeries{ Count: 1, BaseSeries: BaseSeries{ XValues: []float64{ 1, 2, 3, }, }, } widthValues := bs.getWidthValues(300) assert.Equal(barSeriesWidthValues{ columnWidth: 100, columnMargin: 10, margin: 10, barWidth: 80, }, widthValues) // 指定margin bs.Margin = 5 widthValues = bs.getWidthValues(300) assert.Equal(barSeriesWidthValues{ columnWidth: 100, columnMargin: 10, margin: 5, barWidth: 80, }, widthValues) // 指定bar的宽度 bs.BarWidth = 60 widthValues = bs.getWidthValues(300) assert.Equal(barSeriesWidthValues{ columnWidth: 100, columnMargin: 20, margin: 5, barWidth: 60, }, widthValues) } func TestBarSeriesRender(t *testing.T) { assert := assert.New(t) width := 800 height := 400 r, err := chart.SVG(width, height) assert.Nil(err) bs := BarSeries{ Count: 1, CustomStyles: []BarSeriesCustomStyle{ { Index: 0, PointIndex: 1, Style: chart.Style{ StrokeColor: SeriesColorsLight[1], }, }, }, BaseSeries: BaseSeries{ TickPosition: chart.TickPositionBetweenTicks, Style: chart.Style{ StrokeColor: SeriesColorsLight[0], StrokeWidth: 1, }, XValues: []float64{ 0, 1, 2, 3, 4, 5, 6, 7, }, YValues: []float64{ // 第一个点为占位点 0, 120, 200, 150, 80, 70, 110, 130, }, }, } xrange := &chart.ContinuousRange{ Min: 0, Max: 7, Domain: 753, } yrange := &chart.ContinuousRange{ Min: 70, Max: 200, Domain: 362, } bs.Render(r, chart.Box{ Top: 11, Left: 42, Right: 795, Bottom: 373, }, xrange, yrange, chart.Style{}) buffer := bytes.Buffer{} err = r.Save(&buffer) assert.Nil(err) assert.Equal("\\n", buffer.String()) }