diff --git a/axis.go b/axis.go
index 9a9508f..7f3ba4b 100644
--- a/axis.go
+++ b/axis.go
@@ -45,6 +45,21 @@ type AxisStyle struct {
SplitLineColor drawing.Color
}
+type axis struct {
+ d *Draw
+ data *AxisDataList
+ style *AxisStyle
+}
+
+func NewAxis(d *Draw, data AxisDataList, style AxisStyle) *axis {
+ return &axis{
+ d: d,
+ data: &data,
+ style: &style,
+ }
+
+}
+
func (as *AxisStyle) GetLabelMargin() int {
return getDefaultInt(as.LabelMargin, 8)
}
@@ -85,8 +100,6 @@ func (l AxisDataList) TextList() []string {
}
type axisOption struct {
- data *AxisDataList
- style *AxisStyle
textMaxWith int
textMaxHeight int
boundaryGap bool
@@ -102,9 +115,10 @@ func NewAxisDataListFromStringList(textList []string) AxisDataList {
return list
}
-func (d *Draw) axisLabel(opt *axisOption) {
- style := opt.style
- data := *opt.data
+func (a *axis) axisLabel(opt *axisOption) {
+ style := a.style
+ data := *a.data
+ d := a.d
if style.FontColor.IsZero() || len(data) == 0 {
return
}
@@ -177,9 +191,10 @@ func (d *Draw) axisLabel(opt *axisOption) {
}
}
-func (d *Draw) axisLine(opt *axisOption) {
+func (a *axis) axisLine(opt *axisOption) {
+ d := a.d
r := d.Render
- style := opt.style
+ style := a.style
s := style.Style(d.Font)
s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r)
@@ -224,23 +239,24 @@ func (d *Draw) axisLine(opt *axisOption) {
r.FillStroke()
}
-func (d *Draw) axisTick(opt *axisOption) {
+func (a *axis) axisTick(opt *axisOption) {
+ d := a.d
r := d.Render
- style := opt.style
+ style := a.style
s := style.Style(d.Font)
s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r)
width := d.Box.Width()
height := d.Box.Height()
- data := *opt.data
+ data := *a.data
tickCount := len(data)
if !opt.boundaryGap {
tickCount--
}
labelMargin := style.GetLabelMargin()
tickShow := true
- if opt.style.TickShow != nil && !*opt.style.TickShow {
+ if style.TickShow != nil && !*style.TickShow {
tickShow = false
}
@@ -322,9 +338,11 @@ func (d *Draw) axisTick(opt *axisOption) {
}
}
-func (d *Draw) axisMeasureTextMaxWidthHeight(data AxisDataList, style AxisStyle) (int, int) {
+func (a *axis) axisMeasureTextMaxWidthHeight() (int, int) {
+ d := a.d
r := d.Render
- s := style.Style(d.Font)
+ s := a.style.Style(d.Font)
+ data := a.data
s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r)
s.GetTextOptions().WriteTextOptionsToRenderer(r)
return measureTextMaxWidthHeight(data.TextList(), r)
@@ -333,9 +351,10 @@ func (d *Draw) axisMeasureTextMaxWidthHeight(data AxisDataList, style AxisStyle)
// measureAxis return the measurement of axis.
// If the position is left or right, it will be textMaxWidth + labelMargin + tickLength.
// If the position is top or bottom, it will be textMaxHeight + labelMargin + tickLength.
-func (d *Draw) measureAxis(data AxisDataList, style AxisStyle) int {
+func (a *axis) measureAxis() int {
+ style := a.style
value := style.GetLabelMargin() + style.GetTickLength()
- textMaxWidth, textMaxHeight := d.axisMeasureTextMaxWidthHeight(data, style)
+ textMaxWidth, textMaxHeight := a.axisMeasureTextMaxWidthHeight()
if style.Position == PositionLeft ||
style.Position == PositionRight {
return textMaxWidth + value
@@ -343,14 +362,13 @@ func (d *Draw) measureAxis(data AxisDataList, style AxisStyle) int {
return textMaxHeight + value
}
-func (d *Draw) Axis(data AxisDataList, style AxisStyle) {
+func (a *axis) Render() {
+ style := a.style
if style.Show != nil && !*style.Show {
return
}
- textMaxWidth, textMaxHeight := d.axisMeasureTextMaxWidthHeight(data, style)
+ textMaxWidth, textMaxHeight := a.axisMeasureTextMaxWidthHeight()
opt := &axisOption{
- data: &data,
- style: &style,
textMaxWith: textMaxWidth,
textMaxHeight: textMaxHeight,
boundaryGap: true,
@@ -360,8 +378,8 @@ func (d *Draw) Axis(data AxisDataList, style AxisStyle) {
}
// 坐标轴线
- d.axisLine(opt)
- d.axisTick(opt)
+ a.axisLine(opt)
+ a.axisTick(opt)
// 坐标文本
- d.axisLabel(opt)
+ a.axisLabel(opt)
}
diff --git a/axis_test.go b/axis_test.go
index 06b1a97..e8efccd 100644
--- a/axis_test.go
+++ b/axis_test.go
@@ -73,30 +73,27 @@ func TestAxis(t *testing.T) {
"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),
- },
+ 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 {
- newOption func() *axisOption
- result string
+ newStyle func() AxisStyle
+ result string
}{
// 文本按起始位置展示
// axis位于bottom
{
- newOption: func() *axisOption {
- opt := getDefaultOption()
- opt.style.BoundaryGap = FalseFlag()
+ newStyle: func() AxisStyle {
+ opt := getDefaultStyle()
+ opt.BoundaryGap = FalseFlag()
return opt
},
result: "",
@@ -104,8 +101,8 @@ func TestAxis(t *testing.T) {
// 文本居中展示
// axis位于bottom
{
- newOption: func() *axisOption {
- opt := getDefaultOption()
+ newStyle: func() AxisStyle {
+ opt := getDefaultStyle()
return opt
},
result: "",
@@ -113,10 +110,10 @@ func TestAxis(t *testing.T) {
// 文本按起始位置展示
// axis位于top
{
- newOption: func() *axisOption {
- opt := getDefaultOption()
- opt.style.Position = PositionTop
- opt.style.BoundaryGap = FalseFlag()
+ newStyle: func() AxisStyle {
+ opt := getDefaultStyle()
+ opt.Position = PositionTop
+ opt.BoundaryGap = FalseFlag()
return opt
},
result: "",
@@ -124,9 +121,9 @@ func TestAxis(t *testing.T) {
// 文本居中展示
// axis位于top
{
- newOption: func() *axisOption {
- opt := getDefaultOption()
- opt.style.Position = PositionTop
+ newStyle: func() AxisStyle {
+ opt := getDefaultStyle()
+ opt.Position = PositionTop
return opt
},
result: "",
@@ -134,10 +131,10 @@ func TestAxis(t *testing.T) {
// 文本按起始位置展示
// axis位于left
{
- newOption: func() *axisOption {
- opt := getDefaultOption()
- opt.style.Position = PositionLeft
- opt.style.BoundaryGap = FalseFlag()
+ newStyle: func() AxisStyle {
+ opt := getDefaultStyle()
+ opt.Position = PositionLeft
+ opt.BoundaryGap = FalseFlag()
return opt
},
result: "",
@@ -145,9 +142,9 @@ func TestAxis(t *testing.T) {
// 文本居中展示
// axis位于left
{
- newOption: func() *axisOption {
- opt := getDefaultOption()
- opt.style.Position = PositionLeft
+ newStyle: func() AxisStyle {
+ opt := getDefaultStyle()
+ opt.Position = PositionLeft
return opt
},
result: "",
@@ -155,10 +152,10 @@ func TestAxis(t *testing.T) {
// 文本按起始位置展示
// axis位于right
{
- newOption: func() *axisOption {
- opt := getDefaultOption()
- opt.style.Position = PositionRight
- opt.style.BoundaryGap = FalseFlag()
+ newStyle: func() AxisStyle {
+ opt := getDefaultStyle()
+ opt.Position = PositionRight
+ opt.BoundaryGap = FalseFlag()
return opt
},
result: "",
@@ -166,9 +163,9 @@ func TestAxis(t *testing.T) {
// 文本居中展示
// axis位于right
{
- newOption: func() *axisOption {
- opt := getDefaultOption()
- opt.style.Position = PositionRight
+ newStyle: func() AxisStyle {
+ opt := getDefaultStyle()
+ opt.Position = PositionRight
return opt
},
result: "",
@@ -185,9 +182,8 @@ func TestAxis(t *testing.T) {
Bottom: 5,
}))
assert.Nil(err)
- opt := tt.newOption()
-
- d.Axis(*opt.data, *opt.style)
+ style := tt.newStyle()
+ NewAxis(d, data, style).Render()
result, err := d.Bytes()
assert.Nil(err)
@@ -208,17 +204,17 @@ func TestMeasureAxis(t *testing.T) {
"Sun",
})
f, _ := chart.GetDefaultFont()
- width := d.measureAxis(data, AxisStyle{
+ width := NewAxis(d, data, AxisStyle{
FontSize: 12,
Font: f,
Position: PositionLeft,
- })
+ }).measureAxis()
assert.Equal(44, width)
- height := d.measureAxis(data, AxisStyle{
+ height := NewAxis(d, data, AxisStyle{
FontSize: 12,
Font: f,
Position: PositionTop,
- })
+ }).measureAxis()
assert.Equal(28, height)
}
diff --git a/chart.go b/chart.go
index 556e2fe..9383809 100644
--- a/chart.go
+++ b/chart.go
@@ -65,7 +65,7 @@ func (o *ChartOption) FillDefault(t *Theme) {
o.BackgroundColor = t.GetBackgroundColor()
}
if o.Title.Style.FontColor.IsZero() {
- o.Title.Style.FontColor = t.GetTitleColor()
+ o.Title.Style.FontColor = t.GetTextColor()
}
if o.Title.Style.FontSize == 0 {
o.Title.Style.FontSize = 14
@@ -88,7 +88,7 @@ func (o *ChartOption) FillDefault(t *Theme) {
o.Legend.Style.Font = f
}
if o.Legend.Style.FontColor.IsZero() {
- o.Legend.Style.FontColor = t.GetTitleColor()
+ o.Legend.Style.FontColor = t.GetTextColor()
}
}
diff --git a/theme.go b/theme.go
index a67dc02..c47a5d8 100644
--- a/theme.go
+++ b/theme.go
@@ -121,7 +121,7 @@ func (t *Theme) GetBackgroundColor() drawing.Color {
return drawing.ColorWhite
}
-func (t *Theme) GetTitleColor() drawing.Color {
+func (t *Theme) GetTextColor() drawing.Color {
if t.IsDark() {
return drawing.Color{
R: 238,
diff --git a/xaxis.go b/xaxis.go
index 01ef2d3..c814074 100644
--- a/xaxis.go
+++ b/xaxis.go
@@ -31,10 +31,10 @@ type XAxisOption struct {
}
// drawXAxis draws x axis, and returns the height, range of if.
-func drawXAxis(d *Draw, opt *XAxisOption, theme *Theme) (int, *Range, error) {
+func drawXAxis(p *Draw, opt *XAxisOption, theme *Theme) (int, *Range, error) {
dXAxis, err := NewDraw(
DrawOption{
- Parent: d,
+ Parent: p,
},
PaddingOption(chart.Box{
Left: YAxisWidth,
@@ -57,9 +57,10 @@ func drawXAxis(d *Draw, opt *XAxisOption, theme *Theme) (int, *Range, error) {
boundary = false
max--
}
+ axis := NewAxis(dXAxis, data, style)
+ axis.Render()
- dXAxis.Axis(data, style)
- return d.measureAxis(data, style), &Range{
+ return axis.measureAxis(), &Range{
divideCount: len(opt.Data),
Min: 0,
Max: max,
diff --git a/yaxis.go b/yaxis.go
index 1777b02..171cfec 100644
--- a/yaxis.go
+++ b/yaxis.go
@@ -28,7 +28,7 @@ import (
const YAxisWidth = 40
-func drawYAxis(d *Draw, opt *ChartOption, theme *Theme, xAxisHeight int, padding chart.Box) (*Range, error) {
+func drawYAxis(p *Draw, opt *ChartOption, theme *Theme, xAxisHeight int, padding chart.Box) (*Range, error) {
yRange := opt.getYRange(0)
data := NewAxisDataListFromStringList(yRange.Values())
style := AxisStyle{
@@ -40,23 +40,23 @@ func drawYAxis(d *Draw, opt *ChartOption, theme *Theme, xAxisHeight int, padding
SplitLineColor: theme.GetAxisSplitLineColor(),
SplitLineShow: true,
}
- width := d.measureAxis(data, style)
+ width := NewAxis(p, data, style).measureAxis()
padding.Left += (YAxisWidth - width)
dYAxis, err := NewDraw(
DrawOption{
- Parent: d,
- Width: d.Box.Width(),
+ Parent: p,
+ Width: p.Box.Width(),
// 减去x轴的高
- Height: d.Box.Height() - xAxisHeight,
+ Height: p.Box.Height() - xAxisHeight,
},
PaddingOption(padding),
)
if err != nil {
return nil, err
}
- dYAxis.Axis(data, style)
+ NewAxis(dYAxis, data, style).Render()
yRange.Size = dYAxis.Box.Height()
return &yRange, nil
}