// 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 TestAxisOption(t *testing.T) {
assert := assert.New(t)
as := AxisOption{}
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)
axisData := NewAxisDataListFromStringList([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
})
getDefaultOption := func() AxisOption {
return AxisOption{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
FontColor: drawing.ColorBlack,
Show: TrueFlag(),
TickShow: TrueFlag(),
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack.WithAlpha(60),
}
}
tests := []struct {
newOption func() AxisOption
newData func() AxisDataList
result string
}{
// 文本按起始位置展示
// axis位于bottom
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.BoundaryGap = FalseFlag()
return opt
},
result: "",
},
// 文本居中展示
// axis位于bottom
{
newOption: func() AxisOption {
opt := getDefaultOption()
return opt
},
result: "",
},
// 文本按起始位置展示
// axis位于top
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionTop
opt.BoundaryGap = FalseFlag()
return opt
},
result: "",
},
// 文本居中展示
// axis位于top
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionTop
return opt
},
result: "",
},
// 文本按起始位置展示
// axis位于left
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionLeft
opt.BoundaryGap = FalseFlag()
return opt
},
result: "",
},
// 文本居中展示
// axis位于left
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionLeft
return opt
},
result: "",
},
// 文本按起始位置展示
// axis位于right
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionRight
opt.BoundaryGap = FalseFlag()
return opt
},
result: "",
},
// 文本居中展示
// axis位于right
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionRight
return opt
},
result: "",
},
// text较多,仅展示部分
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionBottom
return opt
},
newData: func() AxisDataList {
return NewAxisDataListFromStringList([]string{
"01-01",
"01-02",
"01-03",
"01-04",
"01-05",
"01-06",
"01-07",
"01-08",
"01-09",
"01-10",
"01-11",
"01-12",
"01-13",
"01-14",
"01-15",
"01-16",
"01-17",
"01-18",
"01-19",
"01-20",
"01-21",
})
},
result: "",
},
}
for _, tt := range tests {
p, err := NewPainter(PainterOptions{
Width: 400,
Height: 300,
}, PainterPaddingOption(chart.Box{
Left: 5,
Top: 5,
Right: 5,
Bottom: 5,
}))
assert.Nil(err)
style := tt.newOption()
data := axisData
if tt.newData != nil {
data = tt.newData()
}
NewAxis(p, data, style).Render()
result, err := p.Bytes()
assert.Nil(err)
assert.Equal(tt.result, string(result))
}
}
func TestMeasureAxis(t *testing.T) {
assert := assert.New(t)
p, err := NewPainter(PainterOptions{
Width: 400,
Height: 300,
})
assert.Nil(err)
data := NewAxisDataListFromStringList([]string{
"Mon",
"Sun",
})
f, _ := chart.GetDefaultFont()
width := NewAxis(p, data, AxisOption{
FontSize: 12,
Font: f,
Position: PositionLeft,
}).measure().Width
assert.Equal(44, width)
height := NewAxis(p, data, AxisOption{
FontSize: 12,
Font: f,
Position: PositionTop,
}).measure().Height
assert.Equal(28, height)
}