-
-
Notifications
You must be signed in to change notification settings - Fork 776
Expand file tree
/
Copy pathmvPlotting.cpp
More file actions
4015 lines (3366 loc) · 134 KB
/
mvPlotting.cpp
File metadata and controls
4015 lines (3366 loc) · 134 KB
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mvPlotting.h"
#include <utility>
#include "mvCore.h"
#include "mvContext.h"
#include "mvItemRegistry.h"
#include "mvPyUtils.h"
#include "mvFontItems.h"
#include "mvThemes.h"
#include "mvContainers.h"
#include "mvTextureItems.h"
#include "mvItemHandlers.h"
static void
draw_polygon(const mvAreaSeriesConfig& config)
{
static const std::vector<double>* xptr;
static const std::vector<double>* yptr;
xptr = &(*config.value.get())[0];
yptr = &(*config.value.get())[1];
std::vector<ImVec2> points;
for (unsigned i = 0; i < xptr->size(); i++)
{
auto p = ImPlot::PlotToPixels({ (*xptr)[i], (*yptr)[i] });
points.push_back(p);
}
if (config.fill.r > 0.0f)
{
size_t i;
double y;
double miny, maxy;
double x1, y1;
double x2, y2;
int ind1, ind2;
size_t ints;
size_t n = points.size();
int* polyints = new int[n];
/* Get plot Y range in pixels */
ImPlotRect limits = ImPlot::GetPlotLimits();
auto upperLimitsPix = ImPlot::PlotToPixels({ limits.X.Max, limits.Y.Max });
auto lowerLimitsPix = ImPlot::PlotToPixels({ limits.X.Min, limits.Y.Min });
/* Determine Y range of data*/
miny = (int)points[0].y;
maxy = (int)points[0].y;
for (i = 1; i < n; i++)
{
miny = std::min((int)miny, (int)points[i].y);
maxy = std::max((int)maxy, (int)points[i].y);
}
/* Determine to clip scans based on plot bounds y or data bounds y
when the plot data is converted the min and max y invert (due to plot to graphics coord)
so we comapre min with max and max with min*/
miny = std::max((int)miny, (int)upperLimitsPix.y);
maxy = std::min((int)maxy, (int)lowerLimitsPix.y);
/* Draw, scanning y */
for (y = miny; y <= maxy; y++) {
ints = 0;
for (i = 0; (i < n); i++) {
if (!i)
{
ind1 = (int)n - 1;
ind2 = 0;
}
else
{
ind1 = (int)i - 1;
ind2 = (int)i;
}
y1 = (int)points[ind1].y;
y2 = (int)points[ind2].y;
if (y1 < y2)
{
x1 = (int)points[ind1].x;
x2 = (int)points[ind2].x;
}
else if (y1 > y2)
{
y2 = (int)points[ind1].y;
y1 = (int)points[ind2].y;
x2 = (int)points[ind1].x;
x1 = (int)points[ind2].x;
}
else
continue;
if (((y >= y1) && (y < y2)) || ((y == maxy) && (y > y1) && (y <= y2)))
polyints[ints++] = (y - y1) * (x2 - x1) / (y2 - y1) + x1;
}
auto compare_int = [](const void* a, const void* b)
{
return (*(const int*)a) - (*(const int*)b);
};
qsort(polyints, ints, sizeof(int), compare_int);
for (i = 0; i < ints; i += 2)
ImGui::GetWindowDrawList()->AddLine({ (float)polyints[i], (float)y }, { (float)polyints[i + 1], (float)y }, config.fill, 1.0f);
}
delete[] polyints;
}
}
template <typename T>
int BinarySearch(const T* arr, int l, int r, T x) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return BinarySearch(arr, l, mid - 1, x);
return BinarySearch(arr, mid + 1, r, x);
}
return -1;
}
static void
PlotCandlestick(const char* label_id, const double* xs, const double* opens,
const double* closes, const double* lows, const double* highs, int count,
bool tooltip, float width_percent, const ImVec4& bullCol, const ImVec4& bearCol, int time_unit)
{
ImDrawList* draw_list = ImPlot::GetPlotDrawList();
// calc real value width
float half_width = count > 1 ? ((float)xs[1] - (float)xs[0]) * width_percent : width_percent;
// custom tool
if (ImPlot::IsPlotHovered() && tooltip) {
ImPlotPoint mouse = ImPlot::GetPlotMousePos();
mouse.x = ImPlot::RoundTime(ImPlotTime::FromDouble(mouse.x), time_unit).ToDouble();
float tool_l = ImPlot::PlotToPixels(mouse.x - half_width * 1.5, mouse.y).x;
float tool_r = ImPlot::PlotToPixels(mouse.x + half_width * 1.5, mouse.y).x;
float tool_t = ImPlot::GetPlotPos().y;
float tool_b = tool_t + ImPlot::GetPlotSize().y;
ImPlot::PushPlotClipRect();
draw_list->AddRectFilled(ImVec2(tool_l, tool_t), ImVec2(tool_r, tool_b), IM_COL32(128, 128, 128, 64));
ImPlot::PopPlotClipRect();
// find mouse location index
int idx = BinarySearch(xs, 0, count - 1, mouse.x);
if (idx != -1)
{
if(ImGui::BeginTooltip()) {
if (time_unit == ImPlotTimeUnit_Day)
{
char buff[32];
ImPlot::FormatDate(ImPlotTime::FromDouble(xs[idx]), buff, 32, ImPlotDateFmt_DayMoYr, ImPlot::GetStyle().UseISO8601);
ImGui::Text("Day: %s", buff);
}
else if (time_unit == ImPlotTimeUnit_Us)
{
ImGui::Text("Microsecond: %f", xs[idx]);
}
else if (time_unit == ImPlotTimeUnit_Ms)
{
ImGui::Text("Millisecond: %f", xs[idx]);
}
else if (time_unit == ImPlotTimeUnit_S)
{
ImGui::Text("Second: %f", xs[idx]);
}
else if (time_unit == ImPlotTimeUnit_Min)
{
ImGui::Text("Minute: %f", xs[idx]);
}
else if (time_unit == ImPlotTimeUnit_Hr)
{
ImGui::Text("Hour: %f", xs[idx]);
}
else if (time_unit == ImPlotTimeUnit_Mo)
{
ImGui::Text("Month: %f", xs[idx]);
}
else if (time_unit == ImPlotTimeUnit_Yr)
{
ImGui::Text("Year: %f", xs[idx]);
}
ImGui::Text("Open: $%.2f", opens[idx]);
ImGui::Text("Close: $%.2f", closes[idx]);
ImGui::Text("Low: $%.2f", lows[idx]);
ImGui::Text("High: $%.2f", highs[idx]);
ImGui::EndTooltip();
}
}
}
// begin plot item
if (ImPlot::BeginItem(label_id)) {
// override legend icon color
ImPlot::GetCurrentItem()->Color = ImGui::ColorConvertFloat4ToU32({ 0.25f, 0.25f, 0.25f, 1.0f });
// fit data if requested
if (ImPlot::FitThisFrame()) {
for (int i = 0; i < count; ++i) {
ImPlot::FitPoint(ImPlotPoint(xs[i], lows[i]));
ImPlot::FitPoint(ImPlotPoint(xs[i], highs[i]));
}
}
// render data
for (int i = 0; i < count; ++i) {
ImVec2 open_pos = ImPlot::PlotToPixels(xs[i] - half_width, opens[i]);
ImVec2 close_pos = ImPlot::PlotToPixels(xs[i] + half_width, closes[i]);
ImVec2 low_pos = ImPlot::PlotToPixels(xs[i], lows[i]);
ImVec2 high_pos = ImPlot::PlotToPixels(xs[i], highs[i]);
ImU32 color = ImGui::GetColorU32(opens[i] > closes[i] ? bearCol : bullCol);
draw_list->AddLine(low_pos, high_pos, color);
draw_list->AddRectFilled(open_pos, close_pos, color);
}
// end plot item
ImPlot::EndItem();
}
}
void
DearPyGui::set_data_source(mvAppItem& item, mvUUID dataSource, mvAnnotationConfig& outConfig)
{
if (dataSource == item.config.source) return;
item.config.source = dataSource;
mvAppItem* srcItem = GetItem((*GContext->itemRegistry), dataSource);
if (!srcItem)
{
mvThrowPythonError(mvErrorCode::mvSourceNotFound, "set_value",
"Source item not found: " + std::to_string(dataSource), &item);
return;
}
if (DearPyGui::GetEntityValueType(srcItem->type) != DearPyGui::GetEntityValueType(item.type))
{
mvThrowPythonError(mvErrorCode::mvSourceNotCompatible, "set_value",
"Values types do not match: " + std::to_string(dataSource), &item);
return;
}
outConfig.value = *static_cast<std::shared_ptr<std::array<double, 4>>*>(srcItem->getValue());
}
void
DearPyGui::set_data_source(mvAppItem& item, mvUUID dataSource, mvAxisTagConfig& outConfig)
{
if (dataSource == item.config.source) return;
item.config.source = dataSource;
mvAppItem* srcItem = GetItem((*GContext->itemRegistry), dataSource);
if (!srcItem)
{
mvThrowPythonError(mvErrorCode::mvSourceNotFound, "set_value",
"Source item not found: " + std::to_string(dataSource), &item);
return;
}
if (DearPyGui::GetEntityValueType(srcItem->type) != DearPyGui::GetEntityValueType(item.type))
{
mvThrowPythonError(mvErrorCode::mvSourceNotCompatible, "set_value",
"Values types do not match: " + std::to_string(dataSource), &item);
return;
}
outConfig.value = *static_cast<std::shared_ptr<double>*>(srcItem->getValue());
}
void
DearPyGui::set_data_source(mvAppItem& item, mvUUID dataSource, mvDragLineConfig& outConfig)
{
if (dataSource == item.config.source) return;
item.config.source = dataSource;
mvAppItem* srcItem = GetItem((*GContext->itemRegistry), dataSource);
if (!srcItem)
{
mvThrowPythonError(mvErrorCode::mvSourceNotFound, "set_value",
"Source item not found: " + std::to_string(dataSource), &item);
return;
}
if (DearPyGui::GetEntityValueType(srcItem->type) != DearPyGui::GetEntityValueType(item.type))
{
mvThrowPythonError(mvErrorCode::mvSourceNotCompatible, "set_value",
"Values types do not match: " + std::to_string(dataSource), &item);
return;
}
outConfig.value = *static_cast<std::shared_ptr<double>*>(srcItem->getValue());
}
void
DearPyGui::set_data_source(mvAppItem& item, mvUUID dataSource, mvDragRectConfig& outConfig)
{
if (dataSource == item.config.source) return;
item.config.source = dataSource;
mvAppItem* srcItem = GetItem((*GContext->itemRegistry), dataSource);
if (!srcItem)
{
mvThrowPythonError(mvErrorCode::mvSourceNotFound, "set_value",
"Source item not found: " + std::to_string(dataSource), &item);
return;
}
if (DearPyGui::GetEntityValueType(srcItem->type) != DearPyGui::GetEntityValueType(item.type))
{
mvThrowPythonError(mvErrorCode::mvSourceNotCompatible, "set_value",
"Values types do not match: " + std::to_string(dataSource), &item);
return;
}
outConfig.value = *static_cast<std::shared_ptr<std::array<double, 4>>*>(item.getValue());
}
void
DearPyGui::set_data_source(mvAppItem& item, mvUUID dataSource, mvDragPointConfig& outConfig)
{
if (dataSource == item.config.source) return;
item.config.source = dataSource;
mvAppItem* srcItem = GetItem((*GContext->itemRegistry), dataSource);
if (!srcItem)
{
mvThrowPythonError(mvErrorCode::mvSourceNotFound, "set_value",
"Source item not found: " + std::to_string(dataSource), &item);
return;
}
if (DearPyGui::GetEntityValueType(srcItem->type) != DearPyGui::GetEntityValueType(item.type))
{
mvThrowPythonError(mvErrorCode::mvSourceNotCompatible, "set_value",
"Values types do not match: " + std::to_string(dataSource), &item);
return;
}
outConfig.value = *static_cast<std::shared_ptr<std::array<double, 2>>*>(item.getValue());
}
void
DearPyGui::set_data_source(mvAppItem& item, mvUUID dataSource, std::shared_ptr<std::vector<std::vector<double>>>& outValue)
{
if (dataSource == item.config.source) return;
item.config.source = dataSource;
mvAppItem* srcItem = GetItem((*GContext->itemRegistry), dataSource);
if (!srcItem)
{
mvThrowPythonError(mvErrorCode::mvSourceNotFound, "set_value",
"Source item not found: " + std::to_string(dataSource), &item);
return;
}
if (DearPyGui::GetEntityValueType(srcItem->type) != DearPyGui::GetEntityValueType(item.type))
{
mvThrowPythonError(mvErrorCode::mvSourceNotCompatible, "set_value",
"Values types do not match: " + std::to_string(dataSource), &item);
return;
}
outValue = *static_cast<std::shared_ptr<std::vector<std::vector<double>>>*>(srcItem->getValue());
}
void
DearPyGui::draw_plot(ImDrawList* drawlist, mvAppItem& item, mvPlotConfig& config)
{
if (!item.config.show)
return;
// cache old cursor position
ImVec2 previousCursorPos = ImGui::GetCursorPos();
// set cursor position if user set
if (item.info.dirtyPos)
ImGui::SetCursorPos(item.state.pos);
// update widget's position state
item.state.pos = { ImGui::GetCursorPosX(), ImGui::GetCursorPosY() };
// push font if a font object is attached
if (item.font)
{
ImFont* fontptr = static_cast<mvFont*>(item.font.get())->getFontPtr();
ImGui::PushFont(fontptr);
}
// themes
apply_local_theming(&item);
// Must do this because these items are not avalable as style items
// these are here because they need to be applied every plot
ImPlot::GetStyle().UseLocalTime = config.localTime;
ImPlot::GetStyle().UseISO8601 = config.iSO8601;
ImPlot::GetStyle().Use24HourClock = config.clock24Hour;
if (config._newColorMap)
{
ImPlot::BustColorCache(item.info.internalLabel.c_str());
config._newColorMap = false;
}
if (config._useColorMap)
ImPlot::PushColormap(config._colormap);
// custom input mapping
ImPlot::GetInputMap().Pan = config.pan;
ImPlot::GetInputMap().Fit = config.fit;
ImPlot::GetInputMap().Select = config.select;
ImPlot::GetInputMap().SelectCancel = config.select_cancel;
ImPlot::GetInputMap().Menu = config.menu;
ImPlot::GetInputMap().ZoomRate = config.zoom_rate;
if (config.pan_mod != ImPlot::GetInputMap().PanMod) ImPlot::GetInputMap().PanMod = config.pan_mod;
if (config.select_mod != ImPlot::GetInputMap().SelectMod) ImPlot::GetInputMap().SelectMod = config.select_mod;
if (config.zoom_mod != ImPlot::GetInputMap().ZoomMod) ImPlot::GetInputMap().ZoomMod = config.zoom_mod;
if (config.override_mod != ImPlot::GetInputMap().OverrideMod) ImPlot::GetInputMap().OverrideMod = config.override_mod;
if (config.select_horz_mod != ImPlot::GetInputMap().SelectHorzMod) ImPlot::GetInputMap().SelectHorzMod = config.select_horz_mod;
if (config.select_vert_mod != ImPlot::GetInputMap().SelectVertMod) ImPlot::GetInputMap().SelectVertMod = config.select_vert_mod;
if (config._fitDirty)
{
// This must be called before BeginPlot
for(int i = 0; i < ImAxis_COUNT; i++) {
if (config._axisfitDirty[i] == true) {
ImPlot::SetNextAxisToFit(i);
config._axisfitDirty[i] = false;
}
}
config._fitDirty = false;
}
if (ImPlot::BeginPlot(item.info.internalLabel.c_str(), ImVec2((float)item.config.width, (float)item.config.height), config._flags))
{
// gives axes change to make changes to ticks, limits, etc.
ImAxis next_y_axis = ImAxis_Y1;
for (auto& child : item.childslots[1])
{
// skip item if it's not shown
if (!child->config.show)
continue;
if (child->type == mvAppItemType::mvPlotAxis)
{
mvPlotAxis* axis = static_cast<mvPlotAxis*>(child.get());
ImAxis_ id_axis = static_cast<ImAxis_>(axis->configData.axis);
// auto-assigning additional Y axes for compatibility with DPG 1.11 and earlier versions
auto flags = axis->configData.flags;
if (id_axis == ImAxis_Y1)
{
if (axis->configData.axis < next_y_axis)
{
id_axis = static_cast<ImAxis_>(next_y_axis);
flags |= ImPlotAxisFlags_Opposite;
}
++next_y_axis;
}
ImPlot::SetupAxis(id_axis, axis->config.specifiedLabel.c_str(), flags);
if (axis->configData.setLimits || axis->configData._dirty)
{
ImPlot::SetupAxisLimits(id_axis, axis->configData.limits.Min, axis->configData.limits.Max, ImGuiCond_Always);
axis->configData._dirty = false; // TODO: Check if this is it really useful
}
if (!axis->configData.formatter.empty())
ImPlot::SetupAxisFormat(id_axis, axis->configData.formatter.c_str());
ImPlot::SetupAxisScale(id_axis, axis->configData.scale);
if (axis->configData.setLimitsRange)
ImPlot::SetupAxisLimitsConstraints(id_axis, axis->configData.constraints_range.Min, axis->configData.constraints_range.Max);
if (axis->configData.setZoomRange)
ImPlot::SetupAxisZoomConstraints(id_axis, axis->configData.zoom_range.x, axis->configData.zoom_range.y);
if (!axis->configData.labels.empty())
{
// TODO: Checks (from original dpg)
ImPlot::SetupAxisTicks(id_axis, axis->configData.labelLocations.data(), (int)axis->configData.labels.size(), axis->configData.clabels.data());
}
}
else
child->customAction();
}
auto context = ImPlot::GetCurrentContext();
ImGuiIO& IO = ImGui::GetIO();
// Note: we can't use `config.querying` here because in the frame when
// the query modifier gets pressed, `querying` is still false but we already
// need to disable `OverrideMod`.
if (ImHasFlag(IO.KeyMods, config.query_toggle_mod) &&
(ImGui::IsMouseDown(config.select) || ImGui::IsMouseReleased(config.select)))
{
// Preventing ImPlot from getting stuck on selection if override modifier
// is pressed (e.g. when the override mod is the same as query toggle mod).
ImPlot::GetInputMap().OverrideMod = ImGuiMod_None;
}
else
ImPlot::GetInputMap().OverrideMod = config.override_mod;
bool query_dirty = false;
if (config.query_enabled && config.querying && ImGui::IsMouseReleased(config.select))
{
if (config.max_query_rects != 0 && config.rects.size() >= config.max_query_rects)
config.rects.pop_back();
config.rects.push_back(config.query_rect);
config.querying = false;
// Prevent ImPlot from handling mouse release on its own. This will block
// input handling in the current frame (later we'll reset OverrideMod).
ImPlot::GetInputMap().OverrideMod = IO.KeyMods;
// Note: this will lock the setup and might therefore skip changes
// to the legend, drag points, and lines in this frame. Nothing we
// can do about that, really.
ImPlot::CancelPlotSelection();
// We've updated the list, let's report this
query_dirty = true;
}
// legend, drag point and lines
for (auto& child : item.childslots[0]) // Using "ImPlot::GetPlotPos()" here trigger an assert
child->draw(drawlist, context->CurrentPlot->PlotRect.Min.x, context->CurrentPlot->PlotRect.Min.y);
// axes
for (auto& child : item.childslots[1])
child->draw(drawlist, ImPlot::GetPlotPos().x, ImPlot::GetPlotPos().y);
ImPlot::PushPlotClipRect();
ImPlot::SetAxis(ImAxis_Y1);
// drawings
for (auto& child : item.childslots[2])
{
// skip item if it's not shown
if (!child->config.show)
continue;
//item->draw(ImPlot::GetPlotDrawList(), ImPlot::GetPlotPos().x, ImPlot::GetPlotPos().y);
child->draw(ImPlot::GetPlotDrawList(), 0.0f, 0.0f);
UpdateAppItemState(child->state);
}
ImPlot::PopPlotClipRect();
if (config._useColorMap)
ImPlot::PopColormap();
config.querying = ImHasFlag(IO.KeyMods, config.query_toggle_mod) && ImPlot::IsPlotSelected();
if (config.querying)
config.query_rect = ImPlot::GetPlotSelection();
// While rendering query rects, we'll see which of them the user asks to
// delete (by double-clicking it). We need to run through the entire list
// to make sure that we pick the topmost candidate if there's more than one.
int delete_idx = -1;
for (int i = 0; i < config.rects.size(); ++i) {
// TODO: Implement flags
bool hovered = false;
query_dirty |= ImPlot::DragRect(i,&config.rects[i].X.Min,&config.rects[i].Y.Min,&config.rects[i].X.Max,&config.rects[i].Y.Max, config.query_color, ImPlotDragToolFlags_NoFit, nullptr, &hovered);
if (config.rects.size() > config.min_query_rects) {
if (hovered && ImGui::IsMouseDoubleClicked(config.select_cancel))
{
// remember it for future deletion
delete_idx = i;
}
}
}
// Delete rect on double click.
// We're not interested in double-clicks that modify a query rect
// (in particular, double-clicks on rect edges), and to filter them out,
// we additionally check for `query_dirty` to be false.
if (delete_idx >= 0 && !query_dirty)
{
config.rects.erase(config.rects.begin() + delete_idx);
// Preventing plot auto-fit if it uses the same mouse button.
// Kind of a dirty trick but double-click has already set
// all `FitThisFrame` to true anyway.
if (config.fit == config.select_cancel)
{
context->CurrentPlot->FitThisFrame = false;
for (int j = 0; j < ImAxis_COUNT; ++j)
context->CurrentPlot->Axes[j].FitThisFrame = false;
}
// We've updated the list, let's report this
query_dirty = true;
}
if (item.config.callback != nullptr && query_dirty)
{
if (item.config.alias.empty()) {
mvSubmitCallback([=, &item]() {
PyObject* result = PyTuple_New(config.rects.size());
for (int i = 0; i < config.rects.size(); ++i) {
auto rectMin = config.rects[i].Min();
auto rectMax = config.rects[i].Max();
PyTuple_SetItem(result, i, Py_BuildValue("(dddd)", rectMin.x, rectMin.y, rectMax.x, rectMax.y));
}
mvAddCallback(item.config.callback, item.uuid, result, item.config.user_data);
});
} else {
mvSubmitCallback([=, &item]() {
PyObject* result = PyTuple_New(config.rects.size());
for (int i = 0; i < config.rects.size(); ++i) {
auto rectMin = config.rects[i].Min();
auto rectMax = config.rects[i].Max();
PyTuple_SetItem(result, i, Py_BuildValue("(dddd)", rectMin.x, rectMin.y, rectMax.x, rectMax.y));
}
mvAddCallback(item.config.callback, item.config.alias, result, item.config.user_data);
});
}
}
if (ImPlot::IsPlotHovered())
{
GContext->input.mousePlotPos.x = ImPlot::GetPlotMousePos().x;
GContext->input.mousePlotPos.y = ImPlot::GetPlotMousePos().y;
}
// todo: resolve clipping
if (item.config.dropCallback)
{
ScopedID id(item.uuid);
if (ImPlot::BeginDragDropTargetPlot())
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(item.config.payloadType.c_str()))
{
auto payloadActual = static_cast<const mvDragPayload*>(payload->Data);
if (item.config.alias.empty())
mvAddCallback(item.config.dropCallback, item.uuid, payloadActual->configData.dragData, nullptr);
else
mvAddCallback(item.config.dropCallback, item.config.alias, payloadActual->configData.dragData, nullptr);
}
ImPlot::EndDragDropTarget();
}
}
// update state
config._flags = context->CurrentPlot->Flags;
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows))
{
// update mouse
ImVec2 mousePos = ImGui::GetMousePos();
ImVec2 windowPos = ImGui::GetWindowPos();
GContext->input.mousePos.x = (int)(mousePos.x - windowPos.x);
GContext->input.mousePos.y = (int)(mousePos.y - windowPos.y);
GContext->activeWindow = item.uuid;
}
// TODO: find a better way to handle this
// We could use std::find_if from <algorithm> but it'd require item.childslots[0] to be a std::vector
for (auto& child : item.childslots[0])
{
if (child->type == mvAppItemType::mvPlotLegend)
{
auto legend = static_cast<mvPlotLegend*>(child.get());
legend->configData.legendLocation = context->CurrentPlot->Items.Legend.Location;
legend->configData.flags = context->CurrentPlot->Items.Legend.Flags;
break;
}
}
ImPlot::EndPlot();
}
// set cursor position to cached position
if (item.info.dirtyPos)
ImGui::SetCursorPos(previousCursorPos);
ImPlot::GetInputMap() = config._originalMap;
UpdateAppItemState(item.state);
if (item.font)
{
ImGui::PopFont();
}
if (item.theme)
{
item.theme->pop_theme_components();
}
if (item.handlerRegistry)
item.handlerRegistry->checkEvents(&item.state);
// drag drop
for (auto& child : item.childslots[3])
child->draw(nullptr, ImGui::GetCursorPosX(), ImGui::GetCursorPosY());
}
void
DearPyGui::draw_plot_axis(ImDrawList* drawlist, mvAppItem& item, mvPlotAxisConfig& config)
{
if (!item.config.show)
return;
// todo: add check
ImPlot::SetAxis(config.axis);
for (auto& item : item.childslots[1])
item->draw(drawlist, ImPlot::GetPlotPos().x, ImPlot::GetPlotPos().y);
// x axis
if (config.axis <= ImAxis_X3)
config.limits_actual = ImPlot::GetPlotLimits(config.axis, IMPLOT_AUTO).X;
// y axis
else
config.limits_actual = ImPlot::GetPlotLimits(config.axis, IMPLOT_AUTO).Y;
config.flags = ImPlot::GetCurrentContext()->CurrentPlot->Axes[config.axis].Flags;
UpdateAppItemState(item.state);
if (item.font)
ImGui::PopFont();
if (item.theme)
static_cast<mvTheme*>(item.theme.get())->customAction();
if (item.config.dropCallback)
{
ScopedID id(item.uuid);
if (ImPlot::BeginDragDropTargetAxis(config.axis))
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(item.config.payloadType.c_str()))
{
auto payloadActual = static_cast<const mvDragPayload*>(payload->Data);
mvAddCallback(item.config.dropCallback, item.uuid, payloadActual->configData.dragData, nullptr);
}
ImPlot::EndDragDropTarget();
}
}
}
void
DearPyGui::draw_subplots(ImDrawList* drawlist, mvAppItem& item, mvSubPlotsConfig& config)
{
ScopedID id(item.uuid);
if (ImPlot::BeginSubplots(item.info.internalLabel.c_str(), config.rows, config.cols, ImVec2((float)item.config.width, (float)item.config.height),
config.flags, config.row_ratios.empty() ? nullptr : config.row_ratios.data(), config.col_ratios.empty() ? nullptr : config.col_ratios.data()))
{
// plots
for (auto& item : item.childslots[1])
item->draw(drawlist, 0.0f, 0.0f);
ImPlot::EndSubplots();
}
}
void
DearPyGui::draw_plot_legend(ImDrawList* drawlist, mvAppItem& item, mvPlotLegendConfig& config)
{
if (!item.config.show)
return;
if (config.dirty)
{
ImPlot::SetupLegend(config.legendLocation, config.flags);
config.dirty = false;
}
UpdateAppItemState(item.state);
if (item.font)
{
ImGui::PopFont();
}
if (item.theme)
{
static_cast<mvTheme*>(item.theme.get())->customAction();
}
if (item.config.dropCallback)
{
if (ImPlot::BeginDragDropTargetLegend())
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(item.config.payloadType.c_str()))
{
auto payloadActual = static_cast<const mvDragPayload*>(payload->Data);
mvAddCallback(item.config.dropCallback, item.uuid, payloadActual->configData.dragData, nullptr);
}
ImPlot::EndDragDropTarget();
}
}
}
void
DearPyGui::draw_drag_line(ImDrawList* drawlist, mvAppItem& item, mvDragLineConfig& config)
{
if (!item.config.show)
return;
ScopedID id(item.uuid);
bool hovered = false;
bool held = false;
if (config.vertical)
{
if (ImPlot::DragLineX(item.uuid, config.value.get(), config.color, config.thickness, config.flags, nullptr, &hovered, &held))
{
mvAddCallback(item.config.callback, item.uuid, nullptr, item.config.user_data);
}
if (config.show_label && !item.config.specifiedLabel.empty() && (hovered || held)) {
char buff[IMPLOT_LABEL_MAX_SIZE];
ImPlotContext& gp = *GImPlot;
ImPlotAxis& axis = gp.CurrentPlot->Axes[gp.CurrentPlot->CurrentX];
auto pos = *config.value.get();
ImPlot::LabelAxisValue(axis, pos, buff, sizeof(buff), true);
ImVec4 color = ImPlot::IsColorAuto(config.color.toVec4()) ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : config.color;
ImPlot::Annotation(pos, ImPlot::GetPlotLimits().Min().y, color, ImVec2(0, 0), true, "%s = %s", item.config.specifiedLabel.c_str(), buff);
}
}
else
{
if (ImPlot::DragLineY(item.uuid, config.value.get(), config.color, config.thickness, config.flags, nullptr, &hovered, &held))
{
mvAddCallback(item.config.callback, item.uuid, nullptr, item.config.user_data);
}
if (config.show_label && !item.config.specifiedLabel.empty() && (hovered || held)) {
char buff[IMPLOT_LABEL_MAX_SIZE];
ImPlotContext& gp = *GImPlot;
ImPlotAxis& axis = gp.CurrentPlot->Axes[gp.CurrentPlot->CurrentY];
auto label_pos = *config.value.get();
ImPlot::LabelAxisValue(axis, label_pos, buff, sizeof(buff), true);
ImVec4 color = ImPlot::IsColorAuto(config.color.toVec4()) ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : config.color;
ImPlot::Annotation(ImPlot::GetPlotLimits().Min().x, label_pos, color, ImVec2(0, 0), true, "%s = %s", item.config.specifiedLabel.c_str(), buff);
}
}
}
void
DearPyGui::draw_plot_tag(ImDrawList* drawlist, mvAppItem& item, mvAxisTagConfig& config)
{
if (!item.config.show)
return;
auto parent = (mvPlotAxis*)item.info.parentPtr;
auto axis_id = parent->configData.axis;
const bool vertical = (axis_id >= ImAxis_Y1);
if (vertical) {
if (!item.config.specifiedLabel.empty()) {
ImPlot::TagY(*config.value.get(), config.color, "%s", item.config.specifiedLabel.c_str());
} else {
ImPlot::TagY(*config.value.get(), config.color, config.auto_rounding);
}
}
else {
if (!item.config.specifiedLabel.empty()) {
ImPlot::TagX(*config.value.get(), config.color, "%s", item.config.specifiedLabel.c_str());
} else {
ImPlot::TagX(*config.value.get(), config.color, config.auto_rounding);
}
}
}
void
DearPyGui::draw_drag_rect(ImDrawList* drawlist, mvAppItem& item, mvDragRectConfig& config)
{
if (!item.config.show)
return;
ScopedID id(item.uuid);
static double xmin = (*config.value.get())[0];
static double ymin = (*config.value.get())[1];
static double xmax = (*config.value.get())[2];
static double ymax = (*config.value.get())[3];
// I still don't get why we need to do this
xmin = (*config.value.get())[0];
ymin = (*config.value.get())[1];
xmax = (*config.value.get())[2];
ymax = (*config.value.get())[3];
// item.config.specifiedLabel.c_str(),
if (ImPlot::DragRect(item.uuid, &xmin, &ymin, &xmax, &ymax, config.color, config.flags))
{
(*config.value.get())[0] = xmin;
(*config.value.get())[1] = ymin;
(*config.value.get())[2] = xmax;
(*config.value.get())[3] = ymax;
mvAddCallback(item.config.callback, item.uuid, nullptr, item.config.user_data);
}
}
void
DearPyGui::draw_drag_point(ImDrawList* drawlist, mvAppItem& item, mvDragPointConfig& config)
{
if (!item.config.show)
return;
ScopedID id(item.uuid);
static double dummyx = (*config.value.get())[0];
static double dummyy = (*config.value.get())[1];
dummyx = (*config.value.get())[0];
dummyy = (*config.value.get())[1];
bool hovered = false;
bool held = false;
if (ImPlot::DragPoint(item.uuid, &dummyx, &dummyy, config.color, config.radius, config.flags, nullptr, &hovered, &held))
{
(*config.value.get())[0] = dummyx;
(*config.value.get())[1] = dummyy;
mvAddCallback(item.config.callback, item.uuid, nullptr, item.config.user_data);
}
if (config.show_label && !item.config.specifiedLabel.empty() && (hovered || held)) {
ImPlotContext& gp = *GImPlot;
char x_buff[IMPLOT_LABEL_MAX_SIZE];
ImPlotAxis& x_axis = gp.CurrentPlot->Axes[gp.CurrentPlot->CurrentX];
ImPlot::LabelAxisValue(x_axis, dummyx, x_buff, sizeof(x_buff), true);
char y_buff[IMPLOT_LABEL_MAX_SIZE];
ImPlotAxis& y_axis = gp.CurrentPlot->Axes[gp.CurrentPlot->CurrentY];
ImPlot::LabelAxisValue(y_axis, dummyy, y_buff, sizeof(y_buff), true);
ImVec4 color = ImPlot::IsColorAuto(config.color.toVec4()) ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : config.color;
ImPlot::Annotation(dummyx, dummyy, color, config.pixOffset, config.clamped, "%s = %s, %s", item.config.specifiedLabel.c_str(), x_buff, y_buff);
}
}
void
DearPyGui::draw_bar_series(ImDrawList* drawlist, mvAppItem& item, const mvBarSeriesConfig& config)
{
//-----------------------------------------------------------------------------
// pre draw
//-----------------------------------------------------------------------------
if (!item.config.show)
return;
// push font if a font object is attached
if (item.font)
{
ImFont* fontptr = static_cast<mvFont*>(item.font.get())->getFontPtr();
ImGui::PushFont(fontptr);
}
// themes
apply_local_theming(&item);
//-----------------------------------------------------------------------------
// draw
//-----------------------------------------------------------------------------
{
static const std::vector<double>* xptr;
static const std::vector<double>* yptr;
xptr = &(*config.value.get())[0];
yptr = &(*config.value.get())[1];
ImPlot::PlotBars(item.info.internalLabel.c_str(), xptr->data(), yptr->data(), (int)xptr->size(), config.weight, config.flags);
// Begin a popup for a legend entry.
if (ImPlot::BeginLegendPopup(item.info.internalLabel.c_str(), 1))
{
for (auto& childset : item.childslots)
{
for (auto& item : childset)
{
// skip item if it's not shown
if (!item->config.show)
continue;
item->draw(drawlist, ImPlot::GetPlotPos().x, ImPlot::GetPlotPos().y);
UpdateAppItemState(item->state);
}
}
ImPlot::EndLegendPopup();
}
}
//-----------------------------------------------------------------------------
// update state
// * only update if applicable
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// post draw
//-----------------------------------------------------------------------------
// pop font off stack
if (item.font)
ImGui::PopFont();
// handle popping themes
cleanup_local_theming(&item);
}
void
DearPyGui::draw_bar_group_series(ImDrawList* drawlist, mvAppItem& item, const mvBarGroupSeriesConfig& config)
{
//-----------------------------------------------------------------------------