// 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/golang/freetype/truetype" "github.com/stretchr/testify/assert" "github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2/drawing" ) func TestAxisStyle(t *testing.T) { assert := assert.New(t) as := AxisStyle{} assert.Equal(8, as.GetLabelMargin()) as.LabelMargin = 10 assert.Equal(10, as.GetLabelMargin()) assert.Equal(5, as.GetTickLength()) as.TickLength = 6 assert.Equal(6, as.GetTickLength()) f := &truetype.Font{} style := as.Style(f) assert.Equal(float64(10), style.FontSize) assert.Equal(f, style.Font) } func TestAxisDataList(t *testing.T) { assert := assert.New(t) textList := []string{ "a", "b", } data := NewAxisDataListFromStringList(textList) assert.Equal(textList, data.TextList()) } func TestAxis(t *testing.T) { assert := assert.New(t) data := NewAxisDataListFromStringList([]string{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", }) getDefaultStyle := func() AxisStyle { return AxisStyle{ StrokeColor: drawing.ColorBlack, StrokeWidth: 1, FontColor: drawing.ColorBlack, Show: TrueFlag(), TickShow: TrueFlag(), SplitLineShow: true, SplitLineColor: drawing.ColorBlack.WithAlpha(60), } } tests := []struct { newStyle func() AxisStyle result string }{ // 文本按起始位置展示 // axis位于bottom { newStyle: func() AxisStyle { opt := getDefaultStyle() opt.BoundaryGap = FalseFlag() return opt }, result: "\\nMonTueWedThuFriSatSun", }, // 文本居中展示 // axis位于bottom { newStyle: func() AxisStyle { opt := getDefaultStyle() return opt }, result: "\\nMonTueWedThuFriSatSun", }, // 文本按起始位置展示 // axis位于top { newStyle: func() AxisStyle { opt := getDefaultStyle() opt.Position = PositionTop opt.BoundaryGap = FalseFlag() return opt }, result: "\\nMonTueWedThuFriSatSun", }, // 文本居中展示 // axis位于top { newStyle: func() AxisStyle { opt := getDefaultStyle() opt.Position = PositionTop return opt }, result: "\\nMonTueWedThuFriSatSun", }, // 文本按起始位置展示 // axis位于left { newStyle: func() AxisStyle { opt := getDefaultStyle() opt.Position = PositionLeft opt.BoundaryGap = FalseFlag() return opt }, result: "\\nMonTueWedThuFriSatSun", }, // 文本居中展示 // axis位于left { newStyle: func() AxisStyle { opt := getDefaultStyle() opt.Position = PositionLeft return opt }, result: "\\nMonTueWedThuFriSatSun", }, // 文本按起始位置展示 // axis位于right { newStyle: func() AxisStyle { opt := getDefaultStyle() opt.Position = PositionRight opt.BoundaryGap = FalseFlag() return opt }, result: "\\nMonTueWedThuFriSatSun", }, // 文本居中展示 // axis位于right { newStyle: func() AxisStyle { opt := getDefaultStyle() opt.Position = PositionRight return opt }, result: "\\nMonTueWedThuFriSatSun", }, } for _, tt := range tests { d, err := NewDraw(DrawOption{ Width: 400, Height: 300, }, PaddingOption(chart.Box{ Left: 5, Top: 5, Right: 5, Bottom: 5, })) assert.Nil(err) style := tt.newStyle() NewAxis(d, data, style).Render() result, err := d.Bytes() assert.Nil(err) assert.Equal(tt.result, string(result)) } } func TestMeasureAxis(t *testing.T) { assert := assert.New(t) d, err := NewDraw(DrawOption{ Width: 400, Height: 300, }) assert.Nil(err) data := NewAxisDataListFromStringList([]string{ "Mon", "Sun", }) f, _ := chart.GetDefaultFont() width := NewAxis(d, data, AxisStyle{ FontSize: 12, Font: f, Position: PositionLeft, }).measureAxis() assert.Equal(44, width) height := NewAxis(d, data, AxisStyle{ FontSize: 12, Font: f, Position: PositionTop, }).measureAxis() assert.Equal(28, height) }