-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
implot_demo.cpp
1928 lines (1790 loc) · 95.3 KB
/
implot_demo.cpp
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
// MIT License
// Copyright (c) 2021 Evan Pezent
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ImPlot v0.10 WIP
#include "implot.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef _MSC_VER
#define sprintf sprintf_s
#endif
#ifndef PI
#define PI 3.14159265358979323846
#endif
// Encapsulates examples for customizing ImPlot.
namespace MyImPlot {
// Example for Custom Data and Getters section.
struct Vector2f {
Vector2f(float _x, float _y) { x = _x; y = _y; }
float x, y;
};
// Example for Custom Data and Getters section.
struct WaveData {
double X, Amp, Freq, Offset;
WaveData(double x, double amp, double freq, double offset) { X = x; Amp = amp; Freq = freq; Offset = offset; }
};
ImPlotPoint SineWave(void* wave_data, int idx);
ImPlotPoint SawWave(void* wave_data, int idx);
ImPlotPoint Spiral(void*, int idx);
// Example for Tables section.
void Sparkline(const char* id, const float* values, int count, float min_v, float max_v, int offset, const ImVec4& col, const ImVec2& size);
// Example for Custom Plotters and Tooltips section.
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 = true, float width_percent = 0.25f, ImVec4 bullCol = ImVec4(0,1,0,1), ImVec4 bearCol = ImVec4(1,0,0,1));
// Example for Custom Styles section.
void StyleSeaborn();
} // namespace MyImPlot
namespace ImPlot {
void ShowBenchmarkTool();
template <typename T>
inline T RandomRange(T min, T max) {
T scale = rand() / (T) RAND_MAX;
return min + scale * ( max - min );
}
ImVec4 RandomColor() {
ImVec4 col;
col.x = RandomRange(0.0f,1.0f);
col.y = RandomRange(0.0f,1.0f);
col.z = RandomRange(0.0f,1.0f);
col.w = 1.0f;
return col;
}
double RandomGauss() {
static double V1, V2, S;
static int phase = 0;
double X;
if(phase == 0) {
do {
double U1 = (double)rand() / RAND_MAX;
double U2 = (double)rand() / RAND_MAX;
V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
} while(S >= 1 || S == 0);
X = V1 * sqrt(-2 * log(S) / S);
} else
X = V2 * sqrt(-2 * log(S) / S);
phase = 1 - phase;
return X;
}
template <int N>
struct NormalDistribution {
NormalDistribution(double mean, double sd) {
for (int i = 0; i < N; ++i)
Data[i] = RandomGauss()*sd + mean;
}
double Data[N];
};
// utility structure for realtime plot
struct ScrollingBuffer {
int MaxSize;
int Offset;
ImVector<ImVec2> Data;
ScrollingBuffer(int max_size = 2000) {
MaxSize = max_size;
Offset = 0;
Data.reserve(MaxSize);
}
void AddPoint(float x, float y) {
if (Data.size() < MaxSize)
Data.push_back(ImVec2(x,y));
else {
Data[Offset] = ImVec2(x,y);
Offset = (Offset + 1) % MaxSize;
}
}
void Erase() {
if (Data.size() > 0) {
Data.shrink(0);
Offset = 0;
}
}
};
// utility structure for realtime plot
struct RollingBuffer {
float Span;
ImVector<ImVec2> Data;
RollingBuffer() {
Span = 10.0f;
Data.reserve(2000);
}
void AddPoint(float x, float y) {
float xmod = fmodf(x, Span);
if (!Data.empty() && xmod < Data.back().x)
Data.shrink(0);
Data.push_back(ImVec2(xmod, y));
}
};
// Huge data used by Time Formatting example (~500 MB allocation!)
struct HugeTimeData {
HugeTimeData(double min) {
Ts = new double[Size];
Ys = new double[Size];
for (int i = 0; i < Size; ++i) {
Ts[i] = min + i;
Ys[i] = GetY(Ts[i]);
}
}
~HugeTimeData() { delete[] Ts; delete[] Ys; }
static double GetY(double t) {
return 0.5 + 0.25 * sin(t/86400/12) + 0.005 * sin(t/3600);
}
double* Ts;
double* Ys;
static const int Size = 60*60*24*366;
};
void ShowDemoWindow(bool* p_open) {
double DEMO_TIME = ImGui::GetTime();
static bool show_imgui_metrics = false;
static bool show_implot_metrics = false;
static bool show_imgui_style_editor = false;
static bool show_implot_style_editor = false;
static bool show_implot_benchmark = false;
if (show_imgui_metrics) {
ImGui::ShowMetricsWindow(&show_imgui_metrics);
}
if (show_implot_metrics) {
ImPlot::ShowMetricsWindow(&show_implot_metrics);
}
if (show_imgui_style_editor) {
ImGui::Begin("Style Editor (ImGui)", &show_imgui_style_editor);
ImGui::ShowStyleEditor();
ImGui::End();
}
if (show_implot_style_editor) {
ImGui::SetNextWindowSize(ImVec2(415,762), ImGuiCond_Appearing);
ImGui::Begin("Style Editor (ImPlot)", &show_implot_style_editor);
ImPlot::ShowStyleEditor();
ImGui::End();
}
if (show_implot_benchmark) {
ImGui::SetNextWindowSize(ImVec2(530,740), ImGuiCond_Appearing);
ImGui::Begin("ImPlot Benchmark Tool", &show_implot_benchmark);
ImPlot::ShowBenchmarkTool();
ImGui::End();
return;
}
ImGui::SetNextWindowPos(ImVec2(50, 50), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(600, 750), ImGuiCond_FirstUseEver);
ImGui::Begin("ImPlot Demo", p_open, ImGuiWindowFlags_MenuBar);
if (ImGui::BeginMenuBar()) {
if (ImGui::BeginMenu("Tools")) {
ImGui::MenuItem("Metrics (ImGui)", NULL, &show_imgui_metrics);
ImGui::MenuItem("Metrics (ImPlot)", NULL, &show_implot_metrics);
ImGui::MenuItem("Style Editor (ImGui)", NULL, &show_imgui_style_editor);
ImGui::MenuItem("Style Editor (ImPlot)", NULL, &show_implot_style_editor);
ImGui::MenuItem("Benchmark", NULL, &show_implot_benchmark);
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
//-------------------------------------------------------------------------
ImGui::Text("ImPlot says hello. (%s)", IMPLOT_VERSION);
ImGui::Spacing();
if (ImGui::CollapsingHeader("Help")) {
ImGui::Text("ABOUT THIS DEMO:");
ImGui::BulletText("Sections below are demonstrating many aspects of the library.");
ImGui::BulletText("The \"Tools\" menu above gives access to: Style Editors (ImPlot/ImGui)\n"
"and Metrics (general purpose Dear ImGui debugging tool).");
ImGui::Separator();
ImGui::Text("PROGRAMMER GUIDE:");
ImGui::BulletText("See the ShowDemoWindow() code in implot_demo.cpp. <- you are here!");
ImGui::BulletText("By default, anti-aliased lines are turned OFF.");
ImGui::Indent();
ImGui::BulletText("Software AA can be enabled globally with ImPlotStyle.AntiAliasedLines.");
ImGui::BulletText("Software AA can be enabled per plot with ImPlotFlags_AntiAliased.");
ImGui::BulletText("AA for plots can be toggled from the plot's context menu.");
ImGui::BulletText("If permitable, you are better off using hardware AA (e.g. MSAA).");
ImGui::Unindent();
ImGui::BulletText("If you see visual artifacts, do one of the following:");
ImGui::Indent();
ImGui::BulletText("Handle ImGuiBackendFlags_RendererHasVtxOffset for 16-bit indices in your backend.");
ImGui::BulletText("Or, enable 32-bit indices in imconfig.h.");
ImGui::BulletText("Your current configuration is:");
ImGui::Indent();
ImGui::BulletText("ImDrawIdx: %d-bit", (int)(sizeof(ImDrawIdx) * 8));
ImGui::BulletText("ImGuiBackendFlags_RendererHasVtxOffset: %s", (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ? "True" : "False");
ImGui::Unindent();
ImGui::Unindent();
ImGui::Separator();
ImGui::Text("USER GUIDE:");
ShowUserGuide();
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Configuration")) {
ImGui::ShowFontSelector("Font");
ImGui::ShowStyleSelector("ImGui Style");
ImPlot::ShowStyleSelector("ImPlot Style");
ImPlot::ShowColormapSelector("ImPlot Colormap");
float indent = ImGui::CalcItemWidth() - ImGui::GetFrameHeight();
ImGui::Indent(ImGui::CalcItemWidth() - ImGui::GetFrameHeight());
ImGui::Checkbox("Anti-Aliased Lines", &ImPlot::GetStyle().AntiAliasedLines);
ImGui::Unindent(indent);
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Line Plots")) {
static float xs1[1001], ys1[1001];
for (int i = 0; i < 1001; ++i) {
xs1[i] = i * 0.001f;
ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)DEMO_TIME / 10));
}
static double xs2[11], ys2[11];
for (int i = 0; i < 11; ++i) {
xs2[i] = i * 0.1f;
ys2[i] = xs2[i] * xs2[i];
}
ImGui::BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help).");
if (ImPlot::BeginPlot("Line Plot", "x", "f(x)")) {
ImPlot::PlotLine("sin(x)", xs1, ys1, 1001);
ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
ImPlot::PlotLine("x^2", xs2, ys2, 11);
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Filled Line Plots")) {
static double xs1[101], ys1[101], ys2[101], ys3[101];
srand(0);
for (int i = 0; i < 101; ++i) {
xs1[i] = (float)i;
ys1[i] = RandomRange(400.0,450.0);
ys2[i] = RandomRange(275.0,350.0);
ys3[i] = RandomRange(150.0,225.0);
}
static bool show_lines = true;
static bool show_fills = true;
static float fill_ref = 0;
static int shade_mode = 0;
ImGui::Checkbox("Lines",&show_lines); ImGui::SameLine();
ImGui::Checkbox("Fills",&show_fills);
if (show_fills) {
ImGui::SameLine();
if (ImGui::RadioButton("To -INF",shade_mode == 0))
shade_mode = 0;
ImGui::SameLine();
if (ImGui::RadioButton("To +INF",shade_mode == 1))
shade_mode = 1;
ImGui::SameLine();
if (ImGui::RadioButton("To Ref",shade_mode == 2))
shade_mode = 2;
if (shade_mode == 2) {
ImGui::SameLine();
ImGui::SetNextItemWidth(100);
ImGui::DragFloat("##Ref",&fill_ref, 1, -100, 500);
}
}
ImPlot::SetNextPlotLimits(0,100,0,500);
if (ImPlot::BeginPlot("Stock Prices", "Days", "Price")) {
if (show_fills) {
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref);
ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref);
ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref);
ImPlot::PopStyleVar();
}
if (show_lines) {
ImPlot::PlotLine("Stock 1", xs1, ys1, 101);
ImPlot::PlotLine("Stock 2", xs1, ys2, 101);
ImPlot::PlotLine("Stock 3", xs1, ys3, 101);
}
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Shaded Plots##")) {
static float xs[1001], ys[1001], ys1[1001], ys2[1001], ys3[1001], ys4[1001];
srand(0);
for (int i = 0; i < 1001; ++i) {
xs[i] = i * 0.001f;
ys[i] = 0.25f + 0.25f * sinf(25 * xs[i]) * sinf(5 * xs[i]) + RandomRange(-0.01f, 0.01f);
ys1[i] = ys[i] + RandomRange(0.1f, 0.12f);
ys2[i] = ys[i] - RandomRange(0.1f, 0.12f);
ys3[i] = 0.75f + 0.2f * sinf(25 * xs[i]);
ys4[i] = 0.75f + 0.1f * cosf(25 * xs[i]);
}
static float alpha = 0.25f;
ImGui::DragFloat("Alpha",&alpha,0.01f,0,1);
if (ImPlot::BeginPlot("Shaded Plots", "X-Axis", "Y-Axis")) {
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha);
ImPlot::PlotShaded("Uncertain Data",xs,ys1,ys2,1001);
ImPlot::PlotLine("Uncertain Data", xs, ys, 1001);
ImPlot::PlotShaded("Overlapping",xs,ys3,ys4,1001);
ImPlot::PlotLine("Overlapping",xs,ys3,1001);
ImPlot::PlotLine("Overlapping",xs,ys4,1001);
ImPlot::PopStyleVar();
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Scatter Plots")) {
srand(0);
static float xs1[100], ys1[100];
for (int i = 0; i < 100; ++i) {
xs1[i] = i * 0.01f;
ys1[i] = xs1[i] + 0.1f * ((float)rand() / (float)RAND_MAX);
}
static float xs2[50], ys2[50];
for (int i = 0; i < 50; i++) {
xs2[i] = 0.25f + 0.2f * ((float)rand() / (float)RAND_MAX);
ys2[i] = 0.75f + 0.2f * ((float)rand() / (float)RAND_MAX);
}
if (ImPlot::BeginPlot("Scatter Plot", NULL, NULL)) {
ImPlot::PlotScatter("Data 1", xs1, ys1, 100);
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, ImVec4(0,1,0,0.5f), IMPLOT_AUTO, ImVec4(0,1,0,1));
ImPlot::PlotScatter("Data 2", xs2, ys2, 50);
ImPlot::PopStyleVar();
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Stairstep Plots")) {
static float ys1[101], ys2[101];
for (int i = 0; i < 101; ++i) {
ys1[i] = 0.5f + 0.4f * sinf(50 * i * 0.01f);
ys2[i] = 0.5f + 0.2f * sinf(25 * i * 0.01f);
}
if (ImPlot::BeginPlot("Stairstep Plot", "x", "f(x)")) {
ImPlot::PlotStairs("Signal 1", ys1, 101, 0.01f);
ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 2.0f);
ImPlot::PlotStairs("Signal 2", ys2, 101, 0.01f);
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Bar Plots")) {
static bool horz = false;
static ImS8 midtm[10] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90};
static ImS16 final[10] = {80, 62, 56, 99, 55, 78, 88, 78, 90, 100};
static ImS32 grade[10] = {80, 69, 52, 92, 72, 78, 75, 76, 89, 95};
static const char* labels[] = {"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10"};
static const double positions[] = {0,1,2,3,4,5,6,7,8,9};
ImGui::Checkbox("Horizontal",&horz);
if (horz) {
ImPlot::SetNextPlotLimits(0, 110, -0.5, 9.5, ImGuiCond_Always);
ImPlot::SetNextPlotTicksY(positions, 10, labels);
}
else {
ImPlot::SetNextPlotLimits(-0.5, 9.5, 0, 110, ImGuiCond_Always);
ImPlot::SetNextPlotTicksX(positions, 10, labels);
}
if (ImPlot::BeginPlot("Bar Plot", horz ? "Score" : "Student", horz ? "Student" : "Score",
ImVec2(-1,0), 0, 0, horz ? ImPlotAxisFlags_Invert : 0))
{
if (horz) {
ImPlot::SetLegendLocation(ImPlotLocation_West, ImPlotOrientation_Vertical);
ImPlot::PlotBarsH("Midterm Exam", midtm, 10, 0.2, -0.2);
ImPlot::PlotBarsH("Final Exam", final, 10, 0.2, 0);
ImPlot::PlotBarsH("Course Grade", grade, 10, 0.2, 0.2);
}
else {
ImPlot::SetLegendLocation(ImPlotLocation_South, ImPlotOrientation_Horizontal);
ImPlot::PlotBars("Midterm Exam", midtm, 10, 0.2, -0.2);
ImPlot::PlotBars("Final Exam", final, 10, 0.2, 0);
ImPlot::PlotBars("Course Grade", grade, 10, 0.2, 0.2);
}
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Error Bars")) {
static float xs[5] = {1,2,3,4,5};
static float bar[5] = {1,2,5,3,4};
static float lin1[5] = {8,8,9,7,8};
static float lin2[5] = {6,7,6,9,6};
static float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f};
static float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f};
static float err3[5] = {0.09f, 0.14f, 0.09f, 0.12f, 0.16f};
static float err4[5] = {0.02f, 0.08f, 0.15f, 0.05f, 0.2f};
ImPlot::SetNextPlotLimits(0, 6, 0, 10);
if (ImPlot::BeginPlot("##ErrorBars",NULL,NULL)) {
ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f);
ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5);
ImPlot::SetNextErrorBarStyle(ImPlot::GetColormapColor(1), 0);
ImPlot::PlotErrorBars("Line", xs, lin1, err1, err2, 5);
ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
ImPlot::PlotLine("Line", xs, lin1, 5);
ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(2));
ImPlot::PlotErrorBars("Scatter", xs, lin2, err2, 5);
ImPlot::PlotErrorBarsH("Scatter", xs, lin2, err3, err4, 5);
ImPlot::PopStyleColor();
ImPlot::PlotScatter("Scatter", xs, lin2, 5);
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Stem Plots##")) {
static double xs[51], ys1[51], ys2[51];
for (int i = 0; i < 51; ++i) {
xs[i] = i * 0.02;
ys1[i] = 1.0 + 0.5 * sin(25*xs[i])*cos(2*xs[i]);
ys2[i] = 0.5 + 0.25 * sin(10*xs[i]) * sin(xs[i]);
}
ImPlot::SetNextPlotLimits(0,1,0,1.6);
if (ImPlot::BeginPlot("Stem Plots")) {
ImPlot::PlotStems("Stems 1",xs,ys1,51);
ImPlot::SetNextLineStyle(ImVec4(1,0.5f,0,0.75f));
ImPlot::SetNextMarkerStyle(ImPlotMarker_Square,5,ImVec4(1,0.5f,0,0.25f));
ImPlot::PlotStems("Stems 2", xs, ys2,51);
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Infinite Lines")) {
static double vals[] = {0.25, 0.5, 0.75};
if (ImPlot::BeginPlot("##Infinite",0,0,ImVec2(-1,0),0,ImPlotAxisFlags_NoInitialFit,ImPlotAxisFlags_NoInitialFit)) {
ImPlot::PlotVLines("VLines",vals,3);
ImPlot::PlotHLines("HLines",vals,3);
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Pie Charts")) {
static const char* labels1[] = {"Frogs","Hogs","Dogs","Logs"};
static float data1[] = {0.15f, 0.30f, 0.2f, 0.05f};
static bool normalize = false;
ImGui::SetNextItemWidth(250);
ImGui::DragFloat4("Values", data1, 0.01f, 0, 1);
if ((data1[0] + data1[1] + data1[2] + data1[3]) < 1) {
ImGui::SameLine();
ImGui::Checkbox("Normalize", &normalize);
}
ImPlot::SetNextPlotLimits(0,1,0,1,ImGuiCond_Always);
if (ImPlot::BeginPlot("##Pie1", NULL, NULL, ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMousePos, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) {
ImPlot::PlotPieChart(labels1, data1, 4, 0.5, 0.5, 0.4, normalize, "%.2f");
ImPlot::EndPlot();
}
ImGui::SameLine();
static const char* labels2[] = {"A","B","C","D","E"};
static int data2[] = {1,1,2,3,5};
ImPlot::PushColormap(ImPlotColormap_Pastel);
ImPlot::SetNextPlotLimits(0,1,0,1,ImGuiCond_Always);
if (ImPlot::BeginPlot("##Pie2", NULL, NULL, ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMousePos, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) {
ImPlot::PlotPieChart(labels2, data2, 5, 0.5, 0.5, 0.4, true, "%.0f", 180);
ImPlot::EndPlot();
}
ImPlot::PopColormap();
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Heatmaps")) {
static float values1[7][7] = {{0.8f, 2.4f, 2.5f, 3.9f, 0.0f, 4.0f, 0.0f},
{2.4f, 0.0f, 4.0f, 1.0f, 2.7f, 0.0f, 0.0f},
{1.1f, 2.4f, 0.8f, 4.3f, 1.9f, 4.4f, 0.0f},
{0.6f, 0.0f, 0.3f, 0.0f, 3.1f, 0.0f, 0.0f},
{0.7f, 1.7f, 0.6f, 2.6f, 2.2f, 6.2f, 0.0f},
{1.3f, 1.2f, 0.0f, 0.0f, 0.0f, 3.2f, 5.1f},
{0.1f, 2.0f, 0.0f, 1.4f, 0.0f, 1.9f, 6.3f}};
static float scale_min = 0;
static float scale_max = 6.3f;
static const char* xlabels[] = {"C1","C2","C3","C4","C5","C6","C7"};
static const char* ylabels[] = {"R1","R2","R3","R4","R5","R6","R7"};
static ImPlotColormap map = ImPlotColormap_Viridis;
if (ImPlot::ColormapButton(ImPlot::GetColormapName(map),ImVec2(225,0),map)) {
map = (map + 1) % ImPlot::GetColormapCount();
// We bust the color cache of our plots so that item colors will
// resample the new colormap in the event that they have already
// been created. See documentation in implot.h.
BustColorCache("##Heatmap1");
BustColorCache("##Heatmap2");
}
ImGui::SameLine();
ImGui::LabelText("##Colormap Index", "%s", "Change Colormap");
ImGui::SetNextItemWidth(225);
ImGui::DragFloatRange2("Min / Max",&scale_min, &scale_max, 0.01f, -20, 20);
static ImPlotAxisFlags axes_flags = ImPlotAxisFlags_Lock | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoTickMarks;
ImPlot::PushColormap(map);
SetNextPlotTicksX(0 + 1.0/14.0, 1 - 1.0/14.0, 7, xlabels);
SetNextPlotTicksY(1 - 1.0/14.0, 0 + 1.0/14.0, 7, ylabels);
if (ImPlot::BeginPlot("##Heatmap1",NULL,NULL,ImVec2(225,225),ImPlotFlags_NoLegend|ImPlotFlags_NoMousePos,axes_flags,axes_flags)) {
ImPlot::PlotHeatmap("heat",values1[0],7,7,scale_min,scale_max);
ImPlot::EndPlot();
}
ImGui::SameLine();
ImPlot::ColormapScale("##HeatScale",scale_min, scale_max, ImVec2(60,225));
ImGui::SameLine();
const int size = 200;
static double values2[size*size];
srand((unsigned int)(DEMO_TIME*1000000));
for (int i = 0; i < size*size; ++i)
values2[i] = RandomRange(0.0,1.0);
ImPlot::SetNextPlotLimits(-1,1,-1,1);
if (ImPlot::BeginPlot("##Heatmap2",NULL,NULL,ImVec2(225,225),0,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations)) {
ImPlot::PlotHeatmap("heat1",values2,size,size,0,1,NULL);
ImPlot::PlotHeatmap("heat2",values2,size,size,0,1,NULL, ImPlotPoint(-1,-1), ImPlotPoint(0,0));
ImPlot::EndPlot();
}
ImPlot::PopColormap();
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Histograms")) {
static int bins = 50;
static bool cumulative = false;
static bool density = true;
static bool outliers = true;
static double mu = 5;
static double sigma = 2;
ImGui::SetNextItemWidth(200);
if (ImGui::RadioButton("Sqrt",bins==ImPlotBin_Sqrt)) { bins = ImPlotBin_Sqrt; } ImGui::SameLine();
if (ImGui::RadioButton("Sturges",bins==ImPlotBin_Sturges)) { bins = ImPlotBin_Sturges; } ImGui::SameLine();
if (ImGui::RadioButton("Rice",bins==ImPlotBin_Rice)) { bins = ImPlotBin_Rice; } ImGui::SameLine();
if (ImGui::RadioButton("Scott",bins==ImPlotBin_Scott)) { bins = ImPlotBin_Scott; } ImGui::SameLine();
if (ImGui::RadioButton("N Bins",bins>=0)) bins = 50;
if (bins>=0) {
ImGui::SameLine();
ImGui::SetNextItemWidth(200);
ImGui::SliderInt("##Bins", &bins, 1, 100);
}
if (ImGui::Checkbox("Density", &density))
ImPlot::FitNextPlotAxes();
ImGui::SameLine();
if (ImGui::Checkbox("Cumulative", &cumulative))
ImPlot::FitNextPlotAxes();
ImGui::SameLine();
static bool range = false;
ImGui::Checkbox("Range", &range);
static float rmin = -3;
static float rmax = 13;
if (range) {
ImGui::SameLine();
ImGui::SetNextItemWidth(200);
ImGui::DragFloat2("##Range",&rmin,0.1f,-3,13);
ImGui::SameLine();
ImGui::Checkbox("Outliers",&outliers);
}
static NormalDistribution<10000> dist(mu, sigma);
static double x[100];
static double y[100];
if (density) {
for (int i = 0; i < 100; ++i) {
x[i] = -3 + 16 * (double)i/99.0;
y[i] = exp( - (x[i]-mu)*(x[i]-mu) / (2*sigma*sigma)) / (sigma * sqrt(2*3.141592653589793238));
}
if (cumulative) {
for (int i = 1; i < 100; ++i)
y[i] += y[i-1];
for (int i = 0; i < 100; ++i)
y[i] /= y[99];
}
}
if (ImPlot::BeginPlot("##Histograms")) {
ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.5f);
ImPlot::PlotHistogram("Empirical", dist.Data, 10000, bins, cumulative, density, range ? ImPlotRange(rmin,rmax) : ImPlotRange(), outliers);
if (density && outliers)
ImPlot::PlotLine("Theoretical",x,y,100);
ImPlot::EndPlot();
}
static int count = 500000;
static int xybins[2] = {200,200};
static bool density2 = false;
ImGui::SliderInt("Count",&count,100,500000);
ImGui::SliderInt2("Bins",xybins,1,500);
ImGui::SameLine();
ImGui::Checkbox("Density##2",&density2);
static NormalDistribution<500000> dist1(1, 2);
static NormalDistribution<500000> dist2(1, 1);
double max_count = 0;
ImPlotAxisFlags flags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Foreground;
ImPlot::PushColormap("Hot");
if (ImPlot::BeginPlot("##Hist2D",0,0,ImVec2(ImGui::GetContentRegionAvail().x-100-ImGui::GetStyle().ItemSpacing.x,0),0,flags,flags)) {
max_count = ImPlot::PlotHistogram2D("Hist2D",dist1.Data,dist2.Data,count,xybins[0],xybins[1],density2,ImPlotLimits(-6,6,-6,6));
ImPlot::EndPlot();
}
ImGui::SameLine();
ImPlot::ColormapScale(density2 ? "Density" : "Count",0,max_count,ImVec2(100,0));
ImPlot::PopColormap();
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Digital Plots")) {
ImGui::BulletText("Digital plots do not respond to Y drag and zoom, so that");
ImGui::Indent();
ImGui::Text("you can drag analog plots over the rising/falling digital edge.");
ImGui::Unindent();
static bool paused = false;
static ScrollingBuffer dataDigital[2];
static ScrollingBuffer dataAnalog[2];
static bool showDigital[2] = {true, false};
static bool showAnalog[2] = {true, false};
char label[32];
ImGui::Checkbox("digital_0", &showDigital[0]); ImGui::SameLine();
ImGui::Checkbox("digital_1", &showDigital[1]); ImGui::SameLine();
ImGui::Checkbox("analog_0", &showAnalog[0]); ImGui::SameLine();
ImGui::Checkbox("analog_1", &showAnalog[1]);
static float t = 0;
if (!paused) {
t += ImGui::GetIO().DeltaTime;
//digital signal values
if (showDigital[0])
dataDigital[0].AddPoint(t, sinf(2*t) > 0.45);
if (showDigital[1])
dataDigital[1].AddPoint(t, sinf(2*t) < 0.45);
//Analog signal values
if (showAnalog[0])
dataAnalog[0].AddPoint(t, sinf(2*t));
if (showAnalog[1])
dataAnalog[1].AddPoint(t, cosf(2*t));
}
ImPlot::SetNextPlotLimitsY(-1, 1);
ImPlot::SetNextPlotLimitsX(t - 10.0, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
if (ImPlot::BeginPlot("##Digital")) {
for (int i = 0; i < 2; ++i) {
if (showDigital[i] && dataDigital[i].Data.size() > 0) {
sprintf(label, "digital_%d", i);
ImPlot::PlotDigital(label, &dataDigital[i].Data[0].x, &dataDigital[i].Data[0].y, dataDigital[i].Data.size(), dataDigital[i].Offset, 2 * sizeof(float));
}
}
for (int i = 0; i < 2; ++i) {
if (showAnalog[i]) {
sprintf(label, "analog_%d", i);
if (dataAnalog[i].Data.size() > 0)
ImPlot::PlotLine(label, &dataAnalog[i].Data[0].x, &dataAnalog[i].Data[0].y, dataAnalog[i].Data.size(), dataAnalog[i].Offset, 2 * sizeof(float));
}
}
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Images")) {
ImGui::BulletText("Below we are displaying the font texture, which is the only texture we have\naccess to in this demo.");
ImGui::BulletText("Use the 'ImTextureID' type as storage to pass pointers or identifiers to your\nown texture data.");
ImGui::BulletText("See ImGui Wiki page 'Image Loading and Displaying Examples'.");
static ImVec2 bmin(0,0);
static ImVec2 bmax(1,1);
static ImVec2 uv0(0,0);
static ImVec2 uv1(1,1);
static ImVec4 tint(1,1,1,1);
ImGui::SliderFloat2("Min", &bmin.x, -2, 2, "%.1f");
ImGui::SliderFloat2("Max", &bmax.x, -2, 2, "%.1f");
ImGui::SliderFloat2("UV0", &uv0.x, -2, 2, "%.1f");
ImGui::SliderFloat2("UV1", &uv1.x, -2, 2, "%.1f");
ImGui::ColorEdit4("Tint",&tint.x);
if (ImPlot::BeginPlot("##image")) {
ImPlot::PlotImage("my image",ImGui::GetIO().Fonts->TexID, bmin, bmax, uv0, uv1, tint);
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Realtime Plots")) {
ImGui::BulletText("Move your mouse to change the data!");
ImGui::BulletText("This example assumes 60 FPS. Higher FPS requires larger buffer size.");
static ScrollingBuffer sdata1, sdata2;
static RollingBuffer rdata1, rdata2;
ImVec2 mouse = ImGui::GetMousePos();
static float t = 0;
t += ImGui::GetIO().DeltaTime;
sdata1.AddPoint(t, mouse.x * 0.0005f);
rdata1.AddPoint(t, mouse.x * 0.0005f);
sdata2.AddPoint(t, mouse.y * 0.0005f);
rdata2.AddPoint(t, mouse.y * 0.0005f);
static float history = 10.0f;
ImGui::SliderFloat("History",&history,1,30,"%.1f s");
rdata1.Span = history;
rdata2.Span = history;
static ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels;
ImPlot::SetNextPlotLimitsX(t - history, t, ImGuiCond_Always);
ImPlot::SetNextPlotLimitsY(0,1);
if (ImPlot::BeginPlot("##Scrolling", NULL, NULL, ImVec2(-1,150), 0, flags, flags)) {
ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL,0.5f);
ImPlot::PlotShaded("Mouse X", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), -INFINITY, sdata1.Offset, 2 * sizeof(float));
ImPlot::PlotLine("Mouse Y", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), sdata2.Offset, 2*sizeof(float));
ImPlot::EndPlot();
}
ImPlot::SetNextPlotLimitsX(0, history, ImGuiCond_Always);
ImPlot::SetNextPlotLimitsY(0,1);
if (ImPlot::BeginPlot("##Rolling", NULL, NULL, ImVec2(-1,150), 0, flags, flags)) {
ImPlot::PlotLine("Mouse X", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 2 * sizeof(float));
ImPlot::PlotLine("Mouse Y", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 2 * sizeof(float));
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Markers and Text")) {
static float mk_size = ImPlot::GetStyle().MarkerSize;
static float mk_weight = ImPlot::GetStyle().MarkerWeight;
ImGui::DragFloat("Marker Size",&mk_size,0.1f,2.0f,10.0f,"%.2f px");
ImGui::DragFloat("Marker Weight", &mk_weight,0.05f,0.5f,3.0f,"%.2f px");
ImPlot::SetNextPlotLimits(0, 10, 0, 12);
if (ImPlot::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,0), ImPlotFlags_CanvasOnly, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) {
ImS8 xs[2] = {1,4};
ImS8 ys[2] = {10,11};
// filled markers
for (int m = 0; m < ImPlotMarker_COUNT; ++m) {
ImGui::PushID(m);
ImPlot::SetNextMarkerStyle(m, mk_size, IMPLOT_AUTO_COL, mk_weight);
ImPlot::PlotLine("##Filled", xs, ys, 2);
ImGui::PopID();
ys[0]--; ys[1]--;
}
xs[0] = 6; xs[1] = 9; ys[0] = 10; ys[1] = 11;
// open markers
for (int m = 0; m < ImPlotMarker_COUNT; ++m) {
ImGui::PushID(m);
ImPlot::SetNextMarkerStyle(m, mk_size, ImVec4(0,0,0,0), mk_weight);
ImPlot::PlotLine("##Open", xs, ys, 2);
ImGui::PopID();
ys[0]--; ys[1]--;
}
ImPlot::PlotText("Filled Markers", 2.5f, 6.0f);
ImPlot::PlotText("Open Markers", 7.5f, 6.0f);
ImPlot::PushStyleColor(ImPlotCol_InlayText, ImVec4(1,0,1,1));
ImPlot::PlotText("Vertical Text", 5.0f, 6.0f, true);
ImPlot::PopStyleColor();
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Log Scale")) {
static double xs[1001], ys1[1001], ys2[1001], ys3[1001];
for (int i = 0; i < 1001; ++i) {
xs[i] = i*0.1f;
ys1[i] = sin(xs[i]) + 1;
ys2[i] = log(xs[i]);
ys3[i] = pow(10.0, xs[i]);
}
ImGui::BulletText("Open the plot context menu (right click) to change scales.");
ImPlot::SetNextPlotLimits(0.1, 100, 0, 10);
if (ImPlot::BeginPlot("Log Plot", NULL, NULL, ImVec2(-1,0), 0, ImPlotAxisFlags_LogScale )) {
ImPlot::PlotLine("f(x) = x", xs, xs, 1001);
ImPlot::PlotLine("f(x) = sin(x)+1", xs, ys1, 1001);
ImPlot::PlotLine("f(x) = log(x)", xs, ys2, 1001);
ImPlot::PlotLine("f(x) = 10^x", xs, ys3, 21);
ImPlot::EndPlot();
}
}
if (ImGui::CollapsingHeader("Time Formatted Axes")) {
static double t_min = 1609459200; // 01/01/2021 @ 12:00:00am (UTC)
static double t_max = 1640995200; // 01/01/2022 @ 12:00:00am (UTC)
ImGui::BulletText("When ImPlotAxisFlags_Time is enabled on the X-Axis, values are interpreted as\n"
"UNIX timestamps in seconds and axis labels are formated as date/time.");
ImGui::BulletText("By default, labels are in UTC time but can be set to use local time instead.");
ImGui::Checkbox("Local Time",&ImPlot::GetStyle().UseLocalTime);
ImGui::SameLine();
ImGui::Checkbox("ISO 8601",&ImPlot::GetStyle().UseISO8601);
ImGui::SameLine();
ImGui::Checkbox("24 Hour Clock",&ImPlot::GetStyle().Use24HourClock);
static HugeTimeData* data = NULL;
if (data == NULL) {
ImGui::SameLine();
if (ImGui::Button("Generate Huge Data (~500MB!)")) {
static HugeTimeData sdata(t_min);
data = &sdata;
}
}
ImPlot::SetNextPlotLimits(t_min,t_max,0,1);
if (ImPlot::BeginPlot("##Time", NULL, NULL, ImVec2(-1,0), 0, ImPlotAxisFlags_Time)) {
if (data != NULL) {
// downsample our data
int downsample = (int)ImPlot::GetPlotLimits().X.Size() / 1000 + 1;
int start = (int)(ImPlot::GetPlotLimits().X.Min - t_min);
start = start < 0 ? 0 : start > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : start;
int end = (int)(ImPlot::GetPlotLimits().X.Max - t_min) + 1000;
end = end < 0 ? 0 : end > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : end;
int size = (end - start)/downsample;
// plot it
ImPlot::PlotLine("Time Series", &data->Ts[start], &data->Ys[start], size, 0, sizeof(double)*downsample);
}
// plot time now
double t_now = (double)time(0);
double y_now = HugeTimeData::GetY(t_now);
ImPlot::PlotScatter("Now",&t_now,&y_now,1);
ImPlot::Annotate(t_now,y_now,ImVec2(10,10),ImPlot::GetLastItemColor(),"Now");
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Multiple Y-Axes")) {
static float xs[1001], xs2[1001], ys1[1001], ys2[1001], ys3[1001];
for (int i = 0; i < 1001; ++i) {
xs[i] = (i*0.1f);
ys1[i] = sinf(xs[i]) * 3 + 1;
ys2[i] = cosf(xs[i]) * 0.2f + 0.5f;
ys3[i] = sinf(xs[i]+0.5f) * 100 + 200;
xs2[i] = xs[i] + 10.0f;
}
static bool y2_axis = true;
static bool y3_axis = true;
ImGui::Checkbox("Y-Axis 2", &y2_axis);
ImGui::SameLine();
ImGui::Checkbox("Y-Axis 3", &y3_axis);
ImGui::SameLine();
// you can fit axes programatically
ImGui::SameLine(); if (ImGui::Button("Fit X")) ImPlot::FitNextPlotAxes(true, false, false, false);
ImGui::SameLine(); if (ImGui::Button("Fit Y")) ImPlot::FitNextPlotAxes(false, true, false, false);
ImGui::SameLine(); if (ImGui::Button("Fit Y2")) ImPlot::FitNextPlotAxes(false, false, true, false);
ImGui::SameLine(); if (ImGui::Button("Fit Y3")) ImPlot::FitNextPlotAxes(false, false, false, true);
ImPlot::SetNextPlotLimits(0.1, 100, 0, 10);
ImPlot::SetNextPlotLimitsY(0, 1, ImGuiCond_Once, 1);
ImPlot::SetNextPlotLimitsY(0, 300, ImGuiCond_Once, 2);
if (ImPlot::BeginPlot("Multi-Axis Plot", NULL, "Y-Axis 1", ImVec2(-1,0),
(y2_axis ? ImPlotFlags_YAxis2 : 0) |
(y3_axis ? ImPlotFlags_YAxis3 : 0),
ImPlotAxisFlags_None, ImPlotAxisFlags_None, ImPlotAxisFlags_NoGridLines, ImPlotAxisFlags_NoGridLines,
"Y-Axis 2", "Y-Axis 3")) {
ImPlot::PlotLine("f(x) = x", xs, xs, 1001);
ImPlot::PlotLine("f(x) = sin(x)*3+1", xs, ys1, 1001);
if (y2_axis) {
ImPlot::SetPlotYAxis(ImPlotYAxis_2);
ImPlot::PlotLine("f(x) = cos(x)*.2+.5 (Y2)", xs, ys2, 1001);
}
if (y3_axis) {
ImPlot::SetPlotYAxis(ImPlotYAxis_3);
ImPlot::PlotLine("f(x) = sin(x+.5)*100+200 (Y3)", xs2, ys3, 1001);
}
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Linked Axes")) {
static double xmin = 0, xmax = 1, ymin = 0, ymax = 1;
static bool linkx = true, linky = true;
int data[2] = {0,1};
ImGui::Checkbox("Link X", &linkx);
ImGui::SameLine();
ImGui::Checkbox("Link Y", &linky);
ImPlot::LinkNextPlotLimits(linkx ? &xmin : NULL , linkx ? &xmax : NULL, linky ? &ymin : NULL, linky ? &ymax : NULL);
if (ImPlot::BeginPlot("Plot A")) {
ImPlot::PlotLine("Line",data,2);
ImPlot::EndPlot();
}
ImPlot::LinkNextPlotLimits(linkx ? &xmin : NULL , linkx ? &xmax : NULL, linky ? &ymin : NULL, linky ? &ymax : NULL);
if (ImPlot::BeginPlot("Plot B")) {
ImPlot::PlotLine("Line",data,2);
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Equal Axes")) {
static double xs[1000], ys[1000];
for (int i = 0; i < 1000; ++i) {
double angle = i * 2 * PI / 999.0;
xs[i] = cos(angle); ys[i] = sin(angle);
}
if (ImPlot::BeginPlot("",0,0,ImVec2(-1,0),ImPlotFlags_Equal)) {
ImPlot::PlotLine("Circle",xs,ys,1000);
ImPlot::EndPlot();
}
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Auto-Fitting Data")) {
ImGui::BulletText("The Y-axis has been configured to auto-fit to only the data visible in X-axis range.");
ImGui::BulletText("Zoom and pan the X-axis. Disable Stems to see a difference in fit.");
ImGui::BulletText("If ImPlotAxisFlags_RangeFit is disabled, the axis will fit ALL data.");
static ImPlotAxisFlags xflags = ImPlotAxisFlags_None;
static ImPlotAxisFlags yflags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit;
ImGui::TextUnformatted("X: "); ImGui::SameLine();
ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine();
ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_RangeFit);
ImGui::TextUnformatted("Y: "); ImGui::SameLine();
ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine();
ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_RangeFit);
static double data[101];
srand(0);
for (int i = 0; i < 101; ++i)
data[i] = 1 + sin(i/10.0f);
if (ImPlot::BeginPlot("##DataFitting","X","Y",ImVec2(-1,0),0,xflags,yflags)) {
ImPlot::PlotLine("Line",data,101);
ImPlot::PlotStems("Stems",data,101);
ImPlot::EndPlot();
};
}
//-------------------------------------------------------------------------
if (ImGui::CollapsingHeader("Querying")) {
static ImVector<ImPlotPoint> data;
static ImPlotLimits range, query, select;
ImGui::BulletText("Ctrl + click in the plot area to draw points.");
ImGui::BulletText("Middle click (or Ctrl + right click) and drag to create a query rect.");
ImGui::Indent();
ImGui::BulletText("Hold Alt to expand query horizontally.");
ImGui::BulletText("Hold Shift to expand query vertically.");
ImGui::BulletText("The query rect can be dragged after it's created.");
ImGui::Unindent();
if (ImPlot::BeginPlot("##Drawing", NULL, NULL, ImVec2(-1,0), ImPlotFlags_Query, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) {
if (ImPlot::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl) {
ImPlotPoint pt = ImPlot::GetPlotMousePos();
data.push_back(pt);