forked from beevee/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xaxis.go
210 lines (176 loc) · 5.67 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package chart
import (
"math"
"github.com/moira-alert/go-chart/util"
)
// XAxis represents the horizontal axis.
type XAxis struct {
Name string
NameStyle Style
Style Style
ValueFormatter ValueFormatter
Range Range
TickStyle Style
Ticks []Tick
TickPosition TickPosition
EnablePrettyTicks bool
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
}
// GetValueFormatter returns the value formatter for the axis.
func (xa XAxis) GetValueFormatter() ValueFormatter {
if xa.ValueFormatter != nil {
return xa.ValueFormatter
}
return FloatValueFormatter
}
// GetTickPosition returns the tick position option for the axis.
func (xa XAxis) GetTickPosition(defaults ...TickPosition) TickPosition {
if xa.TickPosition == TickPositionUnset {
if len(defaults) > 0 {
return defaults[0]
}
return TickPositionUnderTick
}
return xa.TickPosition
}
// GetTicks returns the ticks for a series.
// The coalesce priority is:
// - User Supplied Ticks (i.e. Ticks array on the axis itself).
// - Range ticks (i.e. if the range provides ticks).
// - Generating continuous ticks based on minimum spacing and canvas width.
func (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
if len(xa.Ticks) > 0 {
return xa.Ticks
}
if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
return tp.GetTicks(r, defaults, vf)
}
tickStyle := xa.Style.InheritFrom(defaults)
if allowGeneratePrettyContiniousTicks(xa.EnablePrettyTicks, ra) {
return GeneratePrettyContinuousTicks(r, ra, false, tickStyle, vf)
}
return GenerateContinuousTicks(r, ra, false, tickStyle, vf)
}
// 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, xa.GridMajorStyle, xa.GridMinorStyle)
}
// Measure returns the bounds of the axis.
func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box {
tickStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults))
tp := xa.GetTickPosition()
var ltx, rtx int
var tx, ty int
var left, right, bottom = math.MaxInt32, 0, 0
for index, t := range ticks {
v := t.Value
tb := Draw.MeasureText(r, t.Label, tickStyle.GetTextOptions())
tx = canvasBox.Left + ra.Translate(v)
ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
switch tp {
case TickPositionUnderTick, TickPositionUnset:
ltx = tx - tb.Width()>>1
rtx = tx + tb.Width()>>1
break
case TickPositionBetweenTicks:
if index > 0 {
ltx = ra.Translate(ticks[index-1].Value)
rtx = tx
}
break
}
left = util.Math.MinInt(left, ltx)
right = util.Math.MaxInt(right, rtx)
bottom = util.Math.MaxInt(bottom, ty)
}
if xa.NameStyle.Show && len(xa.Name) > 0 {
tb := Draw.MeasureText(r, xa.Name, xa.NameStyle.InheritFrom(defaults))
bottom += DefaultXAxisMargin + tb.Height()
}
return Box{
Top: canvasBox.Bottom,
Left: left,
Right: right,
Bottom: bottom,
}
}
// Render renders the axis
func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) {
tickStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults))
tickStyle.GetStrokeOptions().WriteToRenderer(r)
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.Stroke()
tp := xa.GetTickPosition()
var tx, ty int
var maxTextHeight int
for index, t := range ticks {
v := t.Value
lx := ra.Translate(v)
tx = canvasBox.Left + lx
tickStyle.GetStrokeOptions().WriteToRenderer(r)
r.MoveTo(tx, canvasBox.Bottom)
r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke()
tickWithAxisStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults))
tb := Draw.MeasureText(r, t.Label, tickWithAxisStyle)
switch tp {
case TickPositionUnderTick, TickPositionUnset:
if tickStyle.TextRotationDegrees == 0 {
tx = tx - tb.Width()>>1
ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
} else {
ty = canvasBox.Bottom + (2 * DefaultXAxisMargin)
}
Draw.Text(r, t.Label, tx, ty, tickWithAxisStyle)
maxTextHeight = util.Math.MaxInt(maxTextHeight, tb.Height())
break
case TickPositionBetweenTicks:
if index > 0 {
llx := ra.Translate(ticks[index-1].Value)
ltx := canvasBox.Left + llx
finalTickStyle := tickWithAxisStyle.InheritFrom(Style{TextHorizontalAlign: TextHorizontalAlignCenter})
Draw.TextWithin(r, t.Label, Box{
Left: ltx,
Right: tx,
Top: canvasBox.Bottom + DefaultXAxisMargin,
Bottom: canvasBox.Bottom + DefaultXAxisMargin,
}, finalTickStyle)
ftb := Text.MeasureLines(r, Text.WrapFit(r, t.Label, tx-ltx, finalTickStyle), finalTickStyle)
maxTextHeight = util.Math.MaxInt(maxTextHeight, ftb.Height())
}
break
}
}
nameStyle := xa.NameStyle.InheritFrom(defaults)
if xa.NameStyle.Show && len(xa.Name) > 0 {
tb := Draw.MeasureText(r, xa.Name, nameStyle)
tx := canvasBox.Right - (canvasBox.Width()>>1 + tb.Width()>>1)
ty := canvasBox.Bottom + DefaultXAxisMargin + maxTextHeight + DefaultXAxisMargin + tb.Height()
Draw.Text(r, xa.Name, tx, ty, nameStyle)
}
if xa.GridMajorStyle.Show || xa.GridMinorStyle.Show {
for _, gl := range xa.GetGridLines(ticks) {
if (gl.IsMinor && xa.GridMinorStyle.Show) || (!gl.IsMinor && xa.GridMajorStyle.Show) {
defaults := xa.GridMajorStyle
if gl.IsMinor {
defaults = xa.GridMinorStyle
}
gl.Render(r, canvasBox, ra, true, gl.Style.InheritFrom(defaults))
}
}
}
}