-
Notifications
You must be signed in to change notification settings - Fork 4
/
yaxis.go
250 lines (205 loc) · 6.56 KB
/
yaxis.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package unichart
import (
"math"
"github.com/unidoc/unichart/dataset"
"github.com/unidoc/unichart/dataset/sequence"
"github.com/unidoc/unichart/mathutil"
"github.com/unidoc/unichart/render"
)
// YAxis is a veritcal rule of the range.
// There can be (2) y-axes; a primary and secondary.
type YAxis struct {
Name string
NameStyle render.Style
Style render.Style
Zero GridLine
AxisType dataset.YAxisType
Ascending bool
ValueFormatter dataset.ValueFormatter
Range sequence.Range
TickStyle render.Style
Ticks []Tick
GridLines []GridLine
GridMajorStyle render.Style
GridMinorStyle render.Style
}
// GetName returns the name.
func (ya YAxis) GetName() string {
return ya.Name
}
// GetNameStyle returns the name style.
func (ya YAxis) GetNameStyle() render.Style {
return ya.NameStyle
}
// GetStyle returns the style.
func (ya YAxis) GetStyle() render.Style {
return ya.Style
}
// GetValueFormatter returns the value formatter for the axis.
func (ya YAxis) GetValueFormatter() dataset.ValueFormatter {
if ya.ValueFormatter != nil {
return ya.ValueFormatter
}
return dataset.FloatValueFormatter
}
// GetTickStyle returns the tick style.
func (ya YAxis) GetTickStyle() render.Style {
return ya.TickStyle
}
// 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 (ya YAxis) GetTicks(r render.Renderer, ra sequence.Range, defaults render.Style, vf dataset.ValueFormatter) []Tick {
if len(ya.Ticks) > 0 {
return ya.Ticks
}
if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
return tp.GetTicks(r, defaults, vf)
}
tickStyle := ya.Style.InheritFrom(defaults)
return generateContinuousTicks(r, ra, true, tickStyle, vf)
}
// GetGridLines returns the gridlines for the axis.
func (ya YAxis) GetGridLines(ticks []Tick) []GridLine {
if len(ya.GridLines) > 0 {
return ya.GridLines
}
return GenerateGridLines(ticks, ya.GridMajorStyle, ya.GridMinorStyle)
}
// Measure returns the bounds of the axis.
func (ya YAxis) Measure(r render.Renderer, canvasBox render.Box, ra sequence.Range, defaults render.Style, ticks []Tick) render.Box {
var tx int
if ya.AxisType == dataset.YAxisPrimary {
tx = canvasBox.Right + defaultYAxisMargin
} else if ya.AxisType == dataset.YAxisSecondary {
tx = canvasBox.Left - defaultYAxisMargin
}
ya.TickStyle.InheritFrom(ya.Style.InheritFrom(defaults)).WriteToRenderer(r)
var minx, maxx, miny, maxy = math.MaxInt32, 0, math.MaxInt32, 0
var maxTextHeight int
for _, t := range ticks {
v := t.Value
ly := canvasBox.Bottom - ra.Translate(v)
tb := r.MeasureText(t.Label)
tbh2 := tb.Height() >> 1
finalTextX := tx
if ya.AxisType == dataset.YAxisSecondary {
finalTextX = tx - tb.Width()
}
maxTextHeight = mathutil.MaxInt(tb.Height(), maxTextHeight)
if ya.AxisType == dataset.YAxisPrimary {
minx = canvasBox.Right
maxx = mathutil.MaxInt(maxx, tx+tb.Width())
} else if ya.AxisType == dataset.YAxisSecondary {
minx = mathutil.MinInt(minx, finalTextX)
maxx = mathutil.MaxInt(maxx, tx)
}
miny = mathutil.MinInt(miny, ly-tbh2)
maxy = mathutil.MaxInt(maxy, ly+tbh2)
}
if !ya.NameStyle.Hidden && len(ya.Name) > 0 {
maxx += (defaultYAxisMargin + maxTextHeight)
}
return render.Box{
Top: miny,
Left: minx,
Right: maxx,
Bottom: maxy,
}
}
// Render renders the axis.
func (ya YAxis) Render(r render.Renderer, canvasBox render.Box, ra sequence.Range, defaults render.Style, ticks []Tick) {
tickStyle := ya.TickStyle.InheritFrom(ya.Style.InheritFrom(defaults))
tickStyle.WriteToRenderer(r)
sw := tickStyle.GetStrokeWidth(defaults.StrokeWidth)
var lx int
var tx int
if ya.AxisType == dataset.YAxisPrimary {
lx = canvasBox.Right
tx = lx + defaultYAxisMargin
} else if ya.AxisType == dataset.YAxisSecondary {
lx = canvasBox.Left
tx = lx - defaultYAxisMargin
}
var maxTextWidth int
var finalTextX, finalTextY int
for _, t := range ticks {
v := t.Value
ly := canvasBox.Bottom - ra.Translate(v)
tb := render.Text.Measure(r, t.Label, tickStyle)
if tb.Width() > maxTextWidth {
maxTextWidth = tb.Width()
}
if ya.AxisType == dataset.YAxisSecondary {
finalTextX = tx - tb.Width()
} else {
finalTextX = tx
}
if tickStyle.TextRotationDegrees == 0 {
finalTextY = ly + tb.Height()>>1
} else {
finalTextY = ly
}
tickStyle.WriteToRenderer(r)
if lx < 0 || ly < 0 {
continue
}
r.MoveTo(lx, ly)
if ya.AxisType == dataset.YAxisPrimary {
r.LineTo(lx+defaultHorizontalTickWidth, ly)
} else if ya.AxisType == dataset.YAxisSecondary {
r.LineTo(lx-defaultHorizontalTickWidth, ly)
}
r.Stroke()
render.Text.Draw(r, t.Label, finalTextX, finalTextY, tickStyle)
}
nameStyle := ya.NameStyle.InheritFrom(defaults.InheritFrom(render.Style{TextRotationDegrees: 90}))
if !ya.NameStyle.Hidden && len(ya.Name) > 0 {
nameStyle.GetTextOptions().WriteToRenderer(r)
tb := render.Text.Measure(r, ya.Name, nameStyle)
var tx int
if ya.AxisType == dataset.YAxisPrimary {
tx = canvasBox.Right + int(sw) + defaultYAxisMargin + maxTextWidth + defaultYAxisMargin
} else if ya.AxisType == dataset.YAxisSecondary {
tx = canvasBox.Left - (defaultYAxisMargin + int(sw) + maxTextWidth + defaultYAxisMargin)
}
var ty int
if nameStyle.TextRotationDegrees == 0 {
ty = canvasBox.Top + (canvasBox.Height()>>1 - tb.Width()>>1)
} else {
ty = canvasBox.Top + (canvasBox.Height()>>1 - tb.Height()>>1)
}
render.Text.Draw(r, ya.Name, tx, ty, nameStyle)
}
if !ya.Zero.Style.Hidden {
ya.Zero.Render(r, canvasBox, ra, false, render.Style{})
}
if !ya.GridMajorStyle.Hidden || !ya.GridMinorStyle.Hidden {
for _, gl := range ya.GetGridLines(ticks) {
if (gl.IsMinor && !ya.GridMinorStyle.Hidden) || (!gl.IsMinor && !ya.GridMajorStyle.Hidden) {
defaults := ya.GridMajorStyle
if gl.IsMinor {
defaults = ya.GridMinorStyle
}
gl.Render(r, canvasBox, ra, false, gl.Style.InheritFrom(defaults))
}
}
}
}
func (ya YAxis) RenderAxisLine(r render.Renderer, canvasBox render.Box, ra sequence.Range, defaults render.Style, ticks []Tick) {
tickStyle := ya.TickStyle.InheritFrom(ya.Style.InheritFrom(defaults))
tickStyle.WriteToRenderer(r)
sw := tickStyle.GetStrokeWidth(defaults.StrokeWidth)
var lx int
if ya.AxisType == dataset.YAxisPrimary {
lx = canvasBox.Right
} else if ya.AxisType == dataset.YAxisSecondary {
lx = canvasBox.Left
}
r.SetStrokeWidth(sw)
r.MoveTo(lx, canvasBox.Bottom)
r.LineTo(lx, canvasBox.Top)
r.Stroke()
}