// 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 ( "testing" "github.com/stretchr/testify/assert" "github.com/wcharczuk/go-chart/v2/drawing" ) func TestAxis(t *testing.T) { assert := assert.New(t) tests := []struct { render func(*Painter) ([]byte, error) result string }{ // 底部x轴 { render: func(p *Painter) ([]byte, error) { _, _ = NewAxisPainter(p, AxisOption{ Data: []string{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", }, SplitLineShow: true, SplitLineColor: drawing.ColorBlack, }).Render() return p.Bytes() }, result: "\\nMonTueWedThuFriSatSun", }, // 底部x轴文本居左 { render: func(p *Painter) ([]byte, error) { _, _ = NewAxisPainter(p, AxisOption{ Data: []string{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", }, BoundaryGap: FalseFlag(), }).Render() return p.Bytes() }, result: "\\nMonTueWedThuFriSatSun", }, // 左侧y轴 { render: func(p *Painter) ([]byte, error) { _, _ = NewAxisPainter(p, AxisOption{ Data: []string{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", }, Position: PositionLeft, }).Render() return p.Bytes() }, result: "\\nMonTueWedThuFriSatSun", }, // 左侧y轴居中 { render: func(p *Painter) ([]byte, error) { _, _ = NewAxisPainter(p, AxisOption{ Data: []string{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", }, Position: PositionLeft, BoundaryGap: FalseFlag(), SplitLineShow: true, SplitLineColor: drawing.ColorBlack, }).Render() return p.Bytes() }, result: "\\nMonTueWedThuFriSatSun", }, // 右侧 { render: func(p *Painter) ([]byte, error) { _, _ = NewAxisPainter(p, AxisOption{ Data: []string{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", }, Position: PositionRight, BoundaryGap: FalseFlag(), SplitLineShow: true, SplitLineColor: drawing.ColorBlack, }).Render() return p.Bytes() }, result: "\\nMonTueWedThuFriSatSun", }, // 顶部 { render: func(p *Painter) ([]byte, error) { _, _ = NewAxisPainter(p, AxisOption{ Data: []string{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", }, Formatter: "{value} --", Position: PositionTop, }).Render() return p.Bytes() }, result: "\\nMon --Tue --Wed --Thu --Fri --Sat --Sun --", }, } for _, tt := range tests { p, err := NewPainter(PainterOptions{ Type: ChartOutputSVG, Width: 600, Height: 400, }, PainterThemeOption(defaultTheme)) assert.Nil(err) data, err := tt.render(p) assert.Nil(err) assert.Equal(tt.result, string(data)) } }