// 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 (
"math"
"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 TestPainterOption(t *testing.T) {
assert := assert.New(t)
font := &truetype.Font{}
d, err := NewPainter(PainterOptions{
Width: 800,
Height: 600,
Type: ChartOutputSVG,
},
PainterBoxOption(Box{
Right: 400,
Bottom: 300,
}),
PainterPaddingOption(Box{
Left: 1,
Top: 2,
Right: 3,
Bottom: 4,
}),
PainterFontOption(font),
PainterStyleOption(Style{
ClassName: "test",
}),
)
assert.Nil(err)
assert.Equal(Box{
Left: 1,
Top: 2,
Right: 397,
Bottom: 296,
}, d.box)
assert.Equal(font, d.font)
assert.Equal("test", d.style.ClassName)
}
func TestPainter(t *testing.T) {
assert := assert.New(t)
tests := []struct {
fn func(*Painter)
result string
}{
// moveTo, lineTo
{
fn: func(p *Painter) {
p.MoveTo(1, 1)
p.LineTo(2, 2)
p.Stroke()
},
result: "",
},
// circle
{
fn: func(p *Painter) {
p.Circle(5, 2, 3)
},
result: "",
},
// text
{
fn: func(p *Painter) {
p.Text("hello world!", 3, 6)
},
result: "",
},
// line stroke
{
fn: func(p *Painter) {
p.SetDrawingStyle(Style{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
})
p.LineStroke([]Point{
{
X: 1,
Y: 2,
},
{
X: 3,
Y: 4,
},
})
},
result: "",
},
// set background
{
fn: func(p *Painter) {
p.SetBackground(400, 300, chart.ColorWhite)
},
result: "",
},
// arcTo
{
fn: func(p *Painter) {
p.SetStyle(Style{
StrokeWidth: 1,
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlue,
})
p.ArcTo(100, 100, 100, 100, 0, math.Pi/2)
p.Close()
p.FillStroke()
},
result: "",
},
// pin
{
fn: func(p *Painter) {
p.SetStyle(Style{
StrokeWidth: 1,
StrokeColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
FillColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
})
p.Pin(30, 30, 30)
},
result: "",
},
// arrow left
{
fn: func(p *Painter) {
p.SetStyle(Style{
StrokeWidth: 1,
StrokeColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
FillColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
})
p.ArrowLeft(30, 30, 16, 10)
},
result: "",
},
// arrow right
{
fn: func(p *Painter) {
p.SetStyle(Style{
StrokeWidth: 1,
StrokeColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
FillColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
})
p.ArrowRight(30, 30, 16, 10)
},
result: "",
},
// arrow top
{
fn: func(p *Painter) {
p.SetStyle(Style{
StrokeWidth: 1,
StrokeColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
FillColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
})
p.ArrowTop(30, 30, 10, 16)
},
result: "",
},
// arrow bottom
{
fn: func(p *Painter) {
p.SetStyle(Style{
StrokeWidth: 1,
StrokeColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
FillColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
})
p.ArrowBottom(30, 30, 10, 16)
},
result: "",
},
// mark line
{
fn: func(p *Painter) {
p.SetStyle(Style{
StrokeWidth: 1,
StrokeColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
FillColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
StrokeDashArray: []float64{
4,
2,
},
})
p.MarkLine(0, 20, 300)
},
result: "",
},
// polygon
{
fn: func(p *Painter) {
p.SetStyle(Style{
StrokeWidth: 1,
StrokeColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
})
p.Polygon(Point{
X: 100,
Y: 100,
}, 50, 6)
},
result: "",
},
// FillArea
{
fn: func(p *Painter) {
p.SetDrawingStyle(Style{
FillColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
})
p.FillArea([]Point{
{
X: 0,
Y: 0,
},
{
X: 0,
Y: 100,
},
{
X: 100,
Y: 100,
},
{
X: 0,
Y: 0,
},
})
},
result: "",
},
}
for _, tt := range tests {
d, err := NewPainter(PainterOptions{
Width: 400,
Height: 300,
Type: ChartOutputSVG,
}, PainterPaddingOption(chart.Box{
Left: 5,
Top: 10,
}))
assert.Nil(err)
tt.fn(d)
data, err := d.Bytes()
assert.Nil(err)
assert.Equal(tt.result, string(data))
}
}
func TestRoundedRect(t *testing.T) {
assert := assert.New(t)
p, err := NewPainter(PainterOptions{
Width: 400,
Height: 300,
Type: ChartOutputSVG,
})
assert.Nil(err)
p.OverrideDrawingStyle(Style{
FillColor: drawing.ColorWhite,
StrokeWidth: 1,
StrokeColor: drawing.ColorWhite,
}).RoundedRect(Box{
Left: 10,
Right: 30,
Bottom: 150,
Top: 10,
}, 5)
buf, err := p.Bytes()
assert.Nil(err)
assert.Equal("", string(buf))
}
func TestPainterTextFit(t *testing.T) {
assert := assert.New(t)
p, err := NewPainter(PainterOptions{
Width: 400,
Height: 300,
Type: ChartOutputSVG,
})
assert.Nil(err)
f, _ := GetDefaultFont()
style := Style{
FontSize: 12,
FontColor: chart.ColorBlack,
Font: f,
}
p.SetStyle(style)
box := p.TextFit("Hello World!", 0, 20, 80)
assert.Equal(chart.Box{
Right: 45,
Bottom: 35,
}, box)
box = p.TextFit("Hello World!", 0, 100, 200)
assert.Equal(chart.Box{
Right: 84,
Bottom: 15,
}, box)
buf, err := p.Bytes()
assert.Nil(err)
assert.Equal(``, string(buf))
}