The open source OpenXR runtime
1// MIT License 2 3// Copyright (c) 2023 Evan Pezent 4 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to deal 7// in the Software without restriction, including without limitation the rights 8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9// copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11 12// The above copyright notice and this permission notice shall be included in all 13// copies or substantial portions of the Software. 14 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21// SOFTWARE. 22 23// ImPlot v0.17 24 25// We define this so that the demo does not accidentally use deprecated API 26#ifndef IMPLOT_DISABLE_OBSOLETE_FUNCTIONS 27#define IMPLOT_DISABLE_OBSOLETE_FUNCTIONS 28#endif 29 30#include "implot.h" 31#include <math.h> 32#include <stdio.h> 33#include <stdlib.h> 34#include <time.h> 35 36#ifdef _MSC_VER 37#define sprintf sprintf_s 38#endif 39 40#ifndef PI 41#define PI 3.14159265358979323846 42#endif 43 44#define CHECKBOX_FLAG(flags, flag) ImGui::CheckboxFlags(#flag, (unsigned int*)&flags, flag) 45 46// Encapsulates examples for customizing ImPlot. 47namespace MyImPlot { 48 49// Example for Custom Data and Getters section. 50struct Vector2f { 51 Vector2f(float _x, float _y) { x = _x; y = _y; } 52 float x, y; 53}; 54 55// Example for Custom Data and Getters section. 56struct WaveData { 57 double X, Amp, Freq, Offset; 58 WaveData(double x, double amp, double freq, double offset) { X = x; Amp = amp; Freq = freq; Offset = offset; } 59}; 60ImPlotPoint SineWave(int idx, void* wave_data); 61ImPlotPoint SawWave(int idx, void* wave_data); 62ImPlotPoint Spiral(int idx, void* wave_data); 63 64// Example for Tables section. 65void Sparkline(const char* id, const float* values, int count, float min_v, float max_v, int offset, const ImVec4& col, const ImVec2& size); 66 67// Example for Custom Plotters and Tooltips section. 68void 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)); 69 70// Example for Custom Styles section. 71void StyleSeaborn(); 72 73} // namespace MyImPlot 74 75namespace ImPlot { 76 77template <typename T> 78inline T RandomRange(T min, T max) { 79 T scale = rand() / (T) RAND_MAX; 80 return min + scale * ( max - min ); 81} 82 83ImVec4 RandomColor() { 84 ImVec4 col; 85 col.x = RandomRange(0.0f,1.0f); 86 col.y = RandomRange(0.0f,1.0f); 87 col.z = RandomRange(0.0f,1.0f); 88 col.w = 1.0f; 89 return col; 90} 91 92double RandomGauss() { 93 static double V1, V2, S; 94 static int phase = 0; 95 double X; 96 if(phase == 0) { 97 do { 98 double U1 = (double)rand() / RAND_MAX; 99 double U2 = (double)rand() / RAND_MAX; 100 V1 = 2 * U1 - 1; 101 V2 = 2 * U2 - 1; 102 S = V1 * V1 + V2 * V2; 103 } while(S >= 1 || S == 0); 104 105 X = V1 * sqrt(-2 * log(S) / S); 106 } else 107 X = V2 * sqrt(-2 * log(S) / S); 108 phase = 1 - phase; 109 return X; 110} 111 112template <int N> 113struct NormalDistribution { 114 NormalDistribution(double mean, double sd) { 115 for (int i = 0; i < N; ++i) 116 Data[i] = RandomGauss()*sd + mean; 117 } 118 double Data[N]; 119}; 120 121// utility structure for realtime plot 122struct ScrollingBuffer { 123 int MaxSize; 124 int Offset; 125 ImVector<ImVec2> Data; 126 ScrollingBuffer(int max_size = 2000) { 127 MaxSize = max_size; 128 Offset = 0; 129 Data.reserve(MaxSize); 130 } 131 void AddPoint(float x, float y) { 132 if (Data.size() < MaxSize) 133 Data.push_back(ImVec2(x,y)); 134 else { 135 Data[Offset] = ImVec2(x,y); 136 Offset = (Offset + 1) % MaxSize; 137 } 138 } 139 void Erase() { 140 if (Data.size() > 0) { 141 Data.shrink(0); 142 Offset = 0; 143 } 144 } 145}; 146 147// utility structure for realtime plot 148struct RollingBuffer { 149 float Span; 150 ImVector<ImVec2> Data; 151 RollingBuffer() { 152 Span = 10.0f; 153 Data.reserve(2000); 154 } 155 void AddPoint(float x, float y) { 156 float xmod = fmodf(x, Span); 157 if (!Data.empty() && xmod < Data.back().x) 158 Data.shrink(0); 159 Data.push_back(ImVec2(xmod, y)); 160 } 161}; 162 163// Huge data used by Time Formatting example (~500 MB allocation!) 164struct HugeTimeData { 165 HugeTimeData(double min) { 166 Ts = new double[Size]; 167 Ys = new double[Size]; 168 for (int i = 0; i < Size; ++i) { 169 Ts[i] = min + i; 170 Ys[i] = GetY(Ts[i]); 171 } 172 } 173 ~HugeTimeData() { delete[] Ts; delete[] Ys; } 174 static double GetY(double t) { 175 return 0.5 + 0.25 * sin(t/86400/12) + 0.005 * sin(t/3600); 176 } 177 double* Ts; 178 double* Ys; 179 static const int Size = 60*60*24*366; 180}; 181 182//----------------------------------------------------------------------------- 183// [SECTION] Demo Functions 184//----------------------------------------------------------------------------- 185 186void Demo_Help() { 187 ImGui::Text("ABOUT THIS DEMO:"); 188 ImGui::BulletText("Sections below are demonstrating many aspects of the library."); 189 ImGui::BulletText("The \"Tools\" menu above gives access to: Style Editors (ImPlot/ImGui)\n" 190 "and Metrics (general purpose Dear ImGui debugging tool)."); 191 ImGui::Separator(); 192 ImGui::Text("PROGRAMMER GUIDE:"); 193 ImGui::BulletText("See the ShowDemoWindow() code in implot_demo.cpp. <- you are here!"); 194 ImGui::BulletText("If you see visual artifacts, do one of the following:"); 195 ImGui::Indent(); 196 ImGui::BulletText("Handle ImGuiBackendFlags_RendererHasVtxOffset for 16-bit indices in your backend."); 197 ImGui::BulletText("Or, enable 32-bit indices in imconfig.h."); 198 ImGui::BulletText("Your current configuration is:"); 199 ImGui::Indent(); 200 ImGui::BulletText("ImDrawIdx: %d-bit", (int)(sizeof(ImDrawIdx) * 8)); 201 ImGui::BulletText("ImGuiBackendFlags_RendererHasVtxOffset: %s", (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ? "True" : "False"); 202 ImGui::Unindent(); 203 ImGui::Unindent(); 204 ImGui::Separator(); 205 ImGui::Text("USER GUIDE:"); 206 ShowUserGuide(); 207} 208 209//----------------------------------------------------------------------------- 210 211void ButtonSelector(const char* label, ImGuiMouseButton* b) { 212 ImGui::PushID(label); 213 if (ImGui::RadioButton("LMB",*b == ImGuiMouseButton_Left)) 214 *b = ImGuiMouseButton_Left; 215 ImGui::SameLine(); 216 if (ImGui::RadioButton("RMB",*b == ImGuiMouseButton_Right)) 217 *b = ImGuiMouseButton_Right; 218 ImGui::SameLine(); 219 if (ImGui::RadioButton("MMB",*b == ImGuiMouseButton_Middle)) 220 *b = ImGuiMouseButton_Middle; 221 ImGui::PopID(); 222} 223 224void ModSelector(const char* label, int* k) { 225 ImGui::PushID(label); 226 ImGui::CheckboxFlags("Ctrl", (unsigned int*)k, ImGuiMod_Ctrl); ImGui::SameLine(); 227 ImGui::CheckboxFlags("Shift", (unsigned int*)k, ImGuiMod_Shift); ImGui::SameLine(); 228 ImGui::CheckboxFlags("Alt", (unsigned int*)k, ImGuiMod_Alt); ImGui::SameLine(); 229 ImGui::CheckboxFlags("Super", (unsigned int*)k, ImGuiMod_Super); 230 ImGui::PopID(); 231} 232 233void InputMapping(const char* label, ImGuiMouseButton* b, int* k) { 234 ImGui::LabelText("##","%s",label); 235 if (b != nullptr) { 236 ImGui::SameLine(100); 237 ButtonSelector(label,b); 238 } 239 if (k != nullptr) { 240 ImGui::SameLine(300); 241 ModSelector(label,k); 242 } 243} 244 245void ShowInputMapping() { 246 ImPlotInputMap& map = ImPlot::GetInputMap(); 247 InputMapping("Pan",&map.Pan,&map.PanMod); 248 InputMapping("Fit",&map.Fit,nullptr); 249 InputMapping("Select",&map.Select,&map.SelectMod); 250 InputMapping("SelectHorzMod",nullptr,&map.SelectHorzMod); 251 InputMapping("SelectVertMod",nullptr,&map.SelectVertMod); 252 InputMapping("SelectCancel",&map.SelectCancel,nullptr); 253 InputMapping("Menu",&map.Menu,nullptr); 254 InputMapping("OverrideMod",nullptr,&map.OverrideMod); 255 InputMapping("ZoomMod",nullptr,&map.ZoomMod); 256 ImGui::SliderFloat("ZoomRate",&map.ZoomRate,-1,1); 257} 258 259void Demo_Config() { 260 ImGui::ShowFontSelector("Font"); 261 ImGui::ShowStyleSelector("ImGui Style"); 262 ImPlot::ShowStyleSelector("ImPlot Style"); 263 ImPlot::ShowColormapSelector("ImPlot Colormap"); 264 ImPlot::ShowInputMapSelector("Input Map"); 265 ImGui::Separator(); 266 ImGui::Checkbox("Use Local Time", &ImPlot::GetStyle().UseLocalTime); 267 ImGui::Checkbox("Use ISO 8601", &ImPlot::GetStyle().UseISO8601); 268 ImGui::Checkbox("Use 24 Hour Clock", &ImPlot::GetStyle().Use24HourClock); 269 ImGui::Separator(); 270 if (ImPlot::BeginPlot("Preview")) { 271 static double now = (double)time(nullptr); 272 ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Time); 273 ImPlot::SetupAxisLimits(ImAxis_X1, now, now + 24*3600); 274 for (int i = 0; i < 10; ++i) { 275 double x[2] = {now, now + 24*3600}; 276 double y[2] = {0,i/9.0}; 277 ImGui::PushID(i); 278 ImPlot::PlotLine("##Line",x,y,2); 279 ImGui::PopID(); 280 } 281 ImPlot::EndPlot(); 282 } 283} 284 285//----------------------------------------------------------------------------- 286 287void Demo_LinePlots() { 288 static float xs1[1001], ys1[1001]; 289 for (int i = 0; i < 1001; ++i) { 290 xs1[i] = i * 0.001f; 291 ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)ImGui::GetTime() / 10)); 292 } 293 static double xs2[20], ys2[20]; 294 for (int i = 0; i < 20; ++i) { 295 xs2[i] = i * 1/19.0f; 296 ys2[i] = xs2[i] * xs2[i]; 297 } 298 if (ImPlot::BeginPlot("Line Plots")) { 299 ImPlot::SetupAxes("x","y"); 300 ImPlot::PlotLine("f(x)", xs1, ys1, 1001); 301 ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); 302 ImPlot::PlotLine("g(x)", xs2, ys2, 20,ImPlotLineFlags_Segments); 303 ImPlot::EndPlot(); 304 } 305} 306 307//----------------------------------------------------------------------------- 308 309void Demo_FilledLinePlots() { 310 static double xs1[101], ys1[101], ys2[101], ys3[101]; 311 srand(0); 312 for (int i = 0; i < 101; ++i) { 313 xs1[i] = (float)i; 314 ys1[i] = RandomRange(400.0,450.0); 315 ys2[i] = RandomRange(275.0,350.0); 316 ys3[i] = RandomRange(150.0,225.0); 317 } 318 static bool show_lines = true; 319 static bool show_fills = true; 320 static float fill_ref = 0; 321 static int shade_mode = 0; 322 static ImPlotShadedFlags flags = 0; 323 ImGui::Checkbox("Lines",&show_lines); ImGui::SameLine(); 324 ImGui::Checkbox("Fills",&show_fills); 325 if (show_fills) { 326 ImGui::SameLine(); 327 if (ImGui::RadioButton("To -INF",shade_mode == 0)) 328 shade_mode = 0; 329 ImGui::SameLine(); 330 if (ImGui::RadioButton("To +INF",shade_mode == 1)) 331 shade_mode = 1; 332 ImGui::SameLine(); 333 if (ImGui::RadioButton("To Ref",shade_mode == 2)) 334 shade_mode = 2; 335 if (shade_mode == 2) { 336 ImGui::SameLine(); 337 ImGui::SetNextItemWidth(100); 338 ImGui::DragFloat("##Ref",&fill_ref, 1, -100, 500); 339 } 340 } 341 342 if (ImPlot::BeginPlot("Stock Prices")) { 343 ImPlot::SetupAxes("Days","Price"); 344 ImPlot::SetupAxesLimits(0,100,0,500); 345 if (show_fills) { 346 ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); 347 ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref, flags); 348 ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref, flags); 349 ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref, flags); 350 ImPlot::PopStyleVar(); 351 } 352 if (show_lines) { 353 ImPlot::PlotLine("Stock 1", xs1, ys1, 101); 354 ImPlot::PlotLine("Stock 2", xs1, ys2, 101); 355 ImPlot::PlotLine("Stock 3", xs1, ys3, 101); 356 } 357 ImPlot::EndPlot(); 358 } 359} 360 361//----------------------------------------------------------------------------- 362 363void Demo_ShadedPlots() { 364 static float xs[1001], ys[1001], ys1[1001], ys2[1001], ys3[1001], ys4[1001]; 365 srand(0); 366 for (int i = 0; i < 1001; ++i) { 367 xs[i] = i * 0.001f; 368 ys[i] = 0.25f + 0.25f * sinf(25 * xs[i]) * sinf(5 * xs[i]) + RandomRange(-0.01f, 0.01f); 369 ys1[i] = ys[i] + RandomRange(0.1f, 0.12f); 370 ys2[i] = ys[i] - RandomRange(0.1f, 0.12f); 371 ys3[i] = 0.75f + 0.2f * sinf(25 * xs[i]); 372 ys4[i] = 0.75f + 0.1f * cosf(25 * xs[i]); 373 } 374 static float alpha = 0.25f; 375 ImGui::DragFloat("Alpha",&alpha,0.01f,0,1); 376 377 if (ImPlot::BeginPlot("Shaded Plots")) { 378 ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha); 379 ImPlot::PlotShaded("Uncertain Data",xs,ys1,ys2,1001); 380 ImPlot::PlotLine("Uncertain Data", xs, ys, 1001); 381 ImPlot::PlotShaded("Overlapping",xs,ys3,ys4,1001); 382 ImPlot::PlotLine("Overlapping",xs,ys3,1001); 383 ImPlot::PlotLine("Overlapping",xs,ys4,1001); 384 ImPlot::PopStyleVar(); 385 ImPlot::EndPlot(); 386 } 387} 388 389//----------------------------------------------------------------------------- 390 391void Demo_ScatterPlots() { 392 srand(0); 393 static float xs1[100], ys1[100]; 394 for (int i = 0; i < 100; ++i) { 395 xs1[i] = i * 0.01f; 396 ys1[i] = xs1[i] + 0.1f * ((float)rand() / (float)RAND_MAX); 397 } 398 static float xs2[50], ys2[50]; 399 for (int i = 0; i < 50; i++) { 400 xs2[i] = 0.25f + 0.2f * ((float)rand() / (float)RAND_MAX); 401 ys2[i] = 0.75f + 0.2f * ((float)rand() / (float)RAND_MAX); 402 } 403 404 if (ImPlot::BeginPlot("Scatter Plot")) { 405 ImPlot::PlotScatter("Data 1", xs1, ys1, 100); 406 ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); 407 ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, ImPlot::GetColormapColor(1), IMPLOT_AUTO, ImPlot::GetColormapColor(1)); 408 ImPlot::PlotScatter("Data 2", xs2, ys2, 50); 409 ImPlot::PopStyleVar(); 410 ImPlot::EndPlot(); 411 } 412} 413 414//----------------------------------------------------------------------------- 415 416void Demo_StairstepPlots() { 417 static float ys1[21], ys2[21]; 418 for (int i = 0; i < 21; ++i) { 419 ys1[i] = 0.75f + 0.2f * sinf(10 * i * 0.05f); 420 ys2[i] = 0.25f + 0.2f * sinf(10 * i * 0.05f); 421 } 422 static ImPlotStairsFlags flags = 0; 423 CHECKBOX_FLAG(flags, ImPlotStairsFlags_Shaded); 424 if (ImPlot::BeginPlot("Stairstep Plot")) { 425 ImPlot::SetupAxes("x","f(x)"); 426 ImPlot::SetupAxesLimits(0,1,0,1); 427 428 ImPlot::PushStyleColor(ImPlotCol_Line, ImVec4(0.5f,0.5f,0.5f,1.0f)); 429 ImPlot::PlotLine("##1",ys1,21,0.05f); 430 ImPlot::PlotLine("##2",ys2,21,0.05f); 431 ImPlot::PopStyleColor(); 432 433 ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); 434 ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.25f); 435 ImPlot::PlotStairs("Post Step (default)", ys1, 21, 0.05f, 0, flags); 436 ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); 437 ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.25f); 438 ImPlot::PlotStairs("Pre Step", ys2, 21, 0.05f, 0, flags|ImPlotStairsFlags_PreStep); 439 440 ImPlot::EndPlot(); 441 } 442} 443 444//----------------------------------------------------------------------------- 445 446void Demo_BarPlots() { 447 static ImS8 data[10] = {1,2,3,4,5,6,7,8,9,10}; 448 if (ImPlot::BeginPlot("Bar Plot")) { 449 ImPlot::PlotBars("Vertical",data,10,0.7,1); 450 ImPlot::PlotBars("Horizontal",data,10,0.4,1,ImPlotBarsFlags_Horizontal); 451 ImPlot::EndPlot(); 452 } 453} 454 455//----------------------------------------------------------------------------- 456 457void Demo_BarGroups() { 458 static ImS8 data[30] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90, // midterm 459 80, 62, 56, 99, 55, 78, 88, 78, 90, 100, // final 460 80, 69, 52, 92, 72, 78, 75, 76, 89, 95}; // course 461 462 static const char* ilabels[] = {"Midterm Exam","Final Exam","Course Grade"}; 463 static const char* glabels[] = {"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10"}; 464 static const double positions[] = {0,1,2,3,4,5,6,7,8,9}; 465 466 static int items = 3; 467 static int groups = 10; 468 static float size = 0.67f; 469 470 static ImPlotBarGroupsFlags flags = 0; 471 static bool horz = false; 472 473 ImGui::CheckboxFlags("Stacked", (unsigned int*)&flags, ImPlotBarGroupsFlags_Stacked); 474 ImGui::SameLine(); 475 ImGui::Checkbox("Horizontal",&horz); 476 477 ImGui::SliderInt("Items",&items,1,3); 478 ImGui::SliderFloat("Size",&size,0,1); 479 480 if (ImPlot::BeginPlot("Bar Group")) { 481 ImPlot::SetupLegend(ImPlotLocation_East, ImPlotLegendFlags_Outside); 482 if (horz) { 483 ImPlot::SetupAxes("Score","Student",ImPlotAxisFlags_AutoFit,ImPlotAxisFlags_AutoFit); 484 ImPlot::SetupAxisTicks(ImAxis_Y1,positions, groups, glabels); 485 ImPlot::PlotBarGroups(ilabels,data,items,groups,size,0,flags|ImPlotBarGroupsFlags_Horizontal); 486 } 487 else { 488 ImPlot::SetupAxes("Student","Score",ImPlotAxisFlags_AutoFit,ImPlotAxisFlags_AutoFit); 489 ImPlot::SetupAxisTicks(ImAxis_X1,positions, groups, glabels); 490 ImPlot::PlotBarGroups(ilabels,data,items,groups,size,0,flags); 491 } 492 ImPlot::EndPlot(); 493 } 494} 495 496//----------------------------------------------------------------------------- 497 498void Demo_BarStacks() { 499 500 static ImPlotColormap Liars = -1; 501 if (Liars == -1) { 502 static const ImU32 Liars_Data[6] = { 4282515870, 4282609140, 4287357182, 4294630301, 4294945280, 4294921472 }; 503 Liars = ImPlot::AddColormap("Liars", Liars_Data, 6); 504 } 505 506 static bool diverging = true; 507 ImGui::Checkbox("Diverging",&diverging); 508 509 static const char* politicians[] = {"Trump","Bachman","Cruz","Gingrich","Palin","Santorum","Walker","Perry","Ryan","McCain","Rubio","Romney","Rand Paul","Christie","Biden","Kasich","Sanders","J Bush","H Clinton","Obama"}; 510 static int data_reg[] = {18,26,7,14,10,8,6,11,4,4,3,8,6,8,6,5,0,3,1,2, // Pants on Fire 511 43,36,30,21,30,27,25,17,11,22,15,16,16,17,12,12,14,6,13,12, // False 512 16,13,28,22,15,21,15,18,30,17,24,18,13,10,14,15,17,22,14,12, // Mostly False 513 17,10,13,25,12,22,19,26,23,17,22,27,20,26,29,17,18,22,21,27, // Half True 514 5,7,16,10,10,12,23,13,17,20,22,16,23,19,20,26,36,29,27,26, // Mostly True 515 1,8,6,8,23,10,12,15,15,20,14,15,22,20,19,25,15,18,24,21}; // True 516 static const char* labels_reg[] = {"Pants on Fire","False","Mostly False","Half True","Mostly True","True"}; 517 518 519 static int data_div[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // Pants on Fire (dummy, to order legend logically) 520 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // False (dummy, to order legend logically) 521 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // Mostly False (dummy, to order legend logically) 522 -16,-13,-28,-22,-15,-21,-15,-18,-30,-17,-24,-18,-13,-10,-14,-15,-17,-22,-14,-12, // Mostly False 523 -43,-36,-30,-21,-30,-27,-25,-17,-11,-22,-15,-16,-16,-17,-12,-12,-14,-6,-13,-12, // False 524 -18,-26,-7,-14,-10,-8,-6,-11,-4,-4,-3,-8,-6,-8,-6,-5,0,-3,-1,-2, // Pants on Fire 525 17,10,13,25,12,22,19,26,23,17,22,27,20,26,29,17,18,22,21,27, // Half True 526 5,7,16,10,10,12,23,13,17,20,22,16,23,19,20,26,36,29,27,26, // Mostly True 527 1,8,6,8,23,10,12,15,15,20,14,15,22,20,19,25,15,18,24,21}; // True 528 static const char* labels_div[] = {"Pants on Fire","False","Mostly False","Mostly False","False","Pants on Fire","Half True","Mostly True","True"}; 529 530 ImPlot::PushColormap(Liars); 531 if (ImPlot::BeginPlot("PolitiFact: Who Lies More?",ImVec2(-1,400),ImPlotFlags_NoMouseText)) { 532 ImPlot::SetupLegend(ImPlotLocation_South, ImPlotLegendFlags_Outside|ImPlotLegendFlags_Horizontal); 533 ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Invert); 534 ImPlot::SetupAxisTicks(ImAxis_Y1,0,19,20,politicians,false); 535 if (diverging) 536 ImPlot::PlotBarGroups(labels_div,data_div,9,20,0.75,0,ImPlotBarGroupsFlags_Stacked|ImPlotBarGroupsFlags_Horizontal); 537 else 538 ImPlot::PlotBarGroups(labels_reg,data_reg,6,20,0.75,0,ImPlotBarGroupsFlags_Stacked|ImPlotBarGroupsFlags_Horizontal); 539 ImPlot::EndPlot(); 540 } 541 ImPlot::PopColormap(); 542} 543 544//----------------------------------------------------------------------------- 545 546void Demo_ErrorBars() { 547 static float xs[5] = {1,2,3,4,5}; 548 static float bar[5] = {1,2,5,3,4}; 549 static float lin1[5] = {8,8,9,7,8}; 550 static float lin2[5] = {6,7,6,9,6}; 551 static float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f}; 552 static float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f}; 553 static float err3[5] = {0.09f, 0.14f, 0.09f, 0.12f, 0.16f}; 554 static float err4[5] = {0.02f, 0.08f, 0.15f, 0.05f, 0.2f}; 555 556 557 if (ImPlot::BeginPlot("##ErrorBars")) { 558 ImPlot::SetupAxesLimits(0, 6, 0, 10); 559 ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f); 560 ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5); 561 ImPlot::SetNextErrorBarStyle(ImPlot::GetColormapColor(1), 0); 562 ImPlot::PlotErrorBars("Line", xs, lin1, err1, err2, 5); 563 ImPlot::SetNextMarkerStyle(ImPlotMarker_Square); 564 ImPlot::PlotLine("Line", xs, lin1, 5); 565 ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(2)); 566 ImPlot::PlotErrorBars("Scatter", xs, lin2, err2, 5); 567 ImPlot::PlotErrorBars("Scatter", xs, lin2, err3, err4, 5, ImPlotErrorBarsFlags_Horizontal); 568 ImPlot::PopStyleColor(); 569 ImPlot::PlotScatter("Scatter", xs, lin2, 5); 570 ImPlot::EndPlot(); 571 } 572} 573 574//----------------------------------------------------------------------------- 575 576void Demo_StemPlots() { 577 static double xs[51], ys1[51], ys2[51]; 578 for (int i = 0; i < 51; ++i) { 579 xs[i] = i * 0.02; 580 ys1[i] = 1.0 + 0.5 * sin(25*xs[i])*cos(2*xs[i]); 581 ys2[i] = 0.5 + 0.25 * sin(10*xs[i]) * sin(xs[i]); 582 } 583 if (ImPlot::BeginPlot("Stem Plots")) { 584 ImPlot::SetupAxisLimits(ImAxis_X1,0,1.0); 585 ImPlot::SetupAxisLimits(ImAxis_Y1,0,1.6); 586 ImPlot::PlotStems("Stems 1",xs,ys1,51); 587 ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); 588 ImPlot::PlotStems("Stems 2", xs, ys2,51); 589 ImPlot::EndPlot(); 590 } 591} 592 593//----------------------------------------------------------------------------- 594 595void Demo_InfiniteLines() { 596 static double vals[] = {0.25, 0.5, 0.75}; 597 if (ImPlot::BeginPlot("##Infinite")) { 598 ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoInitialFit,ImPlotAxisFlags_NoInitialFit); 599 ImPlot::PlotInfLines("Vertical",vals,3); 600 ImPlot::PlotInfLines("Horizontal",vals,3,ImPlotInfLinesFlags_Horizontal); 601 ImPlot::EndPlot(); 602 } 603} 604 605//----------------------------------------------------------------------------- 606 607void Demo_PieCharts() { 608 static const char* labels1[] = {"Frogs","Hogs","Dogs","Logs"}; 609 static float data1[] = {0.15f, 0.30f, 0.2f, 0.05f}; 610 static ImPlotPieChartFlags flags = 0; 611 ImGui::SetNextItemWidth(250); 612 ImGui::DragFloat4("Values", data1, 0.01f, 0, 1); 613 CHECKBOX_FLAG(flags, ImPlotPieChartFlags_Normalize); 614 CHECKBOX_FLAG(flags, ImPlotPieChartFlags_IgnoreHidden); 615 616 if (ImPlot::BeginPlot("##Pie1", ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMouseText)) { 617 ImPlot::SetupAxes(nullptr, nullptr, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations); 618 ImPlot::SetupAxesLimits(0, 1, 0, 1); 619 ImPlot::PlotPieChart(labels1, data1, 4, 0.5, 0.5, 0.4, "%.2f", 90, flags); 620 ImPlot::EndPlot(); 621 } 622 623 ImGui::SameLine(); 624 625 static const char* labels2[] = {"A","B","C","D","E"}; 626 static int data2[] = {1,1,2,3,5}; 627 628 ImPlot::PushColormap(ImPlotColormap_Pastel); 629 if (ImPlot::BeginPlot("##Pie2", ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMouseText)) { 630 ImPlot::SetupAxes(nullptr, nullptr, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations); 631 ImPlot::SetupAxesLimits(0, 1, 0, 1); 632 ImPlot::PlotPieChart(labels2, data2, 5, 0.5, 0.5, 0.4, "%.0f", 180, flags); 633 ImPlot::EndPlot(); 634 } 635 ImPlot::PopColormap(); 636} 637 638//----------------------------------------------------------------------------- 639 640void Demo_Heatmaps() { 641 static float values1[7][7] = {{0.8f, 2.4f, 2.5f, 3.9f, 0.0f, 4.0f, 0.0f}, 642 {2.4f, 0.0f, 4.0f, 1.0f, 2.7f, 0.0f, 0.0f}, 643 {1.1f, 2.4f, 0.8f, 4.3f, 1.9f, 4.4f, 0.0f}, 644 {0.6f, 0.0f, 0.3f, 0.0f, 3.1f, 0.0f, 0.0f}, 645 {0.7f, 1.7f, 0.6f, 2.6f, 2.2f, 6.2f, 0.0f}, 646 {1.3f, 1.2f, 0.0f, 0.0f, 0.0f, 3.2f, 5.1f}, 647 {0.1f, 2.0f, 0.0f, 1.4f, 0.0f, 1.9f, 6.3f}}; 648 static float scale_min = 0; 649 static float scale_max = 6.3f; 650 static const char* xlabels[] = {"C1","C2","C3","C4","C5","C6","C7"}; 651 static const char* ylabels[] = {"R1","R2","R3","R4","R5","R6","R7"}; 652 653 static ImPlotColormap map = ImPlotColormap_Viridis; 654 if (ImPlot::ColormapButton(ImPlot::GetColormapName(map),ImVec2(225,0),map)) { 655 map = (map + 1) % ImPlot::GetColormapCount(); 656 // We bust the color cache of our plots so that item colors will 657 // resample the new colormap in the event that they have already 658 // been created. See documentation in implot.h. 659 BustColorCache("##Heatmap1"); 660 BustColorCache("##Heatmap2"); 661 } 662 663 ImGui::SameLine(); 664 ImGui::LabelText("##Colormap Index", "%s", "Change Colormap"); 665 ImGui::SetNextItemWidth(225); 666 ImGui::DragFloatRange2("Min / Max",&scale_min, &scale_max, 0.01f, -20, 20); 667 668 static ImPlotHeatmapFlags hm_flags = 0; 669 670 ImGui::CheckboxFlags("Column Major", (unsigned int*)&hm_flags, ImPlotHeatmapFlags_ColMajor); 671 672 static ImPlotAxisFlags axes_flags = ImPlotAxisFlags_Lock | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoTickMarks; 673 674 ImPlot::PushColormap(map); 675 676 if (ImPlot::BeginPlot("##Heatmap1",ImVec2(225,225),ImPlotFlags_NoLegend|ImPlotFlags_NoMouseText)) { 677 ImPlot::SetupAxes(nullptr, nullptr, axes_flags, axes_flags); 678 ImPlot::SetupAxisTicks(ImAxis_X1,0 + 1.0/14.0, 1 - 1.0/14.0, 7, xlabels); 679 ImPlot::SetupAxisTicks(ImAxis_Y1,1 - 1.0/14.0, 0 + 1.0/14.0, 7, ylabels); 680 ImPlot::PlotHeatmap("heat",values1[0],7,7,scale_min,scale_max,"%g",ImPlotPoint(0,0),ImPlotPoint(1,1),hm_flags); 681 ImPlot::EndPlot(); 682 } 683 ImGui::SameLine(); 684 ImPlot::ColormapScale("##HeatScale",scale_min, scale_max, ImVec2(60,225)); 685 686 ImGui::SameLine(); 687 688 const int size = 80; 689 static double values2[size*size]; 690 srand((unsigned int)(ImGui::GetTime()*1000000)); 691 for (int i = 0; i < size*size; ++i) 692 values2[i] = RandomRange(0.0,1.0); 693 694 if (ImPlot::BeginPlot("##Heatmap2",ImVec2(225,225))) { 695 ImPlot::SetupAxes(nullptr, nullptr, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations); 696 ImPlot::SetupAxesLimits(-1,1,-1,1); 697 ImPlot::PlotHeatmap("heat1",values2,size,size,0,1,nullptr); 698 ImPlot::PlotHeatmap("heat2",values2,size,size,0,1,nullptr, ImPlotPoint(-1,-1), ImPlotPoint(0,0)); 699 ImPlot::EndPlot(); 700 } 701 ImPlot::PopColormap(); 702 703} 704 705//----------------------------------------------------------------------------- 706 707void Demo_Histogram() { 708 static ImPlotHistogramFlags hist_flags = ImPlotHistogramFlags_Density; 709 static int bins = 50; 710 static double mu = 5; 711 static double sigma = 2; 712 ImGui::SetNextItemWidth(200); 713 if (ImGui::RadioButton("Sqrt",bins==ImPlotBin_Sqrt)) { bins = ImPlotBin_Sqrt; } ImGui::SameLine(); 714 if (ImGui::RadioButton("Sturges",bins==ImPlotBin_Sturges)) { bins = ImPlotBin_Sturges; } ImGui::SameLine(); 715 if (ImGui::RadioButton("Rice",bins==ImPlotBin_Rice)) { bins = ImPlotBin_Rice; } ImGui::SameLine(); 716 if (ImGui::RadioButton("Scott",bins==ImPlotBin_Scott)) { bins = ImPlotBin_Scott; } ImGui::SameLine(); 717 if (ImGui::RadioButton("N Bins",bins>=0)) { bins = 50; } 718 if (bins>=0) { 719 ImGui::SameLine(); 720 ImGui::SetNextItemWidth(200); 721 ImGui::SliderInt("##Bins", &bins, 1, 100); 722 } 723 ImGui::CheckboxFlags("Horizontal", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Horizontal); 724 ImGui::SameLine(); 725 ImGui::CheckboxFlags("Density", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Density); 726 ImGui::SameLine(); 727 ImGui::CheckboxFlags("Cumulative", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Cumulative); 728 729 static bool range = false; 730 ImGui::Checkbox("Range", &range); 731 static float rmin = -3; 732 static float rmax = 13; 733 if (range) { 734 ImGui::SameLine(); 735 ImGui::SetNextItemWidth(200); 736 ImGui::DragFloat2("##Range",&rmin,0.1f,-3,13); 737 ImGui::SameLine(); 738 ImGui::CheckboxFlags("Exclude Outliers", (unsigned int*)&hist_flags, ImPlotHistogramFlags_NoOutliers); 739 } 740 static NormalDistribution<10000> dist(mu, sigma); 741 static double x[100]; 742 static double y[100]; 743 if (hist_flags & ImPlotHistogramFlags_Density) { 744 for (int i = 0; i < 100; ++i) { 745 x[i] = -3 + 16 * (double)i/99.0; 746 y[i] = exp( - (x[i]-mu)*(x[i]-mu) / (2*sigma*sigma)) / (sigma * sqrt(2*3.141592653589793238)); 747 } 748 if (hist_flags & ImPlotHistogramFlags_Cumulative) { 749 for (int i = 1; i < 100; ++i) 750 y[i] += y[i-1]; 751 for (int i = 0; i < 100; ++i) 752 y[i] /= y[99]; 753 } 754 } 755 756 if (ImPlot::BeginPlot("##Histograms")) { 757 ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_AutoFit,ImPlotAxisFlags_AutoFit); 758 ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL,0.5f); 759 ImPlot::PlotHistogram("Empirical", dist.Data, 10000, bins, 1.0, range ? ImPlotRange(rmin,rmax) : ImPlotRange(), hist_flags); 760 if ((hist_flags & ImPlotHistogramFlags_Density) && !(hist_flags & ImPlotHistogramFlags_NoOutliers)) { 761 if (hist_flags & ImPlotHistogramFlags_Horizontal) 762 ImPlot::PlotLine("Theoretical",y,x,100); 763 else 764 ImPlot::PlotLine("Theoretical",x,y,100); 765 } 766 ImPlot::EndPlot(); 767 } 768} 769 770//----------------------------------------------------------------------------- 771 772void Demo_Histogram2D() { 773 static int count = 50000; 774 static int xybins[2] = {100,100}; 775 776 static ImPlotHistogramFlags hist_flags = 0; 777 778 ImGui::SliderInt("Count",&count,100,100000); 779 ImGui::SliderInt2("Bins",xybins,1,500); 780 ImGui::SameLine(); 781 ImGui::CheckboxFlags("Density", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Density); 782 783 static NormalDistribution<100000> dist1(1, 2); 784 static NormalDistribution<100000> dist2(1, 1); 785 double max_count = 0; 786 ImPlotAxisFlags flags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Foreground; 787 ImPlot::PushColormap("Hot"); 788 if (ImPlot::BeginPlot("##Hist2D",ImVec2(ImGui::GetContentRegionAvail().x-100-ImGui::GetStyle().ItemSpacing.x,0))) { 789 ImPlot::SetupAxes(nullptr, nullptr, flags, flags); 790 ImPlot::SetupAxesLimits(-6,6,-6,6); 791 max_count = ImPlot::PlotHistogram2D("Hist2D",dist1.Data,dist2.Data,count,xybins[0],xybins[1],ImPlotRect(-6,6,-6,6), hist_flags); 792 ImPlot::EndPlot(); 793 } 794 ImGui::SameLine(); 795 ImPlot::ColormapScale(hist_flags & ImPlotHistogramFlags_Density ? "Density" : "Count",0,max_count,ImVec2(100,0)); 796 ImPlot::PopColormap(); 797} 798 799//----------------------------------------------------------------------------- 800 801void Demo_DigitalPlots() { 802 ImGui::BulletText("Digital plots do not respond to Y drag and zoom, so that"); 803 ImGui::Indent(); 804 ImGui::Text("you can drag analog plots over the rising/falling digital edge."); 805 ImGui::Unindent(); 806 807 static bool paused = false; 808 static ScrollingBuffer dataDigital[2]; 809 static ScrollingBuffer dataAnalog[2]; 810 static bool showDigital[2] = {true, false}; 811 static bool showAnalog[2] = {true, false}; 812 813 char label[32]; 814 ImGui::Checkbox("digital_0", &showDigital[0]); ImGui::SameLine(); 815 ImGui::Checkbox("digital_1", &showDigital[1]); ImGui::SameLine(); 816 ImGui::Checkbox("analog_0", &showAnalog[0]); ImGui::SameLine(); 817 ImGui::Checkbox("analog_1", &showAnalog[1]); 818 819 static float t = 0; 820 if (!paused) { 821 t += ImGui::GetIO().DeltaTime; 822 //digital signal values 823 if (showDigital[0]) 824 dataDigital[0].AddPoint(t, sinf(2*t) > 0.45); 825 if (showDigital[1]) 826 dataDigital[1].AddPoint(t, sinf(2*t) < 0.45); 827 //Analog signal values 828 if (showAnalog[0]) 829 dataAnalog[0].AddPoint(t, sinf(2*t)); 830 if (showAnalog[1]) 831 dataAnalog[1].AddPoint(t, cosf(2*t)); 832 } 833 if (ImPlot::BeginPlot("##Digital")) { 834 ImPlot::SetupAxisLimits(ImAxis_X1, t - 10.0, t, paused ? ImGuiCond_Once : ImGuiCond_Always); 835 ImPlot::SetupAxisLimits(ImAxis_Y1, -1, 1); 836 for (int i = 0; i < 2; ++i) { 837 if (showDigital[i] && dataDigital[i].Data.size() > 0) { 838 snprintf(label, sizeof(label), "digital_%d", i); 839 ImPlot::PlotDigital(label, &dataDigital[i].Data[0].x, &dataDigital[i].Data[0].y, dataDigital[i].Data.size(), 0, dataDigital[i].Offset, 2 * sizeof(float)); 840 } 841 } 842 for (int i = 0; i < 2; ++i) { 843 if (showAnalog[i]) { 844 snprintf(label, sizeof(label), "analog_%d", i); 845 if (dataAnalog[i].Data.size() > 0) 846 ImPlot::PlotLine(label, &dataAnalog[i].Data[0].x, &dataAnalog[i].Data[0].y, dataAnalog[i].Data.size(), 0, dataAnalog[i].Offset, 2 * sizeof(float)); 847 } 848 } 849 ImPlot::EndPlot(); 850 } 851} 852 853//----------------------------------------------------------------------------- 854 855void Demo_Images() { 856 ImGui::BulletText("Below we are displaying the font texture, which is the only texture we have\naccess to in this demo."); 857 ImGui::BulletText("Use the 'ImTextureID' type as storage to pass pointers or identifiers to your\nown texture data."); 858 ImGui::BulletText("See ImGui Wiki page 'Image Loading and Displaying Examples'."); 859 static ImVec2 bmin(0,0); 860 static ImVec2 bmax(1,1); 861 static ImVec2 uv0(0,0); 862 static ImVec2 uv1(1,1); 863 static ImVec4 tint(1,1,1,1); 864 ImGui::SliderFloat2("Min", &bmin.x, -2, 2, "%.1f"); 865 ImGui::SliderFloat2("Max", &bmax.x, -2, 2, "%.1f"); 866 ImGui::SliderFloat2("UV0", &uv0.x, -2, 2, "%.1f"); 867 ImGui::SliderFloat2("UV1", &uv1.x, -2, 2, "%.1f"); 868 ImGui::ColorEdit4("Tint",&tint.x); 869 if (ImPlot::BeginPlot("##image")) { 870 ImPlot::PlotImage("my image",ImGui::GetIO().Fonts->TexID, bmin, bmax, uv0, uv1, tint); 871 ImPlot::EndPlot(); 872 } 873} 874 875//----------------------------------------------------------------------------- 876 877void Demo_RealtimePlots() { 878 ImGui::BulletText("Move your mouse to change the data!"); 879 ImGui::BulletText("This example assumes 60 FPS. Higher FPS requires larger buffer size."); 880 static ScrollingBuffer sdata1, sdata2; 881 static RollingBuffer rdata1, rdata2; 882 ImVec2 mouse = ImGui::GetMousePos(); 883 static float t = 0; 884 t += ImGui::GetIO().DeltaTime; 885 sdata1.AddPoint(t, mouse.x * 0.0005f); 886 rdata1.AddPoint(t, mouse.x * 0.0005f); 887 sdata2.AddPoint(t, mouse.y * 0.0005f); 888 rdata2.AddPoint(t, mouse.y * 0.0005f); 889 890 static float history = 10.0f; 891 ImGui::SliderFloat("History",&history,1,30,"%.1f s"); 892 rdata1.Span = history; 893 rdata2.Span = history; 894 895 static ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels; 896 897 if (ImPlot::BeginPlot("##Scrolling", ImVec2(-1,150))) { 898 ImPlot::SetupAxes(nullptr, nullptr, flags, flags); 899 ImPlot::SetupAxisLimits(ImAxis_X1,t - history, t, ImGuiCond_Always); 900 ImPlot::SetupAxisLimits(ImAxis_Y1,0,1); 901 ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL,0.5f); 902 ImPlot::PlotShaded("Mouse X", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), -INFINITY, 0, sdata1.Offset, 2 * sizeof(float)); 903 ImPlot::PlotLine("Mouse Y", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), 0, sdata2.Offset, 2*sizeof(float)); 904 ImPlot::EndPlot(); 905 } 906 if (ImPlot::BeginPlot("##Rolling", ImVec2(-1,150))) { 907 ImPlot::SetupAxes(nullptr, nullptr, flags, flags); 908 ImPlot::SetupAxisLimits(ImAxis_X1,0,history, ImGuiCond_Always); 909 ImPlot::SetupAxisLimits(ImAxis_Y1,0,1); 910 ImPlot::PlotLine("Mouse X", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 0, 2 * sizeof(float)); 911 ImPlot::PlotLine("Mouse Y", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 0, 2 * sizeof(float)); 912 ImPlot::EndPlot(); 913 } 914} 915 916//----------------------------------------------------------------------------- 917 918void Demo_MarkersAndText() { 919 static float mk_size = ImPlot::GetStyle().MarkerSize; 920 static float mk_weight = ImPlot::GetStyle().MarkerWeight; 921 ImGui::DragFloat("Marker Size",&mk_size,0.1f,2.0f,10.0f,"%.2f px"); 922 ImGui::DragFloat("Marker Weight", &mk_weight,0.05f,0.5f,3.0f,"%.2f px"); 923 924 if (ImPlot::BeginPlot("##MarkerStyles", ImVec2(-1,0), ImPlotFlags_CanvasOnly)) { 925 926 ImPlot::SetupAxes(nullptr, nullptr, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations); 927 ImPlot::SetupAxesLimits(0, 10, 0, 12); 928 929 ImS8 xs[2] = {1,4}; 930 ImS8 ys[2] = {10,11}; 931 932 // filled markers 933 for (int m = 0; m < ImPlotMarker_COUNT; ++m) { 934 ImGui::PushID(m); 935 ImPlot::SetNextMarkerStyle(m, mk_size, IMPLOT_AUTO_COL, mk_weight); 936 ImPlot::PlotLine("##Filled", xs, ys, 2); 937 ImGui::PopID(); 938 ys[0]--; ys[1]--; 939 } 940 xs[0] = 6; xs[1] = 9; ys[0] = 10; ys[1] = 11; 941 // open markers 942 for (int m = 0; m < ImPlotMarker_COUNT; ++m) { 943 ImGui::PushID(m); 944 ImPlot::SetNextMarkerStyle(m, mk_size, ImVec4(0,0,0,0), mk_weight); 945 ImPlot::PlotLine("##Open", xs, ys, 2); 946 ImGui::PopID(); 947 ys[0]--; ys[1]--; 948 } 949 950 ImPlot::PlotText("Filled Markers", 2.5f, 6.0f); 951 ImPlot::PlotText("Open Markers", 7.5f, 6.0f); 952 953 ImPlot::PushStyleColor(ImPlotCol_InlayText, ImVec4(1,0,1,1)); 954 ImPlot::PlotText("Vertical Text", 5.0f, 6.0f, ImVec2(0,0), ImPlotTextFlags_Vertical); 955 ImPlot::PopStyleColor(); 956 957 ImPlot::EndPlot(); 958 } 959} 960 961//----------------------------------------------------------------------------- 962 963void Demo_NaNValues() { 964 965 static bool include_nan = true; 966 static ImPlotLineFlags flags = 0; 967 968 float data1[5] = {0.0f,0.25f,0.5f,0.75f,1.0f}; 969 float data2[5] = {0.0f,0.25f,0.5f,0.75f,1.0f}; 970 971 if (include_nan) 972 data1[2] = NAN; 973 974 ImGui::Checkbox("Include NaN",&include_nan); 975 ImGui::SameLine(); 976 ImGui::CheckboxFlags("Skip NaN", (unsigned int*)&flags, ImPlotLineFlags_SkipNaN); 977 978 if (ImPlot::BeginPlot("##NaNValues")) { 979 ImPlot::SetNextMarkerStyle(ImPlotMarker_Square); 980 ImPlot::PlotLine("line", data1, data2, 5, flags); 981 ImPlot::PlotBars("bars", data1, 5); 982 ImPlot::EndPlot(); 983 } 984} 985 986//----------------------------------------------------------------------------- 987 988void Demo_LogScale() { 989 static double xs[1001], ys1[1001], ys2[1001], ys3[1001]; 990 for (int i = 0; i < 1001; ++i) { 991 xs[i] = i*0.1f; 992 ys1[i] = sin(xs[i]) + 1; 993 ys2[i] = log(xs[i]); 994 ys3[i] = pow(10.0, xs[i]); 995 } 996 if (ImPlot::BeginPlot("Log Plot", ImVec2(-1,0))) { 997 ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Log10); 998 ImPlot::SetupAxesLimits(0.1, 100, 0, 10); 999 ImPlot::PlotLine("f(x) = x", xs, xs, 1001); 1000 ImPlot::PlotLine("f(x) = sin(x)+1", xs, ys1, 1001); 1001 ImPlot::PlotLine("f(x) = log(x)", xs, ys2, 1001); 1002 ImPlot::PlotLine("f(x) = 10^x", xs, ys3, 21); 1003 ImPlot::EndPlot(); 1004 } 1005} 1006 1007//----------------------------------------------------------------------------- 1008 1009void Demo_SymmetricLogScale() { 1010 static double xs[1001], ys1[1001], ys2[1001]; 1011 for (int i = 0; i < 1001; ++i) { 1012 xs[i] = i*0.1f-50; 1013 ys1[i] = sin(xs[i]); 1014 ys2[i] = i*0.002 - 1; 1015 } 1016 if (ImPlot::BeginPlot("SymLog Plot", ImVec2(-1,0))) { 1017 ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_SymLog); 1018 ImPlot::PlotLine("f(x) = a*x+b",xs,ys2,1001); 1019 ImPlot::PlotLine("f(x) = sin(x)",xs,ys1,1001); 1020 ImPlot::EndPlot(); 1021 } 1022} 1023 1024//----------------------------------------------------------------------------- 1025 1026void Demo_TimeScale() { 1027 1028 static double t_min = 1609459200; // 01/01/2021 @ 12:00:00am (UTC) 1029 static double t_max = 1640995200; // 01/01/2022 @ 12:00:00am (UTC) 1030 1031 ImGui::BulletText("When ImPlotAxisFlags_Time is enabled on the X-Axis, values are interpreted as\n" 1032 "UNIX timestamps in seconds and axis labels are formated as date/time."); 1033 ImGui::BulletText("By default, labels are in UTC time but can be set to use local time instead."); 1034 1035 ImGui::Checkbox("Local Time",&ImPlot::GetStyle().UseLocalTime); 1036 ImGui::SameLine(); 1037 ImGui::Checkbox("ISO 8601",&ImPlot::GetStyle().UseISO8601); 1038 ImGui::SameLine(); 1039 ImGui::Checkbox("24 Hour Clock",&ImPlot::GetStyle().Use24HourClock); 1040 1041 static HugeTimeData* data = nullptr; 1042 if (data == nullptr) { 1043 ImGui::SameLine(); 1044 if (ImGui::Button("Generate Huge Data (~500MB!)")) { 1045 static HugeTimeData sdata(t_min); 1046 data = &sdata; 1047 } 1048 } 1049 1050 if (ImPlot::BeginPlot("##Time", ImVec2(-1,0))) { 1051 ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Time); 1052 ImPlot::SetupAxesLimits(t_min,t_max,0,1); 1053 if (data != nullptr) { 1054 // downsample our data 1055 int downsample = (int)ImPlot::GetPlotLimits().X.Size() / 1000 + 1; 1056 int start = (int)(ImPlot::GetPlotLimits().X.Min - t_min); 1057 start = start < 0 ? 0 : start > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : start; 1058 int end = (int)(ImPlot::GetPlotLimits().X.Max - t_min) + 1000; 1059 end = end < 0 ? 0 : end > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : end; 1060 int size = (end - start)/downsample; 1061 // plot it 1062 ImPlot::PlotLine("Time Series", &data->Ts[start], &data->Ys[start], size, 0, 0, sizeof(double)*downsample); 1063 } 1064 // plot time now 1065 double t_now = (double)time(nullptr); 1066 double y_now = HugeTimeData::GetY(t_now); 1067 ImPlot::PlotScatter("Now",&t_now,&y_now,1); 1068 ImPlot::Annotation(t_now,y_now,ImPlot::GetLastItemColor(),ImVec2(10,10),false,"Now"); 1069 ImPlot::EndPlot(); 1070 } 1071} 1072 1073//----------------------------------------------------------------------------- 1074 1075static inline double TransformForward_Sqrt(double v, void*) { 1076 return sqrt(v); 1077} 1078 1079static inline double TransformInverse_Sqrt(double v, void*) { 1080 return v*v; 1081} 1082 1083void Demo_CustomScale() { 1084 static float v[100]; 1085 for (int i = 0; i < 100; ++i) { 1086 v[i] = i*0.01f; 1087 } 1088 if (ImPlot::BeginPlot("Sqrt")) { 1089 ImPlot::SetupAxis(ImAxis_X1, "Linear"); 1090 ImPlot::SetupAxis(ImAxis_Y1, "Sqrt"); 1091 ImPlot::SetupAxisScale(ImAxis_Y1, TransformForward_Sqrt, TransformInverse_Sqrt); 1092 ImPlot::SetupAxisLimitsConstraints(ImAxis_Y1, 0, INFINITY); 1093 ImPlot::PlotLine("##data",v,v,100); 1094 ImPlot::EndPlot(); 1095 } 1096} 1097 1098//----------------------------------------------------------------------------- 1099 1100void Demo_MultipleAxes() { 1101 static float xs[1001], xs2[1001], ys1[1001], ys2[1001], ys3[1001]; 1102 for (int i = 0; i < 1001; ++i) { 1103 xs[i] = (i*0.1f); 1104 xs2[i] = xs[i] + 10.0f; 1105 ys1[i] = sinf(xs[i]) * 3 + 1; 1106 ys2[i] = cosf(xs[i]) * 0.2f + 0.5f; 1107 ys3[i] = sinf(xs[i]+0.5f) * 100 + 200; 1108 } 1109 1110 static bool x2_axis = true; 1111 static bool y2_axis = true; 1112 static bool y3_axis = true; 1113 1114 ImGui::Checkbox("X-Axis 2", &x2_axis); 1115 ImGui::SameLine(); 1116 ImGui::Checkbox("Y-Axis 2", &y2_axis); 1117 ImGui::SameLine(); 1118 ImGui::Checkbox("Y-Axis 3", &y3_axis); 1119 1120 ImGui::BulletText("You can drag axes to the opposite side of the plot."); 1121 ImGui::BulletText("Hover over legend items to see which axis they are plotted on."); 1122 1123 if (ImPlot::BeginPlot("Multi-Axis Plot", ImVec2(-1,0))) { 1124 ImPlot::SetupAxes("X-Axis 1", "Y-Axis 1"); 1125 ImPlot::SetupAxesLimits(0, 100, 0, 10); 1126 if (x2_axis) { 1127 ImPlot::SetupAxis(ImAxis_X2, "X-Axis 2",ImPlotAxisFlags_AuxDefault); 1128 ImPlot::SetupAxisLimits(ImAxis_X2, 0, 100); 1129 } 1130 if (y2_axis) { 1131 ImPlot::SetupAxis(ImAxis_Y2, "Y-Axis 2",ImPlotAxisFlags_AuxDefault); 1132 ImPlot::SetupAxisLimits(ImAxis_Y2, 0, 1); 1133 } 1134 if (y3_axis) { 1135 ImPlot::SetupAxis(ImAxis_Y3, "Y-Axis 3",ImPlotAxisFlags_AuxDefault); 1136 ImPlot::SetupAxisLimits(ImAxis_Y3, 0, 300); 1137 } 1138 1139 ImPlot::PlotLine("f(x) = x", xs, xs, 1001); 1140 if (x2_axis) { 1141 ImPlot::SetAxes(ImAxis_X2, ImAxis_Y1); 1142 ImPlot::PlotLine("f(x) = sin(x)*3+1", xs2, ys1, 1001); 1143 } 1144 if (y2_axis) { 1145 ImPlot::SetAxes(ImAxis_X1, ImAxis_Y2); 1146 ImPlot::PlotLine("f(x) = cos(x)*.2+.5", xs, ys2, 1001); 1147 } 1148 if (x2_axis && y3_axis) { 1149 ImPlot::SetAxes(ImAxis_X2, ImAxis_Y3); 1150 ImPlot::PlotLine("f(x) = sin(x+.5)*100+200 ", xs2, ys3, 1001); 1151 } 1152 ImPlot::EndPlot(); 1153 } 1154} 1155 1156//----------------------------------------------------------------------------- 1157 1158void Demo_LinkedAxes() { 1159 static ImPlotRect lims(0,1,0,1); 1160 static bool linkx = true, linky = true; 1161 int data[2] = {0,1}; 1162 ImGui::Checkbox("Link X", &linkx); 1163 ImGui::SameLine(); 1164 ImGui::Checkbox("Link Y", &linky); 1165 1166 ImGui::DragScalarN("Limits",ImGuiDataType_Double,&lims.X.Min,4,0.01f); 1167 1168 if (BeginAlignedPlots("AlignedGroup")) { 1169 if (ImPlot::BeginPlot("Plot A")) { 1170 ImPlot::SetupAxisLinks(ImAxis_X1, linkx ? &lims.X.Min : nullptr, linkx ? &lims.X.Max : nullptr); 1171 ImPlot::SetupAxisLinks(ImAxis_Y1, linky ? &lims.Y.Min : nullptr, linky ? &lims.Y.Max : nullptr); 1172 ImPlot::PlotLine("Line",data,2); 1173 ImPlot::EndPlot(); 1174 } 1175 if (ImPlot::BeginPlot("Plot B")) { 1176 ImPlot::SetupAxisLinks(ImAxis_X1, linkx ? &lims.X.Min : nullptr, linkx ? &lims.X.Max : nullptr); 1177 ImPlot::SetupAxisLinks(ImAxis_Y1, linky ? &lims.Y.Min : nullptr, linky ? &lims.Y.Max : nullptr); 1178 ImPlot::PlotLine("Line",data,2); 1179 ImPlot::EndPlot(); 1180 } 1181 ImPlot::EndAlignedPlots(); 1182 } 1183} 1184 1185//----------------------------------------------------------------------------- 1186 1187void Demo_AxisConstraints() { 1188 static float constraints[4] = {-10,10,1,20}; 1189 static ImPlotAxisFlags flags; 1190 ImGui::DragFloat2("Limits Constraints", &constraints[0], 0.01f); 1191 ImGui::DragFloat2("Zoom Constraints", &constraints[2], 0.01f); 1192 CHECKBOX_FLAG(flags, ImPlotAxisFlags_PanStretch); 1193 if (ImPlot::BeginPlot("##AxisConstraints",ImVec2(-1,0))) { 1194 ImPlot::SetupAxes("X","Y",flags,flags); 1195 ImPlot::SetupAxesLimits(-1,1,-1,1); 1196 ImPlot::SetupAxisLimitsConstraints(ImAxis_X1,constraints[0], constraints[1]); 1197 ImPlot::SetupAxisZoomConstraints(ImAxis_X1,constraints[2], constraints[3]); 1198 ImPlot::SetupAxisLimitsConstraints(ImAxis_Y1,constraints[0], constraints[1]); 1199 ImPlot::SetupAxisZoomConstraints(ImAxis_Y1,constraints[2], constraints[3]); 1200 ImPlot::EndPlot(); 1201 } 1202} 1203 1204//----------------------------------------------------------------------------- 1205 1206void Demo_EqualAxes() { 1207 ImGui::BulletText("Equal constraint applies to axis pairs (e.g ImAxis_X1/Y1, ImAxis_X2/Y2)"); 1208 static double xs1[360], ys1[360]; 1209 for (int i = 0; i < 360; ++i) { 1210 double angle = i * 2 * PI / 359.0; 1211 xs1[i] = cos(angle); ys1[i] = sin(angle); 1212 } 1213 float xs2[] = {-1,0,1,0,-1}; 1214 float ys2[] = {0,1,0,-1,0}; 1215 if (ImPlot::BeginPlot("##EqualAxes",ImVec2(-1,0),ImPlotFlags_Equal)) { 1216 ImPlot::SetupAxis(ImAxis_X2, nullptr, ImPlotAxisFlags_AuxDefault); 1217 ImPlot::SetupAxis(ImAxis_Y2, nullptr, ImPlotAxisFlags_AuxDefault); 1218 ImPlot::PlotLine("Circle",xs1,ys1,360); 1219 ImPlot::SetAxes(ImAxis_X2, ImAxis_Y2); 1220 ImPlot::PlotLine("Diamond",xs2,ys2,5); 1221 ImPlot::EndPlot(); 1222 } 1223} 1224 1225//----------------------------------------------------------------------------- 1226 1227void Demo_AutoFittingData() { 1228 ImGui::BulletText("The Y-axis has been configured to auto-fit to only the data visible in X-axis range."); 1229 ImGui::BulletText("Zoom and pan the X-axis. Disable Stems to see a difference in fit."); 1230 ImGui::BulletText("If ImPlotAxisFlags_RangeFit is disabled, the axis will fit ALL data."); 1231 1232 static ImPlotAxisFlags xflags = ImPlotAxisFlags_None; 1233 static ImPlotAxisFlags yflags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit; 1234 1235 ImGui::TextUnformatted("X: "); ImGui::SameLine(); 1236 ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine(); 1237 ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_RangeFit); 1238 1239 ImGui::TextUnformatted("Y: "); ImGui::SameLine(); 1240 ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine(); 1241 ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_RangeFit); 1242 1243 static double data[101]; 1244 srand(0); 1245 for (int i = 0; i < 101; ++i) 1246 data[i] = 1 + sin(i/10.0f); 1247 1248 if (ImPlot::BeginPlot("##DataFitting")) { 1249 ImPlot::SetupAxes("X","Y",xflags,yflags); 1250 ImPlot::PlotLine("Line",data,101); 1251 ImPlot::PlotStems("Stems",data,101); 1252 ImPlot::EndPlot(); 1253 }; 1254} 1255 1256//----------------------------------------------------------------------------- 1257 1258ImPlotPoint SinewaveGetter(int i, void* data) { 1259 float f = *(float*)data; 1260 return ImPlotPoint(i,sinf(f*i)); 1261} 1262 1263void Demo_SubplotsSizing() { 1264 1265 static ImPlotSubplotFlags flags = ImPlotSubplotFlags_ShareItems|ImPlotSubplotFlags_NoLegend; 1266 ImGui::CheckboxFlags("ImPlotSubplotFlags_NoResize", (unsigned int*)&flags, ImPlotSubplotFlags_NoResize); 1267 ImGui::CheckboxFlags("ImPlotSubplotFlags_NoTitle", (unsigned int*)&flags, ImPlotSubplotFlags_NoTitle); 1268 1269 static int rows = 3; 1270 static int cols = 3; 1271 ImGui::SliderInt("Rows",&rows,1,5); 1272 ImGui::SliderInt("Cols",&cols,1,5); 1273 if (rows < 1 || cols < 1) { 1274 ImGui::TextColored(ImVec4(1,0,0,1), "Nice try, but the number of rows and columns must be greater than 0!"); 1275 return; 1276 } 1277 static float rratios[] = {5,1,1,1,1,1}; 1278 static float cratios[] = {5,1,1,1,1,1}; 1279 ImGui::DragScalarN("Row Ratios",ImGuiDataType_Float,rratios,rows,0.01f,nullptr); 1280 ImGui::DragScalarN("Col Ratios",ImGuiDataType_Float,cratios,cols,0.01f,nullptr); 1281 if (ImPlot::BeginSubplots("My Subplots", rows, cols, ImVec2(-1,400), flags, rratios, cratios)) { 1282 int id = 0; 1283 for (int i = 0; i < rows*cols; ++i) { 1284 if (ImPlot::BeginPlot("",ImVec2(),ImPlotFlags_NoLegend)) { 1285 ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations); 1286 float fi = 0.01f * (i+1); 1287 if (rows*cols > 1) { 1288 ImPlot::SetNextLineStyle(SampleColormap((float)i/(float)(rows*cols-1),ImPlotColormap_Jet)); 1289 } 1290 char label[16]; 1291 snprintf(label, sizeof(label), "data%d", id++); 1292 ImPlot::PlotLineG(label,SinewaveGetter,&fi,1000); 1293 ImPlot::EndPlot(); 1294 } 1295 } 1296 ImPlot::EndSubplots(); 1297 } 1298} 1299 1300//----------------------------------------------------------------------------- 1301 1302void Demo_SubplotItemSharing() { 1303 static ImPlotSubplotFlags flags = ImPlotSubplotFlags_ShareItems; 1304 ImGui::CheckboxFlags("ImPlotSubplotFlags_ShareItems", (unsigned int*)&flags, ImPlotSubplotFlags_ShareItems); 1305 ImGui::CheckboxFlags("ImPlotSubplotFlags_ColMajor", (unsigned int*)&flags, ImPlotSubplotFlags_ColMajor); 1306 ImGui::BulletText("Drag and drop items from the legend onto plots (except for 'common')"); 1307 static int rows = 2; 1308 static int cols = 3; 1309 static int id[] = {0,1,2,3,4,5}; 1310 static int curj = -1; 1311 if (ImPlot::BeginSubplots("##ItemSharing", rows, cols, ImVec2(-1,400), flags)) { 1312 ImPlot::SetupLegend(ImPlotLocation_South, ImPlotLegendFlags_Sort|ImPlotLegendFlags_Horizontal); 1313 for (int i = 0; i < rows*cols; ++i) { 1314 if (ImPlot::BeginPlot("")) { 1315 float fc = 0.01f; 1316 ImPlot::PlotLineG("common",SinewaveGetter,&fc,1000); 1317 for (int j = 0; j < 6; ++j) { 1318 if (id[j] == i) { 1319 char label[8]; 1320 float fj = 0.01f * (j+2); 1321 snprintf(label, sizeof(label), "data%d", j); 1322 ImPlot::PlotLineG(label,SinewaveGetter,&fj,1000); 1323 if (ImPlot::BeginDragDropSourceItem(label)) { 1324 curj = j; 1325 ImGui::SetDragDropPayload("MY_DND",nullptr,0); 1326 ImPlot::ItemIcon(GetLastItemColor()); ImGui::SameLine(); 1327 ImGui::TextUnformatted(label); 1328 ImPlot::EndDragDropSource(); 1329 } 1330 } 1331 } 1332 if (ImPlot::BeginDragDropTargetPlot()) { 1333 if (ImGui::AcceptDragDropPayload("MY_DND")) 1334 id[curj] = i; 1335 ImPlot::EndDragDropTarget(); 1336 } 1337 ImPlot::EndPlot(); 1338 } 1339 } 1340 ImPlot::EndSubplots(); 1341 } 1342} 1343 1344//----------------------------------------------------------------------------- 1345 1346void Demo_SubplotAxisLinking() { 1347 static ImPlotSubplotFlags flags = ImPlotSubplotFlags_LinkRows | ImPlotSubplotFlags_LinkCols; 1348 ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkRows", (unsigned int*)&flags, ImPlotSubplotFlags_LinkRows); 1349 ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkCols", (unsigned int*)&flags, ImPlotSubplotFlags_LinkCols); 1350 ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkAllX", (unsigned int*)&flags, ImPlotSubplotFlags_LinkAllX); 1351 ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkAllY", (unsigned int*)&flags, ImPlotSubplotFlags_LinkAllY); 1352 1353 static int rows = 2; 1354 static int cols = 2; 1355 if (ImPlot::BeginSubplots("##AxisLinking", rows, cols, ImVec2(-1,400), flags)) { 1356 for (int i = 0; i < rows*cols; ++i) { 1357 if (ImPlot::BeginPlot("")) { 1358 ImPlot::SetupAxesLimits(0,1000,-1,1); 1359 float fc = 0.01f; 1360 ImPlot::PlotLineG("common",SinewaveGetter,&fc,1000); 1361 ImPlot::EndPlot(); 1362 } 1363 } 1364 ImPlot::EndSubplots(); 1365 } 1366} 1367 1368//----------------------------------------------------------------------------- 1369 1370void Demo_LegendOptions() { 1371 static ImPlotLocation loc = ImPlotLocation_East; 1372 ImGui::CheckboxFlags("North", (unsigned int*)&loc, ImPlotLocation_North); ImGui::SameLine(); 1373 ImGui::CheckboxFlags("South", (unsigned int*)&loc, ImPlotLocation_South); ImGui::SameLine(); 1374 ImGui::CheckboxFlags("West", (unsigned int*)&loc, ImPlotLocation_West); ImGui::SameLine(); 1375 ImGui::CheckboxFlags("East", (unsigned int*)&loc, ImPlotLocation_East); 1376 1377 static ImPlotLegendFlags flags = 0; 1378 1379 CHECKBOX_FLAG(flags, ImPlotLegendFlags_Horizontal); 1380 CHECKBOX_FLAG(flags, ImPlotLegendFlags_Outside); 1381 CHECKBOX_FLAG(flags, ImPlotLegendFlags_Sort); 1382 1383 ImGui::SliderFloat2("LegendPadding", (float*)&GetStyle().LegendPadding, 0.0f, 20.0f, "%.0f"); 1384 ImGui::SliderFloat2("LegendInnerPadding", (float*)&GetStyle().LegendInnerPadding, 0.0f, 10.0f, "%.0f"); 1385 ImGui::SliderFloat2("LegendSpacing", (float*)&GetStyle().LegendSpacing, 0.0f, 5.0f, "%.0f"); 1386 1387 static int num_dummy_items = 25; 1388 ImGui::SliderInt("Num Dummy Items (Demo Scrolling)", &num_dummy_items, 0, 100); 1389 1390 if (ImPlot::BeginPlot("##Legend",ImVec2(-1,0))) { 1391 ImPlot::SetupLegend(loc, flags); 1392 static MyImPlot::WaveData data1(0.001, 0.2, 4, 0.2); 1393 static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.4); 1394 static MyImPlot::WaveData data3(0.001, 0.2, 4, 0.6); 1395 static MyImPlot::WaveData data4(0.001, 0.2, 4, 0.8); 1396 static MyImPlot::WaveData data5(0.001, 0.2, 4, 1.0); 1397 1398 ImPlot::PlotLineG("Item 002", MyImPlot::SawWave, &data1, 1000); // "Item B" added to legend 1399 ImPlot::PlotLineG("Item 001##IDText", MyImPlot::SawWave, &data2, 1000); // "Item A" added to legend, text after ## used for ID only 1400 ImPlot::PlotLineG("##NotListed", MyImPlot::SawWave, &data3, 1000); // plotted, but not added to legend 1401 ImPlot::PlotLineG("Item 003", MyImPlot::SawWave, &data4, 1000); // "Item C" added to legend 1402 ImPlot::PlotLineG("Item 003", MyImPlot::SawWave, &data5, 1000); // combined with previous "Item C" 1403 1404 for (int i = 0; i < num_dummy_items; ++i) { 1405 char label[16]; 1406 snprintf(label, sizeof(label), "Item %03d", i+4); 1407 ImPlot::PlotDummy(label); 1408 } 1409 ImPlot::EndPlot(); 1410 } 1411} 1412 1413//----------------------------------------------------------------------------- 1414 1415void Demo_DragPoints() { 1416 ImGui::BulletText("Click and drag each point."); 1417 static ImPlotDragToolFlags flags = ImPlotDragToolFlags_None; 1418 ImGui::CheckboxFlags("NoCursors", (unsigned int*)&flags, ImPlotDragToolFlags_NoCursors); ImGui::SameLine(); 1419 ImGui::CheckboxFlags("NoFit", (unsigned int*)&flags, ImPlotDragToolFlags_NoFit); ImGui::SameLine(); 1420 ImGui::CheckboxFlags("NoInput", (unsigned int*)&flags, ImPlotDragToolFlags_NoInputs); 1421 ImPlotAxisFlags ax_flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoTickMarks; 1422 bool clicked[4] = {false, false, false, false}; 1423 bool hovered[4] = {false, false, false, false}; 1424 bool held[4] = {false, false, false, false}; 1425 if (ImPlot::BeginPlot("##Bezier",ImVec2(-1,0),ImPlotFlags_CanvasOnly)) { 1426 ImPlot::SetupAxes(nullptr,nullptr,ax_flags,ax_flags); 1427 ImPlot::SetupAxesLimits(0,1,0,1); 1428 static ImPlotPoint P[] = {ImPlotPoint(.05f,.05f), ImPlotPoint(0.2,0.4), ImPlotPoint(0.8,0.6), ImPlotPoint(.95f,.95f)}; 1429 1430 ImPlot::DragPoint(0,&P[0].x,&P[0].y, ImVec4(0,0.9f,0,1),4,flags, &clicked[0], &hovered[0], &held[0]); 1431 ImPlot::DragPoint(1,&P[1].x,&P[1].y, ImVec4(1,0.5f,1,1),4,flags, &clicked[1], &hovered[1], &held[1]); 1432 ImPlot::DragPoint(2,&P[2].x,&P[2].y, ImVec4(0,0.5f,1,1),4,flags, &clicked[2], &hovered[2], &held[2]); 1433 ImPlot::DragPoint(3,&P[3].x,&P[3].y, ImVec4(0,0.9f,0,1),4,flags, &clicked[3], &hovered[3], &held[3]); 1434 1435 static ImPlotPoint B[100]; 1436 for (int i = 0; i < 100; ++i) { 1437 double t = i / 99.0; 1438 double u = 1 - t; 1439 double w1 = u*u*u; 1440 double w2 = 3*u*u*t; 1441 double w3 = 3*u*t*t; 1442 double w4 = t*t*t; 1443 B[i] = ImPlotPoint(w1*P[0].x + w2*P[1].x + w3*P[2].x + w4*P[3].x, w1*P[0].y + w2*P[1].y + w3*P[2].y + w4*P[3].y); 1444 } 1445 1446 ImPlot::SetNextLineStyle(ImVec4(1,0.5f,1,1),hovered[1]||held[1] ? 2.0f : 1.0f); 1447 ImPlot::PlotLine("##h1",&P[0].x, &P[0].y, 2, 0, 0, sizeof(ImPlotPoint)); 1448 ImPlot::SetNextLineStyle(ImVec4(0,0.5f,1,1), hovered[2]||held[2] ? 2.0f : 1.0f); 1449 ImPlot::PlotLine("##h2",&P[2].x, &P[2].y, 2, 0, 0, sizeof(ImPlotPoint)); 1450 ImPlot::SetNextLineStyle(ImVec4(0,0.9f,0,1), hovered[0]||held[0]||hovered[3]||held[3] ? 3.0f : 2.0f); 1451 ImPlot::PlotLine("##bez",&B[0].x, &B[0].y, 100, 0, 0, sizeof(ImPlotPoint)); 1452 ImPlot::EndPlot(); 1453 } 1454} 1455 1456//----------------------------------------------------------------------------- 1457 1458void Demo_DragLines() { 1459 ImGui::BulletText("Click and drag the horizontal and vertical lines."); 1460 static double x1 = 0.2; 1461 static double x2 = 0.8; 1462 static double y1 = 0.25; 1463 static double y2 = 0.75; 1464 static double f = 0.1; 1465 bool clicked = false; 1466 bool hovered = false; 1467 bool held = false; 1468 static ImPlotDragToolFlags flags = ImPlotDragToolFlags_None; 1469 ImGui::CheckboxFlags("NoCursors", (unsigned int*)&flags, ImPlotDragToolFlags_NoCursors); ImGui::SameLine(); 1470 ImGui::CheckboxFlags("NoFit", (unsigned int*)&flags, ImPlotDragToolFlags_NoFit); ImGui::SameLine(); 1471 ImGui::CheckboxFlags("NoInput", (unsigned int*)&flags, ImPlotDragToolFlags_NoInputs); 1472 if (ImPlot::BeginPlot("##lines",ImVec2(-1,0))) { 1473 ImPlot::SetupAxesLimits(0,1,0,1); 1474 ImPlot::DragLineX(0,&x1,ImVec4(1,1,1,1),1,flags); 1475 ImPlot::DragLineX(1,&x2,ImVec4(1,1,1,1),1,flags); 1476 ImPlot::DragLineY(2,&y1,ImVec4(1,1,1,1),1,flags); 1477 ImPlot::DragLineY(3,&y2,ImVec4(1,1,1,1),1,flags); 1478 double xs[1000], ys[1000]; 1479 for (int i = 0; i < 1000; ++i) { 1480 xs[i] = (x2+x1)/2+fabs(x2-x1)*(i/1000.0f - 0.5f); 1481 ys[i] = (y1+y2)/2+fabs(y2-y1)/2*sin(f*i/10); 1482 } 1483 ImPlot::DragLineY(120482,&f,ImVec4(1,0.5f,1,1),1,flags, &clicked, &hovered, &held); 1484 ImPlot::SetNextLineStyle(IMPLOT_AUTO_COL, hovered||held ? 2.0f : 1.0f); 1485 ImPlot::PlotLine("Interactive Data", xs, ys, 1000); 1486 ImPlot::EndPlot(); 1487 } 1488} 1489 1490//----------------------------------------------------------------------------- 1491 1492void Demo_DragRects() { 1493 1494 static float x_data[512]; 1495 static float y_data1[512]; 1496 static float y_data2[512]; 1497 static float y_data3[512]; 1498 static float sampling_freq = 44100; 1499 static float freq = 500; 1500 bool clicked = false; 1501 bool hovered = false; 1502 bool held = false; 1503 for (size_t i = 0; i < 512; ++i) { 1504 const float t = i / sampling_freq; 1505 x_data[i] = t; 1506 const float arg = 2 * 3.14f * freq * t; 1507 y_data1[i] = sinf(arg); 1508 y_data2[i] = y_data1[i] * -0.6f + sinf(2 * arg) * 0.4f; 1509 y_data3[i] = y_data2[i] * -0.6f + sinf(3 * arg) * 0.4f; 1510 } 1511 ImGui::BulletText("Click and drag the edges, corners, and center of the rect."); 1512 ImGui::BulletText("Double click edges to expand rect to plot extents."); 1513 static ImPlotRect rect(0.0025,0.0045,0,0.5); 1514 static ImPlotDragToolFlags flags = ImPlotDragToolFlags_None; 1515 ImGui::CheckboxFlags("NoCursors", (unsigned int*)&flags, ImPlotDragToolFlags_NoCursors); ImGui::SameLine(); 1516 ImGui::CheckboxFlags("NoFit", (unsigned int*)&flags, ImPlotDragToolFlags_NoFit); ImGui::SameLine(); 1517 ImGui::CheckboxFlags("NoInput", (unsigned int*)&flags, ImPlotDragToolFlags_NoInputs); 1518 1519 if (ImPlot::BeginPlot("##Main",ImVec2(-1,150))) { 1520 ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoTickLabels,ImPlotAxisFlags_NoTickLabels); 1521 ImPlot::SetupAxesLimits(0,0.01,-1,1); 1522 ImPlot::PlotLine("Signal 1", x_data, y_data1, 512); 1523 ImPlot::PlotLine("Signal 2", x_data, y_data2, 512); 1524 ImPlot::PlotLine("Signal 3", x_data, y_data3, 512); 1525 ImPlot::DragRect(0,&rect.X.Min,&rect.Y.Min,&rect.X.Max,&rect.Y.Max,ImVec4(1,0,1,1),flags, &clicked, &hovered, &held); 1526 ImPlot::EndPlot(); 1527 } 1528 ImVec4 bg_col = held ? ImVec4(0.5f,0,0.5f,1) : (hovered ? ImVec4(0.25f,0,0.25f,1) : ImPlot::GetStyle().Colors[ImPlotCol_PlotBg]); 1529 ImPlot::PushStyleColor(ImPlotCol_PlotBg, bg_col); 1530 if (ImPlot::BeginPlot("##rect",ImVec2(-1,150), ImPlotFlags_CanvasOnly)) { 1531 ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations); 1532 ImPlot::SetupAxesLimits(rect.X.Min, rect.X.Max, rect.Y.Min, rect.Y.Max, ImGuiCond_Always); 1533 ImPlot::PlotLine("Signal 1", x_data, y_data1, 512); 1534 ImPlot::PlotLine("Signal 2", x_data, y_data2, 512); 1535 ImPlot::PlotLine("Signal 3", x_data, y_data3, 512); 1536 ImPlot::EndPlot(); 1537 } 1538 ImPlot::PopStyleColor(); 1539 ImGui::Text("Rect is %sclicked, %shovered, %sheld", clicked ? "" : "not ", hovered ? "" : "not ", held ? "" : "not "); 1540} 1541 1542//----------------------------------------------------------------------------- 1543 1544ImPlotPoint FindCentroid(const ImVector<ImPlotPoint>& data, const ImPlotRect& bounds, int& cnt) { 1545 cnt = 0; 1546 ImPlotPoint avg; 1547 ImPlotRect bounds_fixed; 1548 bounds_fixed.X.Min = bounds.X.Min < bounds.X.Max ? bounds.X.Min : bounds.X.Max; 1549 bounds_fixed.X.Max = bounds.X.Min < bounds.X.Max ? bounds.X.Max : bounds.X.Min; 1550 bounds_fixed.Y.Min = bounds.Y.Min < bounds.Y.Max ? bounds.Y.Min : bounds.Y.Max; 1551 bounds_fixed.Y.Max = bounds.Y.Min < bounds.Y.Max ? bounds.Y.Max : bounds.Y.Min; 1552 for (int i = 0; i < data.size(); ++i) { 1553 if (bounds_fixed.Contains(data[i].x, data[i].y)) { 1554 avg.x += data[i].x; 1555 avg.y += data[i].y; 1556 cnt++; 1557 } 1558 } 1559 if (cnt > 0) { 1560 avg.x = avg.x / cnt; 1561 avg.y = avg.y / cnt; 1562 } 1563 return avg; 1564} 1565 1566//----------------------------------------------------------------------------- 1567 1568void Demo_Querying() { 1569 static ImVector<ImPlotPoint> data; 1570 static ImVector<ImPlotRect> rects; 1571 static ImPlotRect limits, select; 1572 static bool init = true; 1573 if (init) { 1574 for (int i = 0; i < 50; ++i) 1575 { 1576 double x = RandomRange(0.1, 0.9); 1577 double y = RandomRange(0.1, 0.9); 1578 data.push_back(ImPlotPoint(x,y)); 1579 } 1580 init = false; 1581 } 1582 1583 ImGui::BulletText("Box select and left click mouse to create a new query rect."); 1584 ImGui::BulletText("Ctrl + click in the plot area to draw points."); 1585 1586 if (ImGui::Button("Clear Queries")) 1587 rects.shrink(0); 1588 1589 if (ImPlot::BeginPlot("##Centroid")) { 1590 ImPlot::SetupAxesLimits(0,1,0,1); 1591 if (ImPlot::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl) { 1592 ImPlotPoint pt = ImPlot::GetPlotMousePos(); 1593 data.push_back(pt); 1594 } 1595 ImPlot::PlotScatter("Points", &data[0].x, &data[0].y, data.size(), 0, 0, 2 * sizeof(double)); 1596 if (ImPlot::IsPlotSelected()) { 1597 select = ImPlot::GetPlotSelection(); 1598 int cnt; 1599 ImPlotPoint centroid = FindCentroid(data,select,cnt); 1600 if (cnt > 0) { 1601 ImPlot::SetNextMarkerStyle(ImPlotMarker_Square,6); 1602 ImPlot::PlotScatter("Centroid", &centroid.x, &centroid.y, 1); 1603 } 1604 if (ImGui::IsMouseClicked(ImPlot::GetInputMap().SelectCancel)) { 1605 CancelPlotSelection(); 1606 rects.push_back(select); 1607 } 1608 } 1609 for (int i = 0; i < rects.size(); ++i) { 1610 int cnt; 1611 ImPlotPoint centroid = FindCentroid(data,rects[i],cnt); 1612 if (cnt > 0) { 1613 ImPlot::SetNextMarkerStyle(ImPlotMarker_Square,6); 1614 ImPlot::PlotScatter("Centroid", &centroid.x, &centroid.y, 1); 1615 } 1616 ImPlot::DragRect(i,&rects[i].X.Min,&rects[i].Y.Min,&rects[i].X.Max,&rects[i].Y.Max,ImVec4(1,0,1,1)); 1617 } 1618 limits = ImPlot::GetPlotLimits(); 1619 ImPlot::EndPlot(); 1620 } 1621} 1622 1623//----------------------------------------------------------------------------- 1624 1625void Demo_Annotations() { 1626 static bool clamp = false; 1627 ImGui::Checkbox("Clamp",&clamp); 1628 if (ImPlot::BeginPlot("##Annotations")) { 1629 ImPlot::SetupAxesLimits(0,2,0,1); 1630 static float p[] = {0.25f, 0.25f, 0.75f, 0.75f, 0.25f}; 1631 ImPlot::PlotScatter("##Points",&p[0],&p[1],4); 1632 ImVec4 col = GetLastItemColor(); 1633 ImPlot::Annotation(0.25,0.25,col,ImVec2(-15,15),clamp,"BL"); 1634 ImPlot::Annotation(0.75,0.25,col,ImVec2(15,15),clamp,"BR"); 1635 ImPlot::Annotation(0.75,0.75,col,ImVec2(15,-15),clamp,"TR"); 1636 ImPlot::Annotation(0.25,0.75,col,ImVec2(-15,-15),clamp,"TL"); 1637 ImPlot::Annotation(0.5,0.5,col,ImVec2(0,0),clamp,"Center"); 1638 1639 ImPlot::Annotation(1.25,0.75,ImVec4(0,1,0,1),ImVec2(0,0),clamp); 1640 1641 float bx[] = {1.2f,1.5f,1.8f}; 1642 float by[] = {0.25f, 0.5f, 0.75f}; 1643 ImPlot::PlotBars("##Bars",bx,by,3,0.2); 1644 for (int i = 0; i < 3; ++i) 1645 ImPlot::Annotation(bx[i],by[i],ImVec4(0,0,0,0),ImVec2(0,-5),clamp,"B[%d]=%.2f",i,by[i]); 1646 ImPlot::EndPlot(); 1647 } 1648} 1649 1650//----------------------------------------------------------------------------- 1651 1652void Demo_Tags() { 1653 static bool show = true; 1654 ImGui::Checkbox("Show Tags",&show); 1655 if (ImPlot::BeginPlot("##Tags")) { 1656 ImPlot::SetupAxis(ImAxis_X2); 1657 ImPlot::SetupAxis(ImAxis_Y2); 1658 if (show) { 1659 ImPlot::TagX(0.25, ImVec4(1,1,0,1)); 1660 ImPlot::TagY(0.75, ImVec4(1,1,0,1)); 1661 static double drag_tag = 0.25; 1662 ImPlot::DragLineY(0,&drag_tag,ImVec4(1,0,0,1),1,ImPlotDragToolFlags_NoFit); 1663 ImPlot::TagY(drag_tag, ImVec4(1,0,0,1), "Drag"); 1664 SetAxes(ImAxis_X2, ImAxis_Y2); 1665 ImPlot::TagX(0.5, ImVec4(0,1,1,1), "%s", "MyTag"); 1666 ImPlot::TagY(0.5, ImVec4(0,1,1,1), "Tag: %d", 42); 1667 } 1668 ImPlot::EndPlot(); 1669 } 1670} 1671 1672//----------------------------------------------------------------------------- 1673 1674void Demo_DragAndDrop() { 1675 ImGui::BulletText("Drag/drop items from the left column."); 1676 ImGui::BulletText("Drag/drop items between plots."); 1677 ImGui::Indent(); 1678 ImGui::BulletText("Plot 1 Targets: Plot, Y-Axes, Legend"); 1679 ImGui::BulletText("Plot 1 Sources: Legend Item Labels"); 1680 ImGui::BulletText("Plot 2 Targets: Plot, X-Axis, Y-Axis"); 1681 ImGui::BulletText("Plot 2 Sources: Plot, X-Axis, Y-Axis (hold Ctrl)"); 1682 ImGui::Unindent(); 1683 1684 // convenience struct to manage DND items; do this however you like 1685 struct MyDndItem { 1686 int Idx; 1687 int Plt; 1688 ImAxis Yax; 1689 char Label[16]; 1690 ImVector<ImVec2> Data; 1691 ImVec4 Color; 1692 MyDndItem() { 1693 static int i = 0; 1694 Idx = i++; 1695 Plt = 0; 1696 Yax = ImAxis_Y1; 1697 snprintf(Label, sizeof(Label), "%02d Hz", Idx+1); 1698 Color = RandomColor(); 1699 Data.reserve(1001); 1700 for (int k = 0; k < 1001; ++k) { 1701 float t = k * 1.0f / 999; 1702 Data.push_back(ImVec2(t, 0.5f + 0.5f * sinf(2*3.14f*t*(Idx+1)))); 1703 } 1704 } 1705 void Reset() { Plt = 0; Yax = ImAxis_Y1; } 1706 }; 1707 1708 const int k_dnd = 20; 1709 static MyDndItem dnd[k_dnd]; 1710 static MyDndItem* dndx = nullptr; // for plot 2 1711 static MyDndItem* dndy = nullptr; // for plot 2 1712 1713 // child window to serve as initial source for our DND items 1714 ImGui::BeginChild("DND_LEFT",ImVec2(100,400)); 1715 if (ImGui::Button("Reset Data")) { 1716 for (int k = 0; k < k_dnd; ++k) 1717 dnd[k].Reset(); 1718 dndx = dndy = nullptr; 1719 } 1720 for (int k = 0; k < k_dnd; ++k) { 1721 if (dnd[k].Plt > 0) 1722 continue; 1723 ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine(); 1724 ImGui::Selectable(dnd[k].Label, false, 0, ImVec2(100, 0)); 1725 if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) { 1726 ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int)); 1727 ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine(); 1728 ImGui::TextUnformatted(dnd[k].Label); 1729 ImGui::EndDragDropSource(); 1730 } 1731 } 1732 ImGui::EndChild(); 1733 if (ImGui::BeginDragDropTarget()) { 1734 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { 1735 int i = *(int*)payload->Data; dnd[i].Reset(); 1736 } 1737 ImGui::EndDragDropTarget(); 1738 } 1739 1740 ImGui::SameLine(); 1741 ImGui::BeginChild("DND_RIGHT",ImVec2(-1,400)); 1742 // plot 1 (time series) 1743 ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoHighlight; 1744 if (ImPlot::BeginPlot("##DND1", ImVec2(-1,195))) { 1745 ImPlot::SetupAxis(ImAxis_X1, nullptr, flags|ImPlotAxisFlags_Lock); 1746 ImPlot::SetupAxis(ImAxis_Y1, "[drop here]", flags); 1747 ImPlot::SetupAxis(ImAxis_Y2, "[drop here]", flags|ImPlotAxisFlags_Opposite); 1748 ImPlot::SetupAxis(ImAxis_Y3, "[drop here]", flags|ImPlotAxisFlags_Opposite); 1749 1750 for (int k = 0; k < k_dnd; ++k) { 1751 if (dnd[k].Plt == 1 && dnd[k].Data.size() > 0) { 1752 ImPlot::SetAxis(dnd[k].Yax); 1753 ImPlot::SetNextLineStyle(dnd[k].Color); 1754 ImPlot::PlotLine(dnd[k].Label, &dnd[k].Data[0].x, &dnd[k].Data[0].y, dnd[k].Data.size(), 0, 0, 2 * sizeof(float)); 1755 // allow legend item labels to be DND sources 1756 if (ImPlot::BeginDragDropSourceItem(dnd[k].Label)) { 1757 ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int)); 1758 ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine(); 1759 ImGui::TextUnformatted(dnd[k].Label); 1760 ImPlot::EndDragDropSource(); 1761 } 1762 } 1763 } 1764 // allow the main plot area to be a DND target 1765 if (ImPlot::BeginDragDropTargetPlot()) { 1766 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { 1767 int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = ImAxis_Y1; 1768 } 1769 ImPlot::EndDragDropTarget(); 1770 } 1771 // allow each y-axis to be a DND target 1772 for (int y = ImAxis_Y1; y <= ImAxis_Y3; ++y) { 1773 if (ImPlot::BeginDragDropTargetAxis(y)) { 1774 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { 1775 int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = y; 1776 } 1777 ImPlot::EndDragDropTarget(); 1778 } 1779 } 1780 // allow the legend to be a DND target 1781 if (ImPlot::BeginDragDropTargetLegend()) { 1782 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { 1783 int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = ImAxis_Y1; 1784 } 1785 ImPlot::EndDragDropTarget(); 1786 } 1787 ImPlot::EndPlot(); 1788 } 1789 // plot 2 (Lissajous) 1790 if (ImPlot::BeginPlot("##DND2", ImVec2(-1,195))) { 1791 ImPlot::PushStyleColor(ImPlotCol_AxisBg, dndx != nullptr ? dndx->Color : ImPlot::GetStyle().Colors[ImPlotCol_AxisBg]); 1792 ImPlot::SetupAxis(ImAxis_X1, dndx == nullptr ? "[drop here]" : dndx->Label, flags); 1793 ImPlot::PushStyleColor(ImPlotCol_AxisBg, dndy != nullptr ? dndy->Color : ImPlot::GetStyle().Colors[ImPlotCol_AxisBg]); 1794 ImPlot::SetupAxis(ImAxis_Y1, dndy == nullptr ? "[drop here]" : dndy->Label, flags); 1795 ImPlot::PopStyleColor(2); 1796 if (dndx != nullptr && dndy != nullptr) { 1797 ImVec4 mixed((dndx->Color.x + dndy->Color.x)/2,(dndx->Color.y + dndy->Color.y)/2,(dndx->Color.z + dndy->Color.z)/2,(dndx->Color.w + dndy->Color.w)/2); 1798 ImPlot::SetNextLineStyle(mixed); 1799 ImPlot::PlotLine("##dndxy", &dndx->Data[0].y, &dndy->Data[0].y, dndx->Data.size(), 0, 0, 2 * sizeof(float)); 1800 } 1801 // allow the x-axis to be a DND target 1802 if (ImPlot::BeginDragDropTargetAxis(ImAxis_X1)) { 1803 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { 1804 int i = *(int*)payload->Data; dndx = &dnd[i]; 1805 } 1806 ImPlot::EndDragDropTarget(); 1807 } 1808 // allow the x-axis to be a DND source 1809 if (dndx != nullptr && ImPlot::BeginDragDropSourceAxis(ImAxis_X1)) { 1810 ImGui::SetDragDropPayload("MY_DND", &dndx->Idx, sizeof(int)); 1811 ImPlot::ItemIcon(dndx->Color); ImGui::SameLine(); 1812 ImGui::TextUnformatted(dndx->Label); 1813 ImPlot::EndDragDropSource(); 1814 } 1815 // allow the y-axis to be a DND target 1816 if (ImPlot::BeginDragDropTargetAxis(ImAxis_Y1)) { 1817 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { 1818 int i = *(int*)payload->Data; dndy = &dnd[i]; 1819 } 1820 ImPlot::EndDragDropTarget(); 1821 } 1822 // allow the y-axis to be a DND source 1823 if (dndy != nullptr && ImPlot::BeginDragDropSourceAxis(ImAxis_Y1)) { 1824 ImGui::SetDragDropPayload("MY_DND", &dndy->Idx, sizeof(int)); 1825 ImPlot::ItemIcon(dndy->Color); ImGui::SameLine(); 1826 ImGui::TextUnformatted(dndy->Label); 1827 ImPlot::EndDragDropSource(); 1828 } 1829 // allow the plot area to be a DND target 1830 if (ImPlot::BeginDragDropTargetPlot()) { 1831 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { 1832 int i = *(int*)payload->Data; dndx = dndy = &dnd[i]; 1833 } 1834 } 1835 // allow the plot area to be a DND source 1836 if (ImPlot::BeginDragDropSourcePlot()) { 1837 ImGui::TextUnformatted("Yes, you can\ndrag this!"); 1838 ImPlot::EndDragDropSource(); 1839 } 1840 ImPlot::EndPlot(); 1841 } 1842 ImGui::EndChild(); 1843} 1844 1845//----------------------------------------------------------------------------- 1846 1847void Demo_Tables() { 1848#ifdef IMGUI_HAS_TABLE 1849 static ImGuiTableFlags flags = ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | 1850 ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable; 1851 static bool anim = true; 1852 static int offset = 0; 1853 ImGui::BulletText("Plots can be used inside of ImGui tables as another means of creating subplots."); 1854 ImGui::Checkbox("Animate",&anim); 1855 if (anim) 1856 offset = (offset + 1) % 100; 1857 if (ImGui::BeginTable("##table", 3, flags, ImVec2(-1,0))) { 1858 ImGui::TableSetupColumn("Electrode", ImGuiTableColumnFlags_WidthFixed, 75.0f); 1859 ImGui::TableSetupColumn("Voltage", ImGuiTableColumnFlags_WidthFixed, 75.0f); 1860 ImGui::TableSetupColumn("EMG Signal"); 1861 ImGui::TableHeadersRow(); 1862 ImPlot::PushColormap(ImPlotColormap_Cool); 1863 for (int row = 0; row < 10; row++) { 1864 ImGui::TableNextRow(); 1865 static float data[100]; 1866 srand(row); 1867 for (int i = 0; i < 100; ++i) 1868 data[i] = RandomRange(0.0f,10.0f); 1869 ImGui::TableSetColumnIndex(0); 1870 ImGui::Text("EMG %d", row); 1871 ImGui::TableSetColumnIndex(1); 1872 ImGui::Text("%.3f V", data[offset]); 1873 ImGui::TableSetColumnIndex(2); 1874 ImGui::PushID(row); 1875 MyImPlot::Sparkline("##spark",data,100,0,11.0f,offset,ImPlot::GetColormapColor(row),ImVec2(-1, 35)); 1876 ImGui::PopID(); 1877 } 1878 ImPlot::PopColormap(); 1879 ImGui::EndTable(); 1880 } 1881#else 1882 ImGui::BulletText("You need to merge the ImGui 'tables' branch for this section."); 1883#endif 1884} 1885 1886//----------------------------------------------------------------------------- 1887 1888void Demo_OffsetAndStride() { 1889 static const int k_circles = 11; 1890 static const int k_points_per = 50; 1891 static const int k_size = 2 * k_points_per * k_circles; 1892 static double interleaved_data[k_size]; 1893 for (int p = 0; p < k_points_per; ++p) { 1894 for (int c = 0; c < k_circles; ++c) { 1895 double r = (double)c / (k_circles - 1) * 0.2 + 0.2; 1896 interleaved_data[p*2*k_circles + 2*c + 0] = 0.5 + r * cos((double)p/k_points_per * 6.28); 1897 interleaved_data[p*2*k_circles + 2*c + 1] = 0.5 + r * sin((double)p/k_points_per * 6.28); 1898 } 1899 } 1900 static int offset = 0; 1901 ImGui::BulletText("Offsetting is useful for realtime plots (see above) and circular buffers."); 1902 ImGui::BulletText("Striding is useful for interleaved data (e.g. audio) or plotting structs."); 1903 ImGui::BulletText("Here, all circle data is stored in a single interleaved buffer:"); 1904 ImGui::BulletText("[c0.x0 c0.y0 ... cn.x0 cn.y0 c0.x1 c0.y1 ... cn.x1 cn.y1 ... cn.xm cn.ym]"); 1905 ImGui::BulletText("The offset value indicates which circle point index is considered the first."); 1906 ImGui::BulletText("Offsets can be negative and/or larger than the actual data count."); 1907 ImGui::SliderInt("Offset", &offset, -2*k_points_per, 2*k_points_per); 1908 if (ImPlot::BeginPlot("##strideoffset",ImVec2(-1,0),ImPlotFlags_Equal)) { 1909 ImPlot::PushColormap(ImPlotColormap_Jet); 1910 char buff[32]; 1911 for (int c = 0; c < k_circles; ++c) { 1912 snprintf(buff, sizeof(buff), "Circle %d", c); 1913 ImPlot::PlotLine(buff, &interleaved_data[c*2 + 0], &interleaved_data[c*2 + 1], k_points_per, 0, offset, 2*k_circles*sizeof(double)); 1914 } 1915 ImPlot::EndPlot(); 1916 ImPlot::PopColormap(); 1917 } 1918 // offset++; uncomment for animation! 1919} 1920 1921//----------------------------------------------------------------------------- 1922 1923void Demo_CustomDataAndGetters() { 1924 ImGui::BulletText("You can plot custom structs using the stride feature."); 1925 ImGui::BulletText("Most plotters can also be passed a function pointer for getting data."); 1926 ImGui::Indent(); 1927 ImGui::BulletText("You can optionally pass user data to be given to your getter function."); 1928 ImGui::BulletText("C++ lambdas can be passed as function pointers as well!"); 1929 ImGui::Unindent(); 1930 1931 MyImPlot::Vector2f vec2_data[2] = { MyImPlot::Vector2f(0,0), MyImPlot::Vector2f(1,1) }; 1932 1933 if (ImPlot::BeginPlot("##Custom Data")) { 1934 1935 // custom structs using stride example: 1936 ImPlot::PlotLine("Vector2f", &vec2_data[0].x, &vec2_data[0].y, 2, 0, 0, sizeof(MyImPlot::Vector2f) /* or sizeof(float) * 2 */); 1937 1938 // custom getter example 1: 1939 ImPlot::PlotLineG("Spiral", MyImPlot::Spiral, nullptr, 1000); 1940 1941 // custom getter example 2: 1942 static MyImPlot::WaveData data1(0.001, 0.2, 2, 0.75); 1943 static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.25); 1944 ImPlot::PlotLineG("Waves", MyImPlot::SineWave, &data1, 1000); 1945 ImPlot::PlotLineG("Waves", MyImPlot::SawWave, &data2, 1000); 1946 ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); 1947 ImPlot::PlotShadedG("Waves", MyImPlot::SineWave, &data1, MyImPlot::SawWave, &data2, 1000); 1948 ImPlot::PopStyleVar(); 1949 1950 // you can also pass C++ lambdas: 1951 // auto lamda = [](void* data, int idx) { ... return ImPlotPoint(x,y); }; 1952 // ImPlot::PlotLine("My Lambda", lambda, data, 1000); 1953 1954 ImPlot::EndPlot(); 1955 } 1956} 1957 1958//----------------------------------------------------------------------------- 1959 1960int MetricFormatter(double value, char* buff, int size, void* data) { 1961 const char* unit = (const char*)data; 1962 static double v[] = {1000000000,1000000,1000,1,0.001,0.000001,0.000000001}; 1963 static const char* p[] = {"G","M","k","","m","u","n"}; 1964 if (value == 0) { 1965 return snprintf(buff,size,"0 %s", unit); 1966 } 1967 for (int i = 0; i < 7; ++i) { 1968 if (fabs(value) >= v[i]) { 1969 return snprintf(buff,size,"%g %s%s",value/v[i],p[i],unit); 1970 } 1971 } 1972 return snprintf(buff,size,"%g %s%s",value/v[6],p[6],unit); 1973} 1974 1975void Demo_TickLabels() { 1976 static bool custom_fmt = true; 1977 static bool custom_ticks = false; 1978 static bool custom_labels = true; 1979 ImGui::Checkbox("Show Custom Format", &custom_fmt); 1980 ImGui::SameLine(); 1981 ImGui::Checkbox("Show Custom Ticks", &custom_ticks); 1982 if (custom_ticks) { 1983 ImGui::SameLine(); 1984 ImGui::Checkbox("Show Custom Labels", &custom_labels); 1985 } 1986 const double pi = 3.14; 1987 const char* pi_str[] = {"PI"}; 1988 static double yticks[] = {100,300,700,900}; 1989 static const char* ylabels[] = {"One","Three","Seven","Nine"}; 1990 static double yticks_aux[] = {0.2,0.4,0.6}; 1991 static const char* ylabels_aux[] = {"A","B","C","D","E","F"}; 1992 if (ImPlot::BeginPlot("##Ticks")) { 1993 ImPlot::SetupAxesLimits(2.5,5,0,1000); 1994 ImPlot::SetupAxis(ImAxis_Y2, nullptr, ImPlotAxisFlags_AuxDefault); 1995 ImPlot::SetupAxis(ImAxis_Y3, nullptr, ImPlotAxisFlags_AuxDefault); 1996 if (custom_fmt) { 1997 ImPlot::SetupAxisFormat(ImAxis_X1, "%g ms"); 1998 ImPlot::SetupAxisFormat(ImAxis_Y1, MetricFormatter, (void*)"Hz"); 1999 ImPlot::SetupAxisFormat(ImAxis_Y2, "%g dB"); 2000 ImPlot::SetupAxisFormat(ImAxis_Y3, MetricFormatter, (void*)"m"); 2001 } 2002 if (custom_ticks) { 2003 ImPlot::SetupAxisTicks(ImAxis_X1, &pi,1,custom_labels ? pi_str : nullptr, true); 2004 ImPlot::SetupAxisTicks(ImAxis_Y1, yticks, 4, custom_labels ? ylabels : nullptr, false); 2005 ImPlot::SetupAxisTicks(ImAxis_Y2, yticks_aux, 3, custom_labels ? ylabels_aux : nullptr, false); 2006 ImPlot::SetupAxisTicks(ImAxis_Y3, 0, 1, 6, custom_labels ? ylabels_aux : nullptr, false); 2007 } 2008 ImPlot::EndPlot(); 2009 } 2010} 2011 2012//----------------------------------------------------------------------------- 2013 2014void Demo_CustomStyles() { 2015 ImPlot::PushColormap(ImPlotColormap_Deep); 2016 // normally you wouldn't change the entire style each frame 2017 ImPlotStyle backup = ImPlot::GetStyle(); 2018 MyImPlot::StyleSeaborn(); 2019 if (ImPlot::BeginPlot("seaborn style")) { 2020 ImPlot::SetupAxes( "x-axis", "y-axis"); 2021 ImPlot::SetupAxesLimits(-0.5f, 9.5f, 0, 10); 2022 unsigned int lin[10] = {8,8,9,7,8,8,8,9,7,8}; 2023 unsigned int bar[10] = {1,2,5,3,4,1,2,5,3,4}; 2024 unsigned int dot[10] = {7,6,6,7,8,5,6,5,8,7}; 2025 ImPlot::PlotBars("Bars", bar, 10, 0.5f); 2026 ImPlot::PlotLine("Line", lin, 10); 2027 ImPlot::NextColormapColor(); // skip green 2028 ImPlot::PlotScatter("Scatter", dot, 10); 2029 ImPlot::EndPlot(); 2030 } 2031 ImPlot::GetStyle() = backup; 2032 ImPlot::PopColormap(); 2033} 2034 2035//----------------------------------------------------------------------------- 2036 2037void Demo_CustomRendering() { 2038 if (ImPlot::BeginPlot("##CustomRend")) { 2039 ImVec2 cntr = ImPlot::PlotToPixels(ImPlotPoint(0.5f, 0.5f)); 2040 ImVec2 rmin = ImPlot::PlotToPixels(ImPlotPoint(0.25f, 0.75f)); 2041 ImVec2 rmax = ImPlot::PlotToPixels(ImPlotPoint(0.75f, 0.25f)); 2042 ImPlot::PushPlotClipRect(); 2043 ImPlot::GetPlotDrawList()->AddCircleFilled(cntr,20,IM_COL32(255,255,0,255),20); 2044 ImPlot::GetPlotDrawList()->AddRect(rmin, rmax, IM_COL32(128,0,255,255)); 2045 ImPlot::PopPlotClipRect(); 2046 ImPlot::EndPlot(); 2047 } 2048} 2049 2050//----------------------------------------------------------------------------- 2051 2052void Demo_LegendPopups() { 2053 ImGui::BulletText("You can implement legend context menus to inject per-item controls and widgets."); 2054 ImGui::BulletText("Right click the legend label/icon to edit custom item attributes."); 2055 2056 static float frequency = 0.1f; 2057 static float amplitude = 0.5f; 2058 static ImVec4 color = ImVec4(1,1,0,1); 2059 static float alpha = 1.0f; 2060 static bool line = false; 2061 static float thickness = 1; 2062 static bool markers = false; 2063 static bool shaded = false; 2064 2065 static float vals[101]; 2066 for (int i = 0; i < 101; ++i) 2067 vals[i] = amplitude * sinf(frequency * i); 2068 2069 if (ImPlot::BeginPlot("Right Click the Legend")) { 2070 ImPlot::SetupAxesLimits(0,100,-1,1); 2071 // rendering logic 2072 ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha); 2073 if (!line) { 2074 ImPlot::SetNextFillStyle(color); 2075 ImPlot::PlotBars("Right Click Me", vals, 101); 2076 } 2077 else { 2078 if (markers) ImPlot::SetNextMarkerStyle(ImPlotMarker_Square); 2079 ImPlot::SetNextLineStyle(color, thickness); 2080 ImPlot::PlotLine("Right Click Me", vals, 101); 2081 if (shaded) ImPlot::PlotShaded("Right Click Me",vals,101); 2082 } 2083 ImPlot::PopStyleVar(); 2084 // custom legend context menu 2085 if (ImPlot::BeginLegendPopup("Right Click Me")) { 2086 ImGui::SliderFloat("Frequency",&frequency,0,1,"%0.2f"); 2087 ImGui::SliderFloat("Amplitude",&amplitude,0,1,"%0.2f"); 2088 ImGui::Separator(); 2089 ImGui::ColorEdit3("Color",&color.x); 2090 ImGui::SliderFloat("Transparency",&alpha,0,1,"%.2f"); 2091 ImGui::Checkbox("Line Plot", &line); 2092 if (line) { 2093 ImGui::SliderFloat("Thickness", &thickness, 0, 5); 2094 ImGui::Checkbox("Markers", &markers); 2095 ImGui::Checkbox("Shaded",&shaded); 2096 } 2097 ImPlot::EndLegendPopup(); 2098 } 2099 ImPlot::EndPlot(); 2100 } 2101} 2102 2103//----------------------------------------------------------------------------- 2104 2105void Demo_ColormapWidgets() { 2106 static int cmap = ImPlotColormap_Viridis; 2107 2108 if (ImPlot::ColormapButton("Button",ImVec2(0,0),cmap)) { 2109 cmap = (cmap + 1) % ImPlot::GetColormapCount(); 2110 } 2111 2112 static float t = 0.5f; 2113 static ImVec4 col; 2114 ImGui::ColorButton("##Display",col,ImGuiColorEditFlags_NoInputs); 2115 ImGui::SameLine(); 2116 ImPlot::ColormapSlider("Slider", &t, &col, "%.3f", cmap); 2117 2118 ImPlot::ColormapIcon(cmap); ImGui::SameLine(); ImGui::Text("Icon"); 2119 2120 static ImPlotColormapScaleFlags flags = 0; 2121 static float scale[2] = {0, 100}; 2122 ImPlot::ColormapScale("Scale",scale[0],scale[1],ImVec2(0,0),"%g dB",flags,cmap); 2123 ImGui::InputFloat2("Scale",scale); 2124 CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_NoLabel); 2125 CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Opposite); 2126 CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Invert); 2127} 2128 2129//----------------------------------------------------------------------------- 2130 2131void Demo_CustomPlottersAndTooltips() { 2132 ImGui::BulletText("You can create custom plotters or extend ImPlot using implot_internal.h."); 2133 double dates[] = {1546300800,1546387200,1546473600,1546560000,1546819200,1546905600,1546992000,1547078400,1547164800,1547424000,1547510400,1547596800,1547683200,1547769600,1547942400,1548028800,1548115200,1548201600,1548288000,1548374400,1548633600,1548720000,1548806400,1548892800,1548979200,1549238400,1549324800,1549411200,1549497600,1549584000,1549843200,1549929600,1550016000,1550102400,1550188800,1550361600,1550448000,1550534400,1550620800,1550707200,1550793600,1551052800,1551139200,1551225600,1551312000,1551398400,1551657600,1551744000,1551830400,1551916800,1552003200,1552262400,1552348800,1552435200,1552521600,1552608000,1552867200,1552953600,1553040000,1553126400,1553212800,1553472000,1553558400,1553644800,1553731200,1553817600,1554076800,1554163200,1554249600,1554336000,1554422400,1554681600,1554768000,1554854400,1554940800,1555027200,1555286400,1555372800,1555459200,1555545600,1555632000,1555891200,1555977600,1556064000,1556150400,1556236800,1556496000,1556582400,1556668800,1556755200,1556841600,1557100800,1557187200,1557273600,1557360000,1557446400,1557705600,1557792000,1557878400,1557964800,1558051200,1558310400,1558396800,1558483200,1558569600,1558656000,1558828800,1558915200,1559001600,1559088000,1559174400,1559260800,1559520000,1559606400,1559692800,1559779200,1559865600,1560124800,1560211200,1560297600,1560384000,1560470400,1560729600,1560816000,1560902400,1560988800,1561075200,1561334400,1561420800,1561507200,1561593600,1561680000,1561939200,1562025600,1562112000,1562198400,1562284800,1562544000,1562630400,1562716800,1562803200,1562889600,1563148800,1563235200,1563321600,1563408000,1563494400,1563753600,1563840000,1563926400,1564012800,1564099200,1564358400,1564444800,1564531200,1564617600,1564704000,1564963200,1565049600,1565136000,1565222400,1565308800,1565568000,1565654400,1565740800,1565827200,1565913600,1566172800,1566259200,1566345600,1566432000,1566518400,1566777600,1566864000,1566950400,1567036800,1567123200,1567296000,1567382400,1567468800,1567555200,1567641600,1567728000,1567987200,1568073600,1568160000,1568246400,1568332800,1568592000,1568678400,1568764800,1568851200,1568937600,1569196800,1569283200,1569369600,1569456000,1569542400,1569801600,1569888000,1569974400,1570060800,1570147200,1570406400,1570492800,1570579200,1570665600,1570752000,1571011200,1571097600,1571184000,1571270400,1571356800,1571616000,1571702400,1571788800,1571875200,1571961600}; 2134 double opens[] = {1284.7,1319.9,1318.7,1328,1317.6,1321.6,1314.3,1325,1319.3,1323.1,1324.7,1321.3,1323.5,1322,1281.3,1281.95,1311.1,1315,1314,1313.1,1331.9,1334.2,1341.3,1350.6,1349.8,1346.4,1343.4,1344.9,1335.6,1337.9,1342.5,1337,1338.6,1337,1340.4,1324.65,1324.35,1349.5,1371.3,1367.9,1351.3,1357.8,1356.1,1356,1347.6,1339.1,1320.6,1311.8,1314,1312.4,1312.3,1323.5,1319.1,1327.2,1332.1,1320.3,1323.1,1328,1330.9,1338,1333,1335.3,1345.2,1341.1,1332.5,1314,1314.4,1310.7,1314,1313.1,1315,1313.7,1320,1326.5,1329.2,1314.2,1312.3,1309.5,1297.4,1293.7,1277.9,1295.8,1295.2,1290.3,1294.2,1298,1306.4,1299.8,1302.3,1297,1289.6,1302,1300.7,1303.5,1300.5,1303.2,1306,1318.7,1315,1314.5,1304.1,1294.7,1293.7,1291.2,1290.2,1300.4,1284.2,1284.25,1301.8,1295.9,1296.2,1304.4,1323.1,1340.9,1341,1348,1351.4,1351.4,1343.5,1342.3,1349,1357.6,1357.1,1354.7,1361.4,1375.2,1403.5,1414.7,1433.2,1438,1423.6,1424.4,1418,1399.5,1435.5,1421.25,1434.1,1412.4,1409.8,1412.2,1433.4,1418.4,1429,1428.8,1420.6,1441,1460.4,1441.7,1438.4,1431,1439.3,1427.4,1431.9,1439.5,1443.7,1425.6,1457.5,1451.2,1481.1,1486.7,1512.1,1515.9,1509.2,1522.3,1513,1526.6,1533.9,1523,1506.3,1518.4,1512.4,1508.8,1545.4,1537.3,1551.8,1549.4,1536.9,1535.25,1537.95,1535.2,1556,1561.4,1525.6,1516.4,1507,1493.9,1504.9,1506.5,1513.1,1506.5,1509.7,1502,1506.8,1521.5,1529.8,1539.8,1510.9,1511.8,1501.7,1478,1485.4,1505.6,1511.6,1518.6,1498.7,1510.9,1510.8,1498.3,1492,1497.7,1484.8,1494.2,1495.6,1495.6,1487.5,1491.1,1495.1,1506.4}; 2135 double highs[] = {1284.75,1320.6,1327,1330.8,1326.8,1321.6,1326,1328,1325.8,1327.1,1326,1326,1323.5,1322.1,1282.7,1282.95,1315.8,1316.3,1314,1333.2,1334.7,1341.7,1353.2,1354.6,1352.2,1346.4,1345.7,1344.9,1340.7,1344.2,1342.7,1342.1,1345.2,1342,1350,1324.95,1330.75,1369.6,1374.3,1368.4,1359.8,1359,1357,1356,1353.4,1340.6,1322.3,1314.1,1316.1,1312.9,1325.7,1323.5,1326.3,1336,1332.1,1330.1,1330.4,1334.7,1341.1,1344.2,1338.8,1348.4,1345.6,1342.8,1334.7,1322.3,1319.3,1314.7,1316.6,1316.4,1315,1325.4,1328.3,1332.2,1329.2,1316.9,1312.3,1309.5,1299.6,1296.9,1277.9,1299.5,1296.2,1298.4,1302.5,1308.7,1306.4,1305.9,1307,1297.2,1301.7,1305,1305.3,1310.2,1307,1308,1319.8,1321.7,1318.7,1316.2,1305.9,1295.8,1293.8,1293.7,1304.2,1302,1285.15,1286.85,1304,1302,1305.2,1323,1344.1,1345.2,1360.1,1355.3,1363.8,1353,1344.7,1353.6,1358,1373.6,1358.2,1369.6,1377.6,1408.9,1425.5,1435.9,1453.7,1438,1426,1439.1,1418,1435,1452.6,1426.65,1437.5,1421.5,1414.1,1433.3,1441.3,1431.4,1433.9,1432.4,1440.8,1462.3,1467,1443.5,1444,1442.9,1447,1437.6,1440.8,1445.7,1447.8,1458.2,1461.9,1481.8,1486.8,1522.7,1521.3,1521.1,1531.5,1546.1,1534.9,1537.7,1538.6,1523.6,1518.8,1518.4,1514.6,1540.3,1565,1554.5,1556.6,1559.8,1541.9,1542.9,1540.05,1558.9,1566.2,1561.9,1536.2,1523.8,1509.1,1506.2,1532.2,1516.6,1519.7,1515,1519.5,1512.1,1524.5,1534.4,1543.3,1543.3,1542.8,1519.5,1507.2,1493.5,1511.4,1525.8,1522.2,1518.8,1515.3,1518,1522.3,1508,1501.5,1503,1495.5,1501.1,1497.9,1498.7,1492.1,1499.4,1506.9,1520.9}; 2136 double lows[] = {1282.85,1315,1318.7,1309.6,1317.6,1312.9,1312.4,1319.1,1319,1321,1318.1,1321.3,1319.9,1312,1280.5,1276.15,1308,1309.9,1308.5,1312.3,1329.3,1333.1,1340.2,1347,1345.9,1338,1340.8,1335,1332,1337.9,1333,1336.8,1333.2,1329.9,1340.4,1323.85,1324.05,1349,1366.3,1351.2,1349.1,1352.4,1350.7,1344.3,1338.9,1316.3,1308.4,1306.9,1309.6,1306.7,1312.3,1315.4,1319,1327.2,1317.2,1320,1323,1328,1323,1327.8,1331.7,1335.3,1336.6,1331.8,1311.4,1310,1309.5,1308,1310.6,1302.8,1306.6,1313.7,1320,1322.8,1311,1312.1,1303.6,1293.9,1293.5,1291,1277.9,1294.1,1286,1289.1,1293.5,1296.9,1298,1299.6,1292.9,1285.1,1288.5,1296.3,1297.2,1298.4,1298.6,1302,1300.3,1312,1310.8,1301.9,1292,1291.1,1286.3,1289.2,1289.9,1297.4,1283.65,1283.25,1292.9,1295.9,1290.8,1304.2,1322.7,1336.1,1341,1343.5,1345.8,1340.3,1335.1,1341.5,1347.6,1352.8,1348.2,1353.7,1356.5,1373.3,1398,1414.7,1427,1416.4,1412.7,1420.1,1396.4,1398.8,1426.6,1412.85,1400.7,1406,1399.8,1404.4,1415.5,1417.2,1421.9,1415,1413.7,1428.1,1434,1435.7,1427.5,1429.4,1423.9,1425.6,1427.5,1434.8,1422.3,1412.1,1442.5,1448.8,1468.2,1484.3,1501.6,1506.2,1498.6,1488.9,1504.5,1518.3,1513.9,1503.3,1503,1506.5,1502.1,1503,1534.8,1535.3,1541.4,1528.6,1525.6,1535.25,1528.15,1528,1542.6,1514.3,1510.7,1505.5,1492.1,1492.9,1496.8,1493.1,1503.4,1500.9,1490.7,1496.3,1505.3,1505.3,1517.9,1507.4,1507.1,1493.3,1470.5,1465,1480.5,1501.7,1501.4,1493.3,1492.1,1505.1,1495.7,1478,1487.1,1480.8,1480.6,1487,1488.3,1484.8,1484,1490.7,1490.4,1503.1}; 2137 double closes[] = {1283.35,1315.3,1326.1,1317.4,1321.5,1317.4,1323.5,1319.2,1321.3,1323.3,1319.7,1325.1,1323.6,1313.8,1282.05,1279.05,1314.2,1315.2,1310.8,1329.1,1334.5,1340.2,1340.5,1350,1347.1,1344.3,1344.6,1339.7,1339.4,1343.7,1337,1338.9,1340.1,1338.7,1346.8,1324.25,1329.55,1369.6,1372.5,1352.4,1357.6,1354.2,1353.4,1346,1341,1323.8,1311.9,1309.1,1312.2,1310.7,1324.3,1315.7,1322.4,1333.8,1319.4,1327.1,1325.8,1330.9,1325.8,1331.6,1336.5,1346.7,1339.2,1334.7,1313.3,1316.5,1312.4,1313.4,1313.3,1312.2,1313.7,1319.9,1326.3,1331.9,1311.3,1313.4,1309.4,1295.2,1294.7,1294.1,1277.9,1295.8,1291.2,1297.4,1297.7,1306.8,1299.4,1303.6,1302.2,1289.9,1299.2,1301.8,1303.6,1299.5,1303.2,1305.3,1319.5,1313.6,1315.1,1303.5,1293,1294.6,1290.4,1291.4,1302.7,1301,1284.15,1284.95,1294.3,1297.9,1304.1,1322.6,1339.3,1340.1,1344.9,1354,1357.4,1340.7,1342.7,1348.2,1355.1,1355.9,1354.2,1362.1,1360.1,1408.3,1411.2,1429.5,1430.1,1426.8,1423.4,1425.1,1400.8,1419.8,1432.9,1423.55,1412.1,1412.2,1412.8,1424.9,1419.3,1424.8,1426.1,1423.6,1435.9,1440.8,1439.4,1439.7,1434.5,1436.5,1427.5,1432.2,1433.3,1441.8,1437.8,1432.4,1457.5,1476.5,1484.2,1519.6,1509.5,1508.5,1517.2,1514.1,1527.8,1531.2,1523.6,1511.6,1515.7,1515.7,1508.5,1537.6,1537.2,1551.8,1549.1,1536.9,1529.4,1538.05,1535.15,1555.9,1560.4,1525.5,1515.5,1511.1,1499.2,1503.2,1507.4,1499.5,1511.5,1513.4,1515.8,1506.2,1515.1,1531.5,1540.2,1512.3,1515.2,1506.4,1472.9,1489,1507.9,1513.8,1512.9,1504.4,1503.9,1512.8,1500.9,1488.7,1497.6,1483.5,1494,1498.3,1494.1,1488.1,1487.5,1495.7,1504.7,1505.3}; 2138 static bool tooltip = true; 2139 ImGui::Checkbox("Show Tooltip", &tooltip); 2140 ImGui::SameLine(); 2141 static ImVec4 bullCol = ImVec4(0.000f, 1.000f, 0.441f, 1.000f); 2142 static ImVec4 bearCol = ImVec4(0.853f, 0.050f, 0.310f, 1.000f); 2143 ImGui::SameLine(); ImGui::ColorEdit4("##Bull", &bullCol.x, ImGuiColorEditFlags_NoInputs); 2144 ImGui::SameLine(); ImGui::ColorEdit4("##Bear", &bearCol.x, ImGuiColorEditFlags_NoInputs); 2145 ImPlot::GetStyle().UseLocalTime = false; 2146 2147 if (ImPlot::BeginPlot("Candlestick Chart",ImVec2(-1,0))) { 2148 ImPlot::SetupAxes(nullptr,nullptr,0,ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit); 2149 ImPlot::SetupAxesLimits(1546300800, 1571961600, 1250, 1600); 2150 ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Time); 2151 ImPlot::SetupAxisLimitsConstraints(ImAxis_X1, 1546300800, 1571961600); 2152 ImPlot::SetupAxisZoomConstraints(ImAxis_X1, 60*60*24*14, 1571961600-1546300800); 2153 ImPlot::SetupAxisFormat(ImAxis_Y1, "$%.0f"); 2154 MyImPlot::PlotCandlestick("GOOGL",dates, opens, closes, lows, highs, 218, tooltip, 0.25f, bullCol, bearCol); 2155 ImPlot::EndPlot(); 2156 } 2157 } 2158 2159//----------------------------------------------------------------------------- 2160// DEMO WINDOW 2161//----------------------------------------------------------------------------- 2162 2163void DemoHeader(const char* label, void(*demo)()) { 2164 if (ImGui::TreeNodeEx(label)) { 2165 demo(); 2166 ImGui::TreePop(); 2167 } 2168} 2169 2170void ShowDemoWindow(bool* p_open) { 2171 static bool show_implot_metrics = false; 2172 static bool show_implot_style_editor = false; 2173 static bool show_imgui_metrics = false; 2174 static bool show_imgui_style_editor = false; 2175 static bool show_imgui_demo = false; 2176 2177 if (show_implot_metrics) { 2178 ImPlot::ShowMetricsWindow(&show_implot_metrics); 2179 } 2180 if (show_implot_style_editor) { 2181 ImGui::SetNextWindowSize(ImVec2(415,762), ImGuiCond_Appearing); 2182 ImGui::Begin("Style Editor (ImPlot)", &show_implot_style_editor); 2183 ImPlot::ShowStyleEditor(); 2184 ImGui::End(); 2185 } 2186 if (show_imgui_style_editor) { 2187 ImGui::Begin("Style Editor (ImGui)", &show_imgui_style_editor); 2188 ImGui::ShowStyleEditor(); 2189 ImGui::End(); 2190 } 2191 if (show_imgui_metrics) { 2192 ImGui::ShowMetricsWindow(&show_imgui_metrics); 2193 } 2194 if (show_imgui_demo) { 2195 ImGui::ShowDemoWindow(&show_imgui_demo); 2196 } 2197 ImGui::SetNextWindowPos(ImVec2(50, 50), ImGuiCond_FirstUseEver); 2198 ImGui::SetNextWindowSize(ImVec2(600, 750), ImGuiCond_FirstUseEver); 2199 ImGui::Begin("ImPlot Demo", p_open, ImGuiWindowFlags_MenuBar); 2200 if (ImGui::BeginMenuBar()) { 2201 if (ImGui::BeginMenu("Tools")) { 2202 ImGui::MenuItem("Metrics", nullptr, &show_implot_metrics); 2203 ImGui::MenuItem("Style Editor", nullptr, &show_implot_style_editor); 2204 ImGui::Separator(); 2205 ImGui::MenuItem("ImGui Metrics", nullptr, &show_imgui_metrics); 2206 ImGui::MenuItem("ImGui Style Editor", nullptr, &show_imgui_style_editor); 2207 ImGui::MenuItem("ImGui Demo", nullptr, &show_imgui_demo); 2208 ImGui::EndMenu(); 2209 } 2210 ImGui::EndMenuBar(); 2211 } 2212 //------------------------------------------------------------------------- 2213 ImGui::Text("ImPlot says hello. (%s)", IMPLOT_VERSION); 2214 // display warning about 16-bit indices 2215 static bool showWarning = sizeof(ImDrawIdx)*8 == 16 && (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) == false; 2216 if (showWarning) { 2217 ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1,1,0,1)); 2218 ImGui::TextWrapped("WARNING: ImDrawIdx is 16-bit and ImGuiBackendFlags_RendererHasVtxOffset is false. Expect visual glitches and artifacts! See README for more information."); 2219 ImGui::PopStyleColor(); 2220 } 2221 2222 ImGui::Spacing(); 2223 2224 if (ImGui::BeginTabBar("ImPlotDemoTabs")) { 2225 if (ImGui::BeginTabItem("Plots")) { 2226 DemoHeader("Line Plots", Demo_LinePlots); 2227 DemoHeader("Filled Line Plots", Demo_FilledLinePlots); 2228 DemoHeader("Shaded Plots##", Demo_ShadedPlots); 2229 DemoHeader("Scatter Plots", Demo_ScatterPlots); 2230 DemoHeader("Realtime Plots", Demo_RealtimePlots); 2231 DemoHeader("Stairstep Plots", Demo_StairstepPlots); 2232 DemoHeader("Bar Plots", Demo_BarPlots); 2233 DemoHeader("Bar Groups", Demo_BarGroups); 2234 DemoHeader("Bar Stacks", Demo_BarStacks); 2235 DemoHeader("Error Bars", Demo_ErrorBars); 2236 DemoHeader("Stem Plots##", Demo_StemPlots); 2237 DemoHeader("Infinite Lines", Demo_InfiniteLines); 2238 DemoHeader("Pie Charts", Demo_PieCharts); 2239 DemoHeader("Heatmaps", Demo_Heatmaps); 2240 DemoHeader("Histogram", Demo_Histogram); 2241 DemoHeader("Histogram 2D", Demo_Histogram2D); 2242 DemoHeader("Digital Plots", Demo_DigitalPlots); 2243 DemoHeader("Images", Demo_Images); 2244 DemoHeader("Markers and Text", Demo_MarkersAndText); 2245 DemoHeader("NaN Values", Demo_NaNValues); 2246 ImGui::EndTabItem(); 2247 } 2248 if (ImGui::BeginTabItem("Subplots")) { 2249 DemoHeader("Sizing", Demo_SubplotsSizing); 2250 DemoHeader("Item Sharing", Demo_SubplotItemSharing); 2251 DemoHeader("Axis Linking", Demo_SubplotAxisLinking); 2252 DemoHeader("Tables", Demo_Tables); 2253 ImGui::EndTabItem(); 2254 } 2255 if (ImGui::BeginTabItem("Axes")) { 2256 DemoHeader("Log Scale", Demo_LogScale); 2257 DemoHeader("Symmetric Log Scale", Demo_SymmetricLogScale); 2258 DemoHeader("Time Scale", Demo_TimeScale); 2259 DemoHeader("Custom Scale", Demo_CustomScale); 2260 DemoHeader("Multiple Axes", Demo_MultipleAxes); 2261 DemoHeader("Tick Labels", Demo_TickLabels); 2262 DemoHeader("Linked Axes", Demo_LinkedAxes); 2263 DemoHeader("Axis Constraints", Demo_AxisConstraints); 2264 DemoHeader("Equal Axes", Demo_EqualAxes); 2265 DemoHeader("Auto-Fitting Data", Demo_AutoFittingData); 2266 ImGui::EndTabItem(); 2267 } 2268 if (ImGui::BeginTabItem("Tools")) { 2269 DemoHeader("Offset and Stride", Demo_OffsetAndStride); 2270 DemoHeader("Drag Points", Demo_DragPoints); 2271 DemoHeader("Drag Lines", Demo_DragLines); 2272 DemoHeader("Drag Rects", Demo_DragRects); 2273 DemoHeader("Querying", Demo_Querying); 2274 DemoHeader("Annotations", Demo_Annotations); 2275 DemoHeader("Tags", Demo_Tags); 2276 DemoHeader("Drag and Drop", Demo_DragAndDrop); 2277 DemoHeader("Legend Options", Demo_LegendOptions); 2278 DemoHeader("Legend Popups", Demo_LegendPopups); 2279 DemoHeader("Colormap Widgets", Demo_ColormapWidgets); 2280 ImGui::EndTabItem(); 2281 } 2282 if (ImGui::BeginTabItem("Custom")) { 2283 DemoHeader("Custom Styles", Demo_CustomStyles); 2284 DemoHeader("Custom Data and Getters", Demo_CustomDataAndGetters); 2285 DemoHeader("Custom Rendering", Demo_CustomRendering); 2286 DemoHeader("Custom Plotters and Tooltips", Demo_CustomPlottersAndTooltips); 2287 ImGui::EndTabItem(); 2288 } 2289 if (ImGui::BeginTabItem("Config")) { 2290 Demo_Config(); 2291 ImGui::EndTabItem(); 2292 } 2293 if (ImGui::BeginTabItem("Help")) { 2294 Demo_Help(); 2295 ImGui::EndTabItem(); 2296 } 2297 ImGui::EndTabBar(); 2298 } 2299 ImGui::End(); 2300} 2301 2302} // namespace ImPlot 2303 2304namespace MyImPlot { 2305 2306ImPlotPoint SineWave(int idx, void* data) { 2307 WaveData* wd = (WaveData*)data; 2308 double x = idx * wd->X; 2309 return ImPlotPoint(x, wd->Offset + wd->Amp * sin(2 * 3.14 * wd->Freq * x)); 2310} 2311 2312ImPlotPoint SawWave(int idx, void* data) { 2313 WaveData* wd = (WaveData*)data; 2314 double x = idx * wd->X; 2315 return ImPlotPoint(x, wd->Offset + wd->Amp * (-2 / 3.14 * atan(cos(3.14 * wd->Freq * x) / sin(3.14 * wd->Freq * x)))); 2316} 2317 2318ImPlotPoint Spiral(int idx, void*) { 2319 float r = 0.9f; // outer radius 2320 float a = 0; // inner radius 2321 float b = 0.05f; // increment per rev 2322 float n = (r - a) / b; // number of revolutions 2323 double th = 2 * n * 3.14; // angle 2324 float Th = float(th * idx / (1000 - 1)); 2325 return ImPlotPoint(0.5f+(a + b*Th / (2.0f * (float) 3.14))*cos(Th), 2326 0.5f + (a + b*Th / (2.0f * (float)3.14))*sin(Th)); 2327} 2328 2329void Sparkline(const char* id, const float* values, int count, float min_v, float max_v, int offset, const ImVec4& col, const ImVec2& size) { 2330 ImPlot::PushStyleVar(ImPlotStyleVar_PlotPadding, ImVec2(0,0)); 2331 if (ImPlot::BeginPlot(id,size,ImPlotFlags_CanvasOnly)) { 2332 ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations); 2333 ImPlot::SetupAxesLimits(0, count - 1, min_v, max_v, ImGuiCond_Always); 2334 ImPlot::SetNextLineStyle(col); 2335 ImPlot::SetNextFillStyle(col, 0.25); 2336 ImPlot::PlotLine(id, values, count, 1, 0, ImPlotLineFlags_Shaded, offset); 2337 ImPlot::EndPlot(); 2338 } 2339 ImPlot::PopStyleVar(); 2340} 2341 2342void StyleSeaborn() { 2343 2344 ImPlotStyle& style = ImPlot::GetStyle(); 2345 2346 ImVec4* colors = style.Colors; 2347 colors[ImPlotCol_Line] = IMPLOT_AUTO_COL; 2348 colors[ImPlotCol_Fill] = IMPLOT_AUTO_COL; 2349 colors[ImPlotCol_MarkerOutline] = IMPLOT_AUTO_COL; 2350 colors[ImPlotCol_MarkerFill] = IMPLOT_AUTO_COL; 2351 colors[ImPlotCol_ErrorBar] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); 2352 colors[ImPlotCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); 2353 colors[ImPlotCol_PlotBg] = ImVec4(0.92f, 0.92f, 0.95f, 1.00f); 2354 colors[ImPlotCol_PlotBorder] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); 2355 colors[ImPlotCol_LegendBg] = ImVec4(0.92f, 0.92f, 0.95f, 1.00f); 2356 colors[ImPlotCol_LegendBorder] = ImVec4(0.80f, 0.81f, 0.85f, 1.00f); 2357 colors[ImPlotCol_LegendText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); 2358 colors[ImPlotCol_TitleText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); 2359 colors[ImPlotCol_InlayText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); 2360 colors[ImPlotCol_AxisText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); 2361 colors[ImPlotCol_AxisGrid] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); 2362 colors[ImPlotCol_AxisBgHovered] = ImVec4(0.92f, 0.92f, 0.95f, 1.00f); 2363 colors[ImPlotCol_AxisBgActive] = ImVec4(0.92f, 0.92f, 0.95f, 0.75f); 2364 colors[ImPlotCol_Selection] = ImVec4(1.00f, 0.65f, 0.00f, 1.00f); 2365 colors[ImPlotCol_Crosshairs] = ImVec4(0.23f, 0.10f, 0.64f, 0.50f); 2366 2367 style.LineWeight = 1.5; 2368 style.Marker = ImPlotMarker_None; 2369 style.MarkerSize = 4; 2370 style.MarkerWeight = 1; 2371 style.FillAlpha = 1.0f; 2372 style.ErrorBarSize = 5; 2373 style.ErrorBarWeight = 1.5f; 2374 style.DigitalBitHeight = 8; 2375 style.DigitalBitGap = 4; 2376 style.PlotBorderSize = 0; 2377 style.MinorAlpha = 1.0f; 2378 style.MajorTickLen = ImVec2(0,0); 2379 style.MinorTickLen = ImVec2(0,0); 2380 style.MajorTickSize = ImVec2(0,0); 2381 style.MinorTickSize = ImVec2(0,0); 2382 style.MajorGridSize = ImVec2(1.2f,1.2f); 2383 style.MinorGridSize = ImVec2(1.2f,1.2f); 2384 style.PlotPadding = ImVec2(12,12); 2385 style.LabelPadding = ImVec2(5,5); 2386 style.LegendPadding = ImVec2(5,5); 2387 style.MousePosPadding = ImVec2(5,5); 2388 style.PlotMinSize = ImVec2(300,225); 2389} 2390 2391} // namespaece MyImPlot 2392 2393// WARNING: 2394// 2395// You can use "implot_internal.h" to build custom plotting fuctions or extend ImPlot. 2396// However, note that forward compatibility of this file is not guaranteed and the 2397// internal API is subject to change. At some point we hope to bring more of this 2398// into the public API and expose the necessary building blocks to fully support 2399// custom plotters. For now, proceed at your own risk! 2400 2401#include "implot_internal.h" 2402 2403namespace MyImPlot { 2404 2405template <typename T> 2406int BinarySearch(const T* arr, int l, int r, T x) { 2407 if (r >= l) { 2408 int mid = l + (r - l) / 2; 2409 if (arr[mid] == x) 2410 return mid; 2411 if (arr[mid] > x) 2412 return BinarySearch(arr, l, mid - 1, x); 2413 return BinarySearch(arr, mid + 1, r, x); 2414 } 2415 return -1; 2416} 2417 2418void 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, ImVec4 bullCol, ImVec4 bearCol) { 2419 2420 // get ImGui window DrawList 2421 ImDrawList* draw_list = ImPlot::GetPlotDrawList(); 2422 // calc real value width 2423 double half_width = count > 1 ? (xs[1] - xs[0]) * width_percent : width_percent; 2424 2425 // custom tool 2426 if (ImPlot::IsPlotHovered() && tooltip) { 2427 ImPlotPoint mouse = ImPlot::GetPlotMousePos(); 2428 mouse.x = ImPlot::RoundTime(ImPlotTime::FromDouble(mouse.x), ImPlotTimeUnit_Day).ToDouble(); 2429 float tool_l = ImPlot::PlotToPixels(mouse.x - half_width * 1.5, mouse.y).x; 2430 float tool_r = ImPlot::PlotToPixels(mouse.x + half_width * 1.5, mouse.y).x; 2431 float tool_t = ImPlot::GetPlotPos().y; 2432 float tool_b = tool_t + ImPlot::GetPlotSize().y; 2433 ImPlot::PushPlotClipRect(); 2434 draw_list->AddRectFilled(ImVec2(tool_l, tool_t), ImVec2(tool_r, tool_b), IM_COL32(128,128,128,64)); 2435 ImPlot::PopPlotClipRect(); 2436 // find mouse location index 2437 int idx = BinarySearch(xs, 0, count - 1, mouse.x); 2438 // render tool tip (won't be affected by plot clip rect) 2439 if (idx != -1) { 2440 ImGui::BeginTooltip(); 2441 char buff[32]; 2442 ImPlot::FormatDate(ImPlotTime::FromDouble(xs[idx]),buff,32,ImPlotDateFmt_DayMoYr,ImPlot::GetStyle().UseISO8601); 2443 ImGui::Text("Day: %s", buff); 2444 ImGui::Text("Open: $%.2f", opens[idx]); 2445 ImGui::Text("Close: $%.2f", closes[idx]); 2446 ImGui::Text("Low: $%.2f", lows[idx]); 2447 ImGui::Text("High: $%.2f", highs[idx]); 2448 ImGui::EndTooltip(); 2449 } 2450 } 2451 2452 // begin plot item 2453 if (ImPlot::BeginItem(label_id)) { 2454 // override legend icon color 2455 ImPlot::GetCurrentItem()->Color = IM_COL32(64,64,64,255); 2456 // fit data if requested 2457 if (ImPlot::FitThisFrame()) { 2458 for (int i = 0; i < count; ++i) { 2459 ImPlot::FitPoint(ImPlotPoint(xs[i], lows[i])); 2460 ImPlot::FitPoint(ImPlotPoint(xs[i], highs[i])); 2461 } 2462 } 2463 // render data 2464 for (int i = 0; i < count; ++i) { 2465 ImVec2 open_pos = ImPlot::PlotToPixels(xs[i] - half_width, opens[i]); 2466 ImVec2 close_pos = ImPlot::PlotToPixels(xs[i] + half_width, closes[i]); 2467 ImVec2 low_pos = ImPlot::PlotToPixels(xs[i], lows[i]); 2468 ImVec2 high_pos = ImPlot::PlotToPixels(xs[i], highs[i]); 2469 ImU32 color = ImGui::GetColorU32(opens[i] > closes[i] ? bearCol : bullCol); 2470 draw_list->AddLine(low_pos, high_pos, color); 2471 draw_list->AddRectFilled(open_pos, close_pos, color); 2472 } 2473 2474 // end plot item 2475 ImPlot::EndItem(); 2476 } 2477} 2478 2479} // namespace MyImplot