forked from wcharczuk/go-chart
/
xaxis.go
147 lines (124 loc) · 3.99 KB
/
xaxis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package chart
import (
"math"
"sort"
)
// XAxis represents the horizontal axis.
type XAxis struct {
Name string
Style Style
ValueFormatter ValueFormatter
Range Range
Ticks []Tick
GridLines []GridLine
GridMajorStyle Style
GridMinorStyle Style
}
// GetName returns the name.
func (xa XAxis) GetName() string {
return xa.Name
}
// GetStyle returns the style.
func (xa XAxis) GetStyle() Style {
return xa.Style
}
// GetTicks returns the ticks for a series. It coalesces between user provided ticks and
// generated ticks.
func (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
if len(xa.Ticks) > 0 {
return xa.Ticks
}
return xa.generateTicks(r, ra, defaults, vf)
}
func (xa XAxis) generateTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
step := xa.getTickStep(r, ra, defaults, vf)
return GenerateTicksWithStep(ra, step, vf)
}
func (xa XAxis) getTickStep(r Renderer, ra Range, defaults Style, vf ValueFormatter) float64 {
tickCount := xa.getTickCount(r, ra, defaults, vf)
step := ra.Delta() / float64(tickCount)
return step
}
func (xa XAxis) getTickCount(r Renderer, ra Range, defaults Style, vf ValueFormatter) int {
r.SetFont(xa.Style.GetFont(defaults.GetFont()))
r.SetFontSize(xa.Style.GetFontSize(defaults.GetFontSize(DefaultFontSize)))
// take a cut at determining the 'widest' value.
l0 := vf(ra.Min)
ln := vf(ra.Max)
ll := l0
if len(ln) > len(l0) {
ll = ln
}
llb := r.MeasureText(ll)
textWidth := llb.Width()
width := textWidth + DefaultMinimumTickHorizontalSpacing
count := int(math.Ceil(float64(ra.Domain) / float64(width)))
return count
}
// GetGridLines returns the gridlines for the axis.
func (xa XAxis) GetGridLines(ticks []Tick) []GridLine {
if len(xa.GridLines) > 0 {
return xa.GridLines
}
return GenerateGridLines(ticks, true)
}
// Measure returns the bounds of the axis.
func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box {
r.SetStrokeColor(xa.Style.GetStrokeColor(defaults.StrokeColor))
r.SetStrokeWidth(xa.Style.GetStrokeWidth(defaults.StrokeWidth))
r.SetStrokeDashArray(xa.Style.GetStrokeDashArray())
r.SetFont(xa.Style.GetFont(defaults.GetFont()))
r.SetFontColor(xa.Style.GetFontColor(DefaultAxisColor))
r.SetFontSize(xa.Style.GetFontSize(defaults.GetFontSize()))
sort.Sort(Ticks(ticks))
var left, right, top, bottom = math.MaxInt32, 0, math.MaxInt32, 0
for _, t := range ticks {
v := t.Value
lx := ra.Translate(v)
tb := r.MeasureText(t.Label)
tx := canvasBox.Left + lx
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
top = MinInt(top, canvasBox.Bottom)
left = MinInt(left, tx-(tb.Width()>>1))
right = MaxInt(right, tx+(tb.Width()>>1))
bottom = MaxInt(bottom, ty)
}
return Box{
Top: top,
Left: left,
Right: right,
Bottom: bottom,
}
}
// Render renders the axis
func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) {
r.SetStrokeColor(xa.Style.GetStrokeColor(defaults.StrokeColor))
r.SetStrokeWidth(xa.Style.GetStrokeWidth(defaults.StrokeWidth))
r.SetStrokeDashArray(xa.Style.GetStrokeDashArray())
r.SetFont(xa.Style.GetFont(defaults.GetFont()))
r.SetFontColor(xa.Style.GetFontColor(DefaultAxisColor))
r.SetFontSize(xa.Style.GetFontSize(defaults.GetFontSize()))
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.Stroke()
sort.Sort(Ticks(ticks))
for _, t := range ticks {
v := t.Value
lx := ra.Translate(v)
tb := r.MeasureText(t.Label)
tx := canvasBox.Left + lx
ty := canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
r.Text(t.Label, tx-tb.Width()>>1, ty)
r.MoveTo(tx, canvasBox.Bottom)
r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke()
}
if xa.GridMajorStyle.Show || xa.GridMinorStyle.Show {
for _, gl := range xa.GetGridLines(ticks) {
if (gl.IsMinor && xa.GridMinorStyle.Show) ||
(!gl.IsMinor && xa.GridMajorStyle.Show) {
gl.Render(r, canvasBox, ra)
}
}
}
}