implot_demo.cpp 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  1. // MIT License
  2. // Copyright (c) 2021 Evan Pezent
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. // ImPlot v0.11 WIP
  19. #include "implot.h"
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #ifdef _MSC_VER
  25. #define sprintf sprintf_s
  26. #endif
  27. #ifndef PI
  28. #define PI 3.14159265358979323846
  29. #endif
  30. // Encapsulates examples for customizing ImPlot.
  31. namespace MyImPlot {
  32. // Example for Custom Data and Getters section.
  33. struct Vector2f {
  34. Vector2f(float _x, float _y) { x = _x; y = _y; }
  35. float x, y;
  36. };
  37. // Example for Custom Data and Getters section.
  38. struct WaveData {
  39. double X, Amp, Freq, Offset;
  40. WaveData(double x, double amp, double freq, double offset) { X = x; Amp = amp; Freq = freq; Offset = offset; }
  41. };
  42. ImPlotPoint SineWave(void* wave_data, int idx);
  43. ImPlotPoint SawWave(void* wave_data, int idx);
  44. ImPlotPoint Spiral(void*, int idx);
  45. // Example for Tables section.
  46. void Sparkline(const char* id, const float* values, int count, float min_v, float max_v, int offset, const ImVec4& col, const ImVec2& size);
  47. // Example for Custom Plotters and Tooltips section.
  48. void PlotCandlestick(const char* label_id, const double* xs, const double* opens, const double* closes, const double* lows, const double* highs, int count, bool tooltip = true, float width_percent = 0.25f, ImVec4 bullCol = ImVec4(0,1,0,1), ImVec4 bearCol = ImVec4(1,0,0,1));
  49. // Example for Custom Styles section.
  50. void StyleSeaborn();
  51. } // namespace MyImPlot
  52. namespace ImPlot {
  53. void ShowBenchmarkTool();
  54. template <typename T>
  55. inline T RandomRange(T min, T max) {
  56. T scale = rand() / (T) RAND_MAX;
  57. return min + scale * ( max - min );
  58. }
  59. ImVec4 RandomColor() {
  60. ImVec4 col;
  61. col.x = RandomRange(0.0f,1.0f);
  62. col.y = RandomRange(0.0f,1.0f);
  63. col.z = RandomRange(0.0f,1.0f);
  64. col.w = 1.0f;
  65. return col;
  66. }
  67. double RandomGauss() {
  68. static double V1, V2, S;
  69. static int phase = 0;
  70. double X;
  71. if(phase == 0) {
  72. do {
  73. double U1 = (double)rand() / RAND_MAX;
  74. double U2 = (double)rand() / RAND_MAX;
  75. V1 = 2 * U1 - 1;
  76. V2 = 2 * U2 - 1;
  77. S = V1 * V1 + V2 * V2;
  78. } while(S >= 1 || S == 0);
  79. X = V1 * sqrt(-2 * log(S) / S);
  80. } else
  81. X = V2 * sqrt(-2 * log(S) / S);
  82. phase = 1 - phase;
  83. return X;
  84. }
  85. template <int N>
  86. struct NormalDistribution {
  87. NormalDistribution(double mean, double sd) {
  88. for (int i = 0; i < N; ++i)
  89. Data[i] = RandomGauss()*sd + mean;
  90. }
  91. double Data[N];
  92. };
  93. // utility structure for realtime plot
  94. struct ScrollingBuffer {
  95. int MaxSize;
  96. int Offset;
  97. ImVector<ImVec2> Data;
  98. ScrollingBuffer(int max_size = 2000) {
  99. MaxSize = max_size;
  100. Offset = 0;
  101. Data.reserve(MaxSize);
  102. }
  103. void AddPoint(float x, float y) {
  104. if (Data.size() < MaxSize)
  105. Data.push_back(ImVec2(x,y));
  106. else {
  107. Data[Offset] = ImVec2(x,y);
  108. Offset = (Offset + 1) % MaxSize;
  109. }
  110. }
  111. void Erase() {
  112. if (Data.size() > 0) {
  113. Data.shrink(0);
  114. Offset = 0;
  115. }
  116. }
  117. };
  118. // utility structure for realtime plot
  119. struct RollingBuffer {
  120. float Span;
  121. ImVector<ImVec2> Data;
  122. RollingBuffer() {
  123. Span = 10.0f;
  124. Data.reserve(2000);
  125. }
  126. void AddPoint(float x, float y) {
  127. float xmod = fmodf(x, Span);
  128. if (!Data.empty() && xmod < Data.back().x)
  129. Data.shrink(0);
  130. Data.push_back(ImVec2(xmod, y));
  131. }
  132. };
  133. // Huge data used by Time Formatting example (~500 MB allocation!)
  134. struct HugeTimeData {
  135. HugeTimeData(double min) {
  136. Ts = new double[Size];
  137. Ys = new double[Size];
  138. for (int i = 0; i < Size; ++i) {
  139. Ts[i] = min + i;
  140. Ys[i] = GetY(Ts[i]);
  141. }
  142. }
  143. ~HugeTimeData() { delete[] Ts; delete[] Ys; }
  144. static double GetY(double t) {
  145. return 0.5 + 0.25 * sin(t/86400/12) + 0.005 * sin(t/3600);
  146. }
  147. double* Ts;
  148. double* Ys;
  149. static const int Size = 60*60*24*366;
  150. };
  151. //-----------------------------------------------------------------------------
  152. // DEMOS
  153. //-----------------------------------------------------------------------------
  154. void ShowDemo_Help() {
  155. ImGui::Text("ABOUT THIS DEMO:");
  156. ImGui::BulletText("Sections below are demonstrating many aspects of the library.");
  157. ImGui::BulletText("The \"Tools\" menu above gives access to: Style Editors (ImPlot/ImGui)\n"
  158. "and Metrics (general purpose Dear ImGui debugging tool).");
  159. ImGui::Separator();
  160. ImGui::Text("PROGRAMMER GUIDE:");
  161. ImGui::BulletText("See the ShowDemoWindow() code in implot_demo.cpp. <- you are here!");
  162. ImGui::BulletText("By default, anti-aliased lines are turned OFF.");
  163. ImGui::Indent();
  164. ImGui::BulletText("Software AA can be enabled globally with ImPlotStyle.AntiAliasedLines.");
  165. ImGui::BulletText("Software AA can be enabled per plot with ImPlotFlags_AntiAliased.");
  166. ImGui::BulletText("AA for plots can be toggled from the plot's context menu.");
  167. ImGui::BulletText("If permitable, you are better off using hardware AA (e.g. MSAA).");
  168. ImGui::Unindent();
  169. ImGui::BulletText("If you see visual artifacts, do one of the following:");
  170. ImGui::Indent();
  171. ImGui::BulletText("Handle ImGuiBackendFlags_RendererHasVtxOffset for 16-bit indices in your backend.");
  172. ImGui::BulletText("Or, enable 32-bit indices in imconfig.h.");
  173. ImGui::BulletText("Your current configuration is:");
  174. ImGui::Indent();
  175. ImGui::BulletText("ImDrawIdx: %d-bit", (int)(sizeof(ImDrawIdx) * 8));
  176. ImGui::BulletText("ImGuiBackendFlags_RendererHasVtxOffset: %s", (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ? "True" : "False");
  177. ImGui::Unindent();
  178. ImGui::Unindent();
  179. ImGui::Separator();
  180. ImGui::Text("USER GUIDE:");
  181. ShowUserGuide();
  182. }
  183. //-----------------------------------------------------------------------------
  184. void ShowDemo_Configuration() {
  185. ImGui::ShowFontSelector("Font");
  186. ImGui::ShowStyleSelector("ImGui Style");
  187. ImPlot::ShowStyleSelector("ImPlot Style");
  188. ImPlot::ShowColormapSelector("ImPlot Colormap");
  189. float indent = ImGui::CalcItemWidth() - ImGui::GetFrameHeight();
  190. ImGui::Separator();
  191. ImGui::Checkbox("Anti-Aliased Lines", &ImPlot::GetStyle().AntiAliasedLines);
  192. ImGui::Separator();
  193. ImGui::Checkbox("Use Local Time", &ImPlot::GetStyle().UseLocalTime);
  194. ImGui::Checkbox("Use ISO 8601", &ImPlot::GetStyle().UseISO8601);
  195. ImGui::Checkbox("Use 24 Hour Clock", &ImPlot::GetStyle().Use24HourClock);
  196. ImGui::Unindent(indent);
  197. }
  198. //-----------------------------------------------------------------------------
  199. void ShowDemo_LinePlots() {
  200. static float xs1[1001], ys1[1001];
  201. for (int i = 0; i < 1001; ++i) {
  202. xs1[i] = i * 0.001f;
  203. ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)ImGui::GetTime() / 10));
  204. }
  205. static double xs2[11], ys2[11];
  206. for (int i = 0; i < 11; ++i) {
  207. xs2[i] = i * 0.1f;
  208. ys2[i] = xs2[i] * xs2[i];
  209. }
  210. ImGui::BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help).");
  211. if (ImPlot::BeginPlot("Line Plot", "x", "f(x)")) {
  212. ImPlot::PlotLine("sin(x)", xs1, ys1, 1001);
  213. ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
  214. ImPlot::PlotLine("x^2", xs2, ys2, 11);
  215. ImPlot::EndPlot();
  216. }
  217. }
  218. //-----------------------------------------------------------------------------
  219. void ShowDemo_FilledLinePlots() {
  220. static double xs1[101], ys1[101], ys2[101], ys3[101];
  221. srand(0);
  222. for (int i = 0; i < 101; ++i) {
  223. xs1[i] = (float)i;
  224. ys1[i] = RandomRange(400.0,450.0);
  225. ys2[i] = RandomRange(275.0,350.0);
  226. ys3[i] = RandomRange(150.0,225.0);
  227. }
  228. static bool show_lines = true;
  229. static bool show_fills = true;
  230. static float fill_ref = 0;
  231. static int shade_mode = 0;
  232. ImGui::Checkbox("Lines",&show_lines); ImGui::SameLine();
  233. ImGui::Checkbox("Fills",&show_fills);
  234. if (show_fills) {
  235. ImGui::SameLine();
  236. if (ImGui::RadioButton("To -INF",shade_mode == 0))
  237. shade_mode = 0;
  238. ImGui::SameLine();
  239. if (ImGui::RadioButton("To +INF",shade_mode == 1))
  240. shade_mode = 1;
  241. ImGui::SameLine();
  242. if (ImGui::RadioButton("To Ref",shade_mode == 2))
  243. shade_mode = 2;
  244. if (shade_mode == 2) {
  245. ImGui::SameLine();
  246. ImGui::SetNextItemWidth(100);
  247. ImGui::DragFloat("##Ref",&fill_ref, 1, -100, 500);
  248. }
  249. }
  250. ImPlot::SetNextPlotLimits(0,100,0,500);
  251. if (ImPlot::BeginPlot("Stock Prices", "Days", "Price")) {
  252. if (show_fills) {
  253. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
  254. ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref);
  255. ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref);
  256. ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref);
  257. ImPlot::PopStyleVar();
  258. }
  259. if (show_lines) {
  260. ImPlot::PlotLine("Stock 1", xs1, ys1, 101);
  261. ImPlot::PlotLine("Stock 2", xs1, ys2, 101);
  262. ImPlot::PlotLine("Stock 3", xs1, ys3, 101);
  263. }
  264. ImPlot::EndPlot();
  265. }
  266. }
  267. //-----------------------------------------------------------------------------
  268. void ShowDemo_ShadedPlots() {
  269. static float xs[1001], ys[1001], ys1[1001], ys2[1001], ys3[1001], ys4[1001];
  270. srand(0);
  271. for (int i = 0; i < 1001; ++i) {
  272. xs[i] = i * 0.001f;
  273. ys[i] = 0.25f + 0.25f * sinf(25 * xs[i]) * sinf(5 * xs[i]) + RandomRange(-0.01f, 0.01f);
  274. ys1[i] = ys[i] + RandomRange(0.1f, 0.12f);
  275. ys2[i] = ys[i] - RandomRange(0.1f, 0.12f);
  276. ys3[i] = 0.75f + 0.2f * sinf(25 * xs[i]);
  277. ys4[i] = 0.75f + 0.1f * cosf(25 * xs[i]);
  278. }
  279. static float alpha = 0.25f;
  280. ImGui::DragFloat("Alpha",&alpha,0.01f,0,1);
  281. if (ImPlot::BeginPlot("Shaded Plots", "X-Axis", "Y-Axis")) {
  282. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha);
  283. ImPlot::PlotShaded("Uncertain Data",xs,ys1,ys2,1001);
  284. ImPlot::PlotLine("Uncertain Data", xs, ys, 1001);
  285. ImPlot::PlotShaded("Overlapping",xs,ys3,ys4,1001);
  286. ImPlot::PlotLine("Overlapping",xs,ys3,1001);
  287. ImPlot::PlotLine("Overlapping",xs,ys4,1001);
  288. ImPlot::PopStyleVar();
  289. ImPlot::EndPlot();
  290. }
  291. }
  292. //-----------------------------------------------------------------------------
  293. void ShowDemo_ScatterPlots() {
  294. srand(0);
  295. static float xs1[100], ys1[100];
  296. for (int i = 0; i < 100; ++i) {
  297. xs1[i] = i * 0.01f;
  298. ys1[i] = xs1[i] + 0.1f * ((float)rand() / (float)RAND_MAX);
  299. }
  300. static float xs2[50], ys2[50];
  301. for (int i = 0; i < 50; i++) {
  302. xs2[i] = 0.25f + 0.2f * ((float)rand() / (float)RAND_MAX);
  303. ys2[i] = 0.75f + 0.2f * ((float)rand() / (float)RAND_MAX);
  304. }
  305. if (ImPlot::BeginPlot("Scatter Plot", NULL, NULL)) {
  306. ImPlot::PlotScatter("Data 1", xs1, ys1, 100);
  307. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
  308. ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, ImVec4(0,1,0,0.5f), IMPLOT_AUTO, ImVec4(0,1,0,1));
  309. ImPlot::PlotScatter("Data 2", xs2, ys2, 50);
  310. ImPlot::PopStyleVar();
  311. ImPlot::EndPlot();
  312. }
  313. }
  314. //-----------------------------------------------------------------------------
  315. void ShowDemo_StairstepPlots() {
  316. static float ys1[101], ys2[101];
  317. for (int i = 0; i < 101; ++i) {
  318. ys1[i] = 0.5f + 0.4f * sinf(50 * i * 0.01f);
  319. ys2[i] = 0.5f + 0.2f * sinf(25 * i * 0.01f);
  320. }
  321. if (ImPlot::BeginPlot("Stairstep Plot", "x", "f(x)")) {
  322. ImPlot::PlotStairs("Signal 1", ys1, 101, 0.01f);
  323. ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 2.0f);
  324. ImPlot::PlotStairs("Signal 2", ys2, 101, 0.01f);
  325. ImPlot::EndPlot();
  326. }
  327. }
  328. //-----------------------------------------------------------------------------
  329. void ShowDemo_BarPlots() {
  330. static bool horz = false;
  331. static ImS8 midtm[10] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90};
  332. static ImS16 final[10] = {80, 62, 56, 99, 55, 78, 88, 78, 90, 100};
  333. static ImS32 grade[10] = {80, 69, 52, 92, 72, 78, 75, 76, 89, 95};
  334. static const char* labels[] = {"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10"};
  335. static const double positions[] = {0,1,2,3,4,5,6,7,8,9};
  336. ImGui::Checkbox("Horizontal",&horz);
  337. if (horz) {
  338. ImPlot::SetNextPlotLimits(0, 110, -0.5, 9.5, ImGuiCond_Always);
  339. ImPlot::SetNextPlotTicksY(positions, 10, labels);
  340. }
  341. else {
  342. ImPlot::SetNextPlotLimits(-0.5, 9.5, 0, 110, ImGuiCond_Always);
  343. ImPlot::SetNextPlotTicksX(positions, 10, labels);
  344. }
  345. if (ImPlot::BeginPlot("Bar Plot", horz ? "Score" : "Student", horz ? "Student" : "Score",
  346. ImVec2(-1,0), 0, 0, horz ? ImPlotAxisFlags_Invert : 0))
  347. {
  348. if (horz) {
  349. ImPlot::SetLegendLocation(ImPlotLocation_West, ImPlotOrientation_Vertical);
  350. ImPlot::PlotBarsH("Midterm Exam", midtm, 10, 0.2, -0.2);
  351. ImPlot::PlotBarsH("Final Exam", final, 10, 0.2, 0);
  352. ImPlot::PlotBarsH("Course Grade", grade, 10, 0.2, 0.2);
  353. }
  354. else {
  355. ImPlot::SetLegendLocation(ImPlotLocation_South, ImPlotOrientation_Horizontal);
  356. ImPlot::PlotBars("Midterm Exam", midtm, 10, 0.2, -0.2);
  357. ImPlot::PlotBars("Final Exam", final, 10, 0.2, 0);
  358. ImPlot::PlotBars("Course Grade", grade, 10, 0.2, 0.2);
  359. }
  360. ImPlot::EndPlot();
  361. }
  362. }
  363. //-----------------------------------------------------------------------------
  364. void ShowDemo_ErrorBars() {
  365. static float xs[5] = {1,2,3,4,5};
  366. static float bar[5] = {1,2,5,3,4};
  367. static float lin1[5] = {8,8,9,7,8};
  368. static float lin2[5] = {6,7,6,9,6};
  369. static float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f};
  370. static float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f};
  371. static float err3[5] = {0.09f, 0.14f, 0.09f, 0.12f, 0.16f};
  372. static float err4[5] = {0.02f, 0.08f, 0.15f, 0.05f, 0.2f};
  373. ImPlot::SetNextPlotLimits(0, 6, 0, 10);
  374. if (ImPlot::BeginPlot("##ErrorBars",NULL,NULL)) {
  375. ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f);
  376. ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5);
  377. ImPlot::SetNextErrorBarStyle(ImPlot::GetColormapColor(1), 0);
  378. ImPlot::PlotErrorBars("Line", xs, lin1, err1, err2, 5);
  379. ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
  380. ImPlot::PlotLine("Line", xs, lin1, 5);
  381. ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(2));
  382. ImPlot::PlotErrorBars("Scatter", xs, lin2, err2, 5);
  383. ImPlot::PlotErrorBarsH("Scatter", xs, lin2, err3, err4, 5);
  384. ImPlot::PopStyleColor();
  385. ImPlot::PlotScatter("Scatter", xs, lin2, 5);
  386. ImPlot::EndPlot();
  387. }
  388. }
  389. void ShowDemo_StemPlots() {
  390. static double xs[51], ys1[51], ys2[51];
  391. for (int i = 0; i < 51; ++i) {
  392. xs[i] = i * 0.02;
  393. ys1[i] = 1.0 + 0.5 * sin(25*xs[i])*cos(2*xs[i]);
  394. ys2[i] = 0.5 + 0.25 * sin(10*xs[i]) * sin(xs[i]);
  395. }
  396. ImPlot::SetNextPlotLimits(0,1,0,1.6);
  397. if (ImPlot::BeginPlot("Stem Plots")) {
  398. ImPlot::PlotStems("Stems 1",xs,ys1,51);
  399. ImPlot::SetNextLineStyle(ImVec4(1,0.5f,0,0.75f));
  400. ImPlot::SetNextMarkerStyle(ImPlotMarker_Square,5,ImVec4(1,0.5f,0,0.25f));
  401. ImPlot::PlotStems("Stems 2", xs, ys2,51);
  402. ImPlot::EndPlot();
  403. }
  404. }
  405. void ShowDemo_InfiniteLines() {
  406. static double vals[] = {0.25, 0.5, 0.75};
  407. if (ImPlot::BeginPlot("##Infinite",0,0,ImVec2(-1,0),0,ImPlotAxisFlags_NoInitialFit,ImPlotAxisFlags_NoInitialFit)) {
  408. ImPlot::PlotVLines("VLines",vals,3);
  409. ImPlot::PlotHLines("HLines",vals,3);
  410. ImPlot::EndPlot();
  411. }
  412. }
  413. void ShowDemo_PieCharts() {
  414. static const char* labels1[] = {"Frogs","Hogs","Dogs","Logs"};
  415. static float data1[] = {0.15f, 0.30f, 0.2f, 0.05f};
  416. static bool normalize = false;
  417. ImGui::SetNextItemWidth(250);
  418. ImGui::DragFloat4("Values", data1, 0.01f, 0, 1);
  419. if ((data1[0] + data1[1] + data1[2] + data1[3]) < 1) {
  420. ImGui::SameLine();
  421. ImGui::Checkbox("Normalize", &normalize);
  422. }
  423. ImPlot::SetNextPlotLimits(0,1,0,1,ImGuiCond_Always);
  424. if (ImPlot::BeginPlot("##Pie1", NULL, NULL, ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMousePos, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) {
  425. ImPlot::PlotPieChart(labels1, data1, 4, 0.5, 0.5, 0.4, normalize, "%.2f");
  426. ImPlot::EndPlot();
  427. }
  428. ImGui::SameLine();
  429. static const char* labels2[] = {"A","B","C","D","E"};
  430. static int data2[] = {1,1,2,3,5};
  431. ImPlot::PushColormap(ImPlotColormap_Pastel);
  432. ImPlot::SetNextPlotLimits(0,1,0,1,ImGuiCond_Always);
  433. if (ImPlot::BeginPlot("##Pie2", NULL, NULL, ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMousePos, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) {
  434. ImPlot::PlotPieChart(labels2, data2, 5, 0.5, 0.5, 0.4, true, "%.0f", 180);
  435. ImPlot::EndPlot();
  436. }
  437. ImPlot::PopColormap();
  438. }
  439. void ShowDemo_Heatmaps() {
  440. static float values1[7][7] = {{0.8f, 2.4f, 2.5f, 3.9f, 0.0f, 4.0f, 0.0f},
  441. {2.4f, 0.0f, 4.0f, 1.0f, 2.7f, 0.0f, 0.0f},
  442. {1.1f, 2.4f, 0.8f, 4.3f, 1.9f, 4.4f, 0.0f},
  443. {0.6f, 0.0f, 0.3f, 0.0f, 3.1f, 0.0f, 0.0f},
  444. {0.7f, 1.7f, 0.6f, 2.6f, 2.2f, 6.2f, 0.0f},
  445. {1.3f, 1.2f, 0.0f, 0.0f, 0.0f, 3.2f, 5.1f},
  446. {0.1f, 2.0f, 0.0f, 1.4f, 0.0f, 1.9f, 6.3f}};
  447. static float scale_min = 0;
  448. static float scale_max = 6.3f;
  449. static const char* xlabels[] = {"C1","C2","C3","C4","C5","C6","C7"};
  450. static const char* ylabels[] = {"R1","R2","R3","R4","R5","R6","R7"};
  451. static ImPlotColormap map = ImPlotColormap_Viridis;
  452. if (ImPlot::ColormapButton(ImPlot::GetColormapName(map),ImVec2(225,0),map)) {
  453. map = (map + 1) % ImPlot::GetColormapCount();
  454. // We bust the color cache of our plots so that item colors will
  455. // resample the new colormap in the event that they have already
  456. // been created. See documentation in implot.h.
  457. BustColorCache("##Heatmap1");
  458. BustColorCache("##Heatmap2");
  459. }
  460. ImGui::SameLine();
  461. ImGui::LabelText("##Colormap Index", "%s", "Change Colormap");
  462. ImGui::SetNextItemWidth(225);
  463. ImGui::DragFloatRange2("Min / Max",&scale_min, &scale_max, 0.01f, -20, 20);
  464. static ImPlotAxisFlags axes_flags = ImPlotAxisFlags_Lock | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoTickMarks;
  465. ImPlot::PushColormap(map);
  466. SetNextPlotTicksX(0 + 1.0/14.0, 1 - 1.0/14.0, 7, xlabels);
  467. SetNextPlotTicksY(1 - 1.0/14.0, 0 + 1.0/14.0, 7, ylabels);
  468. if (ImPlot::BeginPlot("##Heatmap1",NULL,NULL,ImVec2(225,225),ImPlotFlags_NoLegend|ImPlotFlags_NoMousePos,axes_flags,axes_flags)) {
  469. ImPlot::PlotHeatmap("heat",values1[0],7,7,scale_min,scale_max);
  470. ImPlot::EndPlot();
  471. }
  472. ImGui::SameLine();
  473. ImPlot::ColormapScale("##HeatScale",scale_min, scale_max, ImVec2(60,225));
  474. ImGui::SameLine();
  475. const int size = 200;
  476. static double values2[size*size];
  477. srand((unsigned int)(ImGui::GetTime()*1000000));
  478. for (int i = 0; i < size*size; ++i)
  479. values2[i] = RandomRange(0.0,1.0);
  480. ImPlot::SetNextPlotLimits(-1,1,-1,1);
  481. if (ImPlot::BeginPlot("##Heatmap2",NULL,NULL,ImVec2(225,225),0,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations)) {
  482. ImPlot::PlotHeatmap("heat1",values2,size,size,0,1,NULL);
  483. ImPlot::PlotHeatmap("heat2",values2,size,size,0,1,NULL, ImPlotPoint(-1,-1), ImPlotPoint(0,0));
  484. ImPlot::EndPlot();
  485. }
  486. ImPlot::PopColormap();
  487. }
  488. void ShowDemo_Histogram() {
  489. static int bins = 50;
  490. static bool cumulative = false;
  491. static bool density = true;
  492. static bool outliers = true;
  493. static double mu = 5;
  494. static double sigma = 2;
  495. ImGui::SetNextItemWidth(200);
  496. if (ImGui::RadioButton("Sqrt",bins==ImPlotBin_Sqrt)) { bins = ImPlotBin_Sqrt; } ImGui::SameLine();
  497. if (ImGui::RadioButton("Sturges",bins==ImPlotBin_Sturges)) { bins = ImPlotBin_Sturges; } ImGui::SameLine();
  498. if (ImGui::RadioButton("Rice",bins==ImPlotBin_Rice)) { bins = ImPlotBin_Rice; } ImGui::SameLine();
  499. if (ImGui::RadioButton("Scott",bins==ImPlotBin_Scott)) { bins = ImPlotBin_Scott; } ImGui::SameLine();
  500. if (ImGui::RadioButton("N Bins",bins>=0)) bins = 50;
  501. if (bins>=0) {
  502. ImGui::SameLine();
  503. ImGui::SetNextItemWidth(200);
  504. ImGui::SliderInt("##Bins", &bins, 1, 100);
  505. }
  506. if (ImGui::Checkbox("Density", &density))
  507. ImPlot::FitNextPlotAxes();
  508. ImGui::SameLine();
  509. if (ImGui::Checkbox("Cumulative", &cumulative))
  510. ImPlot::FitNextPlotAxes();
  511. ImGui::SameLine();
  512. static bool range = false;
  513. ImGui::Checkbox("Range", &range);
  514. static float rmin = -3;
  515. static float rmax = 13;
  516. if (range) {
  517. ImGui::SameLine();
  518. ImGui::SetNextItemWidth(200);
  519. ImGui::DragFloat2("##Range",&rmin,0.1f,-3,13);
  520. ImGui::SameLine();
  521. ImGui::Checkbox("Outliers",&outliers);
  522. }
  523. static NormalDistribution<10000> dist(mu, sigma);
  524. static double x[100];
  525. static double y[100];
  526. if (density) {
  527. for (int i = 0; i < 100; ++i) {
  528. x[i] = -3 + 16 * (double)i/99.0;
  529. y[i] = exp( - (x[i]-mu)*(x[i]-mu) / (2*sigma*sigma)) / (sigma * sqrt(2*3.141592653589793238));
  530. }
  531. if (cumulative) {
  532. for (int i = 1; i < 100; ++i)
  533. y[i] += y[i-1];
  534. for (int i = 0; i < 100; ++i)
  535. y[i] /= y[99];
  536. }
  537. }
  538. if (ImPlot::BeginPlot("##Histograms")) {
  539. ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.5f);
  540. ImPlot::PlotHistogram("Empirical", dist.Data, 10000, bins, cumulative, density, range ? ImPlotRange(rmin,rmax) : ImPlotRange(), outliers);
  541. if (density && outliers)
  542. ImPlot::PlotLine("Theoretical",x,y,100);
  543. ImPlot::EndPlot();
  544. }
  545. }
  546. void ShowDemo_Histogram2D() {
  547. static int count = 500000;
  548. static int xybins[2] = {200,200};
  549. static bool density2 = false;
  550. ImGui::SliderInt("Count",&count,100,500000);
  551. ImGui::SliderInt2("Bins",xybins,1,500);
  552. ImGui::SameLine();
  553. ImGui::Checkbox("Density##2",&density2);
  554. static NormalDistribution<500000> dist1(1, 2);
  555. static NormalDistribution<500000> dist2(1, 1);
  556. double max_count = 0;
  557. ImPlotAxisFlags flags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Foreground;
  558. ImPlot::PushColormap("Hot");
  559. ImPlot::SetNextPlotLimits(-6,6,-6,6);
  560. if (ImPlot::BeginPlot("##Hist2D",0,0,ImVec2(ImGui::GetContentRegionAvail().x-100-ImGui::GetStyle().ItemSpacing.x,0),0,flags,flags)) {
  561. max_count = ImPlot::PlotHistogram2D("Hist2D",dist1.Data,dist2.Data,count,xybins[0],xybins[1],density2,ImPlotLimits(-6,6,-6,6));
  562. ImPlot::EndPlot();
  563. }
  564. ImGui::SameLine();
  565. ImPlot::ColormapScale(density2 ? "Density" : "Count",0,max_count,ImVec2(100,0));
  566. ImPlot::PopColormap();
  567. }
  568. void ShowDemo_DigitalPlots() {
  569. ImGui::BulletText("Digital plots do not respond to Y drag and zoom, so that");
  570. ImGui::Indent();
  571. ImGui::Text("you can drag analog plots over the rising/falling digital edge.");
  572. ImGui::Unindent();
  573. static bool paused = false;
  574. static ScrollingBuffer dataDigital[2];
  575. static ScrollingBuffer dataAnalog[2];
  576. static bool showDigital[2] = {true, false};
  577. static bool showAnalog[2] = {true, false};
  578. char label[32];
  579. ImGui::Checkbox("digital_0", &showDigital[0]); ImGui::SameLine();
  580. ImGui::Checkbox("digital_1", &showDigital[1]); ImGui::SameLine();
  581. ImGui::Checkbox("analog_0", &showAnalog[0]); ImGui::SameLine();
  582. ImGui::Checkbox("analog_1", &showAnalog[1]);
  583. static float t = 0;
  584. if (!paused) {
  585. t += ImGui::GetIO().DeltaTime;
  586. //digital signal values
  587. if (showDigital[0])
  588. dataDigital[0].AddPoint(t, sinf(2*t) > 0.45);
  589. if (showDigital[1])
  590. dataDigital[1].AddPoint(t, sinf(2*t) < 0.45);
  591. //Analog signal values
  592. if (showAnalog[0])
  593. dataAnalog[0].AddPoint(t, sinf(2*t));
  594. if (showAnalog[1])
  595. dataAnalog[1].AddPoint(t, cosf(2*t));
  596. }
  597. ImPlot::SetNextPlotLimitsY(-1, 1);
  598. ImPlot::SetNextPlotLimitsX(t - 10.0, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
  599. if (ImPlot::BeginPlot("##Digital")) {
  600. for (int i = 0; i < 2; ++i) {
  601. if (showDigital[i] && dataDigital[i].Data.size() > 0) {
  602. sprintf(label, "digital_%d", i);
  603. ImPlot::PlotDigital(label, &dataDigital[i].Data[0].x, &dataDigital[i].Data[0].y, dataDigital[i].Data.size(), dataDigital[i].Offset, 2 * sizeof(float));
  604. }
  605. }
  606. for (int i = 0; i < 2; ++i) {
  607. if (showAnalog[i]) {
  608. sprintf(label, "analog_%d", i);
  609. if (dataAnalog[i].Data.size() > 0)
  610. ImPlot::PlotLine(label, &dataAnalog[i].Data[0].x, &dataAnalog[i].Data[0].y, dataAnalog[i].Data.size(), dataAnalog[i].Offset, 2 * sizeof(float));
  611. }
  612. }
  613. ImPlot::EndPlot();
  614. }
  615. }
  616. void ShowDemo_Images() {
  617. ImGui::BulletText("Below we are displaying the font texture, which is the only texture we have\naccess to in this demo.");
  618. ImGui::BulletText("Use the 'ImTextureID' type as storage to pass pointers or identifiers to your\nown texture data.");
  619. ImGui::BulletText("See ImGui Wiki page 'Image Loading and Displaying Examples'.");
  620. static ImVec2 bmin(0,0);
  621. static ImVec2 bmax(1,1);
  622. static ImVec2 uv0(0,0);
  623. static ImVec2 uv1(1,1);
  624. static ImVec4 tint(1,1,1,1);
  625. ImGui::SliderFloat2("Min", &bmin.x, -2, 2, "%.1f");
  626. ImGui::SliderFloat2("Max", &bmax.x, -2, 2, "%.1f");
  627. ImGui::SliderFloat2("UV0", &uv0.x, -2, 2, "%.1f");
  628. ImGui::SliderFloat2("UV1", &uv1.x, -2, 2, "%.1f");
  629. ImGui::ColorEdit4("Tint",&tint.x);
  630. if (ImPlot::BeginPlot("##image")) {
  631. ImPlot::PlotImage("my image",ImGui::GetIO().Fonts->TexID, bmin, bmax, uv0, uv1, tint);
  632. ImPlot::EndPlot();
  633. }
  634. }
  635. void ShowDemo_RealtimePlots() {
  636. ImGui::BulletText("Move your mouse to change the data!");
  637. ImGui::BulletText("This example assumes 60 FPS. Higher FPS requires larger buffer size.");
  638. static ScrollingBuffer sdata1, sdata2;
  639. static RollingBuffer rdata1, rdata2;
  640. ImVec2 mouse = ImGui::GetMousePos();
  641. static float t = 0;
  642. t += ImGui::GetIO().DeltaTime;
  643. sdata1.AddPoint(t, mouse.x * 0.0005f);
  644. rdata1.AddPoint(t, mouse.x * 0.0005f);
  645. sdata2.AddPoint(t, mouse.y * 0.0005f);
  646. rdata2.AddPoint(t, mouse.y * 0.0005f);
  647. static float history = 10.0f;
  648. ImGui::SliderFloat("History",&history,1,30,"%.1f s");
  649. rdata1.Span = history;
  650. rdata2.Span = history;
  651. static ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels;
  652. ImPlot::SetNextPlotLimitsX(t - history, t, ImGuiCond_Always);
  653. ImPlot::SetNextPlotLimitsY(0,1);
  654. if (ImPlot::BeginPlot("##Scrolling", NULL, NULL, ImVec2(-1,150), 0, flags, flags)) {
  655. ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL,0.5f);
  656. ImPlot::PlotShaded("Mouse X", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), -INFINITY, sdata1.Offset, 2 * sizeof(float));
  657. ImPlot::PlotLine("Mouse Y", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), sdata2.Offset, 2*sizeof(float));
  658. ImPlot::EndPlot();
  659. }
  660. ImPlot::SetNextPlotLimitsX(0, history, ImGuiCond_Always);
  661. ImPlot::SetNextPlotLimitsY(0,1);
  662. if (ImPlot::BeginPlot("##Rolling", NULL, NULL, ImVec2(-1,150), 0, flags, flags)) {
  663. ImPlot::PlotLine("Mouse X", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 2 * sizeof(float));
  664. ImPlot::PlotLine("Mouse Y", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 2 * sizeof(float));
  665. ImPlot::EndPlot();
  666. }
  667. }
  668. void ShowDemo_MarkersAndText() {
  669. static float mk_size = ImPlot::GetStyle().MarkerSize;
  670. static float mk_weight = ImPlot::GetStyle().MarkerWeight;
  671. ImGui::DragFloat("Marker Size",&mk_size,0.1f,2.0f,10.0f,"%.2f px");
  672. ImGui::DragFloat("Marker Weight", &mk_weight,0.05f,0.5f,3.0f,"%.2f px");
  673. ImPlot::SetNextPlotLimits(0, 10, 0, 12);
  674. if (ImPlot::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,0), ImPlotFlags_CanvasOnly, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) {
  675. ImS8 xs[2] = {1,4};
  676. ImS8 ys[2] = {10,11};
  677. // filled markers
  678. for (int m = 0; m < ImPlotMarker_COUNT; ++m) {
  679. ImGui::PushID(m);
  680. ImPlot::SetNextMarkerStyle(m, mk_size, IMPLOT_AUTO_COL, mk_weight);
  681. ImPlot::PlotLine("##Filled", xs, ys, 2);
  682. ImGui::PopID();
  683. ys[0]--; ys[1]--;
  684. }
  685. xs[0] = 6; xs[1] = 9; ys[0] = 10; ys[1] = 11;
  686. // open markers
  687. for (int m = 0; m < ImPlotMarker_COUNT; ++m) {
  688. ImGui::PushID(m);
  689. ImPlot::SetNextMarkerStyle(m, mk_size, ImVec4(0,0,0,0), mk_weight);
  690. ImPlot::PlotLine("##Open", xs, ys, 2);
  691. ImGui::PopID();
  692. ys[0]--; ys[1]--;
  693. }
  694. ImPlot::PlotText("Filled Markers", 2.5f, 6.0f);
  695. ImPlot::PlotText("Open Markers", 7.5f, 6.0f);
  696. ImPlot::PushStyleColor(ImPlotCol_InlayText, ImVec4(1,0,1,1));
  697. ImPlot::PlotText("Vertical Text", 5.0f, 6.0f, true);
  698. ImPlot::PopStyleColor();
  699. ImPlot::EndPlot();
  700. }
  701. }
  702. void ShowDemo_LogAxes() {
  703. static double xs[1001], ys1[1001], ys2[1001], ys3[1001];
  704. for (int i = 0; i < 1001; ++i) {
  705. xs[i] = i*0.1f;
  706. ys1[i] = sin(xs[i]) + 1;
  707. ys2[i] = log(xs[i]);
  708. ys3[i] = pow(10.0, xs[i]);
  709. }
  710. ImGui::BulletText("Open the plot context menu (right click) to change scales.");
  711. ImPlot::SetNextPlotLimits(0.1, 100, 0, 10);
  712. if (ImPlot::BeginPlot("Log Plot", NULL, NULL, ImVec2(-1,0), 0, ImPlotAxisFlags_LogScale )) {
  713. ImPlot::PlotLine("f(x) = x", xs, xs, 1001);
  714. ImPlot::PlotLine("f(x) = sin(x)+1", xs, ys1, 1001);
  715. ImPlot::PlotLine("f(x) = log(x)", xs, ys2, 1001);
  716. ImPlot::PlotLine("f(x) = 10^x", xs, ys3, 21);
  717. ImPlot::EndPlot();
  718. }
  719. }
  720. void ShowDemo_TimeAxes() {
  721. static double t_min = 1609459200; // 01/01/2021 @ 12:00:00am (UTC)
  722. static double t_max = 1640995200; // 01/01/2022 @ 12:00:00am (UTC)
  723. ImGui::BulletText("When ImPlotAxisFlags_Time is enabled on the X-Axis, values are interpreted as\n"
  724. "UNIX timestamps in seconds and axis labels are formated as date/time.");
  725. ImGui::BulletText("By default, labels are in UTC time but can be set to use local time instead.");
  726. ImGui::Checkbox("Local Time",&ImPlot::GetStyle().UseLocalTime);
  727. ImGui::SameLine();
  728. ImGui::Checkbox("ISO 8601",&ImPlot::GetStyle().UseISO8601);
  729. ImGui::SameLine();
  730. ImGui::Checkbox("24 Hour Clock",&ImPlot::GetStyle().Use24HourClock);
  731. static HugeTimeData* data = NULL;
  732. if (data == NULL) {
  733. ImGui::SameLine();
  734. if (ImGui::Button("Generate Huge Data (~500MB!)")) {
  735. static HugeTimeData sdata(t_min);
  736. data = &sdata;
  737. }
  738. }
  739. ImPlot::SetNextPlotLimits(t_min,t_max,0,1);
  740. if (ImPlot::BeginPlot("##Time", NULL, NULL, ImVec2(-1,0), 0, ImPlotAxisFlags_Time)) {
  741. if (data != NULL) {
  742. // downsample our data
  743. int downsample = (int)ImPlot::GetPlotLimits().X.Size() / 1000 + 1;
  744. int start = (int)(ImPlot::GetPlotLimits().X.Min - t_min);
  745. start = start < 0 ? 0 : start > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : start;
  746. int end = (int)(ImPlot::GetPlotLimits().X.Max - t_min) + 1000;
  747. end = end < 0 ? 0 : end > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : end;
  748. int size = (end - start)/downsample;
  749. // plot it
  750. ImPlot::PlotLine("Time Series", &data->Ts[start], &data->Ys[start], size, 0, sizeof(double)*downsample);
  751. }
  752. // plot time now
  753. double t_now = (double)time(0);
  754. double y_now = HugeTimeData::GetY(t_now);
  755. ImPlot::PlotScatter("Now",&t_now,&y_now,1);
  756. ImPlot::Annotate(t_now,y_now,ImVec2(10,10),ImPlot::GetLastItemColor(),"Now");
  757. ImPlot::EndPlot();
  758. }
  759. }
  760. void ShowDemo_MultipleYAxes() {
  761. static float xs[1001], xs2[1001], ys1[1001], ys2[1001], ys3[1001];
  762. for (int i = 0; i < 1001; ++i) {
  763. xs[i] = (i*0.1f);
  764. ys1[i] = sinf(xs[i]) * 3 + 1;
  765. ys2[i] = cosf(xs[i]) * 0.2f + 0.5f;
  766. ys3[i] = sinf(xs[i]+0.5f) * 100 + 200;
  767. xs2[i] = xs[i] + 10.0f;
  768. }
  769. static bool y2_axis = true;
  770. static bool y3_axis = true;
  771. ImGui::Checkbox("Y-Axis 2", &y2_axis);
  772. ImGui::SameLine();
  773. ImGui::Checkbox("Y-Axis 3", &y3_axis);
  774. ImGui::SameLine();
  775. // you can fit axes programatically
  776. ImGui::SameLine(); if (ImGui::Button("Fit X")) ImPlot::FitNextPlotAxes(true, false, false, false);
  777. ImGui::SameLine(); if (ImGui::Button("Fit Y")) ImPlot::FitNextPlotAxes(false, true, false, false);
  778. ImGui::SameLine(); if (ImGui::Button("Fit Y2")) ImPlot::FitNextPlotAxes(false, false, true, false);
  779. ImGui::SameLine(); if (ImGui::Button("Fit Y3")) ImPlot::FitNextPlotAxes(false, false, false, true);
  780. ImPlot::SetNextPlotLimits(0.1, 100, 0, 10);
  781. ImPlot::SetNextPlotLimitsY(0, 1, ImGuiCond_Once, 1);
  782. ImPlot::SetNextPlotLimitsY(0, 300, ImGuiCond_Once, 2);
  783. if (ImPlot::BeginPlot("Multi-Axis Plot", NULL, "Y-Axis 1", ImVec2(-1,0),
  784. (y2_axis ? ImPlotFlags_YAxis2 : 0) |
  785. (y3_axis ? ImPlotFlags_YAxis3 : 0),
  786. ImPlotAxisFlags_None, ImPlotAxisFlags_None, ImPlotAxisFlags_NoGridLines, ImPlotAxisFlags_NoGridLines,
  787. "Y-Axis 2", "Y-Axis 3")) {
  788. ImPlot::PlotLine("f(x) = x", xs, xs, 1001);
  789. ImPlot::PlotLine("f(x) = sin(x)*3+1", xs, ys1, 1001);
  790. if (y2_axis) {
  791. ImPlot::SetPlotYAxis(ImPlotYAxis_2);
  792. ImPlot::PlotLine("f(x) = cos(x)*.2+.5 (Y2)", xs, ys2, 1001);
  793. }
  794. if (y3_axis) {
  795. ImPlot::SetPlotYAxis(ImPlotYAxis_3);
  796. ImPlot::PlotLine("f(x) = sin(x+.5)*100+200 (Y3)", xs2, ys3, 1001);
  797. }
  798. ImPlot::EndPlot();
  799. }
  800. }
  801. void ShowDemo_LinkedAxes() {
  802. static double xmin = 0, xmax = 1, ymin = 0, ymax = 1;
  803. static bool linkx = true, linky = true;
  804. int data[2] = {0,1};
  805. ImGui::Checkbox("Link X", &linkx);
  806. ImGui::SameLine();
  807. ImGui::Checkbox("Link Y", &linky);
  808. if (BeginAlignedPlots("AlignedGroup")) {
  809. ImPlot::LinkNextPlotLimits(linkx ? &xmin : NULL , linkx ? &xmax : NULL, linky ? &ymin : NULL, linky ? &ymax : NULL);
  810. if (ImPlot::BeginPlot("Plot A")) {
  811. ImPlot::PlotLine("Line",data,2);
  812. ImPlot::EndPlot();
  813. }
  814. ImPlot::LinkNextPlotLimits(linkx ? &xmin : NULL , linkx ? &xmax : NULL, linky ? &ymin : NULL, linky ? &ymax : NULL);
  815. if (ImPlot::BeginPlot("Plot B")) {
  816. ImPlot::PlotLine("Line",data,2);
  817. ImPlot::EndPlot();
  818. }
  819. ImPlot::EndAlignedPlots();
  820. }
  821. }
  822. void ShowDemo_EqualAxes() {
  823. static double xs[1000], ys[1000];
  824. for (int i = 0; i < 1000; ++i) {
  825. double angle = i * 2 * PI / 999.0;
  826. xs[i] = cos(angle); ys[i] = sin(angle);
  827. }
  828. if (ImPlot::BeginPlot("",0,0,ImVec2(-1,0),ImPlotFlags_Equal)) {
  829. ImPlot::PlotLine("Circle",xs,ys,1000);
  830. ImPlot::EndPlot();
  831. }
  832. }
  833. void ShowDemo_AutoFittingData() {
  834. ImGui::BulletText("The Y-axis has been configured to auto-fit to only the data visible in X-axis range.");
  835. ImGui::BulletText("Zoom and pan the X-axis. Disable Stems to see a difference in fit.");
  836. ImGui::BulletText("If ImPlotAxisFlags_RangeFit is disabled, the axis will fit ALL data.");
  837. static ImPlotAxisFlags xflags = ImPlotAxisFlags_None;
  838. static ImPlotAxisFlags yflags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit;
  839. ImGui::TextUnformatted("X: "); ImGui::SameLine();
  840. ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine();
  841. ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_RangeFit);
  842. ImGui::TextUnformatted("Y: "); ImGui::SameLine();
  843. ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine();
  844. ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_RangeFit);
  845. static double data[101];
  846. srand(0);
  847. for (int i = 0; i < 101; ++i)
  848. data[i] = 1 + sin(i/10.0f);
  849. if (ImPlot::BeginPlot("##DataFitting","X","Y",ImVec2(-1,0),0,xflags,yflags)) {
  850. ImPlot::PlotLine("Line",data,101);
  851. ImPlot::PlotStems("Stems",data,101);
  852. ImPlot::EndPlot();
  853. };
  854. }
  855. ImPlotPoint SinewaveGetter(void* data, int i) {
  856. float f = *(float*)data;
  857. return ImPlotPoint(i,sinf(f*i));
  858. }
  859. void ShowDemo_SubplotsSizing() {
  860. static ImPlotSubplotFlags flags = ImPlotSubplotFlags_None;
  861. ImGui::CheckboxFlags("ImPlotSubplotFlags_NoResize", (unsigned int*)&flags, ImPlotSubplotFlags_NoResize);
  862. ImGui::CheckboxFlags("ImPlotSubplotFlags_NoTitle", (unsigned int*)&flags, ImPlotSubplotFlags_NoTitle);
  863. static int rows = 3;
  864. static int cols = 3;
  865. ImGui::SliderInt("Rows",&rows,1,5);
  866. ImGui::SliderInt("Cols",&cols,1,5);
  867. static float rratios[] = {5,1,1,1,1,1};
  868. static float cratios[] = {5,1,1,1,1,1};
  869. ImGui::DragScalarN("Row Ratios",ImGuiDataType_Float,rratios,rows,0.01f,0);
  870. ImGui::DragScalarN("Col Ratios",ImGuiDataType_Float,cratios,cols,0.01f,0);
  871. if (ImPlot::BeginSubplots("My Subplots", rows, cols, ImVec2(-1,400), flags, rratios, cratios)) {
  872. for (int i = 0; i < rows*cols; ++i) {
  873. if (ImPlot::BeginPlot("",NULL,NULL,ImVec2(),ImPlotFlags_NoLegend,ImPlotAxisFlags_NoTickLabels,ImPlotAxisFlags_NoTickLabels)) {
  874. char buffer[8];
  875. float fi = 0.01f * (i+1);
  876. sprintf(buffer, "data%d", i);
  877. if (i == 0)
  878. ImPlot::SetNextLineStyle(ImVec4(0,1,0,1));
  879. ImPlot::PlotLineG(buffer,SinewaveGetter,&fi,1000);
  880. ImPlot::EndPlot();
  881. }
  882. }
  883. ImPlot::EndSubplots();
  884. }
  885. }
  886. void ShowDemo_SubplotItemSharing() {
  887. static ImPlotSubplotFlags flags = ImPlotSubplotFlags_ShareItems;
  888. ImGui::CheckboxFlags("ImPlotSubplotFlags_ShareItems", (unsigned int*)&flags, ImPlotSubplotFlags_ShareItems);
  889. ImGui::CheckboxFlags("ImPlotSubplotFlags_ColMajor", (unsigned int*)&flags, ImPlotSubplotFlags_ColMajor);
  890. static int rows = 2;
  891. static int cols = 3;
  892. static int id[] = {0,1,2,3,4,5};
  893. static int curj = -1;
  894. if (ImPlot::BeginSubplots("##ItemSharing", rows, cols, ImVec2(-1,400), flags)) {
  895. for (int i = 0; i < rows*cols; ++i) {
  896. if (ImPlot::BeginPlot("")) {
  897. float fc = 0.01f;
  898. ImPlot::PlotLineG("common",SinewaveGetter,&fc,1000);
  899. for (int j = 0; j < 6; ++j) {
  900. if (id[j] == i) {
  901. char label[8];
  902. float fj = 0.01f * (j+2);
  903. sprintf(label, "data%d", j);
  904. ImPlot::PlotLineG(label,SinewaveGetter,&fj,1000);
  905. if (ImPlot::BeginDragDropSourceItem(label)) {
  906. curj = j;
  907. ImGui::SetDragDropPayload("MY_DND",NULL,0);
  908. ImPlot::ItemIcon(GetLastItemColor()); ImGui::SameLine();
  909. ImGui::TextUnformatted(label);
  910. ImPlot::EndDragDropSource();
  911. }
  912. }
  913. }
  914. if (ImPlot::BeginDragDropTarget()) {
  915. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND"))
  916. id[curj] = i;
  917. ImPlot::EndDragDropTarget();
  918. }
  919. ImPlot::EndPlot();
  920. }
  921. }
  922. ImPlot::EndSubplots();
  923. }
  924. }
  925. void ShowDemo_SubplotAxisLinking() {
  926. static ImPlotSubplotFlags flags = ImPlotSubplotFlags_LinkRows | ImPlotSubplotFlags_LinkCols;
  927. ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkRows", (unsigned int*)&flags, ImPlotSubplotFlags_LinkRows);
  928. ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkCols", (unsigned int*)&flags, ImPlotSubplotFlags_LinkCols);
  929. ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkAllX", (unsigned int*)&flags, ImPlotSubplotFlags_LinkAllX);
  930. ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkAllY", (unsigned int*)&flags, ImPlotSubplotFlags_LinkAllY);
  931. static int rows = 2;
  932. static int cols = 2;
  933. if (ImPlot::BeginSubplots("##AxisLinking", rows, cols, ImVec2(-1,400), flags)) {
  934. for (int i = 0; i < rows*cols; ++i) {
  935. ImPlot::SetNextPlotLimits(0,1000,-1,1);
  936. if (ImPlot::BeginPlot("")) {
  937. float fc = 0.01f;
  938. ImPlot::PlotLineG("common",SinewaveGetter,&fc,1000);
  939. ImPlot::EndPlot();
  940. }
  941. }
  942. ImPlot::EndSubplots();
  943. }
  944. }
  945. void ShowDemo_Querying() {
  946. static ImVector<ImPlotPoint> data;
  947. static ImPlotLimits range, query, select;
  948. static bool init = true;
  949. if (init) {
  950. for (int i = 0; i < 50; ++i)
  951. {
  952. double x = RandomRange(0.0, 1.0);
  953. double y = RandomRange(0.0, 1.0);
  954. data.push_back(ImPlotPoint(x,y));
  955. }
  956. init = false;
  957. }
  958. ImGui::BulletText("Middle click (or Ctrl + right click) and drag to create a query rect.");
  959. ImGui::Indent();
  960. ImGui::BulletText("Hold Alt to expand query horizontally.");
  961. ImGui::BulletText("Hold Shift to expand query vertically.");
  962. ImGui::BulletText("The query rect can be dragged after it's created.");
  963. ImGui::Unindent();
  964. ImGui::BulletText("Ctrl + click in the plot area to draw points.");
  965. ImPlot::SetNextPlotLimits(0,1,0,1);
  966. if (ImPlot::BeginPlot("##Centroid", NULL, NULL, ImVec2(-1,0), ImPlotFlags_Query)) {
  967. if (ImPlot::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl) {
  968. ImPlotPoint pt = ImPlot::GetPlotMousePos();
  969. data.push_back(pt);
  970. }
  971. if (data.size() > 0)
  972. ImPlot::PlotScatter("Points", &data[0].x, &data[0].y, data.size(), 0, 2 * sizeof(double));
  973. if (ImPlot::IsPlotQueried() && data.size() > 0) {
  974. ImPlotLimits range2 = ImPlot::GetPlotQuery();
  975. int cnt = 0;
  976. ImPlotPoint avg;
  977. for (int i = 0; i < data.size(); ++i) {
  978. if (range2.Contains(data[i].x, data[i].y)) {
  979. avg.x += data[i].x;
  980. avg.y += data[i].y;
  981. cnt++;
  982. }
  983. }
  984. if (cnt > 0) {
  985. avg.x = avg.x / cnt;
  986. avg.y = avg.y / cnt;
  987. ImPlot::SetNextMarkerStyle(ImPlotMarker_Square);
  988. ImPlot::PlotScatter("Centroid", &avg.x, &avg.y, 1);
  989. }
  990. }
  991. range = ImPlot::GetPlotLimits();
  992. query = ImPlot::GetPlotQuery();
  993. select = ImPlot::GetPlotSelection();
  994. ImPlot::EndPlot();
  995. }
  996. ImGui::Text("Limits: [%g,%g,%g,%g]", range.X.Min, range.X.Max, range.Y.Min, range.Y.Max);
  997. ImGui::Text("Query: [%g,%g,%g,%g]", query.X.Min, query.X.Max, query.Y.Min, query.Y.Max);
  998. ImGui::Text("Selection: [%g,%g,%g,%g]", select.X.Min, select.X.Max, select.Y.Min, select.Y.Max);
  999. }
  1000. void ShowDemo_Views() {
  1001. static float x_data[512];
  1002. static float y_data1[512];
  1003. static float y_data2[512];
  1004. static float y_data3[512];
  1005. static float sampling_freq = 44100;
  1006. static float freq = 500;
  1007. for (size_t i = 0; i < 512; ++i) {
  1008. const float t = i / sampling_freq;
  1009. x_data[i] = t;
  1010. const float arg = 2 * 3.14f * freq * t;
  1011. y_data1[i] = sinf(arg);
  1012. y_data2[i] = y_data1[i] * -0.6f + sinf(2 * arg) * 0.4f;
  1013. y_data3[i] = y_data2[i] * -0.6f + sinf(3 * arg) * 0.4f;
  1014. }
  1015. ImGui::BulletText("Query the first plot to render a subview in the second plot (see above for controls).");
  1016. ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels;
  1017. static bool use_selection = false;
  1018. ImGui::Checkbox("Use Box Selection",&use_selection);
  1019. bool is_viewed = false;
  1020. ImPlotLimits view;
  1021. ImPlot::SetNextPlotLimits(0,0.01,-1,1);
  1022. if (ImPlot::BeginPlot("##View1",NULL,NULL,ImVec2(-1,150), ImPlotFlags_Query, flags, flags)) {
  1023. ImPlot::PlotLine("Signal 1", x_data, y_data1, 512);
  1024. ImPlot::PlotLine("Signal 2", x_data, y_data2, 512);
  1025. ImPlot::PlotLine("Signal 3", x_data, y_data3, 512);
  1026. is_viewed = use_selection ? ImPlot::IsPlotSelected() : ImPlot::IsPlotQueried();
  1027. view = use_selection ? ImPlot::GetPlotSelection() : ImPlot::GetPlotQuery();
  1028. ImPlot::EndPlot();
  1029. }
  1030. ImPlot::SetNextPlotLimits(view.X.Min, view.X.Max, view.Y.Min, view.Y.Max, ImGuiCond_Always);
  1031. if (ImPlot::BeginPlot("##View2",NULL,NULL,ImVec2(-1,150), ImPlotFlags_CanvasOnly, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) {
  1032. if (is_viewed) {
  1033. ImPlot::PlotLine("Signal 1", x_data, y_data1, 512);
  1034. ImPlot::PlotLine("Signal 2", x_data, y_data2, 512);
  1035. ImPlot::PlotLine("Signal 3", x_data, y_data3, 512);
  1036. }
  1037. ImPlot::EndPlot();
  1038. }
  1039. }
  1040. void ShowDemo_LegendOptions() {
  1041. static ImPlotLocation loc = ImPlotLocation_East;
  1042. static bool h = false; static bool o = true;
  1043. ImGui::CheckboxFlags("North", (unsigned int*)&loc, ImPlotLocation_North); ImGui::SameLine();
  1044. ImGui::CheckboxFlags("South", (unsigned int*)&loc, ImPlotLocation_South); ImGui::SameLine();
  1045. ImGui::CheckboxFlags("West", (unsigned int*)&loc, ImPlotLocation_West); ImGui::SameLine();
  1046. ImGui::CheckboxFlags("East", (unsigned int*)&loc, ImPlotLocation_East); ImGui::SameLine();
  1047. ImGui::Checkbox("Horizontal##2", &h); ImGui::SameLine();
  1048. ImGui::Checkbox("Outside", &o);
  1049. ImGui::SliderFloat2("LegendPadding", (float*)&GetStyle().LegendPadding, 0.0f, 20.0f, "%.0f");
  1050. ImGui::SliderFloat2("LegendInnerPadding", (float*)&GetStyle().LegendInnerPadding, 0.0f, 10.0f, "%.0f");
  1051. ImGui::SliderFloat2("LegendSpacing", (float*)&GetStyle().LegendSpacing, 0.0f, 5.0f, "%.0f");
  1052. if (ImPlot::BeginPlot("##Legend","x","y",ImVec2(-1,0))) {
  1053. ImPlot::SetLegendLocation(loc, h ? ImPlotOrientation_Horizontal : ImPlotOrientation_Vertical, o);
  1054. static MyImPlot::WaveData data1(0.001, 0.2, 2, 0.75);
  1055. static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.25);
  1056. static MyImPlot::WaveData data3(0.001, 0.2, 6, 0.5);
  1057. ImPlot::PlotLineG("Item 1", MyImPlot::SineWave, &data1, 1000); // "Item 1" added to legend
  1058. ImPlot::PlotLineG("Item 2##IDText", MyImPlot::SawWave, &data2, 1000); // "Item 2" added to legend, text after ## used for ID only
  1059. ImPlot::PlotLineG("##NotListed", MyImPlot::SawWave, &data3, 1000); // plotted, but not added to legend
  1060. ImPlot::PlotLineG("Item 3", MyImPlot::SineWave, &data1, 1000); // "Item 3" added to legend
  1061. ImPlot::PlotLineG("Item 3", MyImPlot::SawWave, &data2, 1000); // combined with previous "Item 3"
  1062. ImPlot::EndPlot();
  1063. }
  1064. }
  1065. void ShowDemo_DragLines() {
  1066. ImGui::BulletText("Click and drag the horizontal and vertical lines.");
  1067. static double x1 = 0.2;
  1068. static double x2 = 0.8;
  1069. static double y1 = 0.25;
  1070. static double y2 = 0.75;
  1071. static double f = 0.1;
  1072. static bool show_labels = true;
  1073. ImGui::Checkbox("Show Labels##1",&show_labels);
  1074. ImPlot::SetNextPlotLimits(0,1,0,1);
  1075. if (ImPlot::BeginPlot("##guides",0,0,ImVec2(-1,0),ImPlotFlags_YAxis2)) {
  1076. ImPlot::DragLineX("x1",&x1,show_labels);
  1077. ImPlot::DragLineX("x2",&x2,show_labels);
  1078. ImPlot::DragLineY("y1",&y1,show_labels);
  1079. ImPlot::DragLineY("y2",&y2,show_labels);
  1080. double xs[1000], ys[1000];
  1081. for (int i = 0; i < 1000; ++i) {
  1082. xs[i] = (x2+x1)/2+fabs(x2-x1)*(i/1000.0f - 0.5f);
  1083. ys[i] = (y1+y2)/2+fabs(y2-y1)/2*sin(f*i/10);
  1084. }
  1085. ImPlot::PlotLine("Interactive Data", xs, ys, 1000);
  1086. ImPlot::SetPlotYAxis(ImPlotYAxis_2);
  1087. ImPlot::DragLineY("f",&f,show_labels,ImVec4(1,0.5f,1,1));
  1088. ImPlot::EndPlot();
  1089. }
  1090. }
  1091. void ShowDemo_DragPoints() {
  1092. static bool show_labels = true;
  1093. ImGui::BulletText("Click and drag any point.");
  1094. ImGui::Checkbox("Show Labels##2",&show_labels);
  1095. ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoTickMarks;
  1096. ImPlot::SetNextPlotLimits(0,1,0,1);
  1097. if (ImPlot::BeginPlot("##Bezier",0,0,ImVec2(-1,0),ImPlotFlags_CanvasOnly,flags,flags)) {
  1098. static ImPlotPoint P[] = {ImPlotPoint(.05f,.05f), ImPlotPoint(0.2,0.4), ImPlotPoint(0.8,0.6), ImPlotPoint(.95f,.95f)};
  1099. static ImPlotPoint B[100];
  1100. for (int i = 0; i < 100; ++i) {
  1101. double t = i / 99.0;
  1102. double u = 1 - t;
  1103. double w1 = u*u*u;
  1104. double w2 = 3*u*u*t;
  1105. double w3 = 3*u*t*t;
  1106. double w4 = t*t*t;
  1107. 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);
  1108. }
  1109. ImPlot::SetNextLineStyle(ImVec4(0,0.9f,0,1), 2);
  1110. ImPlot::PlotLine("##bez",&B[0].x, &B[0].y, 100, 0, sizeof(ImPlotPoint));
  1111. ImPlot::SetNextLineStyle(ImVec4(1,0.5f,1,1));
  1112. ImPlot::PlotLine("##h1",&P[0].x, &P[0].y, 2, 0, sizeof(ImPlotPoint));
  1113. ImPlot::SetNextLineStyle(ImVec4(0,0.5f,1,1));
  1114. ImPlot::PlotLine("##h2",&P[2].x, &P[2].y, 2, 0, sizeof(ImPlotPoint));
  1115. ImPlot::DragPoint("P0",&P[0].x,&P[0].y, show_labels, ImVec4(0,0.9f,0,1));
  1116. ImPlot::DragPoint("P1",&P[1].x,&P[1].y, show_labels, ImVec4(1,0.5f,1,1));
  1117. ImPlot::DragPoint("P2",&P[2].x,&P[2].y, show_labels, ImVec4(0,0.5f,1,1));
  1118. ImPlot::DragPoint("P3",&P[3].x,&P[3].y, show_labels, ImVec4(0,0.9f,0,1));
  1119. ImPlot::EndPlot();
  1120. }
  1121. }
  1122. void ShowDemo_Annotations() {
  1123. static bool clamp = false;
  1124. ImGui::Checkbox("Clamp",&clamp);
  1125. ImPlot::SetNextPlotLimits(0,2,0,1);
  1126. if (ImPlot::BeginPlot("##Annotations")) {
  1127. static float p[] = {0.25f, 0.25f, 0.75f, 0.75f, 0.25f};
  1128. ImPlot::PlotScatter("##Points",&p[0],&p[1],4);
  1129. ImVec4 col = GetLastItemColor();
  1130. clamp ? ImPlot::AnnotateClamped(0.25,0.25,ImVec2(-15,15),col,"BL") : ImPlot::Annotate(0.25,0.25,ImVec2(-15,15),col,"BL");
  1131. clamp ? ImPlot::AnnotateClamped(0.75,0.25,ImVec2(15,15),col,"BR") : ImPlot::Annotate(0.75,0.25,ImVec2(15,15),col,"BR");
  1132. clamp ? ImPlot::AnnotateClamped(0.75,0.75,ImVec2(15,-15),col,"TR") : ImPlot::Annotate(0.75,0.75,ImVec2(15,-15),col,"TR");
  1133. clamp ? ImPlot::AnnotateClamped(0.25,0.75,ImVec2(-15,-15),col,"TL") : ImPlot::Annotate(0.25,0.75,ImVec2(-15,-15),col,"TL");
  1134. clamp ? ImPlot::AnnotateClamped(0.5,0.5,ImVec2(0,0),col,"Center") : ImPlot::Annotate(0.5,0.5,ImVec2(0,0),col,"Center");
  1135. float bx[] = {1.2f,1.5f,1.8f};
  1136. float by[] = {0.25f, 0.5f, 0.75f};
  1137. ImPlot::PlotBars("##Bars",bx,by,3,0.2);
  1138. for (int i = 0; i < 3; ++i)
  1139. ImPlot::Annotate(bx[i],by[i],ImVec2(0,-5),"B[%d]=%.2f",i,by[i]);
  1140. ImPlot::EndPlot();
  1141. }
  1142. }
  1143. void ShowDemo_DragAndDrop() {
  1144. ImGui::BulletText("Drag/drop items from the left column.");
  1145. ImGui::BulletText("Drag/drop items between plots.");
  1146. ImGui::Indent();
  1147. ImGui::BulletText("Plot 1 Targets: Plot, Y-Axes, Legend");
  1148. ImGui::BulletText("Plot 1 Sources: Legend Item Labels");
  1149. ImGui::BulletText("Plot 2 Targets: Plot, X-Axis, Y-Axis");
  1150. ImGui::BulletText("Plot 2 Sources: Plot, X-Axis, Y-Axis (hold Ctrl)");
  1151. ImGui::Unindent();
  1152. // convenience struct to manage DND items; do this however you like
  1153. struct MyDndItem {
  1154. int Idx;
  1155. int Plt;
  1156. int Yax;
  1157. char Label[16];
  1158. ImVector<ImVec2> Data;
  1159. ImVec4 Color;
  1160. MyDndItem() {
  1161. static int i = 0;
  1162. Idx = i++;
  1163. Plt = 0;
  1164. Yax = ImPlotYAxis_1;
  1165. sprintf(Label, "%02d Hz", Idx+1);
  1166. Color = RandomColor();
  1167. Data.reserve(1001);
  1168. for (int k = 0; k < 1001; ++k) {
  1169. float t = k * 1.0f / 999;
  1170. Data.push_back(ImVec2(t, 0.5f + 0.5f * sinf(2*3.14f*t*(Idx+1))));
  1171. }
  1172. }
  1173. void Reset() { Plt = 0; Yax = ImPlotYAxis_1; }
  1174. };
  1175. const int k_dnd = 20;
  1176. static MyDndItem dnd[k_dnd];
  1177. static MyDndItem* dndx = NULL; // for plot 2
  1178. static MyDndItem* dndy = NULL; // for plot 2
  1179. // child window to serve as initial source for our DND items
  1180. ImGui::BeginChild("DND_LEFT",ImVec2(100,400));
  1181. if (ImGui::Button("Reset Data", ImVec2(100, 0))) {
  1182. for (int k = 0; k < k_dnd; ++k)
  1183. dnd[k].Reset();
  1184. dndx = dndy = NULL;
  1185. }
  1186. for (int k = 0; k < k_dnd; ++k) {
  1187. if (dnd[k].Plt > 0)
  1188. continue;
  1189. ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine();
  1190. ImGui::Selectable(dnd[k].Label, false, 0, ImVec2(100, 0));
  1191. if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
  1192. ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int));
  1193. ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine();
  1194. ImGui::TextUnformatted(dnd[k].Label);
  1195. ImGui::EndDragDropSource();
  1196. }
  1197. }
  1198. ImGui::EndChild();
  1199. if (ImGui::BeginDragDropTarget()) {
  1200. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1201. int i = *(int*)payload->Data; dnd[i].Reset();
  1202. }
  1203. ImGui::EndDragDropTarget();
  1204. }
  1205. ImGui::SameLine();
  1206. ImGui::BeginChild("DND_RIGHT",ImVec2(-1,400));
  1207. // plot 1 (time series)
  1208. ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoGridLines;
  1209. if (ImPlot::BeginPlot("##DND1", NULL, "[drop here]", ImVec2(-1,195), ImPlotFlags_YAxis2 | ImPlotFlags_YAxis3, flags | ImPlotAxisFlags_Lock, flags, flags, flags, "[drop here]", "[drop here]")) {
  1210. for (int k = 0; k < k_dnd; ++k) {
  1211. if (dnd[k].Plt == 1 && dnd[k].Data.size() > 0) {
  1212. ImPlot::SetPlotYAxis(dnd[k].Yax);
  1213. ImPlot::SetNextLineStyle(dnd[k].Color);
  1214. static char label[32];
  1215. sprintf(label,"%s (Y%d)", dnd[k].Label, dnd[k].Yax+1);
  1216. ImPlot::PlotLine(label, &dnd[k].Data[0].x, &dnd[k].Data[0].y, dnd[k].Data.size(), 0, 2 * sizeof(float));
  1217. // allow legend item labels to be DND sources
  1218. if (ImPlot::BeginDragDropSourceItem(label)) {
  1219. ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int));
  1220. ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine();
  1221. ImGui::TextUnformatted(dnd[k].Label);
  1222. ImPlot::EndDragDropSource();
  1223. }
  1224. }
  1225. }
  1226. // allow the main plot area to be a DND target
  1227. if (ImPlot::BeginDragDropTarget()) {
  1228. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1229. int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = 0;
  1230. }
  1231. ImPlot::EndDragDropTarget();
  1232. }
  1233. // allow each y-axis to be a DND target
  1234. for (int y = 0; y < 3; ++y) {
  1235. if (ImPlot::BeginDragDropTargetY(y)) {
  1236. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1237. int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = y;
  1238. }
  1239. ImPlot::EndDragDropTarget();
  1240. }
  1241. }
  1242. // allow the legend to be a DND target
  1243. if (ImPlot::BeginDragDropTargetLegend()) {
  1244. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1245. int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = 0;
  1246. }
  1247. ImPlot::EndDragDropTarget();
  1248. }
  1249. ImPlot::EndPlot();
  1250. }
  1251. // plot 2 (Lissajous)
  1252. ImPlot::PushStyleColor(ImPlotCol_XAxis, dndx == NULL ? ImPlot::GetStyle().Colors[ImPlotCol_XAxis] : dndx->Color);
  1253. ImPlot::PushStyleColor(ImPlotCol_YAxis, dndy == NULL ? ImPlot::GetStyle().Colors[ImPlotCol_YAxis] : dndy->Color);
  1254. if (ImPlot::BeginPlot("##DND2", dndx == NULL ? "[drop here]" : dndx->Label, dndy == NULL ? "[drop here]" : dndy->Label, ImVec2(-1,195), 0, flags, flags )) {
  1255. if (dndx != NULL && dndy != NULL) {
  1256. 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);
  1257. ImPlot::SetNextLineStyle(mixed);
  1258. ImPlot::PlotLine("##dndxy", &dndx->Data[0].y, &dndy->Data[0].y, dndx->Data.size(), 0, 2 * sizeof(float));
  1259. }
  1260. // allow the x-axis to be a DND target
  1261. if (ImPlot::BeginDragDropTargetX()) {
  1262. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1263. int i = *(int*)payload->Data; dndx = &dnd[i];
  1264. }
  1265. ImPlot::EndDragDropTarget();
  1266. }
  1267. // allow the x-axis to be a DND source
  1268. if (dndx != NULL && ImPlot::BeginDragDropSourceX()) {
  1269. ImGui::SetDragDropPayload("MY_DND", &dndx->Idx, sizeof(int));
  1270. ImPlot::ItemIcon(dndx->Color); ImGui::SameLine();
  1271. ImGui::TextUnformatted(dndx->Label);
  1272. ImPlot::EndDragDropSource();
  1273. }
  1274. // allow the y-axis to be a DND target
  1275. if (ImPlot::BeginDragDropTargetY()) {
  1276. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1277. int i = *(int*)payload->Data; dndy = &dnd[i];
  1278. }
  1279. ImPlot::EndDragDropTarget();
  1280. }
  1281. // allow the y-axis to be a DND source
  1282. if (dndy != NULL && ImPlot::BeginDragDropSourceY()) {
  1283. ImGui::SetDragDropPayload("MY_DND", &dndy->Idx, sizeof(int));
  1284. ImPlot::ItemIcon(dndy->Color); ImGui::SameLine();
  1285. ImGui::TextUnformatted(dndy->Label);
  1286. ImPlot::EndDragDropSource();
  1287. }
  1288. // allow the plot area to be a DND target
  1289. if (ImPlot::BeginDragDropTarget()) {
  1290. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1291. int i = *(int*)payload->Data; dndx = dndy = &dnd[i];
  1292. }
  1293. }
  1294. // allow the plot area to be a DND source
  1295. if (ImPlot::BeginDragDropSource()) {
  1296. ImGui::TextUnformatted("Yes, you can\ndrag this!");
  1297. ImPlot::EndDragDropSource();
  1298. }
  1299. ImPlot::EndPlot();
  1300. }
  1301. ImPlot::PopStyleColor(2);
  1302. ImGui::EndChild();
  1303. }
  1304. void ShowDemo_Tables() {
  1305. #ifdef IMGUI_HAS_TABLE
  1306. static ImGuiTableFlags flags = ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_RowBg;
  1307. static bool anim = true;
  1308. static int offset = 0;
  1309. ImGui::BulletText("Plots can be used inside of ImGui tables as a means of creating subplots.");
  1310. ImGui::Checkbox("Animate",&anim);
  1311. if (anim)
  1312. offset = (offset + 1) % 100;
  1313. if (ImGui::BeginTable("##table", 3, flags, ImVec2(-1,0))) {
  1314. ImGui::TableSetupColumn("Electrode", ImGuiTableColumnFlags_WidthFixed, 75.0f);
  1315. ImGui::TableSetupColumn("Voltage", ImGuiTableColumnFlags_WidthFixed, 75.0f);
  1316. ImGui::TableSetupColumn("EMG Signal");
  1317. ImGui::TableHeadersRow();
  1318. ImPlot::PushColormap(ImPlotColormap_Cool);
  1319. for (int row = 0; row < 10; row++) {
  1320. ImGui::TableNextRow();
  1321. static float data[100];
  1322. srand(row);
  1323. for (int i = 0; i < 100; ++i)
  1324. data[i] = RandomRange(0.0f,10.0f);
  1325. ImGui::TableSetColumnIndex(0);
  1326. ImGui::Text("EMG %d", row);
  1327. ImGui::TableSetColumnIndex(1);
  1328. ImGui::Text("%.3f V", data[offset]);
  1329. ImGui::TableSetColumnIndex(2);
  1330. ImGui::PushID(row);
  1331. MyImPlot::Sparkline("##spark",data,100,0,11.0f,offset,ImPlot::GetColormapColor(row),ImVec2(-1, 35));
  1332. ImGui::PopID();
  1333. }
  1334. ImPlot::PopColormap();
  1335. ImGui::EndTable();
  1336. }
  1337. #else
  1338. ImGui::BulletText("You need to merge the ImGui 'tables' branch for this section.");
  1339. #endif
  1340. }
  1341. void ShowDemo_OffsetAndStride() {
  1342. static const int k_circles = 11;
  1343. static const int k_points_per = 50;
  1344. static const int k_size = 2 * k_points_per * k_circles;
  1345. static double interleaved_data[k_size];
  1346. for (int p = 0; p < k_points_per; ++p) {
  1347. for (int c = 0; c < k_circles; ++c) {
  1348. double r = (double)c / (k_circles - 1) * 0.2 + 0.2;
  1349. interleaved_data[p*2*k_circles + 2*c + 0] = 0.5 + r * cos((double)p/k_points_per * 6.28);
  1350. interleaved_data[p*2*k_circles + 2*c + 1] = 0.5 + r * sin((double)p/k_points_per * 6.28);
  1351. }
  1352. }
  1353. static int offset = 0;
  1354. ImGui::BulletText("Offsetting is useful for realtime plots (see above) and circular buffers.");
  1355. ImGui::BulletText("Striding is useful for interleaved data (e.g. audio) or plotting structs.");
  1356. ImGui::BulletText("Here, all circle data is stored in a single interleaved buffer:");
  1357. ImGui::BulletText("[c0.x0 c0.y0 ... cn.x0 cn.y0 c0.x1 c0.y1 ... cn.x1 cn.y1 ... cn.xm cn.ym]");
  1358. ImGui::BulletText("The offset value indicates which circle point index is considered the first.");
  1359. ImGui::BulletText("Offsets can be negative and/or larger than the actual data count.");
  1360. ImGui::SliderInt("Offset", &offset, -2*k_points_per, 2*k_points_per);
  1361. if (ImPlot::BeginPlot("##strideoffset",0,0,ImVec2(-1,0), ImPlotFlags_Equal)) {
  1362. ImPlot::PushColormap(ImPlotColormap_Jet);
  1363. char buff[16];
  1364. for (int c = 0; c < k_circles; ++c) {
  1365. sprintf(buff, "Circle %d", c);
  1366. ImPlot::PlotLine(buff, &interleaved_data[c*2 + 0], &interleaved_data[c*2 + 1], k_points_per, offset, 2*k_circles*sizeof(double));
  1367. }
  1368. ImPlot::EndPlot();
  1369. ImPlot::PopColormap();
  1370. }
  1371. // offset++; uncomment for animation!
  1372. }
  1373. void ShowDemo_CustomDataAndGetters() {
  1374. ImGui::BulletText("You can plot custom structs using the stride feature.");
  1375. ImGui::BulletText("Most plotters can also be passed a function pointer for getting data.");
  1376. ImGui::Indent();
  1377. ImGui::BulletText("You can optionally pass user data to be given to your getter function.");
  1378. ImGui::BulletText("C++ lambdas can be passed as function pointers as well!");
  1379. ImGui::Unindent();
  1380. MyImPlot::Vector2f vec2_data[2] = { MyImPlot::Vector2f(0,0), MyImPlot::Vector2f(1,1) };
  1381. if (ImPlot::BeginPlot("##Custom Data")) {
  1382. // custom structs using stride example:
  1383. ImPlot::PlotLine("Vector2f", &vec2_data[0].x, &vec2_data[0].y, 2, 0, sizeof(MyImPlot::Vector2f) /* or sizeof(float) * 2 */);
  1384. // custom getter example 1:
  1385. ImPlot::PlotLineG("Spiral", MyImPlot::Spiral, NULL, 1000);
  1386. // custom getter example 2:
  1387. static MyImPlot::WaveData data1(0.001, 0.2, 2, 0.75);
  1388. static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.25);
  1389. ImPlot::PlotLineG("Waves", MyImPlot::SineWave, &data1, 1000);
  1390. ImPlot::PlotLineG("Waves", MyImPlot::SawWave, &data2, 1000);
  1391. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
  1392. ImPlot::PlotShadedG("Waves", MyImPlot::SineWave, &data1, MyImPlot::SawWave, &data2, 1000);
  1393. ImPlot::PopStyleVar();
  1394. // you can also pass C++ lambdas:
  1395. // auto lamda = [](void* data, int idx) { ... return ImPlotPoint(x,y); };
  1396. // ImPlot::PlotLine("My Lambda", lambda, data, 1000);
  1397. ImPlot::EndPlot();
  1398. }
  1399. }
  1400. void ShowDemo_TickLabels() {
  1401. static bool custom_fmt = true;
  1402. static bool custom_ticks = false;
  1403. static bool custom_labels = true;
  1404. ImGui::Checkbox("Show Custom Format", &custom_fmt);
  1405. ImGui::SameLine();
  1406. ImGui::Checkbox("Show Custom Ticks", &custom_ticks);
  1407. if (custom_ticks) {
  1408. ImGui::SameLine();
  1409. ImGui::Checkbox("Show Custom Labels", &custom_labels);
  1410. }
  1411. double pi = 3.14;
  1412. const char* pi_str[] = {"PI"};
  1413. static double yticks[] = {1,3,7,9};
  1414. static const char* ylabels[] = {"One","Three","Seven","Nine"};
  1415. static double yticks_aux[] = {0.2,0.4,0.6};
  1416. static const char* ylabels_aux[] = {"A","B","C","D","E","F"};
  1417. if (custom_fmt) {
  1418. ImPlot::SetNextPlotFormatX("%g ms");
  1419. ImPlot::SetNextPlotFormatY("%g Hz", ImPlotYAxis_1);
  1420. ImPlot::SetNextPlotFormatY("%g dB", ImPlotYAxis_2);
  1421. ImPlot::SetNextPlotFormatY("%g km", ImPlotYAxis_3);
  1422. }
  1423. if (custom_ticks) {
  1424. ImPlot::SetNextPlotTicksX(&pi,1,custom_labels ? pi_str : NULL, true);
  1425. ImPlot::SetNextPlotTicksY(yticks, 4, custom_labels ? ylabels : NULL, ImPlotYAxis_1);
  1426. ImPlot::SetNextPlotTicksY(yticks_aux, 3, custom_labels ? ylabels_aux : NULL, false, ImPlotYAxis_2);
  1427. ImPlot::SetNextPlotTicksY(0, 1, 6, custom_labels ? ylabels_aux : NULL, false, ImPlotYAxis_3);
  1428. }
  1429. ImPlot::SetNextPlotLimits(2.5,5,0,10);
  1430. if (ImPlot::BeginPlot("##Ticks", NULL, NULL, ImVec2(-1,0), ImPlotFlags_YAxis2 | ImPlotFlags_YAxis3)) {
  1431. // nothing to see here, just the ticks
  1432. ImPlot::EndPlot();
  1433. }
  1434. }
  1435. void ShowDemo_CustomStyles() {
  1436. ImPlot::PushColormap(ImPlotColormap_Deep);
  1437. // normally you wouldn't change the entire style each frame
  1438. ImPlotStyle backup = ImPlot::GetStyle();
  1439. MyImPlot::StyleSeaborn();
  1440. ImPlot::SetNextPlotLimits(-0.5f, 9.5f, 0, 10);
  1441. if (ImPlot::BeginPlot("seaborn style", "x-axis", "y-axis")) {
  1442. unsigned int lin[10] = {8,8,9,7,8,8,8,9,7,8};
  1443. unsigned int bar[10] = {1,2,5,3,4,1,2,5,3,4};
  1444. unsigned int dot[10] = {7,6,6,7,8,5,6,5,8,7};
  1445. ImPlot::PlotBars("Bars", bar, 10, 0.5f);
  1446. ImPlot::PlotLine("Line", lin, 10);
  1447. ImPlot::NextColormapColor(); // skip green
  1448. ImPlot::PlotScatter("Scatter", dot, 10);
  1449. ImPlot::EndPlot();
  1450. }
  1451. ImPlot::GetStyle() = backup;
  1452. ImPlot::PopColormap();
  1453. }
  1454. void ShowDemo_CustomRendering() {
  1455. if (ImPlot::BeginPlot("##CustomRend")) {
  1456. ImVec2 cntr = ImPlot::PlotToPixels(ImPlotPoint(0.5f, 0.5f));
  1457. ImVec2 rmin = ImPlot::PlotToPixels(ImPlotPoint(0.25f, 0.75f));
  1458. ImVec2 rmax = ImPlot::PlotToPixels(ImPlotPoint(0.75f, 0.25f));
  1459. ImPlot::PushPlotClipRect();
  1460. ImPlot::GetPlotDrawList()->AddCircleFilled(cntr,20,IM_COL32(255,255,0,255),20);
  1461. ImPlot::GetPlotDrawList()->AddRect(rmin, rmax, IM_COL32(128,0,255,255));
  1462. ImPlot::PopPlotClipRect();
  1463. ImPlot::EndPlot();
  1464. }
  1465. }
  1466. void ShowDemo_LegendPopups() {
  1467. ImGui::BulletText("You can implement legend context menus to inject per-item controls and widgets.");
  1468. ImGui::BulletText("Right click the legend label/icon to edit custom item attributes.");
  1469. static float frequency = 0.1f;
  1470. static float amplitude = 0.5f;
  1471. static ImVec4 color = ImVec4(1,1,0,1);
  1472. static float alpha = 1.0f;
  1473. static bool line = false;
  1474. static float thickness = 1;
  1475. static bool markers = false;
  1476. static bool shaded = false;
  1477. static float vals[101];
  1478. for (int i = 0; i < 101; ++i)
  1479. vals[i] = amplitude * sinf(frequency * i);
  1480. ImPlot::SetNextPlotLimits(0,100,-1,1);
  1481. if (ImPlot::BeginPlot("Right Click the Legend")) {
  1482. // rendering logic
  1483. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha);
  1484. if (!line) {
  1485. ImPlot::SetNextFillStyle(color);
  1486. ImPlot::PlotBars("Right Click Me", vals, 101);
  1487. }
  1488. else {
  1489. if (markers) ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
  1490. ImPlot::SetNextLineStyle(color, thickness);
  1491. ImPlot::PlotLine("Right Click Me", vals, 101);
  1492. if (shaded) ImPlot::PlotShaded("Right Click Me",vals,101);
  1493. }
  1494. ImPlot::PopStyleVar();
  1495. // custom legend context menu
  1496. if (ImPlot::BeginLegendPopup("Right Click Me")) {
  1497. ImGui::SliderFloat("Frequency",&frequency,0,1,"%0.2f");
  1498. ImGui::SliderFloat("Amplitude",&amplitude,0,1,"%0.2f");
  1499. ImGui::Separator();
  1500. ImGui::ColorEdit3("Color",&color.x);
  1501. ImGui::SliderFloat("Transparency",&alpha,0,1,"%.2f");
  1502. ImGui::Checkbox("Line Plot", &line);
  1503. if (line) {
  1504. ImGui::SliderFloat("Thickness", &thickness, 0, 5);
  1505. ImGui::Checkbox("Markers", &markers);
  1506. ImGui::Checkbox("Shaded",&shaded);
  1507. }
  1508. ImPlot::EndLegendPopup();
  1509. }
  1510. ImPlot::EndPlot();
  1511. }
  1512. }
  1513. void ShowDemo_CustomPlottersAndTooltips() {
  1514. ImGui::BulletText("You can create custom plotters or extend ImPlot using implot_internal.h.");
  1515. 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};
  1516. 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};
  1517. 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};
  1518. 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};
  1519. 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};
  1520. static bool tooltip = true;
  1521. ImGui::Checkbox("Show Tooltip", &tooltip);
  1522. ImGui::SameLine();
  1523. static ImVec4 bullCol = ImVec4(0.000f, 1.000f, 0.441f, 1.000f);
  1524. static ImVec4 bearCol = ImVec4(0.853f, 0.050f, 0.310f, 1.000f);
  1525. ImGui::SameLine(); ImGui::ColorEdit4("##Bull", &bullCol.x, ImGuiColorEditFlags_NoInputs);
  1526. ImGui::SameLine(); ImGui::ColorEdit4("##Bear", &bearCol.x, ImGuiColorEditFlags_NoInputs);
  1527. ImPlot::GetStyle().UseLocalTime = false;
  1528. ImPlot::SetNextPlotFormatY("$%.0f");
  1529. ImPlot::SetNextPlotLimits(1546300800, 1571961600, 1250, 1600);
  1530. if (ImPlot::BeginPlot("Candlestick Chart",NULL,NULL,ImVec2(-1,0),0,ImPlotAxisFlags_Time,ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit)) {
  1531. MyImPlot::PlotCandlestick("GOOGL",dates, opens, closes, lows, highs, 218, tooltip, 0.25f, bullCol, bearCol);
  1532. ImPlot::EndPlot();
  1533. }
  1534. }
  1535. //-----------------------------------------------------------------------------
  1536. // DEMO WINDOW
  1537. //-----------------------------------------------------------------------------
  1538. void ShowDemoWindow(bool* p_open) {
  1539. static bool show_imgui_metrics = false;
  1540. static bool show_implot_metrics = false;
  1541. static bool show_imgui_style_editor = false;
  1542. static bool show_implot_style_editor = false;
  1543. static bool show_implot_benchmark = false;
  1544. if (show_imgui_metrics) {
  1545. ImGui::ShowMetricsWindow(&show_imgui_metrics);
  1546. }
  1547. if (show_implot_metrics) {
  1548. ImPlot::ShowMetricsWindow(&show_implot_metrics);
  1549. }
  1550. if (show_imgui_style_editor) {
  1551. ImGui::Begin("Style Editor (ImGui)", &show_imgui_style_editor);
  1552. ImGui::ShowStyleEditor();
  1553. ImGui::End();
  1554. }
  1555. if (show_implot_style_editor) {
  1556. ImGui::SetNextWindowSize(ImVec2(415,762), ImGuiCond_Appearing);
  1557. ImGui::Begin("Style Editor (ImPlot)", &show_implot_style_editor);
  1558. ImPlot::ShowStyleEditor();
  1559. ImGui::End();
  1560. }
  1561. if (show_implot_benchmark) {
  1562. ImGui::SetNextWindowSize(ImVec2(530,740), ImGuiCond_Appearing);
  1563. ImGui::Begin("ImPlot Benchmark Tool", &show_implot_benchmark);
  1564. ImPlot::ShowBenchmarkTool();
  1565. ImGui::End();
  1566. return;
  1567. }
  1568. ImGui::SetNextWindowPos(ImVec2(50, 50), ImGuiCond_FirstUseEver);
  1569. ImGui::SetNextWindowSize(ImVec2(600, 750), ImGuiCond_FirstUseEver);
  1570. ImGui::Begin("ImPlot Demo", p_open, ImGuiWindowFlags_MenuBar);
  1571. if (ImGui::BeginMenuBar()) {
  1572. if (ImGui::BeginMenu("Tools")) {
  1573. ImGui::MenuItem("Metrics (ImGui)", NULL, &show_imgui_metrics);
  1574. ImGui::MenuItem("Metrics (ImPlot)", NULL, &show_implot_metrics);
  1575. ImGui::MenuItem("Style Editor (ImGui)", NULL, &show_imgui_style_editor);
  1576. ImGui::MenuItem("Style Editor (ImPlot)", NULL, &show_implot_style_editor);
  1577. ImGui::MenuItem("Benchmark", NULL, &show_implot_benchmark);
  1578. ImGui::EndMenu();
  1579. }
  1580. ImGui::EndMenuBar();
  1581. }
  1582. //-------------------------------------------------------------------------
  1583. ImGui::Text("ImPlot says hello. (%s)", IMPLOT_VERSION);
  1584. // display warning about 16-bit indices
  1585. static bool showWarning = sizeof(ImDrawIdx)*8 == 16 && (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) == false;
  1586. if (showWarning) {
  1587. ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1,1,0,1));
  1588. ImGui::TextWrapped("WARNING: ImDrawIdx is 16-bit and ImGuiBackendFlags_RendererHasVtxOffset is false. Expect visual glitches and artifacts! See README for more information.");
  1589. ImGui::PopStyleColor();
  1590. }
  1591. ImGui::Spacing();
  1592. if (ImGui::BeginTabBar("ImPlotDemoTabs")) {
  1593. if (ImGui::BeginTabItem("Plots")) {
  1594. if (ImGui::CollapsingHeader("Line Plots"))
  1595. ShowDemo_LinePlots();
  1596. if (ImGui::CollapsingHeader("Filled Line Plots"))
  1597. ShowDemo_FilledLinePlots();
  1598. if (ImGui::CollapsingHeader("Shaded Plots##"))
  1599. ShowDemo_ShadedPlots();
  1600. if (ImGui::CollapsingHeader("Scatter Plots"))
  1601. ShowDemo_ScatterPlots();
  1602. if (ImGui::CollapsingHeader("Realtime Plots"))
  1603. ShowDemo_RealtimePlots();
  1604. if (ImGui::CollapsingHeader("Stairstep Plots"))
  1605. ShowDemo_StairstepPlots();
  1606. if (ImGui::CollapsingHeader("Bar Plots"))
  1607. ShowDemo_BarPlots();
  1608. if (ImGui::CollapsingHeader("Error Bars"))
  1609. ShowDemo_ErrorBars();
  1610. if (ImGui::CollapsingHeader("Stem Plots##"))
  1611. ShowDemo_StemPlots();
  1612. if (ImGui::CollapsingHeader("Infinite Lines"))
  1613. ShowDemo_InfiniteLines();
  1614. if (ImGui::CollapsingHeader("Pie Charts"))
  1615. ShowDemo_PieCharts();
  1616. if (ImGui::CollapsingHeader("Heatmaps"))
  1617. ShowDemo_Heatmaps();
  1618. if (ImGui::CollapsingHeader("Histogram"))
  1619. ShowDemo_Histogram();
  1620. if (ImGui::CollapsingHeader("Histogram 2D"))
  1621. ShowDemo_Histogram2D();
  1622. if (ImGui::CollapsingHeader("Digital Plots"))
  1623. ShowDemo_DigitalPlots();
  1624. if (ImGui::CollapsingHeader("Images"))
  1625. ShowDemo_Images();
  1626. if (ImGui::CollapsingHeader("Markers and Text"))
  1627. ShowDemo_MarkersAndText();
  1628. ImGui::EndTabItem();
  1629. }
  1630. if (ImGui::BeginTabItem("Subplots")) {
  1631. if (ImGui::CollapsingHeader("Sizing"))
  1632. ShowDemo_SubplotsSizing();
  1633. if (ImGui::CollapsingHeader("Item Sharing"))
  1634. ShowDemo_SubplotItemSharing();
  1635. if (ImGui::CollapsingHeader("Axis Linking"))
  1636. ShowDemo_SubplotAxisLinking();
  1637. if (ImGui::CollapsingHeader("Tables"))
  1638. ShowDemo_Tables();
  1639. ImGui::EndTabItem();
  1640. }
  1641. if (ImGui::BeginTabItem("Axes")) {
  1642. if (ImGui::CollapsingHeader("Log Axes"))
  1643. ShowDemo_LogAxes();
  1644. if (ImGui::CollapsingHeader("Time Axes"))
  1645. ShowDemo_TimeAxes();
  1646. if (ImGui::CollapsingHeader("Multiple Y-Axes"))
  1647. ShowDemo_MultipleYAxes();
  1648. if (ImGui::CollapsingHeader("Tick Labels"))
  1649. ShowDemo_TickLabels();
  1650. if (ImGui::CollapsingHeader("Linked Axes"))
  1651. ShowDemo_LinkedAxes();
  1652. if (ImGui::CollapsingHeader("Equal Axes"))
  1653. ShowDemo_EqualAxes();
  1654. if (ImGui::CollapsingHeader("Auto-Fitting Data"))
  1655. ShowDemo_AutoFittingData();
  1656. ImGui::EndTabItem();
  1657. }
  1658. if (ImGui::BeginTabItem("Tools")) {
  1659. if (ImGui::CollapsingHeader("Offset and Stride"))
  1660. ShowDemo_OffsetAndStride();
  1661. if (ImGui::CollapsingHeader("Querying"))
  1662. ShowDemo_Querying();
  1663. if (ImGui::CollapsingHeader("Views"))
  1664. ShowDemo_Views();
  1665. if (ImGui::CollapsingHeader("Drag Lines"))
  1666. ShowDemo_DragLines();
  1667. if (ImGui::CollapsingHeader("Drag Points"))
  1668. ShowDemo_DragPoints();
  1669. if (ImGui::CollapsingHeader("Annotations"))
  1670. ShowDemo_Annotations();
  1671. if (ImGui::CollapsingHeader("Drag and Drop"))
  1672. ShowDemo_DragAndDrop();
  1673. if (ImGui::CollapsingHeader("Legend Options"))
  1674. ShowDemo_LegendOptions();
  1675. if (ImGui::CollapsingHeader("Legend Popups"))
  1676. ShowDemo_LegendPopups();
  1677. ImGui::EndTabItem();
  1678. }
  1679. if (ImGui::BeginTabItem("Custom")) {
  1680. if (ImGui::CollapsingHeader("Custom Styles"))
  1681. ShowDemo_CustomStyles();
  1682. if (ImGui::CollapsingHeader("Custom Data and Getters"))
  1683. ShowDemo_CustomDataAndGetters();
  1684. if (ImGui::CollapsingHeader("Custom Rendering"))
  1685. ShowDemo_CustomRendering();
  1686. if (ImGui::CollapsingHeader("Custom Plotters and Tooltips"))
  1687. ShowDemo_CustomPlottersAndTooltips();
  1688. ImGui::EndTabItem();
  1689. }
  1690. if (ImGui::BeginTabItem("Config")) {
  1691. ShowDemo_Configuration();
  1692. ImGui::EndTabItem();
  1693. }
  1694. if (ImGui::BeginTabItem("Help")) {
  1695. ShowDemo_Help();
  1696. ImGui::EndTabItem();
  1697. }
  1698. ImGui::EndTabBar();
  1699. }
  1700. ImGui::End();
  1701. }
  1702. } // namespace ImPlot
  1703. namespace MyImPlot {
  1704. ImPlotPoint SineWave(void* data , int idx) {
  1705. WaveData* wd = (WaveData*)data;
  1706. double x = idx * wd->X;
  1707. return ImPlotPoint(x, wd->Offset + wd->Amp * sin(2 * 3.14 * wd->Freq * x));
  1708. }
  1709. ImPlotPoint SawWave(void* data, int idx) {
  1710. WaveData* wd = (WaveData*)data;
  1711. double x = idx * wd->X;
  1712. return ImPlotPoint(x, wd->Offset + wd->Amp * (-2 / 3.14 * atan(cos(3.14 * wd->Freq * x) / sin(3.14 * wd->Freq * x))));
  1713. }
  1714. ImPlotPoint Spiral(void*, int idx) {
  1715. float r = 0.9f; // outer radius
  1716. float a = 0; // inner radius
  1717. float b = 0.05f; // increment per rev
  1718. float n = (r - a) / b; // number of revolutions
  1719. double th = 2 * n * 3.14; // angle
  1720. float Th = float(th * idx / (1000 - 1));
  1721. return ImPlotPoint(0.5f+(a + b*Th / (2.0f * (float) 3.14))*cos(Th),
  1722. 0.5f + (a + b*Th / (2.0f * (float)3.14))*sin(Th));
  1723. }
  1724. // Example for Tables section. Generates a quick and simple shaded line plot. See implementation at bottom.
  1725. void Sparkline(const char* id, const float* values, int count, float min_v, float max_v, int offset, const ImVec4& col, const ImVec2& size) {
  1726. ImPlot::PushStyleVar(ImPlotStyleVar_PlotPadding, ImVec2(0,0));
  1727. ImPlot::SetNextPlotLimits(0, count - 1, min_v, max_v, ImGuiCond_Always);
  1728. if (ImPlot::BeginPlot(id,0,0,size,ImPlotFlags_CanvasOnly|ImPlotFlags_NoChild,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations)) {
  1729. ImPlot::PushStyleColor(ImPlotCol_Line, col);
  1730. ImPlot::PlotLine(id, values, count, 1, 0, offset);
  1731. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
  1732. ImPlot::PlotShaded(id, values, count, 0, 1, 0, offset);
  1733. ImPlot::PopStyleVar();
  1734. ImPlot::PopStyleColor();
  1735. ImPlot::EndPlot();
  1736. }
  1737. ImPlot::PopStyleVar();
  1738. }
  1739. void StyleSeaborn() {
  1740. ImPlotStyle& style = ImPlot::GetStyle();
  1741. ImVec4* colors = style.Colors;
  1742. colors[ImPlotCol_Line] = IMPLOT_AUTO_COL;
  1743. colors[ImPlotCol_Fill] = IMPLOT_AUTO_COL;
  1744. colors[ImPlotCol_MarkerOutline] = IMPLOT_AUTO_COL;
  1745. colors[ImPlotCol_MarkerFill] = IMPLOT_AUTO_COL;
  1746. colors[ImPlotCol_ErrorBar] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  1747. colors[ImPlotCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  1748. colors[ImPlotCol_PlotBg] = ImVec4(0.92f, 0.92f, 0.95f, 1.00f);
  1749. colors[ImPlotCol_PlotBorder] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
  1750. colors[ImPlotCol_LegendBg] = ImVec4(0.92f, 0.92f, 0.95f, 1.00f);
  1751. colors[ImPlotCol_LegendBorder] = ImVec4(0.80f, 0.81f, 0.85f, 1.00f);
  1752. colors[ImPlotCol_LegendText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  1753. colors[ImPlotCol_TitleText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  1754. colors[ImPlotCol_InlayText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  1755. colors[ImPlotCol_XAxis] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  1756. colors[ImPlotCol_XAxisGrid] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  1757. colors[ImPlotCol_YAxis] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  1758. colors[ImPlotCol_YAxisGrid] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  1759. colors[ImPlotCol_YAxis2] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  1760. colors[ImPlotCol_YAxisGrid2] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  1761. colors[ImPlotCol_YAxis3] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  1762. colors[ImPlotCol_YAxisGrid3] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  1763. colors[ImPlotCol_Selection] = ImVec4(1.00f, 0.65f, 0.00f, 1.00f);
  1764. colors[ImPlotCol_Query] = ImVec4(0.23f, 0.10f, 0.64f, 1.00f);
  1765. colors[ImPlotCol_Crosshairs] = ImVec4(0.23f, 0.10f, 0.64f, 0.50f);
  1766. style.LineWeight = 1.5;
  1767. style.Marker = ImPlotMarker_None;
  1768. style.MarkerSize = 4;
  1769. style.MarkerWeight = 1;
  1770. style.FillAlpha = 1.0f;
  1771. style.ErrorBarSize = 5;
  1772. style.ErrorBarWeight = 1.5f;
  1773. style.DigitalBitHeight = 8;
  1774. style.DigitalBitGap = 4;
  1775. style.PlotBorderSize = 0;
  1776. style.MinorAlpha = 1.0f;
  1777. style.MajorTickLen = ImVec2(0,0);
  1778. style.MinorTickLen = ImVec2(0,0);
  1779. style.MajorTickSize = ImVec2(0,0);
  1780. style.MinorTickSize = ImVec2(0,0);
  1781. style.MajorGridSize = ImVec2(1.2f,1.2f);
  1782. style.MinorGridSize = ImVec2(1.2f,1.2f);
  1783. style.PlotPadding = ImVec2(12,12);
  1784. style.LabelPadding = ImVec2(5,5);
  1785. style.LegendPadding = ImVec2(5,5);
  1786. style.MousePosPadding = ImVec2(5,5);
  1787. style.PlotMinSize = ImVec2(300,225);
  1788. }
  1789. } // namespaece MyImPlot
  1790. // WARNING:
  1791. //
  1792. // You can use "implot_internal.h" to build custom plotting fuctions or extend ImPlot.
  1793. // However, note that forward compatibility of this file is not guaranteed and the
  1794. // internal API is subject to change. At some point we hope to bring more of this
  1795. // into the public API and expose the necessary building blocks to fully support
  1796. // custom plotters. For now, proceed at your own risk!
  1797. #include "implot_internal.h"
  1798. namespace MyImPlot {
  1799. template <typename T>
  1800. int BinarySearch(const T* arr, int l, int r, T x) {
  1801. if (r >= l) {
  1802. int mid = l + (r - l) / 2;
  1803. if (arr[mid] == x)
  1804. return mid;
  1805. if (arr[mid] > x)
  1806. return BinarySearch(arr, l, mid - 1, x);
  1807. return BinarySearch(arr, mid + 1, r, x);
  1808. }
  1809. return -1;
  1810. }
  1811. void PlotCandlestick(const char* label_id, const double* xs, const double* opens, const double* closes, const double* lows, const double* highs, int count, bool tooltip, float width_percent, ImVec4 bullCol, ImVec4 bearCol) {
  1812. // get ImGui window DrawList
  1813. ImDrawList* draw_list = ImPlot::GetPlotDrawList();
  1814. // calc real value width
  1815. double half_width = count > 1 ? (xs[1] - xs[0]) * width_percent : width_percent;
  1816. // custom tool
  1817. if (ImPlot::IsPlotHovered() && tooltip) {
  1818. ImPlotPoint mouse = ImPlot::GetPlotMousePos();
  1819. mouse.x = ImPlot::RoundTime(ImPlotTime::FromDouble(mouse.x), ImPlotTimeUnit_Day).ToDouble();
  1820. float tool_l = ImPlot::PlotToPixels(mouse.x - half_width * 1.5, mouse.y).x;
  1821. float tool_r = ImPlot::PlotToPixels(mouse.x + half_width * 1.5, mouse.y).x;
  1822. float tool_t = ImPlot::GetPlotPos().y;
  1823. float tool_b = tool_t + ImPlot::GetPlotSize().y;
  1824. ImPlot::PushPlotClipRect();
  1825. draw_list->AddRectFilled(ImVec2(tool_l, tool_t), ImVec2(tool_r, tool_b), IM_COL32(128,128,128,64));
  1826. ImPlot::PopPlotClipRect();
  1827. // find mouse location index
  1828. int idx = BinarySearch(xs, 0, count - 1, mouse.x);
  1829. // render tool tip (won't be affected by plot clip rect)
  1830. if (idx != -1) {
  1831. ImGui::BeginTooltip();
  1832. char buff[32];
  1833. ImPlot::FormatDate(ImPlotTime::FromDouble(xs[idx]),buff,32,ImPlotDateFmt_DayMoYr,ImPlot::GetStyle().UseISO8601);
  1834. ImGui::Text("Day: %s", buff);
  1835. ImGui::Text("Open: $%.2f", opens[idx]);
  1836. ImGui::Text("Close: $%.2f", closes[idx]);
  1837. ImGui::Text("Low: $%.2f", lows[idx]);
  1838. ImGui::Text("High: $%.2f", highs[idx]);
  1839. ImGui::EndTooltip();
  1840. }
  1841. }
  1842. // begin plot item
  1843. if (ImPlot::BeginItem(label_id)) {
  1844. // override legend icon color
  1845. ImPlot::GetCurrentItem()->Color = IM_COL32(64,64,64,255);
  1846. // fit data if requested
  1847. if (ImPlot::FitThisFrame()) {
  1848. for (int i = 0; i < count; ++i) {
  1849. ImPlot::FitPoint(ImPlotPoint(xs[i], lows[i]));
  1850. ImPlot::FitPoint(ImPlotPoint(xs[i], highs[i]));
  1851. }
  1852. }
  1853. // render data
  1854. for (int i = 0; i < count; ++i) {
  1855. ImVec2 open_pos = ImPlot::PlotToPixels(xs[i] - half_width, opens[i]);
  1856. ImVec2 close_pos = ImPlot::PlotToPixels(xs[i] + half_width, closes[i]);
  1857. ImVec2 low_pos = ImPlot::PlotToPixels(xs[i], lows[i]);
  1858. ImVec2 high_pos = ImPlot::PlotToPixels(xs[i], highs[i]);
  1859. ImU32 color = ImGui::GetColorU32(opens[i] > closes[i] ? bearCol : bullCol);
  1860. draw_list->AddLine(low_pos, high_pos, color);
  1861. draw_list->AddRectFilled(open_pos, close_pos, color);
  1862. }
  1863. // end plot item
  1864. ImPlot::EndItem();
  1865. }
  1866. }
  1867. } // namespace MyImplot
  1868. namespace ImPlot {
  1869. //-----------------------------------------------------------------------------
  1870. // BENCHMARK
  1871. //-----------------------------------------------------------------------------
  1872. struct BenchData {
  1873. BenchData() {
  1874. float y = RandomRange(0.0f,1.0f);
  1875. Data = new float[1000];
  1876. for (int i = 0; i < 1000; ++i) {
  1877. Data[i] = y + RandomRange(-0.01f,0.01f);
  1878. }
  1879. Col = ImVec4(RandomRange(0.0f,1.0f),RandomRange(0.0f,1.0f),RandomRange(0.0f,1.0f),0.5f);
  1880. }
  1881. ~BenchData() { delete[] Data; }
  1882. float* Data;
  1883. ImVec4 Col;
  1884. };
  1885. enum BenchMode {
  1886. Line = 0,
  1887. LineG = 1,
  1888. Shaded = 2,
  1889. Scatter = 3,
  1890. Bars = 4
  1891. };
  1892. struct BenchRecord {
  1893. int Mode;
  1894. bool AA;
  1895. ImVector<ImPlotPoint> Data;
  1896. };
  1897. ImPlotPoint BenchmarkGetter(void* data, int idx) {
  1898. float* values = (float*)data;
  1899. return ImPlotPoint(idx, values[idx]);
  1900. }
  1901. void ShowBenchmarkTool() {
  1902. static const int max_items = 500;
  1903. static BenchData items[max_items];
  1904. static bool running = false;
  1905. static int frames = 60;
  1906. static int L = 0;
  1907. static int F = 0;
  1908. static double t1, t2;
  1909. static int mode = BenchMode::Line;
  1910. const char* names[] = {"Line","LineG","Shaded","Scatter","Bars"};
  1911. static ImVector<BenchRecord> records;
  1912. if (running) {
  1913. F++;
  1914. if (F == frames) {
  1915. t2 = ImGui::GetTime();
  1916. records.back().Data.push_back(ImPlotPoint(L, frames / (t2 - t1)));
  1917. L += 5;
  1918. F = 0;
  1919. t1 = ImGui::GetTime();
  1920. }
  1921. if (L > max_items) {
  1922. running = false;
  1923. L = max_items;
  1924. }
  1925. }
  1926. ImGui::Text("ImDrawIdx: %d-bit", (int)(sizeof(ImDrawIdx) * 8));
  1927. ImGui::Text("ImGuiBackendFlags_RendererHasVtxOffset: %s", (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ? "True" : "False");
  1928. ImGui::Text("%.2f FPS", ImGui::GetIO().Framerate);
  1929. ImGui::Separator();
  1930. bool was_running = running;
  1931. if (was_running) {
  1932. ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
  1933. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.25f);
  1934. }
  1935. if (ImGui::Button("Benchmark")) {
  1936. running = true;
  1937. L = F = 0;
  1938. records.push_back(BenchRecord());
  1939. records.back().Data.reserve(max_items+1);
  1940. records.back().Mode = mode;
  1941. records.back().AA = ImPlot::GetStyle().AntiAliasedLines;
  1942. t1 = ImGui::GetTime();
  1943. }
  1944. ImGui::SameLine();
  1945. ImGui::SetNextItemWidth(200);
  1946. ImGui::Combo("##Mode",&mode,names,4);
  1947. ImGui::SameLine();
  1948. ImGui::Checkbox("Anti-Aliased Lines", &ImPlot::GetStyle().AntiAliasedLines);
  1949. if (was_running) { ImGui::PopItemFlag(); ImGui::PopStyleVar(); }
  1950. ImGui::ProgressBar((float)L / (float)(max_items - 1));
  1951. ImPlot::SetNextPlotLimits(0,1000,0,1,ImGuiCond_Always);
  1952. if (ImPlot::BeginPlot("##Bench",NULL,NULL,ImVec2(-1,0),ImPlotFlags_NoChild | ImPlotFlags_CanvasOnly,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations)) {
  1953. if (running) {
  1954. if (mode == BenchMode::Line) {
  1955. for (int i = 0; i < L; ++i) {
  1956. ImGui::PushID(i);
  1957. ImPlot::SetNextLineStyle(items[i].Col);
  1958. ImPlot::PlotLine("##item", items[i].Data, 1000);
  1959. ImGui::PopID();
  1960. }
  1961. }
  1962. else if (mode == BenchMode::LineG) {
  1963. for (int i = 0; i < L; ++i) {
  1964. ImGui::PushID(i);
  1965. ImPlot::SetNextLineStyle(items[i].Col);
  1966. ImPlot::PlotLineG("##item",BenchmarkGetter,items[i].Data,1000);
  1967. ImGui::PopID();
  1968. }
  1969. }
  1970. else if (mode == BenchMode::Shaded) {
  1971. for (int i = 0; i < L; ++i) {
  1972. ImGui::PushID(i);
  1973. ImPlot::SetNextFillStyle(items[i].Col,0.5f);
  1974. ImPlot::PlotShaded("##item", items[i].Data, 1000);
  1975. ImGui::PopID();
  1976. }
  1977. }
  1978. else if (mode == BenchMode::Scatter) {
  1979. for (int i = 0; i < L; ++i) {
  1980. ImGui::PushID(i);
  1981. ImPlot::SetNextLineStyle(items[i].Col);
  1982. ImPlot::PlotScatter("##item", items[i].Data, 1000);
  1983. ImGui::PopID();
  1984. }
  1985. }
  1986. else if (mode == BenchMode::Bars) {
  1987. for (int i = 0; i < L; ++i) {
  1988. ImGui::PushID(i);
  1989. ImPlot::SetNextFillStyle(items[i].Col,0.5f);
  1990. ImPlot::PlotBars("##item", items[i].Data, 1000);
  1991. ImGui::PopID();
  1992. }
  1993. }
  1994. }
  1995. ImPlot::EndPlot();
  1996. }
  1997. ImPlot::SetNextPlotLimits(0,500,0,500,ImGuiCond_Always);
  1998. static char buffer[64];
  1999. if (ImPlot::BeginPlot("##Stats", "Items (1,000 pts each)", "Framerate (Hz)", ImVec2(-1,0), ImPlotFlags_NoChild)) {
  2000. for (int run = 0; run < records.size(); ++run) {
  2001. if (records[run].Data.Size > 1) {
  2002. sprintf(buffer, "B%d-%s%s", run + 1, names[records[run].Mode], records[run].AA ? "-AA" : "");
  2003. ImVector<ImPlotPoint>& d = records[run].Data;
  2004. ImPlot::PlotLine(buffer, &d[0].x, &d[0].y, d.Size, 0, 2*sizeof(double));
  2005. }
  2006. }
  2007. ImPlot::EndPlot();
  2008. }
  2009. }
  2010. }