-
Notifications
You must be signed in to change notification settings - Fork 71
/
draw.go
451 lines (402 loc) · 10.7 KB
/
draw.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright (c) 2024, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Adapted from gonum/plot:
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package plot
import (
"bufio"
"bytes"
"image"
"io"
"os"
"cogentcore.org/core/colors"
"cogentcore.org/core/math32"
"cogentcore.org/core/styles"
)
// SVGString returns an SVG representation of the plot as a string
func (pt *Plot) SVGString() string {
b := &bytes.Buffer{}
pt.Paint.SVGOut = b
pt.svgDraw()
pt.Paint.SVGOut = nil
return b.String()
}
// svgDraw draws SVGOut writer that must already be set in Paint
func (pt *Plot) svgDraw() {
pt.drawConfig()
io.WriteString(pt.Paint.SVGOut, pt.Paint.SVGStart())
pt.Draw()
io.WriteString(pt.Paint.SVGOut, pt.Paint.SVGEnd())
}
// SVGToFile saves the SVG to given file
func (pt *Plot) SVGToFile(filename string) error {
fp, err := os.Create(filename)
if err != nil {
return err
}
defer fp.Close()
bw := bufio.NewWriter(fp)
pt.Paint.SVGOut = bw
pt.svgDraw()
pt.Paint.SVGOut = nil
return bw.Flush()
}
// drawConfig configures everything for drawing
func (pt *Plot) drawConfig() {
pt.Resize(pt.Size) // ensure
pt.Legend.TextStyle.openFont(pt)
pt.Paint.ToDots()
}
// Draw draws the plot to image.
// Plotters are drawn in the order in which they were
// added to the plot.
func (pt *Plot) Draw() {
pt.drawConfig()
pc := pt.Paint
ptw := float32(pt.Size.X)
pth := float32(pt.Size.X)
ptb := image.Rectangle{Max: pt.Size}
pc.PushBounds(ptb)
if pt.Background != nil {
pc.BlitBox(math32.Vector2{}, math32.Vector2FromPoint(pt.Size), colors.C(pt.Background))
}
if pt.Title.Text != "" {
pt.Title.Config(pt)
pos := pt.Title.PosX(ptw)
pad := pt.Title.Style.Padding.Dots
pos.Y = pad
pt.Title.Draw(pt, pos)
th := pt.Title.PaintText.BBox.Size().Y + 2*pad
pth -= th
ptb.Min.Y += int(math32.Ceil(th))
}
pt.X.SanitizeRange()
pt.Y.SanitizeRange()
ywidth, tickWidth, tpad, bpad := pt.Y.sizeY(pt)
xheight, lpad, rpad := pt.X.sizeX(pt, float32(pt.Size.X-int(ywidth)))
tb := ptb
tb.Min.X += ywidth
pc.PushBounds(tb)
pt.X.drawX(pt, lpad, rpad)
pc.PopBounds()
tb = ptb
tb.Max.Y -= xheight
pc.PushBounds(tb)
pt.Y.drawY(pt, tickWidth, tpad, bpad)
pc.PopBounds()
tb = ptb
tb.Min.X += ywidth + lpad
tb.Max.X -= rpad
tb.Max.Y -= xheight + bpad
tb.Min.Y += tpad
pt.PlotBox.SetFromRect(tb)
// don't cut off lines
tb.Min.X -= 2
tb.Min.Y -= 2
tb.Max.X += 2
tb.Max.Y += 2
pc.PushBounds(tb)
for _, plt := range pt.Plotters {
plt.Plot(pt)
}
pt.Legend.draw(pt)
pc.PopBounds()
pc.PopBounds() // global
}
////////////////////////////////////////////////////////////////
// Axis
// drawTicks returns true if the tick marks should be drawn.
func (ax *Axis) drawTicks() bool {
return ax.TickLine.Width.Value > 0 && ax.TickLength.Value > 0
}
// sizeX returns the total height of the axis, left and right padding
func (ax *Axis) sizeX(pt *Plot, axw float32) (ht, lpad, rpad int) {
pc := pt.Paint
uc := &pc.UnitContext
ax.TickLength.ToDots(uc)
ax.ticks = ax.Ticker.Ticks(ax.Min, ax.Max)
h := float32(0)
if ax.Label.Text != "" { // We assume that the label isn't rotated.
ax.Label.Config(pt)
h += ax.Label.PaintText.BBox.Size().Y
h += ax.Label.Style.Padding.Dots
}
lw := ax.Line.Width.Dots
lpad = int(math32.Ceil(lw)) + 2
rpad = int(math32.Ceil(lw)) + 2
tht := float32(0)
if len(ax.ticks) > 0 {
if ax.drawTicks() {
h += ax.TickLength.Dots
}
ftk := ax.firstTickLabel()
if ftk.Label != "" {
px, _ := ax.tickPosX(pt, ftk, axw)
if px < 0 {
lpad += int(math32.Ceil(-px))
}
tht = max(tht, ax.TickText.PaintText.BBox.Size().Y)
}
ltk := ax.lastTickLabel()
if ltk.Label != "" {
px, wd := ax.tickPosX(pt, ltk, axw)
if px+wd > axw {
rpad += int(math32.Ceil((px + wd) - axw))
}
tht = max(tht, ax.TickText.PaintText.BBox.Size().Y)
}
ax.TickText.Text = ax.longestTickLabel()
if ax.TickText.Text != "" {
ax.TickText.Config(pt)
tht = max(tht, ax.TickText.PaintText.BBox.Size().Y)
}
h += ax.TickText.Style.Padding.Dots
}
h += tht + lw + ax.Padding.Dots
ht = int(math32.Ceil(h))
return
}
// tickLabelPosX returns the relative position and width for given tick along X axis
// for given total axis width
func (ax *Axis) tickPosX(pt *Plot, t Tick, axw float32) (px, wd float32) {
x := axw * float32(ax.Norm(t.Value))
if x < 0 || x > axw {
return
}
ax.TickText.Text = t.Label
ax.TickText.Config(pt)
pos := ax.TickText.PosX(0)
px = pos.X + x
wd = ax.TickText.PaintText.BBox.Size().X
return
}
func (ax *Axis) firstTickLabel() Tick {
for _, tk := range ax.ticks {
if tk.Label != "" {
return tk
}
}
return Tick{}
}
func (ax *Axis) lastTickLabel() Tick {
n := len(ax.ticks)
for i := n - 1; i >= 0; i-- {
tk := ax.ticks[i]
if tk.Label != "" {
return tk
}
}
return Tick{}
}
func (ax *Axis) longestTickLabel() string {
lst := ""
for _, tk := range ax.ticks {
if len(tk.Label) > len(lst) {
lst = tk.Label
}
}
return lst
}
func (ax *Axis) sizeY(pt *Plot) (ywidth, tickWidth, tpad, bpad int) {
pc := pt.Paint
uc := &pc.UnitContext
ax.ticks = ax.Ticker.Ticks(ax.Min, ax.Max)
ax.TickLength.ToDots(uc)
w := float32(0)
if ax.Label.Text != "" {
ax.Label.Config(pt)
w += ax.Label.PaintText.BBox.Size().X
w += ax.Label.Style.Padding.Dots
}
lw := ax.Line.Width.Dots
tpad = int(math32.Ceil(lw)) + 2
bpad = int(math32.Ceil(lw)) + 2
if len(ax.ticks) > 0 {
if ax.drawTicks() {
w += ax.TickLength.Dots
}
ax.TickText.Text = ax.longestTickLabel()
if ax.TickText.Text != "" {
ax.TickText.Config(pt)
tw := ax.TickText.PaintText.BBox.Size().X
w += tw
tickWidth = int(math32.Ceil(tw))
w += ax.TickText.Style.Padding.Dots
tht := int(math32.Ceil(0.5 * ax.TickText.PaintText.BBox.Size().X))
tpad += tht
bpad += tht
}
}
w += lw + ax.Padding.Dots
ywidth = int(math32.Ceil(w))
return
}
// drawX draws the horizontal axis
func (ax *Axis) drawX(pt *Plot, lpad, rpad int) {
ab := pt.Paint.Bounds
ab.Min.X += lpad
ab.Max.X -= rpad
axw := float32(ab.Size().X)
// axh := float32(ab.Size().Y) // height of entire plot
if ax.Label.Text != "" {
ax.Label.Config(pt)
pos := ax.Label.PosX(axw)
pos.X += float32(ab.Min.X)
th := ax.Label.PaintText.BBox.Size().Y
pos.Y = float32(ab.Max.Y) - th
ax.Label.Draw(pt, pos)
ab.Max.Y -= int(math32.Ceil(th + ax.Label.Style.Padding.Dots))
}
tickHt := float32(0)
for _, t := range ax.ticks {
x := axw * float32(ax.Norm(t.Value))
if x < 0 || x > axw || t.IsMinor() {
continue
}
ax.TickText.Text = t.Label
ax.TickText.Config(pt)
pos := ax.TickText.PosX(0)
pos.X += x + float32(ab.Min.X)
tickHt = ax.TickText.PaintText.BBox.Size().Y + ax.TickText.Style.Padding.Dots
pos.Y += float32(ab.Max.Y) - tickHt
ax.TickText.Draw(pt, pos)
}
if len(ax.ticks) > 0 {
ab.Max.Y -= int(math32.Ceil(tickHt))
// } else {
// y += ax.Width / 2
}
if len(ax.ticks) > 0 && ax.drawTicks() {
ln := ax.TickLength.Dots
for _, t := range ax.ticks {
yoff := float32(0)
if t.IsMinor() {
yoff = 0.5 * ln
}
x := axw * float32(ax.Norm(t.Value))
if x < 0 || x > axw {
continue
}
x += float32(ab.Min.X)
ax.TickLine.Draw(pt, math32.Vec2(x, float32(ab.Max.Y)-yoff), math32.Vec2(x, float32(ab.Max.Y)-ln))
}
ab.Max.Y -= int(ln - 0.5*ax.Line.Width.Dots)
}
ax.Line.Draw(pt, math32.Vec2(float32(ab.Min.X), float32(ab.Max.Y)), math32.Vec2(float32(ab.Min.X)+axw, float32(ab.Max.Y)))
}
// drawY draws the Y axis along the left side
func (ax *Axis) drawY(pt *Plot, tickWidth, tpad, bpad int) {
ab := pt.Paint.Bounds
ab.Min.Y += tpad
ab.Max.Y -= bpad
axh := float32(ab.Size().Y)
if ax.Label.Text != "" {
ax.Label.Style.Align = styles.Center
pos := ax.Label.PosY(axh)
tw := ax.Label.PaintText.BBox.Size().X
pos.Y += float32(ab.Min.Y) + ax.Label.PaintText.BBox.Size().Y
pos.X = float32(ab.Min.X)
ax.Label.Draw(pt, pos)
ab.Min.X += int(math32.Ceil(tw + ax.Label.Style.Padding.Dots))
}
tickWd := float32(0)
for _, t := range ax.ticks {
y := axh * (1 - float32(ax.Norm(t.Value)))
if y < 0 || y > axh || t.IsMinor() {
continue
}
ax.TickText.Text = t.Label
ax.TickText.Config(pt)
pos := ax.TickText.PosX(float32(tickWidth))
pos.X += float32(ab.Min.X)
pos.Y = float32(ab.Min.Y) + y - 0.5*ax.TickText.PaintText.BBox.Size().Y
tickWd = max(tickWd, ax.TickText.PaintText.BBox.Size().X+ax.TickText.Style.Padding.Dots)
ax.TickText.Draw(pt, pos)
}
if len(ax.ticks) > 0 {
ab.Min.X += int(math32.Ceil(tickWd))
// } else {
// y += ax.Width / 2
}
if len(ax.ticks) > 0 && ax.drawTicks() {
ln := ax.TickLength.Dots
for _, t := range ax.ticks {
xoff := float32(0)
if t.IsMinor() {
xoff = 0.5 * ln
}
y := axh * (1 - float32(ax.Norm(t.Value)))
if y < 0 || y > axh {
continue
}
y += float32(ab.Min.Y)
ax.TickLine.Draw(pt, math32.Vec2(float32(ab.Min.X)+xoff, y), math32.Vec2(float32(ab.Min.X)+ln, y))
}
ab.Min.X += int(ln + 0.5*ax.Line.Width.Dots)
}
ax.Line.Draw(pt, math32.Vec2(float32(ab.Min.X), float32(ab.Min.Y)), math32.Vec2(float32(ab.Min.X), float32(ab.Max.Y)))
}
////////////////////////////////////////////////
// Legend
// draw draws the legend
func (lg *Legend) draw(pt *Plot) {
pc := pt.Paint
uc := &pc.UnitContext
ptb := pc.Bounds
lg.ThumbnailWidth.ToDots(uc)
lg.TextStyle.ToDots(uc)
lg.Position.XOffs.ToDots(uc)
lg.Position.YOffs.ToDots(uc)
lg.TextStyle.openFont(pt)
em := lg.TextStyle.Font.Face.Metrics.Em
pad := math32.Ceil(lg.TextStyle.Padding.Dots)
var ltxt Text
ltxt.Style = lg.TextStyle
var sz image.Point
maxTht := 0
for _, e := range lg.Entries {
ltxt.Text = e.Text
ltxt.Config(pt)
sz.X = max(sz.X, int(math32.Ceil(ltxt.PaintText.BBox.Size().X)))
tht := int(math32.Ceil(ltxt.PaintText.BBox.Size().Y + pad))
maxTht = max(tht, maxTht)
}
sz.X += int(em)
sz.Y = len(lg.Entries) * maxTht
txsz := sz
sz.X += int(lg.ThumbnailWidth.Dots)
pos := ptb.Min
if lg.Position.Left {
pos.X += int(lg.Position.XOffs.Dots)
} else {
pos.X = ptb.Max.X - sz.X - int(lg.Position.XOffs.Dots)
}
if lg.Position.Top {
pos.Y += int(lg.Position.YOffs.Dots)
} else {
pos.Y = ptb.Max.Y - sz.Y - int(lg.Position.YOffs.Dots)
}
if lg.FillColor != nil {
pc.FillBox(math32.Vector2FromPoint(pos), math32.Vector2FromPoint(sz), colors.C(lg.FillColor))
}
cp := pos
thsz := image.Point{X: int(lg.ThumbnailWidth.Dots), Y: maxTht - 2*int(pad)}
for _, e := range lg.Entries {
tp := cp
tp.X += int(txsz.X)
tp.Y += int(pad)
tb := image.Rectangle{Min: tp, Max: tp.Add(thsz)}
pc.PushBounds(tb)
for _, t := range e.Thumbs {
t.Thumbnail(pt)
}
pc.PopBounds()
ltxt.Text = e.Text
ltxt.Config(pt)
ltxt.Draw(pt, math32.Vector2FromPoint(cp))
cp.Y += maxTht
}
}