Debian Codegraph @

xaxis.go

raw symbols SCIP
1 package chart
2
3 import (
4 "math"
5 )
6
7 // HideXAxis hides the x-axis.
8 func HideXAxis() XAxis {
9 return XAxis{
10 Style: Hidden(),
11 }
12 }
13
14 // XAxis represents the horizontal axis.
15 type XAxis struct {
16 Name string
17 NameStyle Style
18
19 Style Style
20 ValueFormatter ValueFormatter
21 Range Range
22
23 TickStyle Style
24 Ticks []Tick
25 TickPosition TickPosition
26
27 GridLines []GridLine
28 GridMajorStyle Style
29 GridMinorStyle Style
30 }
31
32 // GetName returns the name.
33 func (xa XAxis) GetName() string {
34 return xa.Name
35 }
36
37 // GetStyle returns the style.
38 func (xa XAxis) GetStyle() Style {
39 return xa.Style
40 }
41
42 // GetValueFormatter returns the value formatter for the axis.
43 func (xa XAxis) GetValueFormatter() ValueFormatter {
44 if xa.ValueFormatter != nil {
45 return xa.ValueFormatter
46 }
47 return FloatValueFormatter
48 }
49
50 // GetTickPosition returns the tick position option for the axis.
51 func (xa XAxis) GetTickPosition(defaults ...TickPosition) TickPosition {
52 if xa.TickPosition == TickPositionUnset {
53 if len(defaults) > 0 {
54 return defaults[0]
55 }
56 return TickPositionUnderTick
57 }
58 return xa.TickPosition
59 }
60
61 // GetTicks returns the ticks for a series.
62 // The coalesce priority is:
63 // - User Supplied Ticks (i.e. Ticks array on the axis itself).
64 // - Range ticks (i.e. if the range provides ticks).
65 // - Generating continuous ticks based on minimum spacing and canvas width.
66 func (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter) []Tick {
67 if len(xa.Ticks) > 0 {
68 return xa.Ticks
69 }
70 if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
71 return tp.GetTicks(r, defaults, vf)
72 }
73 tickStyle := xa.Style.InheritFrom(defaults)
74 return GenerateContinuousTicks(r, ra, false, tickStyle, vf)
75 }
76
77 // GetGridLines returns the gridlines for the axis.
78 func (xa XAxis) GetGridLines(ticks []Tick) []GridLine {
79 if len(xa.GridLines) > 0 {
80 return xa.GridLines
81 }
82 return GenerateGridLines(ticks, xa.GridMajorStyle, xa.GridMinorStyle)
83 }
84
85 // Measure returns the bounds of the axis.
86 func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) Box {
87 tickStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults))
88
89 tp := xa.GetTickPosition()
90
91 var ltx, rtx int
92 var tx, ty int
93 var left, right, bottom = math.MaxInt32, 0, 0
94 for index, t := range ticks {
95 v := t.Value
96 tb := Draw.MeasureText(r, t.Label, tickStyle.GetTextOptions())
97
98 tx = canvasBox.Left + ra.Translate(v)
99 ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
100 switch tp {
101 case TickPositionUnderTick, TickPositionUnset:
102 ltx = tx - tb.Width()>>1
103 rtx = tx + tb.Width()>>1
104 break
105 case TickPositionBetweenTicks:
106 if index > 0 {
107 ltx = ra.Translate(ticks[index-1].Value)
108 rtx = tx
109 }
110 break
111 }
112
113 left = MinInt(left, ltx)
114 right = MaxInt(right, rtx)
115 bottom = MaxInt(bottom, ty)
116 }
117
118 if !xa.NameStyle.Hidden && len(xa.Name) > 0 {
119 tb := Draw.MeasureText(r, xa.Name, xa.NameStyle.InheritFrom(defaults))
120 bottom += DefaultXAxisMargin + tb.Height()
121 }
122
123 return Box{
124 Top: canvasBox.Bottom,
125 Left: left,
126 Right: right,
127 Bottom: bottom,
128 }
129 }
130
131 // Render renders the axis
132 func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, ticks []Tick) {
133 tickStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults))
134
135 tickStyle.GetStrokeOptions().WriteToRenderer(r)
136 r.MoveTo(canvasBox.Left, canvasBox.Bottom)
137 r.LineTo(canvasBox.Right, canvasBox.Bottom)
138 r.Stroke()
139
140 tp := xa.GetTickPosition()
141
142 var tx, ty int
143 var maxTextHeight int
144 for index, t := range ticks {
145 v := t.Value
146 lx := ra.Translate(v)
147
148 tx = canvasBox.Left + lx
149
150 tickStyle.GetStrokeOptions().WriteToRenderer(r)
151 r.MoveTo(tx, canvasBox.Bottom)
152 r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
153 r.Stroke()
154
155 tickWithAxisStyle := xa.TickStyle.InheritFrom(xa.Style.InheritFrom(defaults))
156 tb := Draw.MeasureText(r, t.Label, tickWithAxisStyle)
157
158 switch tp {
159 case TickPositionUnderTick, TickPositionUnset:
160 if tickStyle.TextRotationDegrees == 0 {
161 tx = tx - tb.Width()>>1
162 ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
163 } else {
164 ty = canvasBox.Bottom + (2 * DefaultXAxisMargin)
165 }
166 Draw.Text(r, t.Label, tx, ty, tickWithAxisStyle)
167 maxTextHeight = MaxInt(maxTextHeight, tb.Height())
168 break
169 case TickPositionBetweenTicks:
170 if index > 0 {
171 llx := ra.Translate(ticks[index-1].Value)
172 ltx := canvasBox.Left + llx
173 finalTickStyle := tickWithAxisStyle.InheritFrom(Style{TextHorizontalAlign: TextHorizontalAlignCenter})
174
175 Draw.TextWithin(r, t.Label, Box{
176 Left: ltx,
177 Right: tx,
178 Top: canvasBox.Bottom + DefaultXAxisMargin,
179 Bottom: canvasBox.Bottom + DefaultXAxisMargin,
180 }, finalTickStyle)
181
182 ftb := Text.MeasureLines(r, Text.WrapFit(r, t.Label, tx-ltx, finalTickStyle), finalTickStyle)
183 maxTextHeight = MaxInt(maxTextHeight, ftb.Height())
184 }
185 break
186 }
187 }
188
189 nameStyle := xa.NameStyle.InheritFrom(defaults)
190 if !xa.NameStyle.Hidden && len(xa.Name) > 0 {
191 tb := Draw.MeasureText(r, xa.Name, nameStyle)
192 tx := canvasBox.Right - (canvasBox.Width()>>1 + tb.Width()>>1)
193 ty := canvasBox.Bottom + DefaultXAxisMargin + maxTextHeight + DefaultXAxisMargin + tb.Height()
194 Draw.Text(r, xa.Name, tx, ty, nameStyle)
195 }
196
197 if !xa.GridMajorStyle.Hidden || !xa.GridMinorStyle.Hidden {
198 for _, gl := range xa.GetGridLines(ticks) {
199 if (gl.IsMinor && !xa.GridMinorStyle.Hidden) || (!gl.IsMinor && !xa.GridMajorStyle.Hidden) {
200 defaults := xa.GridMajorStyle
201 if gl.IsMinor {
202 defaults = xa.GridMinorStyle
203 }
204 gl.Render(r, canvasBox, ra, true, gl.Style.InheritFrom(defaults))
205 }
206 }
207 }
208 }