Debian Codegraph @

draw.go

raw symbols SCIP
1 package chart
2
3 import (
4 "math"
5 )
6
7 var (
8 // Draw contains helpers for drawing common objects.
9 Draw = &draw{}
10 )
11
12 type draw struct{}
13
14 // LineSeries draws a line series with a renderer.
15 func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, vs ValuesProvider) {
16 if vs.Len() == 0 {
17 return
18 }
19
20 cb := canvasBox.Bottom
21 cl := canvasBox.Left
22
23 v0x, v0y := vs.GetValues(0)
24 x0 := cl + xrange.Translate(v0x)
25 y0 := cb - yrange.Translate(v0y)
26
27 yv0 := yrange.Translate(0)
28
29 var vx, vy float64
30 var x, y int
31
32 if style.ShouldDrawStroke() && style.ShouldDrawFill() {
33 style.GetFillOptions().WriteDrawingOptionsToRenderer(r)
34 r.MoveTo(x0, y0)
35 for i := 1; i < vs.Len(); i++ {
36 vx, vy = vs.GetValues(i)
37 x = cl + xrange.Translate(vx)
38 y = cb - yrange.Translate(vy)
39 r.LineTo(x, y)
40 }
41 r.LineTo(x, MinInt(cb, cb-yv0))
42 r.LineTo(x0, MinInt(cb, cb-yv0))
43 r.LineTo(x0, y0)
44 r.Fill()
45 }
46
47 if style.ShouldDrawStroke() {
48 style.GetStrokeOptions().WriteDrawingOptionsToRenderer(r)
49
50 r.MoveTo(x0, y0)
51 for i := 1; i < vs.Len(); i++ {
52 vx, vy = vs.GetValues(i)
53 x = cl + xrange.Translate(vx)
54 y = cb - yrange.Translate(vy)
55 r.LineTo(x, y)
56 }
57 r.Stroke()
58 }
59
60 if style.ShouldDrawDot() {
61 defaultDotWidth := style.GetDotWidth()
62
63 style.GetDotOptions().WriteDrawingOptionsToRenderer(r)
64 for i := 0; i < vs.Len(); i++ {
65 vx, vy = vs.GetValues(i)
66 x = cl + xrange.Translate(vx)
67 y = cb - yrange.Translate(vy)
68
69 dotWidth := defaultDotWidth
70 if style.DotWidthProvider != nil {
71 dotWidth = style.DotWidthProvider(xrange, yrange, i, vx, vy)
72 }
73
74 if style.DotColorProvider != nil {
75 dotColor := style.DotColorProvider(xrange, yrange, i, vx, vy)
76
77 r.SetFillColor(dotColor)
78 r.SetStrokeColor(dotColor)
79 }
80
81 r.Circle(dotWidth, x, y)
82 r.FillStroke()
83 }
84 }
85 }
86
87 // BoundedSeries draws a series that implements BoundedValuesProvider.
88 func (d draw) BoundedSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, bbs BoundedValuesProvider, drawOffsetIndexes ...int) {
89 drawOffsetIndex := 0
90 if len(drawOffsetIndexes) > 0 {
91 drawOffsetIndex = drawOffsetIndexes[0]
92 }
93
94 cb := canvasBox.Bottom
95 cl := canvasBox.Left
96
97 v0x, v0y1, v0y2 := bbs.GetBoundedValues(0)
98 x0 := cl + xrange.Translate(v0x)
99 y0 := cb - yrange.Translate(v0y1)
100
101 var vx, vy1, vy2 float64
102 var x, y int
103
104 xvalues := make([]float64, bbs.Len())
105 xvalues[0] = v0x
106 y2values := make([]float64, bbs.Len())
107 y2values[0] = v0y2
108
109 style.GetFillAndStrokeOptions().WriteToRenderer(r)
110 r.MoveTo(x0, y0)
111 for i := 1; i < bbs.Len(); i++ {
112 vx, vy1, vy2 = bbs.GetBoundedValues(i)
113
114 xvalues[i] = vx
115 y2values[i] = vy2
116
117 x = cl + xrange.Translate(vx)
118 y = cb - yrange.Translate(vy1)
119 if i > drawOffsetIndex {
120 r.LineTo(x, y)
121 } else {
122 r.MoveTo(x, y)
123 }
124 }
125 y = cb - yrange.Translate(vy2)
126 r.LineTo(x, y)
127 for i := bbs.Len() - 1; i >= drawOffsetIndex; i-- {
128 vx, vy2 = xvalues[i], y2values[i]
129 x = cl + xrange.Translate(vx)
130 y = cb - yrange.Translate(vy2)
131 r.LineTo(x, y)
132 }
133 r.Close()
134 r.FillStroke()
135 }
136
137 // HistogramSeries draws a value provider as boxes from 0.
138 func (d draw) HistogramSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, vs ValuesProvider, barWidths ...int) {
139 if vs.Len() == 0 {
140 return
141 }
142
143 //calculate bar width?
144 seriesLength := vs.Len()
145 barWidth := int(math.Floor(float64(xrange.GetDomain()) / float64(seriesLength)))
146 if len(barWidths) > 0 {
147 barWidth = barWidths[0]
148 }
149
150 cb := canvasBox.Bottom
151 cl := canvasBox.Left
152
153 //foreach datapoint, draw a box.
154 for index := 0; index < seriesLength; index++ {
155 vx, vy := vs.GetValues(index)
156 y0 := yrange.Translate(0)
157 x := cl + xrange.Translate(vx)
158 y := yrange.Translate(vy)
159
160 d.Box(r, Box{
161 Top: cb - y0,
162 Left: x - (barWidth >> 1),
163 Right: x + (barWidth >> 1),
164 Bottom: cb - y,
165 }, style)
166 }
167 }
168
169 // MeasureAnnotation measures how big an annotation would be.
170 func (d draw) MeasureAnnotation(r Renderer, canvasBox Box, style Style, lx, ly int, label string) Box {
171 style.WriteToRenderer(r)
172 defer r.ResetStyle()
173
174 textBox := r.MeasureText(label)
175 textWidth := textBox.Width()
176 textHeight := textBox.Height()
177 halfTextHeight := textHeight >> 1
178
179 pt := style.Padding.GetTop(DefaultAnnotationPadding.Top)
180 pl := style.Padding.GetLeft(DefaultAnnotationPadding.Left)
181 pr := style.Padding.GetRight(DefaultAnnotationPadding.Right)
182 pb := style.Padding.GetBottom(DefaultAnnotationPadding.Bottom)
183
184 strokeWidth := style.GetStrokeWidth()
185
186 top := ly - (pt + halfTextHeight)
187 right := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth + int(strokeWidth)
188 bottom := ly + (pb + halfTextHeight)
189
190 return Box{
191 Top: top,
192 Left: lx,
193 Right: right,
194 Bottom: bottom,
195 }
196 }
197
198 // Annotation draws an anotation with a renderer.
199 func (d draw) Annotation(r Renderer, canvasBox Box, style Style, lx, ly int, label string) {
200 style.GetTextOptions().WriteToRenderer(r)
201 defer r.ResetStyle()
202
203 textBox := r.MeasureText(label)
204 textWidth := textBox.Width()
205 halfTextHeight := textBox.Height() >> 1
206
207 style.GetFillAndStrokeOptions().WriteToRenderer(r)
208
209 pt := style.Padding.GetTop(DefaultAnnotationPadding.Top)
210 pl := style.Padding.GetLeft(DefaultAnnotationPadding.Left)
211 pr := style.Padding.GetRight(DefaultAnnotationPadding.Right)
212 pb := style.Padding.GetBottom(DefaultAnnotationPadding.Bottom)
213
214 textX := lx + pl + DefaultAnnotationDeltaWidth
215 textY := ly + halfTextHeight
216
217 ltx := lx + DefaultAnnotationDeltaWidth
218 lty := ly - (pt + halfTextHeight)
219
220 rtx := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth
221 rty := ly - (pt + halfTextHeight)
222
223 rbx := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth
224 rby := ly + (pb + halfTextHeight)
225
226 lbx := lx + DefaultAnnotationDeltaWidth
227 lby := ly + (pb + halfTextHeight)
228
229 r.MoveTo(lx, ly)
230 r.LineTo(ltx, lty)
231 r.LineTo(rtx, rty)
232 r.LineTo(rbx, rby)
233 r.LineTo(lbx, lby)
234 r.LineTo(lx, ly)
235 r.Close()
236 r.FillStroke()
237
238 style.GetTextOptions().WriteToRenderer(r)
239 r.Text(label, textX, textY)
240 }
241
242 // Box draws a box with a given style.
243 func (d draw) Box(r Renderer, b Box, s Style) {
244 s.GetFillAndStrokeOptions().WriteToRenderer(r)
245 defer r.ResetStyle()
246
247 r.MoveTo(b.Left, b.Top)
248 r.LineTo(b.Right, b.Top)
249 r.LineTo(b.Right, b.Bottom)
250 r.LineTo(b.Left, b.Bottom)
251 r.LineTo(b.Left, b.Top)
252 r.FillStroke()
253 }
254
255 func (d draw) BoxRotated(r Renderer, b Box, thetaDegrees float64, s Style) {
256 d.BoxCorners(r, b.Corners().Rotate(thetaDegrees), s)
257 }
258
259 func (d draw) BoxCorners(r Renderer, bc BoxCorners, s Style) {
260 s.GetFillAndStrokeOptions().WriteToRenderer(r)
261 defer r.ResetStyle()
262
263 r.MoveTo(bc.TopLeft.X, bc.TopLeft.Y)
264 r.LineTo(bc.TopRight.X, bc.TopRight.Y)
265 r.LineTo(bc.BottomRight.X, bc.BottomRight.Y)
266 r.LineTo(bc.BottomLeft.X, bc.BottomLeft.Y)
267 r.Close()
268 r.FillStroke()
269 }
270
271 // DrawText draws text with a given style.
272 func (d draw) Text(r Renderer, text string, x, y int, style Style) {
273 style.GetTextOptions().WriteToRenderer(r)
274 defer r.ResetStyle()
275
276 r.Text(text, x, y)
277 }
278
279 func (d draw) MeasureText(r Renderer, text string, style Style) Box {
280 style.GetTextOptions().WriteToRenderer(r)
281 defer r.ResetStyle()
282
283 return r.MeasureText(text)
284 }
285
286 // TextWithin draws the text within a given box.
287 func (d draw) TextWithin(r Renderer, text string, box Box, style Style) {
288 style.GetTextOptions().WriteToRenderer(r)
289 defer r.ResetStyle()
290
291 lines := Text.WrapFit(r, text, box.Width(), style)
292 linesBox := Text.MeasureLines(r, lines, style)
293
294 y := box.Top
295
296 switch style.GetTextVerticalAlign() {
297 case TextVerticalAlignBottom, TextVerticalAlignBaseline: // i have to build better baseline handling into measure text
298 y = y - linesBox.Height()
299 case TextVerticalAlignMiddle:
300 y = y + (box.Height() >> 1) - (linesBox.Height() >> 1)
301 case TextVerticalAlignMiddleBaseline:
302 y = y + (box.Height() >> 1) - linesBox.Height()
303 }
304
305 var tx, ty int
306 for _, line := range lines {
307 lineBox := r.MeasureText(line)
308 switch style.GetTextHorizontalAlign() {
309 case TextHorizontalAlignCenter:
310 tx = box.Left + ((box.Width() - lineBox.Width()) >> 1)
311 case TextHorizontalAlignRight:
312 tx = box.Right - lineBox.Width()
313 default:
314 tx = box.Left
315 }
316 if style.TextRotationDegrees == 0 {
317 ty = y + lineBox.Height()
318 } else {
319 ty = y
320 }
321
322 r.Text(line, tx, ty)
323 y += lineBox.Height() + style.GetTextLineSpacing()
324 }
325 }