refactor: adjust axis function

This commit is contained in:
vicanso 2022-05-16 20:41:13 +08:00
parent 5068828ca7
commit 7e80e9a848
7 changed files with 134 additions and 107 deletions

118
axis.go
View file

@ -27,7 +27,6 @@ import (
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
type AxisOption struct { type AxisOption struct {
@ -42,7 +41,7 @@ type AxisOption struct {
SplitNumber int SplitNumber int
ClassName string ClassName string
// The line color of axis // The line color of axis
StrokeColor drawing.Color StrokeColor Color
// The line width // The line width
StrokeWidth float64 StrokeWidth float64
// The length of the axis tick // The length of the axis tick
@ -56,17 +55,17 @@ type AxisOption struct {
// The font of label // The font of label
Font *truetype.Font Font *truetype.Font
// The color of label // The color of label
FontColor drawing.Color FontColor Color
// The flag for show axis split line, set this to true will show axis split line // The flag for show axis split line, set this to true will show axis split line
SplitLineShow bool SplitLineShow bool
// The color of split line // The color of split line
SplitLineColor drawing.Color SplitLineColor Color
} }
type axis struct { type axis struct {
d *Draw painter *Painter
data *AxisDataList data *AxisDataList
option *AxisOption option *AxisOption
} }
type axisMeasurement struct { type axisMeasurement struct {
Width int Width int
@ -74,11 +73,11 @@ type axisMeasurement struct {
} }
// NewAxis creates a new axis with data and style options // NewAxis creates a new axis with data and style options
func NewAxis(d *Draw, data AxisDataList, option AxisOption) *axis { func NewAxis(p *Painter, data AxisDataList, option AxisOption) *axis {
return &axis{ return &axis{
d: d, painter: p,
data: &data, data: &data,
option: &option, option: &option,
} }
} }
@ -149,17 +148,19 @@ func NewAxisDataListFromStringList(textList []string) AxisDataList {
func (a *axis) axisLabel(renderOpt *axisRenderOption) { func (a *axis) axisLabel(renderOpt *axisRenderOption) {
option := a.option option := a.option
data := *a.data data := *a.data
d := a.d // d := a.d
if option.FontColor.IsZero() || len(data) == 0 { if option.FontColor.IsZero() || len(data) == 0 {
return return
} }
r := d.Render // r := d.Render
s := option.Style(d.Font) // s.GetTextOptions().WriteTextOptionsToRenderer(r)
s.GetTextOptions().WriteTextOptionsToRenderer(r) p := a.painter
s := option.Style(p.font)
p.SetTextStyle(s)
width := d.Box.Width() width := p.Width()
height := d.Box.Height() height := p.Height()
textList := data.TextList() textList := data.TextList()
count := len(textList) count := len(textList)
@ -188,7 +189,7 @@ func (a *axis) axisLabel(renderOpt *axisRenderOption) {
reverseIntSlice(values) reverseIntSlice(values)
for index, text := range textList { for index, text := range textList {
y := values[index] - 2 y := values[index] - 2
b := r.MeasureText(text) b := p.MeasureText(text)
if boundaryGap { if boundaryGap {
height := y - values[index+1] height := y - values[index+1]
y -= (height - b.Height()) >> 1 y -= (height - b.Height()) >> 1
@ -200,7 +201,7 @@ func (a *axis) axisLabel(renderOpt *axisRenderOption) {
if position == PositionLeft { if position == PositionLeft {
x = labelWidth - b.Width() - 1 x = labelWidth - b.Width() - 1
} }
d.text(text, x, y) p.Text(text, x, y)
} }
default: default:
// 定位bottom重新计算y0的定位 // 定位bottom重新计算y0的定位
@ -215,7 +216,7 @@ func (a *axis) axisLabel(renderOpt *axisRenderOption) {
} }
x := values[index] x := values[index]
leftOffset := 0 leftOffset := 0
b := r.MeasureText(text) b := p.MeasureText(text)
if boundaryGap { if boundaryGap {
width := values[index+1] - x width := values[index+1] - x
leftOffset = (width - b.Width()) >> 1 leftOffset = (width - b.Width()) >> 1
@ -223,24 +224,25 @@ func (a *axis) axisLabel(renderOpt *axisRenderOption) {
// 左移文本长度 // 左移文本长度
leftOffset = -b.Width() >> 1 leftOffset = -b.Width() >> 1
} }
d.text(text, x+leftOffset, y0) p.Text(text, x+leftOffset, y0)
} }
} }
} }
func (a *axis) axisLine(renderOpt *axisRenderOption) { func (a *axis) axisLine(renderOpt *axisRenderOption) {
d := a.d // d := a.d
r := d.Render // r := d.Render
p := a.painter
option := a.option option := a.option
s := option.Style(d.Font) s := option.Style(p.font)
s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r) p.SetDrawingStyle(s.GetStrokeOptions())
x0 := 0 x0 := 0
y0 := 0 y0 := 0
x1 := 0 x1 := 0
y1 := 0 y1 := 0
width := d.Box.Width() width := p.Width()
height := d.Box.Height() height := p.Height()
labelMargin := option.GetLabelMargin() labelMargin := option.GetLabelMargin()
// 轴线 // 轴线
@ -271,21 +273,22 @@ func (a *axis) axisLine(renderOpt *axisRenderOption) {
y1 = y0 y1 = y0
} }
d.moveTo(x0, y0) p.MoveTo(x0, y0)
d.lineTo(x1, y1) p.LineTo(x1, y1)
r.FillStroke() p.FillStroke()
} }
func (a *axis) axisTick(renderOpt *axisRenderOption) { func (a *axis) axisTick(renderOpt *axisRenderOption) {
d := a.d // d := a.d
r := d.Render // r := d.Render
p := a.painter
option := a.option option := a.option
s := option.Style(d.Font) s := option.Style(p.font)
s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r) p.SetDrawingStyle(s.GetStrokeOptions())
width := d.Box.Width() width := p.Width()
height := d.Box.Height() height := p.Height()
data := *a.data data := *a.data
tickCount := len(data) tickCount := len(data)
if tickCount == 0 { if tickCount == 0 {
@ -319,14 +322,14 @@ func (a *axis) axisTick(renderOpt *axisRenderOption) {
for _, v := range values { for _, v := range values {
x := x0 x := x0
y := v y := v
d.moveTo(x, y) p.MoveTo(x, y)
d.lineTo(x+tickLengthValue, y) p.LineTo(x+tickLengthValue, y)
r.Stroke() p.Stroke()
} }
} }
// 辅助线 // 辅助线
if option.SplitLineShow && !option.SplitLineColor.IsZero() { if option.SplitLineShow && !option.SplitLineColor.IsZero() {
r.SetStrokeColor(option.SplitLineColor) p.SetStrokeColor(option.SplitLineColor)
splitLineWidth := width - labelWidth - tickLengthValue splitLineWidth := width - labelWidth - tickLengthValue
x0 = labelWidth + tickLengthValue x0 = labelWidth + tickLengthValue
if position == PositionRight { if position == PositionRight {
@ -336,9 +339,9 @@ func (a *axis) axisTick(renderOpt *axisRenderOption) {
for _, v := range values[0 : len(values)-1] { for _, v := range values[0 : len(values)-1] {
x := x0 x := x0
y := v y := v
d.moveTo(x, y) p.MoveTo(x, y)
d.lineTo(x+splitLineWidth, y) p.LineTo(x+splitLineWidth, y)
r.Stroke() p.Stroke()
} }
} }
default: default:
@ -355,14 +358,14 @@ func (a *axis) axisTick(renderOpt *axisRenderOption) {
} }
x := v x := v
y := y0 y := y0
d.moveTo(x, y-tickLengthValue) p.MoveTo(x, y-tickLengthValue)
d.lineTo(x, y) p.LineTo(x, y)
r.Stroke() p.Stroke()
} }
} }
// 辅助线 // 辅助线
if option.SplitLineShow && !option.SplitLineColor.IsZero() { if option.SplitLineShow && !option.SplitLineColor.IsZero() {
r.SetStrokeColor(option.SplitLineColor) p.SetStrokeColor(option.SplitLineColor)
y0 = 0 y0 = 0
splitLineHeight := height - labelHeight - tickLengthValue splitLineHeight := height - labelHeight - tickLengthValue
if position == PositionTop { if position == PositionTop {
@ -377,22 +380,23 @@ func (a *axis) axisTick(renderOpt *axisRenderOption) {
x := v x := v
y := y0 y := y0
d.moveTo(x, y) p.MoveTo(x, y)
d.lineTo(x, y0+splitLineHeight) p.LineTo(x, y0+splitLineHeight)
r.Stroke() p.Stroke()
} }
} }
} }
} }
func (a *axis) measureTextMaxWidthHeight() (int, int) { func (a *axis) measureTextMaxWidthHeight() (int, int) {
d := a.d // d := a.d
r := d.Render // r := d.Render
s := a.option.Style(d.Font) p := a.painter
s := a.option.Style(p.font)
data := a.data data := a.data
s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r) p.SetDrawingStyle(s.GetStrokeOptions())
s.GetTextOptions().WriteTextOptionsToRenderer(r) p.SetTextStyle(s.GetTextOptions())
return measureTextMaxWidthHeight(data.TextList(), r) return measureTextMaxWidthHeight(data.TextList(), p)
} }
// measure returns the measurement of axis. // measure returns the measurement of axis.
@ -429,7 +433,7 @@ func (a *axis) Render() {
} }
unitCount := chart.MaxInt(option.SplitNumber, 1) unitCount := chart.MaxInt(option.SplitNumber, 1)
width := a.d.Box.Width() width := a.painter.Width()
textList := a.data.TextList() textList := a.data.TextList()
count := len(textList) count := len(textList)

View file

@ -207,10 +207,10 @@ func TestAxis(t *testing.T) {
}, },
} }
for _, tt := range tests { for _, tt := range tests {
d, err := NewDraw(DrawOption{ p, err := NewPainter(PainterOptions{
Width: 400, Width: 400,
Height: 300, Height: 300,
}, PaddingOption(chart.Box{ }, PainterPaddingOption(chart.Box{
Left: 5, Left: 5,
Top: 5, Top: 5,
Right: 5, Right: 5,
@ -222,9 +222,9 @@ func TestAxis(t *testing.T) {
if tt.newData != nil { if tt.newData != nil {
data = tt.newData() data = tt.newData()
} }
NewAxis(d, data, style).Render() NewAxis(p, data, style).Render()
result, err := d.Bytes() result, err := p.Bytes()
assert.Nil(err) assert.Nil(err)
assert.Equal(tt.result, string(result)) assert.Equal(tt.result, string(result))
} }
@ -233,7 +233,7 @@ func TestAxis(t *testing.T) {
func TestMeasureAxis(t *testing.T) { func TestMeasureAxis(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
d, err := NewDraw(DrawOption{ p, err := NewPainter(PainterOptions{
Width: 400, Width: 400,
Height: 300, Height: 300,
}) })
@ -243,14 +243,14 @@ func TestMeasureAxis(t *testing.T) {
"Sun", "Sun",
}) })
f, _ := chart.GetDefaultFont() f, _ := chart.GetDefaultFont()
width := NewAxis(d, data, AxisOption{ width := NewAxis(p, data, AxisOption{
FontSize: 12, FontSize: 12,
Font: f, Font: f,
Position: PositionLeft, Position: PositionLeft,
}).measure().Width }).measure().Width
assert.Equal(44, width) assert.Equal(44, width)
height := NewAxis(d, data, AxisOption{ height := NewAxis(p, data, AxisOption{
FontSize: 12, FontSize: 12,
Font: f, Font: f,
Position: PositionTop, Position: PositionTop,

View file

@ -38,14 +38,15 @@ type Painter struct {
parent *Painter parent *Painter
style Style style Style
previousStyle Style previousStyle Style
theme *Theme
} }
type PainterOptions struct { type PainterOptions struct {
// Draw type, "svg" or "png", default type is "svg" // Draw type, "svg" or "png", default type is "svg"
Type string Type string
// The width of draw canvas // The width of draw painter
Width int Width int
// The height of draw canvas // The height of draw painter
Height int Height int
// The font for painter // The font for painter
Font *truetype.Font Font *truetype.Font
@ -53,7 +54,7 @@ type PainterOptions struct {
type PainterOption func(*Painter) type PainterOption func(*Painter)
// PainterPaddingOption sets the padding of draw canvas // PainterPaddingOption sets the padding of draw painter
func PainterPaddingOption(padding Box) PainterOption { func PainterPaddingOption(padding Box) PainterOption {
return func(p *Painter) { return func(p *Painter) {
p.box.Left += padding.Left p.box.Left += padding.Left
@ -63,7 +64,7 @@ func PainterPaddingOption(padding Box) PainterOption {
} }
} }
// PainterBoxOption sets the box of draw canvas // PainterBoxOption sets the box of draw painter
func PainterBoxOption(box Box) PainterOption { func PainterBoxOption(box Box) PainterOption {
return func(p *Painter) { return func(p *Painter) {
if box.IsZero() { if box.IsZero() {
@ -73,21 +74,45 @@ func PainterBoxOption(box Box) PainterOption {
} }
} }
// PainterFontOption sets the font of draw canvas // PainterFontOption sets the font of draw painter
func PainterFontOption(font *truetype.Font) PainterOption { func PainterFontOption(font *truetype.Font) PainterOption {
return func(p *Painter) { return func(p *Painter) {
if font == nil {
return
}
p.font = font p.font = font
} }
} }
// PainterStyleOption sets the style of draw canvas // PainterStyleOption sets the style of draw painter
func PainterStyleOption(style Style) PainterOption { func PainterStyleOption(style Style) PainterOption {
return func(p *Painter) { return func(p *Painter) {
p.SetDrawingStyle(style) p.SetDrawingStyle(style)
} }
} }
// NewPainter creates a new Painter // PainterThemeOption sets the theme of draw painter
func PainterThemeOption(theme *Theme) PainterOption {
return func(p *Painter) {
if theme == nil {
return
}
p.theme = theme
}
}
func PainterWidthHeightOption(width, height int) PainterOption {
return func(p *Painter) {
if width > 0 {
p.box.Right = p.box.Left + width
}
if height > 0 {
p.box.Bottom = p.box.Top + height
}
}
}
// NewPainter creates a new painter
func NewPainter(opts PainterOptions, opt ...PainterOption) (*Painter, error) { func NewPainter(opts PainterOptions, opt ...PainterOption) (*Painter, error) {
if opts.Width <= 0 || opts.Height <= 0 { if opts.Width <= 0 || opts.Height <= 0 {
return nil, errors.New("width/height can not be nil") return nil, errors.New("width/height can not be nil")
@ -135,6 +160,7 @@ func (p *Painter) Child(opt ...PainterOption) *Painter {
parent: p, parent: p,
style: p.style, style: p.style,
previousStyle: p.previousStyle, previousStyle: p.previousStyle,
theme: p.theme,
} }
child.setOptions(opt...) child.setOptions(opt...)
return child return child
@ -286,6 +312,22 @@ func (p *Painter) Fill() {
p.render.Fill() p.render.Fill()
} }
func (p *Painter) Width() int {
return p.box.Width()
}
func (p *Painter) Height() int {
return p.box.Height()
}
func (p *Painter) MeasureText(text string) Box {
return p.render.MeasureText(text)
}
func (p *Painter) SetStrokeColor(color Color) {
p.render.SetStrokeColor(color)
}
func (p *Painter) LineStroke(points []Point, style LineStyle) { func (p *Painter) LineStroke(points []Point, style LineStyle) {
s := style.Style() s := style.Style()
if !s.ShouldDrawStroke() { if !s.ShouldDrawStroke() {

View file

@ -76,11 +76,11 @@ func autoDivide(max, size int) []int {
} }
// measureTextMaxWidthHeight returns maxWidth and maxHeight of text list // measureTextMaxWidthHeight returns maxWidth and maxHeight of text list
func measureTextMaxWidthHeight(textList []string, r chart.Renderer) (int, int) { func measureTextMaxWidthHeight(textList []string, p *Painter) (int, int) {
maxWidth := 0 maxWidth := 0
maxHeight := 0 maxHeight := 0
for _, text := range textList { for _, text := range textList {
box := r.MeasureText(text) box := p.MeasureText(text)
maxWidth = chart.MaxInt(maxWidth, box.Width()) maxWidth = chart.MaxInt(maxWidth, box.Width())
maxHeight = chart.MaxInt(maxHeight, box.Height()) maxHeight = chart.MaxInt(maxHeight, box.Height())
} }

View file

@ -24,7 +24,6 @@ package charts
import ( import (
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2"
) )
type XAxisOption struct { type XAxisOption struct {
@ -53,27 +52,19 @@ func NewXAxisOption(data []string, boundaryGap ...*bool) XAxisOption {
} }
// drawXAxis draws x axis, and returns the height, range of if. // drawXAxis draws x axis, and returns the height, range of if.
func drawXAxis(p *Draw, opt *XAxisOption, yAxisCount int) (int, *Range, error) { func drawXAxis(p *Painter, opt *XAxisOption, yAxisCount int) (int, *Range, error) {
if opt.Hidden { if opt.Hidden {
return 0, nil, nil return 0, nil, nil
} }
left := YAxisWidth left := YAxisWidth
right := (yAxisCount - 1) * YAxisWidth right := (yAxisCount - 1) * YAxisWidth
dXAxis, err := NewDraw( pXAxis := p.Child(
DrawOption{ PainterPaddingOption(Box{
Parent: p,
},
PaddingOption(chart.Box{
Left: left, Left: left,
Right: right, Right: right,
}), }),
PainterFontOption(opt.Font),
) )
if opt.Font != nil {
dXAxis.Font = opt.Font
}
if err != nil {
return 0, nil, err
}
theme := NewTheme(opt.Theme) theme := NewTheme(opt.Theme)
data := NewAxisDataListFromStringList(opt.Data) data := NewAxisDataListFromStringList(opt.Data)
style := AxisOption{ style := AxisOption{
@ -90,13 +81,13 @@ func drawXAxis(p *Draw, opt *XAxisOption, yAxisCount int) (int, *Range, error) {
boundary = false boundary = false
max-- max--
} }
axis := NewAxis(dXAxis, data, style) axis := NewAxis(pXAxis, data, style)
axis.Render() axis.Render()
return axis.measure().Height, &Range{ return axis.measure().Height, &Range{
divideCount: len(opt.Data), divideCount: len(opt.Data),
Min: 0, Min: 0,
Max: max, Max: max,
Size: dXAxis.Box.Width(), Size: pXAxis.Width(),
Boundary: boundary, Boundary: boundary,
}, nil }, nil
} }

View file

@ -45,7 +45,7 @@ type YAxisOption struct {
// TODO 长度是否可以变化 // TODO 长度是否可以变化
const YAxisWidth = 40 const YAxisWidth = 40
func drawYAxis(p *Draw, opt *ChartOption, axisIndex, xAxisHeight int, padding chart.Box) (*Range, error) { func drawYAxis(p *Painter, opt *ChartOption, axisIndex, xAxisHeight int, padding chart.Box) (*Range, error) {
theme := NewTheme(opt.Theme) theme := NewTheme(opt.Theme)
yRange := opt.newYRange(axisIndex) yRange := opt.newYRange(axisIndex)
values := yRange.Values() values := yRange.Values()
@ -74,32 +74,22 @@ func drawYAxis(p *Draw, opt *ChartOption, axisIndex, xAxisHeight int, padding ch
width := NewAxis(p, data, style).measure().Width width := NewAxis(p, data, style).measure().Width
yAxisCount := len(opt.YAxisList) yAxisCount := len(opt.YAxisList)
boxWidth := p.Box.Width() boxWidth := p.Width()
if axisIndex > 0 { if axisIndex > 0 {
style.SplitLineShow = false style.SplitLineShow = false
style.Position = PositionRight style.Position = PositionRight
padding.Right += (axisIndex - 1) * YAxisWidth padding.Right += (axisIndex - 1) * YAxisWidth
} else { } else {
boxWidth = p.Box.Width() - (yAxisCount-1)*YAxisWidth boxWidth = p.Width() - (yAxisCount-1)*YAxisWidth
padding.Left += (YAxisWidth - width) padding.Left += (YAxisWidth - width)
} }
dYAxis, err := NewDraw( pYAxis := p.Child(
DrawOption{ PainterWidthHeightOption(boxWidth, p.Height()-xAxisHeight),
Parent: p, PainterPaddingOption(padding),
Width: boxWidth, PainterFontOption(opt.Font),
// 减去x轴的高
Height: p.Box.Height() - xAxisHeight,
},
PaddingOption(padding),
) )
if err != nil { NewAxis(pYAxis, data, style).Render()
return nil, err yRange.Size = pYAxis.Height()
}
if opt.Font != nil {
dYAxis.Font = opt.Font
}
NewAxis(dYAxis, data, style).Render()
yRange.Size = dYAxis.Box.Height()
return &yRange, nil return &yRange, nil
} }