-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Plot.go
578 lines (501 loc) · 11.7 KB
/
Plot.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package giu
import (
"github.com/AllenDang/cimgui-go"
"image"
)
// PlotWidget is implemented by all the particular plots, which can be used
// in (*PlotCanvasWidget).Plots.
type PlotWidget interface {
Plot()
}
// ImPlotYAxis represents y axis settings.
type ImPlotYAxis int
// ImPlotYAxis enum:.
const (
ImPlotYAxisLeft ImPlotYAxis = 0 // left (default)
ImPlotYAxisFirstOnRight ImPlotYAxis = 1 // first on right side
ImPlotYAxisSecondOnRight ImPlotYAxis = 2 // second on right side
)
// PlotTicker represents axis ticks.
type PlotTicker struct {
Position float64
Label string
}
// PlotCanvasWidget represents a giu plot widget.
type PlotCanvasWidget struct {
title string
xLabel string
yLabel string
width int
height int
flags PlotFlags
xFlags, yFlags, y2Flags, y3Flags PlotAxisFlags
y2Label string
y3Label string
xMin, xMax, yMin, yMax float64
axisLimitCondition ExecCondition
xTicksValue, yTicksValue []float64
xTicksLabel, yTicksLabel []string
xTicksShowDefault bool
yTicksShowDefault bool
yTicksYAxis ImPlotYAxis
plots []PlotWidget
}
// Plot adds creates a new plot widget.
func Plot(title string) *PlotCanvasWidget {
return &PlotCanvasWidget{
title: title,
xLabel: "",
yLabel: "",
width: -1,
height: 0,
flags: PlotFlagsNone,
xFlags: PlotAxisFlagsNone,
yFlags: PlotAxisFlagsNone,
y2Flags: PlotAxisFlagsNoGridLines,
y3Flags: PlotAxisFlagsNoGridLines,
y2Label: "",
y3Label: "",
xMin: 0,
xMax: 10,
yMin: 0,
yMax: 10,
xTicksShowDefault: true,
yTicksShowDefault: true,
yTicksYAxis: 0,
axisLimitCondition: ConditionOnce,
}
}
// AxisLimits sets X and Y axis limits.
func (p *PlotCanvasWidget) AxisLimits(xmin, xmax, ymin, ymax float64, cond ExecCondition) *PlotCanvasWidget {
p.xMin = xmin
p.xMax = xmax
p.yMin = ymin
p.yMax = ymax
p.axisLimitCondition = cond
return p
}
// XTicks sets x axis ticks.
func (p *PlotCanvasWidget) XTicks(ticks []PlotTicker, showDefault bool) *PlotCanvasWidget {
length := len(ticks)
if length == 0 {
return p
}
values := make([]float64, length)
labels := make([]string, length)
for i, t := range ticks {
values[i] = t.Position
labels[i] = t.Label
}
p.xTicksValue = values
p.xTicksLabel = labels
p.xTicksShowDefault = showDefault
return p
}
// YTicks sets y axis ticks.
func (p *PlotCanvasWidget) YTicks(ticks []PlotTicker, showDefault bool, yAxis ImPlotYAxis) *PlotCanvasWidget {
length := len(ticks)
if length == 0 {
return p
}
values := make([]float64, length)
labels := make([]string, length)
for i, t := range ticks {
values[i] = t.Position
labels[i] = t.Label
}
p.yTicksValue = values
p.yTicksLabel = labels
p.yTicksShowDefault = showDefault
p.yTicksYAxis = yAxis
return p
}
// Flags sets plot canvas flags.
func (p *PlotCanvasWidget) Flags(flags PlotFlags) *PlotCanvasWidget {
p.flags = flags
return p
}
// XAxeFlags sets x axis fags.
func (p *PlotCanvasWidget) XAxeFlags(flags PlotAxisFlags) *PlotCanvasWidget {
p.xFlags = flags
return p
}
// YAxeFlags sets y axis flags.
func (p *PlotCanvasWidget) YAxeFlags(yFlags, y2Flags, y3Flags PlotAxisFlags) *PlotCanvasWidget {
p.yFlags = yFlags
p.y2Flags = y2Flags
p.y3Flags = y3Flags
return p
}
// Plots adds plots to plot canvas.
func (p *PlotCanvasWidget) Plots(plots ...PlotWidget) *PlotCanvasWidget {
p.plots = plots
return p
}
// Size set canvas size.
func (p *PlotCanvasWidget) Size(width, height int) *PlotCanvasWidget {
p.width = width
p.height = height
return p
}
// Build implements Widget interface.
func (p *PlotCanvasWidget) Build() {
if len(p.plots) == 0 {
return
}
if imgui.PlotBeginPlotV(
Context.FontAtlas.RegisterString(p.title),
ToVec2(image.Pt(p.width, p.height)),
imgui.PlotFlags(p.flags),
) {
imgui.PlotSetupAxisLimitsV(
imgui.AxisX1,
p.xMin,
p.xMax,
imgui.PlotCond(p.axisLimitCondition),
)
imgui.PlotSetupAxisLimitsV(
imgui.AxisY1,
p.yMin,
p.yMax,
imgui.PlotCond(p.axisLimitCondition),
)
if len(p.xTicksValue) > 0 {
imgui.PlotSetupAxisTicksdoubleV(
imgui.AxisX1,
p.xTicksValue[0],
p.xTicksValue[1], // <- TODO: why is it so strangely saved?
-1, // TODO
p.xTicksLabel,
p.xTicksShowDefault,
)
}
if len(p.yTicksValue) > 0 {
imgui.PlotSetupAxisTicksdoubleV(
imgui.AxisY1,
p.xTicksValue[0],
p.xTicksValue[1], // <- TODO: why is it so strangely saved?
-1, // TODO
p.xTicksLabel,
p.xTicksShowDefault,
)
}
imgui.PlotSetupAxisV(
imgui.AxisX,
Context.FontAtlas.RegisterString(p.xLabel),
imgui.PlotAxisFlags(p.xFlags),
)
imgui.PlotSetupAxisV(
imgui.AxisY1,
Context.FontAtlas.RegisterString(p.yLabel),
imgui.PlotAxisFlags(p.yFlags),
)
imgui.PlotSetupAxisV(
imgui.AxisY2,
Context.FontAtlas.RegisterString(p.y2Label),
imgui.PlotAxisFlags(p.y2Flags),
)
imgui.PlotSetupAxisV(
imgui.AxisY3,
Context.FontAtlas.RegisterString(p.y3Label),
imgui.PlotAxisFlags(p.y3Flags),
)
for _, plot := range p.plots {
plot.Plot()
}
imgui.PlotEndPlot()
}
}
// BarPlot adds bar plot (column chart) to the canvas.
type BarPlot struct {
title string
data []float64
width float64
shift float64
offset int
}
// Bar adds a plot bar (column chart).
func Bar(title string, data []float64) *BarPlot {
return &BarPlot{
title: title,
data: data,
width: 0.2,
shift: 0,
offset: 0,
}
}
// Width sets bar width.
func (p *BarPlot) Width(width float64) *BarPlot {
p.width = width
return p
}
// Shift sets shift of the bar.
func (p *BarPlot) Shift(shift float64) *BarPlot {
p.shift = shift
return p
}
// Offset sets bar's offset.
func (p *BarPlot) Offset(offset int) *BarPlot {
p.offset = offset
return p
}
// Plot implements Plot interface.
func (p *BarPlot) Plot() {
imgui.PlotPlotBarsdoublePtrIntV(
p.title,
&p.data,
int32(len(p.data)),
p.width,
p.shift,
0, // TODO: implement
int32(p.offset),
0, //TODO: implement
)
}
// BarHPlot represents a column chart on Y axis.
type BarHPlot struct {
title string
data []float64
height float64
shift float64
offset int
}
// BarH adds plot bars on y axis.
func BarH(title string, data []float64) *BarHPlot {
return &BarHPlot{
title: title,
data: data,
height: 0.2,
shift: 0,
offset: 0,
}
}
// Height sets bar height (in fact bars' width).
func (p *BarHPlot) Height(height float64) *BarHPlot {
p.height = height
return p
}
// Shift sets shift.
func (p *BarHPlot) Shift(shift float64) *BarHPlot {
p.shift = shift
return p
}
// Offset sets offset.
func (p *BarHPlot) Offset(offset int) *BarHPlot {
p.offset = offset
return p
}
// Plot implements plot interface.
func (p *BarHPlot) Plot() {
imgui.PlotPlotBarsdoublePtrIntV(
Context.FontAtlas.RegisterString(p.title),
&p.data,
int32(len(p.data)),
p.height,
p.shift,
imgui.PlotErrorBarsFlagsHorizontal,
int32(p.offset),
0,
)
}
// LinePlot represents a plot line (linear chart).
type LinePlot struct {
title string
values []float64
xScale, x0 float64
offset int
yAxis ImPlotYAxis
}
// Line adds a new plot line to the canvas.
func Line(title string, values []float64) *LinePlot {
return &LinePlot{
title: title,
values: values,
xScale: 1,
x0: 0,
offset: 0,
}
}
// SetPlotYAxis sets yAxis parameters.
func (p *LinePlot) SetPlotYAxis(yAxis ImPlotYAxis) *LinePlot {
p.yAxis = yAxis
return p
}
// XScale sets x-axis-scale.
func (p *LinePlot) XScale(scale float64) *LinePlot {
p.xScale = scale
return p
}
// X0 sets a start position on x axis.
func (p *LinePlot) X0(x0 float64) *LinePlot {
p.x0 = x0
return p
}
// Offset sets chart offset.
func (p *LinePlot) Offset(offset int) *LinePlot {
p.offset = offset
return p
}
// Plot implements Plot interface.
func (p *LinePlot) Plot() {
imgui.PlotSetupAxis(
imgui.PlotAxisEnum(p.yAxis),
)
// TODO: no idea what should it be...
//imgui.PlotDragLineX(Context.FontAtlas.RegisterString(p.title), p.values, p.xScale, p.x0, p.offset)
//imgui.PlotDragLineX(
// Context.FontAtlas.RegisterString(p.title),
// p.values,
// p.xScale,
// p.x0,
// p.offset,
//)
}
// LineXYPlot adds XY plot line.
type LineXYPlot struct {
title string
xs, ys []float64
offset int
yAxis ImPlotYAxis
}
// LineXY adds XY plot line to canvas.
func LineXY(title string, xvalues, yvalues []float64) *LineXYPlot {
return &LineXYPlot{
title: title,
xs: xvalues,
ys: yvalues,
offset: 0,
}
}
// SetPlotYAxis sets yAxis parameters.
func (p *LineXYPlot) SetPlotYAxis(yAxis ImPlotYAxis) *LineXYPlot {
p.yAxis = yAxis
return p
}
// Offset sets chart's offset.
func (p *LineXYPlot) Offset(offset int) *LineXYPlot {
p.offset = offset
return p
}
// Plot implements Plot interface.
func (p *LineXYPlot) Plot() {
// TODO: migrate this
//imgui.ImPlotSetPlotYAxis(imgui.ImPlotYAxis(p.yAxis))
//imgui.ImPlotLineXY(Context.FontAtlas.RegisterString(p.title), p.xs, p.ys, p.offset)
}
// PieChartPlot represents a pie chart.
type PieChartPlot struct {
labels []string
values []float64
x, y, radius float64
normalize bool
labelFormat string
angle0 float64
}
// PieChart adds pie chart to the canvas.
func PieChart(labels []string, values []float64, x, y, radius float64) *PieChartPlot {
return &PieChartPlot{
labels: labels,
values: values,
x: x,
y: y,
radius: radius,
normalize: false,
labelFormat: "%.1f",
angle0: 90,
}
}
func (p *PieChartPlot) Normalize(n bool) *PieChartPlot {
p.normalize = n
return p
}
// LabelFormat sets format of labels.
func (p *PieChartPlot) LabelFormat(fmtStr string) *PieChartPlot {
p.labelFormat = fmtStr
return p
}
func (p *PieChartPlot) Angle0(a float64) *PieChartPlot {
p.angle0 = a
return p
}
func (p *PieChartPlot) Plot() {
// TODO: p.normalized not used anymore - replace with flags
imgui.PlotPlotPieChartdoublePtrV(
Context.FontAtlas.RegisterStringSlice(p.labels),
&p.values,
int32(len(p.values)),
p.x,
p.y,
p.radius,
p.labelFormat,
p.angle0,
imgui.PlotPieChartFlagsNormalize,
)
}
type ScatterPlot struct {
label string
values []float64
xscale, x0 float64
offset int
}
func Scatter(label string, values []float64) *ScatterPlot {
return &ScatterPlot{
label: label,
values: values,
xscale: 1,
x0: 0,
offset: 0,
}
}
func (p *ScatterPlot) XScale(s float64) *ScatterPlot {
p.xscale = s
return p
}
func (p *ScatterPlot) X0(x float64) *ScatterPlot {
p.x0 = x
return p
}
func (p *ScatterPlot) Offset(offset int) *ScatterPlot {
p.offset = offset
return p
}
func (p *ScatterPlot) Plot() {
imgui.PlotPlotScatterdoublePtrIntV(
Context.FontAtlas.RegisterString(p.label),
&p.values,
int32(len(p.values)),
p.xscale,
p.x0,
0, // TODO: implement flags
int32(p.offset),
0, // TODO: implement
)
}
type ScatterXYPlot struct {
label string
xs, ys []float64
offset int
}
func ScatterXY(label string, xs, ys []float64) *ScatterXYPlot {
return &ScatterXYPlot{
label: label,
xs: xs,
ys: ys,
offset: 0,
}
}
func (p *ScatterXYPlot) Offset(offset int) *ScatterXYPlot {
p.offset = offset
return p
}
func (p *ScatterXYPlot) Plot() {
imgui.PlotPlotScatterdoublePtrdoublePtrV(
Context.FontAtlas.RegisterString(p.label),
&p.xs,
&p.ys,
int32(len(p.xs)),
0, // TODO: implement
int32(p.offset),
0, // TODO: implement
)
}