// 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",
})
getDefaultOption := func() *axisOption {
return &axisOption{
data: &data,
style: &AxisStyle{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
FontColor: drawing.ColorBlack,
Show: TrueFlag(),
TickShow: TrueFlag(),
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack.WithAlpha(60),
},
}
}
tests := []struct {
newOption func() *axisOption
result string
}{
// 文本按起始位置展示
// axis位于bottom
{
newOption: func() *axisOption {
opt := getDefaultOption()
opt.style.BoundaryGap = FalseFlag()
return opt
},
result: "",
},
// 文本居中展示
// axis位于bottom
{
newOption: func() *axisOption {
opt := getDefaultOption()
return opt
},
result: "",
},
// 文本按起始位置展示
// axis位于top
{
newOption: func() *axisOption {
opt := getDefaultOption()
opt.style.Position = PositionTop
opt.style.BoundaryGap = FalseFlag()
return opt
},
result: "",
},
// 文本居中展示
// axis位于top
{
newOption: func() *axisOption {
opt := getDefaultOption()
opt.style.Position = PositionTop
return opt
},
result: "",
},
// 文本按起始位置展示
// axis位于left
{
newOption: func() *axisOption {
opt := getDefaultOption()
opt.style.Position = PositionLeft
opt.style.BoundaryGap = FalseFlag()
return opt
},
result: "",
},
// 文本居中展示
// axis位于left
{
newOption: func() *axisOption {
opt := getDefaultOption()
opt.style.Position = PositionLeft
return opt
},
result: "",
},
// 文本按起始位置展示
// axis位于right
{
newOption: func() *axisOption {
opt := getDefaultOption()
opt.style.Position = PositionRight
opt.style.BoundaryGap = FalseFlag()
return opt
},
result: "",
},
// 文本居中展示
// axis位于right
{
newOption: func() *axisOption {
opt := getDefaultOption()
opt.style.Position = PositionRight
return opt
},
result: "",
},
}
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)
opt := tt.newOption()
d.Axis(*opt.data, *opt.style)
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 := d.measureAxis(data, AxisStyle{
FontSize: 12,
Font: f,
Position: PositionLeft,
})
assert.Equal(44, width)
height := d.measureAxis(data, AxisStyle{
FontSize: 12,
Font: f,
Position: PositionTop,
})
assert.Equal(28, height)
}