refactor: enhance axis render function
This commit is contained in:
parent
29a1bdc1fb
commit
9dbea37f55
6 changed files with 97 additions and 82 deletions
62
axis.go
62
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)
|
||||
}
|
||||
|
|
|
|||
74
axis_test.go
74
axis_test.go
|
|
@ -73,10 +73,8 @@ func TestAxis(t *testing.T) {
|
|||
"Sat",
|
||||
"Sun",
|
||||
})
|
||||
getDefaultOption := func() *axisOption {
|
||||
return &axisOption{
|
||||
data: &data,
|
||||
style: &AxisStyle{
|
||||
getDefaultStyle := func() AxisStyle {
|
||||
return AxisStyle{
|
||||
StrokeColor: drawing.ColorBlack,
|
||||
StrokeWidth: 1,
|
||||
FontColor: drawing.ColorBlack,
|
||||
|
|
@ -84,19 +82,18 @@ func TestAxis(t *testing.T) {
|
|||
TickShow: TrueFlag(),
|
||||
SplitLineShow: true,
|
||||
SplitLineColor: drawing.ColorBlack.WithAlpha(60),
|
||||
},
|
||||
}
|
||||
}
|
||||
tests := []struct {
|
||||
newOption func() *axisOption
|
||||
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: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 270\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 270\nL 5 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 70 270\nL 70 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 135 270\nL 135 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 200 270\nL 200 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 265 270\nL 265 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 330 270\nL 330 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 395 270\nL 395 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 5\nL 5 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 70 5\nL 70 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 135 5\nL 135 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 200 5\nL 200 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 265 5\nL 265 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 330 5\nL 330 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 395 5\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"-8\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"59\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"122\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"189\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"257\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"320\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"384\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
|
||||
|
|
@ -104,8 +101,8 @@ func TestAxis(t *testing.T) {
|
|||
// 文本居中展示
|
||||
// axis位于bottom
|
||||
{
|
||||
newOption: func() *axisOption {
|
||||
opt := getDefaultOption()
|
||||
newStyle: func() AxisStyle {
|
||||
opt := getDefaultStyle()
|
||||
return opt
|
||||
},
|
||||
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 270\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 270\nL 5 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 61 270\nL 61 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 117 270\nL 117 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 173 270\nL 173 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 229 270\nL 229 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 285 270\nL 285 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 340 270\nL 340 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 395 270\nL 395 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 5\nL 5 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 61 5\nL 61 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 117 5\nL 117 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 173 5\nL 173 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 229 5\nL 229 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 285 5\nL 285 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 340 5\nL 340 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 395 5\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"20\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"78\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"132\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"190\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"249\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"303\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"356\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
|
||||
|
|
@ -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: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 25\nL 395 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 20\nL 5 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 70 20\nL 70 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 135 20\nL 135 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 200 20\nL 200 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 265 20\nL 265 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 330 20\nL 330 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 395 20\nL 395 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 25\nL 5 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 70 25\nL 70 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 135 25\nL 135 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 200 25\nL 200 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 265 25\nL 265 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 330 25\nL 330 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 395 25\nL 395 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"-8\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"59\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"122\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"189\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"257\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"320\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"384\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
|
||||
|
|
@ -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: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 25\nL 395 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 20\nL 5 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 61 20\nL 61 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 117 20\nL 117 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 173 20\nL 173 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 229 20\nL 229 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 285 20\nL 285 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 340 20\nL 340 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 395 20\nL 395 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 25\nL 5 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 61 25\nL 61 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 117 25\nL 117 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 173 25\nL 173 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 229 25\nL 229 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 285 25\nL 285 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 340 25\nL 340 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 395 25\nL 395 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"20\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"78\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"132\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"190\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"249\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"303\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"356\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
|
||||
|
|
@ -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: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 44 5\nL 44 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 5\nL 44 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 54\nL 44 54\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 103\nL 44 103\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 151\nL 44 151\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 199\nL 44 199\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 247\nL 44 247\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 295\nL 44 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 44 5\nL 395 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 54\nL 395 54\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 103\nL 395 103\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 151\nL 395 151\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 199\nL 395 199\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 247\nL 395 247\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"12\" y=\"299\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"16\" y=\"251\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"12\" y=\"203\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"16\" y=\"155\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"23\" y=\"107\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"19\" y=\"58\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"16\" y=\"9\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
|
||||
|
|
@ -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: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 44 5\nL 44 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 5\nL 44 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 47\nL 44 47\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 89\nL 44 89\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 131\nL 44 131\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 172\nL 44 172\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 213\nL 44 213\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 254\nL 44 254\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 295\nL 44 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 44 5\nL 395 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 47\nL 395 47\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 89\nL 395 89\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 131\nL 395 131\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 172\nL 395 172\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 213\nL 395 213\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 254\nL 395 254\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"12\" y=\"280\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"16\" y=\"239\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"12\" y=\"198\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"16\" y=\"157\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"23\" y=\"115\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"19\" y=\"73\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"16\" y=\"31\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
|
||||
|
|
@ -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: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 361 5\nL 361 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 5\nL 366 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 54\nL 366 54\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 103\nL 366 103\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 151\nL 366 151\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 199\nL 366 199\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 247\nL 366 247\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 295\nL 366 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 5\nL 360 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 54\nL 360 54\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 103\nL 360 103\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 151\nL 360 151\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 199\nL 360 199\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 247\nL 360 247\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"369\" y=\"299\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"369\" y=\"251\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"369\" y=\"203\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"369\" y=\"155\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"369\" y=\"107\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"369\" y=\"58\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"369\" y=\"9\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
|
||||
|
|
@ -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: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 361 5\nL 361 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 5\nL 366 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 47\nL 366 47\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 89\nL 366 89\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 131\nL 366 131\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 172\nL 366 172\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 213\nL 366 213\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 254\nL 366 254\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 295\nL 366 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 5\nL 360 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 47\nL 360 47\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 89\nL 360 89\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 131\nL 360 131\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 172\nL 360 172\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 213\nL 360 213\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 254\nL 360 254\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"369\" y=\"280\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"369\" y=\"239\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"369\" y=\"198\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"369\" y=\"157\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"369\" y=\"115\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"369\" y=\"73\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"369\" y=\"31\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
4
chart.go
4
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
2
theme.go
2
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,
|
||||
|
|
|
|||
9
xaxis.go
9
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,
|
||||
|
|
|
|||
12
yaxis.go
12
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue