Debian Codegraph @

yaxis.go

raw symbols SCIP
1 package chart
2
3 import (
4 "math"
5 )
6
7 // HideYAxis hides a y-axis.
8 func HideYAxis() YAxis {
9 return YAxis{
10 Style: Hidden(),
11 }
12 }
13
14 // YAxis is a veritcal rule of the range.
15 // There can be (2) y-axes; a primary and secondary.
16 type YAxis struct {
17 Name string
18 NameStyle Style
19
20 Style Style
21
22 Zero GridLine
23
24 AxisType YAxisType
25 Ascending bool
26
27 ValueFormatter ValueFormatter
28 Range Range
29
30 TickStyle Style
31 Ticks []Tick
32
33 GridLines []GridLine
34 GridMajorStyle Style
35 GridMinorStyle Style
36 }
37
38 // GetName returns the name.
39 func (ya YAxis) GetName() string {
40 return ya.Name
41 }
42
43 // GetNameStyle returns the name style.
44 func (ya YAxis) GetNameStyle() Style {
45 return ya.NameStyle
46 }
47
48 // GetStyle returns the style.
49 func (ya YAxis) GetStyle() Style {
50 return ya.Style
51 }
52
53 // GetValueFormatter returns the value formatter for the axis.
54 func (ya YAxis) GetValueFormatter() ValueFormatter {
55 if ya.ValueFormatter != nil {
56 return ya.ValueFormatter
57 }
58 return FloatValueFormatter
59 }
60
61 // GetTickStyle returns the tick style.
62 func (ya YAxis) GetTickStyle() Style {
63 return ya.TickStyle
64 }
65
66 // GetTicks returns the ticks for a series.
67 // The coalesce priority is:
68 // - User Supplied Ticks (i.e. Ticks array on the axis itself).
69 // - Range ticks (i.e. if the range provides ticks).
70 // - Generating continuous ticks based on minimum spacing and canvas width.
71 func (ya YAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
72 if len(ya.Ticks) > 0 {
73 return ya.Ticks
74 }
75 if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
76 return tp.GetTicks(r, defaults, vf)
77 }
78 tickStyle := ya.Style.InheritFrom(defaults)
79 return GenerateContinuousTicks(r, ra, true, tickStyle, vf)
80 }
81
82 // GetGridLines returns the gridlines for the axis.
83 func (ya YAxis) GetGridLines(ticks []Tick) []GridLine {
84 if len(ya.GridLines) > 0 {
85 return ya.GridLines
86 }
87 return GenerateGridLines(ticks, ya.GridMajorStyle, ya.GridMinorStyle)
88 }
89
90 // Measure returns the bounds of the axis.
91 func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box {
92 var tx int
93 if ya.AxisType == YAxisPrimary {
94 tx = canvasBox.Right + DefaultYAxisMargin
95 } else if ya.AxisType == YAxisSecondary {
96 tx = canvasBox.Left - DefaultYAxisMargin
97 }
98
99 ya.TickStyle.InheritFrom(ya.Style.InheritFrom(defaults)).WriteToRenderer(r)
100 var minx, maxx, miny, maxy = math.MaxInt32, 0, math.MaxInt32, 0
101 var maxTextHeight int
102 for _, t := range ticks {
103 v := t.Value
104 ly := canvasBox.Bottom - ra.Translate(v)
105
106 tb := r.MeasureText(t.Label)
107 tbh2 := tb.Height() >> 1
108 finalTextX := tx
109 if ya.AxisType == YAxisSecondary {
110 finalTextX = tx - tb.Width()
111 }
112
113 maxTextHeight = MaxInt(tb.Height(), maxTextHeight)
114
115 if ya.AxisType == YAxisPrimary {
116 minx = canvasBox.Right
117 maxx = MaxInt(maxx, tx+tb.Width())
118 } else if ya.AxisType == YAxisSecondary {
119 minx = MinInt(minx, finalTextX)
120 maxx = MaxInt(maxx, tx)
121 }
122
123 miny = MinInt(miny, ly-tbh2)
124 maxy = MaxInt(maxy, ly+tbh2)
125 }
126
127 if !ya.NameStyle.Hidden && len(ya.Name) > 0 {
128 maxx += (DefaultYAxisMargin + maxTextHeight)
129 }
130
131 return Box{
132 Top: miny,
133 Left: minx,
134 Right: maxx,
135 Bottom: maxy,
136 }
137 }
138
139 // Render renders the axis.
140 func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) {
141 tickStyle := ya.TickStyle.InheritFrom(ya.Style.InheritFrom(defaults))
142 tickStyle.WriteToRenderer(r)
143
144 sw := tickStyle.GetStrokeWidth(defaults.StrokeWidth)
145
146 var lx int
147 var tx int
148 if ya.AxisType == YAxisPrimary {
149 lx = canvasBox.Right + int(sw)
150 tx = lx + DefaultYAxisMargin
151 } else if ya.AxisType == YAxisSecondary {
152 lx = canvasBox.Left - int(sw)
153 tx = lx - DefaultYAxisMargin
154 }
155
156 r.MoveTo(lx, canvasBox.Bottom)
157 r.LineTo(lx, canvasBox.Top)
158 r.Stroke()
159
160 var maxTextWidth int
161 var finalTextX, finalTextY int
162 for _, t := range ticks {
163 v := t.Value
164 ly := canvasBox.Bottom - ra.Translate(v)
165
166 tb := Draw.MeasureText(r, t.Label, tickStyle)
167
168 if tb.Width() > maxTextWidth {
169 maxTextWidth = tb.Width()
170 }
171
172 if ya.AxisType == YAxisSecondary {
173 finalTextX = tx - tb.Width()
174 } else {
175 finalTextX = tx
176 }
177
178 if tickStyle.TextRotationDegrees == 0 {
179 finalTextY = ly + tb.Height()>>1
180 } else {
181 finalTextY = ly
182 }
183
184 tickStyle.WriteToRenderer(r)
185
186 r.MoveTo(lx, ly)
187 if ya.AxisType == YAxisPrimary {
188 r.LineTo(lx+DefaultHorizontalTickWidth, ly)
189 } else if ya.AxisType == YAxisSecondary {
190 r.LineTo(lx-DefaultHorizontalTickWidth, ly)
191 }
192 r.Stroke()
193
194 Draw.Text(r, t.Label, finalTextX, finalTextY, tickStyle)
195 }
196
197 nameStyle := ya.NameStyle.InheritFrom(defaults.InheritFrom(Style{TextRotationDegrees: 90}))
198 if !ya.NameStyle.Hidden && len(ya.Name) > 0 {
199 nameStyle.GetTextOptions().WriteToRenderer(r)
200 tb := Draw.MeasureText(r, ya.Name, nameStyle)
201
202 var tx int
203 if ya.AxisType == YAxisPrimary {
204 tx = canvasBox.Right + int(sw) + DefaultYAxisMargin + maxTextWidth + DefaultYAxisMargin
205 } else if ya.AxisType == YAxisSecondary {
206 tx = canvasBox.Left - (DefaultYAxisMargin + int(sw) + maxTextWidth + DefaultYAxisMargin)
207 }
208
209 var ty int
210 if nameStyle.TextRotationDegrees == 0 {
211 ty = canvasBox.Top + (canvasBox.Height()>>1 - tb.Width()>>1)
212 } else {
213 ty = canvasBox.Top + (canvasBox.Height()>>1 - tb.Height()>>1)
214 }
215
216 Draw.Text(r, ya.Name, tx, ty, nameStyle)
217 }
218
219 if !ya.Zero.Style.Hidden {
220 ya.Zero.Render(r, canvasBox, ra, false, Style{})
221 }
222
223 if !ya.GridMajorStyle.Hidden || !ya.GridMinorStyle.Hidden {
224 for _, gl := range ya.GetGridLines(ticks) {
225 if (gl.IsMinor && !ya.GridMinorStyle.Hidden) || (!gl.IsMinor && !ya.GridMajorStyle.Hidden) {
226 defaults := ya.GridMajorStyle
227 if gl.IsMinor {
228 defaults = ya.GridMinorStyle
229 }
230 gl.Render(r, canvasBox, ra, false, gl.Style.InheritFrom(defaults))
231 }
232 }
233 }
234 }