implot_demo.cpp 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  1. // MIT License
  2. // Copyright (c) 2023 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.17
  19. // We define this so that the demo does not accidentally use deprecated API
  20. #ifndef IMPLOT_DISABLE_OBSOLETE_FUNCTIONS
  21. #define IMPLOT_DISABLE_OBSOLETE_FUNCTIONS
  22. #endif
  23. #include "implot.h"
  24. #ifndef IMGUI_DISABLE
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <time.h>
  29. #ifdef _MSC_VER
  30. #define sprintf sprintf_s
  31. #endif
  32. #ifndef PI
  33. #define PI 3.14159265358979323846
  34. #endif
  35. #define CHECKBOX_FLAG(flags, flag) ImGui::CheckboxFlags(#flag, (unsigned int*)&flags, flag)
  36. #if !defined(IMGUI_DISABLE_DEMO_WINDOWS)
  37. // Encapsulates examples for customizing ImPlot.
  38. namespace MyImPlot {
  39. // Example for Custom Data and Getters section.
  40. struct Vector2f {
  41. Vector2f(float _x, float _y) { x = _x; y = _y; }
  42. float x, y;
  43. };
  44. // Example for Custom Data and Getters section.
  45. struct WaveData {
  46. double X, Amp, Freq, Offset;
  47. WaveData(double x, double amp, double freq, double offset) { X = x; Amp = amp; Freq = freq; Offset = offset; }
  48. };
  49. ImPlotPoint SineWave(int idx, void* wave_data);
  50. ImPlotPoint SawWave(int idx, void* wave_data);
  51. ImPlotPoint Spiral(int idx, void* wave_data);
  52. // Example for Tables section.
  53. void Sparkline(const char* id, const float* values, int count, float min_v, float max_v, int offset, const ImVec4& col, const ImVec2& size);
  54. // Example for Custom Plotters and Tooltips section.
  55. 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));
  56. // Example for Custom Styles section.
  57. void StyleSeaborn();
  58. } // namespace MyImPlot
  59. namespace ImPlot {
  60. template <typename T>
  61. inline T RandomRange(T min, T max) {
  62. T scale = rand() / (T) RAND_MAX;
  63. return min + scale * ( max - min );
  64. }
  65. ImVec4 RandomColor() {
  66. ImVec4 col;
  67. col.x = RandomRange(0.0f,1.0f);
  68. col.y = RandomRange(0.0f,1.0f);
  69. col.z = RandomRange(0.0f,1.0f);
  70. col.w = 1.0f;
  71. return col;
  72. }
  73. double RandomGauss() {
  74. static double V1, V2, S;
  75. static int phase = 0;
  76. double X;
  77. if(phase == 0) {
  78. do {
  79. double U1 = (double)rand() / RAND_MAX;
  80. double U2 = (double)rand() / RAND_MAX;
  81. V1 = 2 * U1 - 1;
  82. V2 = 2 * U2 - 1;
  83. S = V1 * V1 + V2 * V2;
  84. } while(S >= 1 || S == 0);
  85. X = V1 * sqrt(-2 * log(S) / S);
  86. } else
  87. X = V2 * sqrt(-2 * log(S) / S);
  88. phase = 1 - phase;
  89. return X;
  90. }
  91. template <int N>
  92. struct NormalDistribution {
  93. NormalDistribution(double mean, double sd) {
  94. for (int i = 0; i < N; ++i)
  95. Data[i] = RandomGauss()*sd + mean;
  96. }
  97. double Data[N];
  98. };
  99. // utility structure for realtime plot
  100. struct ScrollingBuffer {
  101. int MaxSize;
  102. int Offset;
  103. ImVector<ImVec2> Data;
  104. ScrollingBuffer(int max_size = 2000) {
  105. MaxSize = max_size;
  106. Offset = 0;
  107. Data.reserve(MaxSize);
  108. }
  109. void AddPoint(float x, float y) {
  110. if (Data.size() < MaxSize)
  111. Data.push_back(ImVec2(x,y));
  112. else {
  113. Data[Offset] = ImVec2(x,y);
  114. Offset = (Offset + 1) % MaxSize;
  115. }
  116. }
  117. void Erase() {
  118. if (Data.size() > 0) {
  119. Data.shrink(0);
  120. Offset = 0;
  121. }
  122. }
  123. };
  124. // utility structure for realtime plot
  125. struct RollingBuffer {
  126. float Span;
  127. ImVector<ImVec2> Data;
  128. RollingBuffer() {
  129. Span = 10.0f;
  130. Data.reserve(2000);
  131. }
  132. void AddPoint(float x, float y) {
  133. float xmod = fmodf(x, Span);
  134. if (!Data.empty() && xmod < Data.back().x)
  135. Data.shrink(0);
  136. Data.push_back(ImVec2(xmod, y));
  137. }
  138. };
  139. // Huge data used by Time Formatting example (~500 MB allocation!)
  140. struct HugeTimeData {
  141. HugeTimeData(double min) {
  142. Ts = new double[Size];
  143. Ys = new double[Size];
  144. for (int i = 0; i < Size; ++i) {
  145. Ts[i] = min + i;
  146. Ys[i] = GetY(Ts[i]);
  147. }
  148. }
  149. ~HugeTimeData() { delete[] Ts; delete[] Ys; }
  150. static double GetY(double t) {
  151. return 0.5 + 0.25 * sin(t/86400/12) + 0.005 * sin(t/3600);
  152. }
  153. double* Ts;
  154. double* Ys;
  155. static const int Size = 60*60*24*366;
  156. };
  157. //-----------------------------------------------------------------------------
  158. // [SECTION] Demo Functions
  159. //-----------------------------------------------------------------------------
  160. void Demo_Help() {
  161. ImGui::Text("ABOUT THIS DEMO:");
  162. ImGui::BulletText("Sections below are demonstrating many aspects of the library.");
  163. ImGui::BulletText("The \"Tools\" menu above gives access to: Style Editors (ImPlot/ImGui)\n"
  164. "and Metrics (general purpose Dear ImGui debugging tool).");
  165. ImGui::Separator();
  166. ImGui::Text("PROGRAMMER GUIDE:");
  167. ImGui::BulletText("See the ShowDemoWindow() code in implot_demo.cpp. <- you are here!");
  168. ImGui::BulletText("If you see visual artifacts, do one of the following:");
  169. ImGui::Indent();
  170. ImGui::BulletText("Handle ImGuiBackendFlags_RendererHasVtxOffset for 16-bit indices in your backend.");
  171. ImGui::BulletText("Or, enable 32-bit indices in imconfig.h.");
  172. ImGui::BulletText("Your current configuration is:");
  173. ImGui::Indent();
  174. ImGui::BulletText("ImDrawIdx: %d-bit", (int)(sizeof(ImDrawIdx) * 8));
  175. ImGui::BulletText("ImGuiBackendFlags_RendererHasVtxOffset: %s", (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ? "True" : "False");
  176. ImGui::Unindent();
  177. ImGui::Unindent();
  178. ImGui::Separator();
  179. ImGui::Text("USER GUIDE:");
  180. ShowUserGuide();
  181. }
  182. //-----------------------------------------------------------------------------
  183. void ButtonSelector(const char* label, ImGuiMouseButton* b) {
  184. ImGui::PushID(label);
  185. if (ImGui::RadioButton("LMB",*b == ImGuiMouseButton_Left))
  186. *b = ImGuiMouseButton_Left;
  187. ImGui::SameLine();
  188. if (ImGui::RadioButton("RMB",*b == ImGuiMouseButton_Right))
  189. *b = ImGuiMouseButton_Right;
  190. ImGui::SameLine();
  191. if (ImGui::RadioButton("MMB",*b == ImGuiMouseButton_Middle))
  192. *b = ImGuiMouseButton_Middle;
  193. ImGui::PopID();
  194. }
  195. void ModSelector(const char* label, int* k) {
  196. ImGui::PushID(label);
  197. ImGui::CheckboxFlags("Ctrl", (unsigned int*)k, ImGuiMod_Ctrl); ImGui::SameLine();
  198. ImGui::CheckboxFlags("Shift", (unsigned int*)k, ImGuiMod_Shift); ImGui::SameLine();
  199. ImGui::CheckboxFlags("Alt", (unsigned int*)k, ImGuiMod_Alt); ImGui::SameLine();
  200. ImGui::CheckboxFlags("Super", (unsigned int*)k, ImGuiMod_Super);
  201. ImGui::PopID();
  202. }
  203. void InputMapping(const char* label, ImGuiMouseButton* b, int* k) {
  204. ImGui::LabelText("##","%s",label);
  205. if (b != nullptr) {
  206. ImGui::SameLine(100);
  207. ButtonSelector(label,b);
  208. }
  209. if (k != nullptr) {
  210. ImGui::SameLine(300);
  211. ModSelector(label,k);
  212. }
  213. }
  214. void ShowInputMapping() {
  215. ImPlotInputMap& map = ImPlot::GetInputMap();
  216. InputMapping("Pan",&map.Pan,&map.PanMod);
  217. InputMapping("Fit",&map.Fit,nullptr);
  218. InputMapping("Select",&map.Select,&map.SelectMod);
  219. InputMapping("SelectHorzMod",nullptr,&map.SelectHorzMod);
  220. InputMapping("SelectVertMod",nullptr,&map.SelectVertMod);
  221. InputMapping("SelectCancel",&map.SelectCancel,nullptr);
  222. InputMapping("Menu",&map.Menu,nullptr);
  223. InputMapping("OverrideMod",nullptr,&map.OverrideMod);
  224. InputMapping("ZoomMod",nullptr,&map.ZoomMod);
  225. ImGui::SliderFloat("ZoomRate",&map.ZoomRate,-1,1);
  226. }
  227. void Demo_Config() {
  228. ImGui::ShowFontSelector("Font");
  229. ImGui::ShowStyleSelector("ImGui Style");
  230. ImPlot::ShowStyleSelector("ImPlot Style");
  231. ImPlot::ShowColormapSelector("ImPlot Colormap");
  232. ImPlot::ShowInputMapSelector("Input Map");
  233. ImGui::Separator();
  234. ImGui::Checkbox("Use Local Time", &ImPlot::GetStyle().UseLocalTime);
  235. ImGui::Checkbox("Use ISO 8601", &ImPlot::GetStyle().UseISO8601);
  236. ImGui::Checkbox("Use 24 Hour Clock", &ImPlot::GetStyle().Use24HourClock);
  237. ImGui::Separator();
  238. if (ImPlot::BeginPlot("Preview")) {
  239. static double now = (double)time(nullptr);
  240. ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Time);
  241. ImPlot::SetupAxisLimits(ImAxis_X1, now, now + 24*3600);
  242. for (int i = 0; i < 10; ++i) {
  243. double x[2] = {now, now + 24*3600};
  244. double y[2] = {0,i/9.0};
  245. ImGui::PushID(i);
  246. ImPlot::PlotLine("##Line",x,y,2);
  247. ImGui::PopID();
  248. }
  249. ImPlot::EndPlot();
  250. }
  251. }
  252. //-----------------------------------------------------------------------------
  253. void Demo_LinePlots() {
  254. static float xs1[1001], ys1[1001];
  255. for (int i = 0; i < 1001; ++i) {
  256. xs1[i] = i * 0.001f;
  257. ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)ImGui::GetTime() / 10));
  258. }
  259. static double xs2[20], ys2[20];
  260. for (int i = 0; i < 20; ++i) {
  261. xs2[i] = i * 1/19.0f;
  262. ys2[i] = xs2[i] * xs2[i];
  263. }
  264. if (ImPlot::BeginPlot("Line Plots")) {
  265. ImPlot::SetupAxes("x","y");
  266. ImPlot::PlotLine("f(x)", xs1, ys1, 1001);
  267. ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
  268. ImPlot::PlotLine("g(x)", xs2, ys2, 20,ImPlotLineFlags_Segments);
  269. ImPlot::EndPlot();
  270. }
  271. }
  272. //-----------------------------------------------------------------------------
  273. void Demo_FilledLinePlots() {
  274. static double xs1[101], ys1[101], ys2[101], ys3[101];
  275. srand(0);
  276. for (int i = 0; i < 101; ++i) {
  277. xs1[i] = (float)i;
  278. ys1[i] = RandomRange(400.0,450.0);
  279. ys2[i] = RandomRange(275.0,350.0);
  280. ys3[i] = RandomRange(150.0,225.0);
  281. }
  282. static bool show_lines = true;
  283. static bool show_fills = true;
  284. static float fill_ref = 0;
  285. static int shade_mode = 0;
  286. static ImPlotShadedFlags flags = 0;
  287. ImGui::Checkbox("Lines",&show_lines); ImGui::SameLine();
  288. ImGui::Checkbox("Fills",&show_fills);
  289. if (show_fills) {
  290. ImGui::SameLine();
  291. if (ImGui::RadioButton("To -INF",shade_mode == 0))
  292. shade_mode = 0;
  293. ImGui::SameLine();
  294. if (ImGui::RadioButton("To +INF",shade_mode == 1))
  295. shade_mode = 1;
  296. ImGui::SameLine();
  297. if (ImGui::RadioButton("To Ref",shade_mode == 2))
  298. shade_mode = 2;
  299. if (shade_mode == 2) {
  300. ImGui::SameLine();
  301. ImGui::SetNextItemWidth(100);
  302. ImGui::DragFloat("##Ref",&fill_ref, 1, -100, 500);
  303. }
  304. }
  305. if (ImPlot::BeginPlot("Stock Prices")) {
  306. ImPlot::SetupAxes("Days","Price");
  307. ImPlot::SetupAxesLimits(0,100,0,500);
  308. if (show_fills) {
  309. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
  310. ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref, flags);
  311. ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref, flags);
  312. ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref, flags);
  313. ImPlot::PopStyleVar();
  314. }
  315. if (show_lines) {
  316. ImPlot::PlotLine("Stock 1", xs1, ys1, 101);
  317. ImPlot::PlotLine("Stock 2", xs1, ys2, 101);
  318. ImPlot::PlotLine("Stock 3", xs1, ys3, 101);
  319. }
  320. ImPlot::EndPlot();
  321. }
  322. }
  323. //-----------------------------------------------------------------------------
  324. void Demo_ShadedPlots() {
  325. static float xs[1001], ys[1001], ys1[1001], ys2[1001], ys3[1001], ys4[1001];
  326. srand(0);
  327. for (int i = 0; i < 1001; ++i) {
  328. xs[i] = i * 0.001f;
  329. ys[i] = 0.25f + 0.25f * sinf(25 * xs[i]) * sinf(5 * xs[i]) + RandomRange(-0.01f, 0.01f);
  330. ys1[i] = ys[i] + RandomRange(0.1f, 0.12f);
  331. ys2[i] = ys[i] - RandomRange(0.1f, 0.12f);
  332. ys3[i] = 0.75f + 0.2f * sinf(25 * xs[i]);
  333. ys4[i] = 0.75f + 0.1f * cosf(25 * xs[i]);
  334. }
  335. static float alpha = 0.25f;
  336. ImGui::DragFloat("Alpha",&alpha,0.01f,0,1);
  337. if (ImPlot::BeginPlot("Shaded Plots")) {
  338. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha);
  339. ImPlot::PlotShaded("Uncertain Data",xs,ys1,ys2,1001);
  340. ImPlot::PlotLine("Uncertain Data", xs, ys, 1001);
  341. ImPlot::PlotShaded("Overlapping",xs,ys3,ys4,1001);
  342. ImPlot::PlotLine("Overlapping",xs,ys3,1001);
  343. ImPlot::PlotLine("Overlapping",xs,ys4,1001);
  344. ImPlot::PopStyleVar();
  345. ImPlot::EndPlot();
  346. }
  347. }
  348. //-----------------------------------------------------------------------------
  349. void Demo_ScatterPlots() {
  350. srand(0);
  351. static float xs1[100], ys1[100];
  352. for (int i = 0; i < 100; ++i) {
  353. xs1[i] = i * 0.01f;
  354. ys1[i] = xs1[i] + 0.1f * ((float)rand() / (float)RAND_MAX);
  355. }
  356. static float xs2[50], ys2[50];
  357. for (int i = 0; i < 50; i++) {
  358. xs2[i] = 0.25f + 0.2f * ((float)rand() / (float)RAND_MAX);
  359. ys2[i] = 0.75f + 0.2f * ((float)rand() / (float)RAND_MAX);
  360. }
  361. if (ImPlot::BeginPlot("Scatter Plot")) {
  362. ImPlot::PlotScatter("Data 1", xs1, ys1, 100);
  363. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
  364. ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, ImPlot::GetColormapColor(1), IMPLOT_AUTO, ImPlot::GetColormapColor(1));
  365. ImPlot::PlotScatter("Data 2", xs2, ys2, 50);
  366. ImPlot::PopStyleVar();
  367. ImPlot::EndPlot();
  368. }
  369. }
  370. //-----------------------------------------------------------------------------
  371. void Demo_StairstepPlots() {
  372. static float ys1[21], ys2[21];
  373. for (int i = 0; i < 21; ++i) {
  374. ys1[i] = 0.75f + 0.2f * sinf(10 * i * 0.05f);
  375. ys2[i] = 0.25f + 0.2f * sinf(10 * i * 0.05f);
  376. }
  377. static ImPlotStairsFlags flags = 0;
  378. CHECKBOX_FLAG(flags, ImPlotStairsFlags_Shaded);
  379. if (ImPlot::BeginPlot("Stairstep Plot")) {
  380. ImPlot::SetupAxes("x","f(x)");
  381. ImPlot::SetupAxesLimits(0,1,0,1);
  382. ImPlot::PushStyleColor(ImPlotCol_Line, ImVec4(0.5f,0.5f,0.5f,1.0f));
  383. ImPlot::PlotLine("##1",ys1,21,0.05f);
  384. ImPlot::PlotLine("##2",ys2,21,0.05f);
  385. ImPlot::PopStyleColor();
  386. ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
  387. ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.25f);
  388. ImPlot::PlotStairs("Post Step (default)", ys1, 21, 0.05f, 0, flags);
  389. ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
  390. ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.25f);
  391. ImPlot::PlotStairs("Pre Step", ys2, 21, 0.05f, 0, flags|ImPlotStairsFlags_PreStep);
  392. ImPlot::EndPlot();
  393. }
  394. }
  395. //-----------------------------------------------------------------------------
  396. void Demo_BarPlots() {
  397. static ImS8 data[10] = {1,2,3,4,5,6,7,8,9,10};
  398. if (ImPlot::BeginPlot("Bar Plot")) {
  399. ImPlot::PlotBars("Vertical",data,10,0.7,1);
  400. ImPlot::PlotBars("Horizontal",data,10,0.4,1,ImPlotBarsFlags_Horizontal);
  401. ImPlot::EndPlot();
  402. }
  403. }
  404. //-----------------------------------------------------------------------------
  405. void Demo_BarGroups() {
  406. static ImS8 data[30] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90, // midterm
  407. 80, 62, 56, 99, 55, 78, 88, 78, 90, 100, // final
  408. 80, 69, 52, 92, 72, 78, 75, 76, 89, 95}; // course
  409. static const char* ilabels[] = {"Midterm Exam","Final Exam","Course Grade"};
  410. static const char* glabels[] = {"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10"};
  411. static const double positions[] = {0,1,2,3,4,5,6,7,8,9};
  412. static int items = 3;
  413. static int groups = 10;
  414. static float size = 0.67f;
  415. static ImPlotBarGroupsFlags flags = 0;
  416. static bool horz = false;
  417. ImGui::CheckboxFlags("Stacked", (unsigned int*)&flags, ImPlotBarGroupsFlags_Stacked);
  418. ImGui::SameLine();
  419. ImGui::Checkbox("Horizontal",&horz);
  420. ImGui::SliderInt("Items",&items,1,3);
  421. ImGui::SliderFloat("Size",&size,0,1);
  422. if (ImPlot::BeginPlot("Bar Group")) {
  423. ImPlot::SetupLegend(ImPlotLocation_East, ImPlotLegendFlags_Outside);
  424. if (horz) {
  425. ImPlot::SetupAxes("Score","Student",ImPlotAxisFlags_AutoFit,ImPlotAxisFlags_AutoFit);
  426. ImPlot::SetupAxisTicks(ImAxis_Y1,positions, groups, glabels);
  427. ImPlot::PlotBarGroups(ilabels,data,items,groups,size,0,flags|ImPlotBarGroupsFlags_Horizontal);
  428. }
  429. else {
  430. ImPlot::SetupAxes("Student","Score",ImPlotAxisFlags_AutoFit,ImPlotAxisFlags_AutoFit);
  431. ImPlot::SetupAxisTicks(ImAxis_X1,positions, groups, glabels);
  432. ImPlot::PlotBarGroups(ilabels,data,items,groups,size,0,flags);
  433. }
  434. ImPlot::EndPlot();
  435. }
  436. }
  437. //-----------------------------------------------------------------------------
  438. void Demo_BarStacks() {
  439. static ImPlotColormap Liars = -1;
  440. if (Liars == -1) {
  441. static const ImU32 Liars_Data[6] = { 4282515870, 4282609140, 4287357182, 4294630301, 4294945280, 4294921472 };
  442. Liars = ImPlot::AddColormap("Liars", Liars_Data, 6);
  443. }
  444. static bool diverging = true;
  445. ImGui::Checkbox("Diverging",&diverging);
  446. static const char* politicians[] = {"Trump","Bachman","Cruz","Gingrich","Palin","Santorum","Walker","Perry","Ryan","McCain","Rubio","Romney","Rand Paul","Christie","Biden","Kasich","Sanders","J Bush","H Clinton","Obama"};
  447. static int data_reg[] = {18,26,7,14,10,8,6,11,4,4,3,8,6,8,6,5,0,3,1,2, // Pants on Fire
  448. 43,36,30,21,30,27,25,17,11,22,15,16,16,17,12,12,14,6,13,12, // False
  449. 16,13,28,22,15,21,15,18,30,17,24,18,13,10,14,15,17,22,14,12, // Mostly False
  450. 17,10,13,25,12,22,19,26,23,17,22,27,20,26,29,17,18,22,21,27, // Half True
  451. 5,7,16,10,10,12,23,13,17,20,22,16,23,19,20,26,36,29,27,26, // Mostly True
  452. 1,8,6,8,23,10,12,15,15,20,14,15,22,20,19,25,15,18,24,21}; // True
  453. static const char* labels_reg[] = {"Pants on Fire","False","Mostly False","Half True","Mostly True","True"};
  454. static int data_div[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // Pants on Fire (dummy, to order legend logically)
  455. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // False (dummy, to order legend logically)
  456. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // Mostly False (dummy, to order legend logically)
  457. -16,-13,-28,-22,-15,-21,-15,-18,-30,-17,-24,-18,-13,-10,-14,-15,-17,-22,-14,-12, // Mostly False
  458. -43,-36,-30,-21,-30,-27,-25,-17,-11,-22,-15,-16,-16,-17,-12,-12,-14,-6,-13,-12, // False
  459. -18,-26,-7,-14,-10,-8,-6,-11,-4,-4,-3,-8,-6,-8,-6,-5,0,-3,-1,-2, // Pants on Fire
  460. 17,10,13,25,12,22,19,26,23,17,22,27,20,26,29,17,18,22,21,27, // Half True
  461. 5,7,16,10,10,12,23,13,17,20,22,16,23,19,20,26,36,29,27,26, // Mostly True
  462. 1,8,6,8,23,10,12,15,15,20,14,15,22,20,19,25,15,18,24,21}; // True
  463. static const char* labels_div[] = {"Pants on Fire","False","Mostly False","Mostly False","False","Pants on Fire","Half True","Mostly True","True"};
  464. ImPlot::PushColormap(Liars);
  465. if (ImPlot::BeginPlot("PolitiFact: Who Lies More?",ImVec2(-1,400),ImPlotFlags_NoMouseText)) {
  466. ImPlot::SetupLegend(ImPlotLocation_South, ImPlotLegendFlags_Outside|ImPlotLegendFlags_Horizontal);
  467. ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Invert);
  468. ImPlot::SetupAxisTicks(ImAxis_Y1,0,19,20,politicians,false);
  469. if (diverging)
  470. ImPlot::PlotBarGroups(labels_div,data_div,9,20,0.75,0,ImPlotBarGroupsFlags_Stacked|ImPlotBarGroupsFlags_Horizontal);
  471. else
  472. ImPlot::PlotBarGroups(labels_reg,data_reg,6,20,0.75,0,ImPlotBarGroupsFlags_Stacked|ImPlotBarGroupsFlags_Horizontal);
  473. ImPlot::EndPlot();
  474. }
  475. ImPlot::PopColormap();
  476. }
  477. //-----------------------------------------------------------------------------
  478. void Demo_ErrorBars() {
  479. static float xs[5] = {1,2,3,4,5};
  480. static float bar[5] = {1,2,5,3,4};
  481. static float lin1[5] = {8,8,9,7,8};
  482. static float lin2[5] = {6,7,6,9,6};
  483. static float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f};
  484. static float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f};
  485. static float err3[5] = {0.09f, 0.14f, 0.09f, 0.12f, 0.16f};
  486. static float err4[5] = {0.02f, 0.08f, 0.15f, 0.05f, 0.2f};
  487. if (ImPlot::BeginPlot("##ErrorBars")) {
  488. ImPlot::SetupAxesLimits(0, 6, 0, 10);
  489. ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f);
  490. ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5);
  491. ImPlot::SetNextErrorBarStyle(ImPlot::GetColormapColor(1), 0);
  492. ImPlot::PlotErrorBars("Line", xs, lin1, err1, err2, 5);
  493. ImPlot::SetNextMarkerStyle(ImPlotMarker_Square);
  494. ImPlot::PlotLine("Line", xs, lin1, 5);
  495. ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(2));
  496. ImPlot::PlotErrorBars("Scatter", xs, lin2, err2, 5);
  497. ImPlot::PlotErrorBars("Scatter", xs, lin2, err3, err4, 5, ImPlotErrorBarsFlags_Horizontal);
  498. ImPlot::PopStyleColor();
  499. ImPlot::PlotScatter("Scatter", xs, lin2, 5);
  500. ImPlot::EndPlot();
  501. }
  502. }
  503. //-----------------------------------------------------------------------------
  504. void Demo_StemPlots() {
  505. static double xs[51], ys1[51], ys2[51];
  506. for (int i = 0; i < 51; ++i) {
  507. xs[i] = i * 0.02;
  508. ys1[i] = 1.0 + 0.5 * sin(25*xs[i])*cos(2*xs[i]);
  509. ys2[i] = 0.5 + 0.25 * sin(10*xs[i]) * sin(xs[i]);
  510. }
  511. if (ImPlot::BeginPlot("Stem Plots")) {
  512. ImPlot::SetupAxisLimits(ImAxis_X1,0,1.0);
  513. ImPlot::SetupAxisLimits(ImAxis_Y1,0,1.6);
  514. ImPlot::PlotStems("Stems 1",xs,ys1,51);
  515. ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
  516. ImPlot::PlotStems("Stems 2", xs, ys2,51);
  517. ImPlot::EndPlot();
  518. }
  519. }
  520. //-----------------------------------------------------------------------------
  521. void Demo_InfiniteLines() {
  522. static double vals[] = {0.25, 0.5, 0.75};
  523. if (ImPlot::BeginPlot("##Infinite")) {
  524. ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoInitialFit,ImPlotAxisFlags_NoInitialFit);
  525. ImPlot::PlotInfLines("Vertical",vals,3);
  526. ImPlot::PlotInfLines("Horizontal",vals,3,ImPlotInfLinesFlags_Horizontal);
  527. ImPlot::EndPlot();
  528. }
  529. }
  530. //-----------------------------------------------------------------------------
  531. void Demo_PieCharts() {
  532. static const char* labels1[] = {"Frogs","Hogs","Dogs","Logs"};
  533. static float data1[] = {0.15f, 0.30f, 0.2f, 0.05f};
  534. static ImPlotPieChartFlags flags = 0;
  535. ImGui::SetNextItemWidth(250);
  536. ImGui::DragFloat4("Values", data1, 0.01f, 0, 1);
  537. CHECKBOX_FLAG(flags, ImPlotPieChartFlags_Normalize);
  538. CHECKBOX_FLAG(flags, ImPlotPieChartFlags_IgnoreHidden);
  539. CHECKBOX_FLAG(flags, ImPlotPieChartFlags_Exploding);
  540. if (ImPlot::BeginPlot("##Pie1", ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMouseText)) {
  541. ImPlot::SetupAxes(nullptr, nullptr, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations);
  542. ImPlot::SetupAxesLimits(0, 1, 0, 1);
  543. ImPlot::PlotPieChart(labels1, data1, 4, 0.5, 0.5, 0.4, "%.2f", 90, flags);
  544. ImPlot::EndPlot();
  545. }
  546. ImGui::SameLine();
  547. static const char* labels2[] = {"A","B","C","D","E"};
  548. static int data2[] = {1,1,2,3,5};
  549. ImPlot::PushColormap(ImPlotColormap_Pastel);
  550. if (ImPlot::BeginPlot("##Pie2", ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMouseText)) {
  551. ImPlot::SetupAxes(nullptr, nullptr, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations);
  552. ImPlot::SetupAxesLimits(0, 1, 0, 1);
  553. ImPlot::PlotPieChart(labels2, data2, 5, 0.5, 0.5, 0.4, "%.0f", 180, flags);
  554. ImPlot::EndPlot();
  555. }
  556. ImPlot::PopColormap();
  557. }
  558. //-----------------------------------------------------------------------------
  559. void Demo_Heatmaps() {
  560. static float values1[7][7] = {{0.8f, 2.4f, 2.5f, 3.9f, 0.0f, 4.0f, 0.0f},
  561. {2.4f, 0.0f, 4.0f, 1.0f, 2.7f, 0.0f, 0.0f},
  562. {1.1f, 2.4f, 0.8f, 4.3f, 1.9f, 4.4f, 0.0f},
  563. {0.6f, 0.0f, 0.3f, 0.0f, 3.1f, 0.0f, 0.0f},
  564. {0.7f, 1.7f, 0.6f, 2.6f, 2.2f, 6.2f, 0.0f},
  565. {1.3f, 1.2f, 0.0f, 0.0f, 0.0f, 3.2f, 5.1f},
  566. {0.1f, 2.0f, 0.0f, 1.4f, 0.0f, 1.9f, 6.3f}};
  567. static float scale_min = 0;
  568. static float scale_max = 6.3f;
  569. static const char* xlabels[] = {"C1","C2","C3","C4","C5","C6","C7"};
  570. static const char* ylabels[] = {"R1","R2","R3","R4","R5","R6","R7"};
  571. static ImPlotColormap map = ImPlotColormap_Viridis;
  572. if (ImPlot::ColormapButton(ImPlot::GetColormapName(map),ImVec2(225,0),map)) {
  573. map = (map + 1) % ImPlot::GetColormapCount();
  574. // We bust the color cache of our plots so that item colors will
  575. // resample the new colormap in the event that they have already
  576. // been created. See documentation in implot.h.
  577. BustColorCache("##Heatmap1");
  578. BustColorCache("##Heatmap2");
  579. }
  580. ImGui::SameLine();
  581. ImGui::LabelText("##Colormap Index", "%s", "Change Colormap");
  582. ImGui::SetNextItemWidth(225);
  583. ImGui::DragFloatRange2("Min / Max",&scale_min, &scale_max, 0.01f, -20, 20);
  584. static ImPlotHeatmapFlags hm_flags = 0;
  585. ImGui::CheckboxFlags("Column Major", (unsigned int*)&hm_flags, ImPlotHeatmapFlags_ColMajor);
  586. static ImPlotAxisFlags axes_flags = ImPlotAxisFlags_Lock | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoTickMarks;
  587. ImPlot::PushColormap(map);
  588. if (ImPlot::BeginPlot("##Heatmap1",ImVec2(225,225),ImPlotFlags_NoLegend|ImPlotFlags_NoMouseText)) {
  589. ImPlot::SetupAxes(nullptr, nullptr, axes_flags, axes_flags);
  590. ImPlot::SetupAxisTicks(ImAxis_X1,0 + 1.0/14.0, 1 - 1.0/14.0, 7, xlabels);
  591. ImPlot::SetupAxisTicks(ImAxis_Y1,1 - 1.0/14.0, 0 + 1.0/14.0, 7, ylabels);
  592. ImPlot::PlotHeatmap("heat",values1[0],7,7,scale_min,scale_max,"%g",ImPlotPoint(0,0),ImPlotPoint(1,1),hm_flags);
  593. ImPlot::EndPlot();
  594. }
  595. ImGui::SameLine();
  596. ImPlot::ColormapScale("##HeatScale",scale_min, scale_max, ImVec2(60,225));
  597. ImGui::SameLine();
  598. const int size = 80;
  599. static double values2[size*size];
  600. srand((unsigned int)(ImGui::GetTime()*1000000));
  601. for (int i = 0; i < size*size; ++i)
  602. values2[i] = RandomRange(0.0,1.0);
  603. if (ImPlot::BeginPlot("##Heatmap2",ImVec2(225,225))) {
  604. ImPlot::SetupAxes(nullptr, nullptr, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations);
  605. ImPlot::SetupAxesLimits(-1,1,-1,1);
  606. ImPlot::PlotHeatmap("heat1",values2,size,size,0,1,nullptr);
  607. ImPlot::PlotHeatmap("heat2",values2,size,size,0,1,nullptr, ImPlotPoint(-1,-1), ImPlotPoint(0,0));
  608. ImPlot::EndPlot();
  609. }
  610. ImPlot::PopColormap();
  611. }
  612. //-----------------------------------------------------------------------------
  613. void Demo_Histogram() {
  614. static ImPlotHistogramFlags hist_flags = ImPlotHistogramFlags_Density;
  615. static int bins = 50;
  616. static double mu = 5;
  617. static double sigma = 2;
  618. ImGui::SetNextItemWidth(200);
  619. if (ImGui::RadioButton("Sqrt",bins==ImPlotBin_Sqrt)) { bins = ImPlotBin_Sqrt; } ImGui::SameLine();
  620. if (ImGui::RadioButton("Sturges",bins==ImPlotBin_Sturges)) { bins = ImPlotBin_Sturges; } ImGui::SameLine();
  621. if (ImGui::RadioButton("Rice",bins==ImPlotBin_Rice)) { bins = ImPlotBin_Rice; } ImGui::SameLine();
  622. if (ImGui::RadioButton("Scott",bins==ImPlotBin_Scott)) { bins = ImPlotBin_Scott; } ImGui::SameLine();
  623. if (ImGui::RadioButton("N Bins",bins>=0)) { bins = 50; }
  624. if (bins>=0) {
  625. ImGui::SameLine();
  626. ImGui::SetNextItemWidth(200);
  627. ImGui::SliderInt("##Bins", &bins, 1, 100);
  628. }
  629. ImGui::CheckboxFlags("Horizontal", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Horizontal);
  630. ImGui::SameLine();
  631. ImGui::CheckboxFlags("Density", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Density);
  632. ImGui::SameLine();
  633. ImGui::CheckboxFlags("Cumulative", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Cumulative);
  634. static bool range = false;
  635. ImGui::Checkbox("Range", &range);
  636. static float rmin = -3;
  637. static float rmax = 13;
  638. if (range) {
  639. ImGui::SameLine();
  640. ImGui::SetNextItemWidth(200);
  641. ImGui::DragFloat2("##Range",&rmin,0.1f,-3,13);
  642. ImGui::SameLine();
  643. ImGui::CheckboxFlags("Exclude Outliers", (unsigned int*)&hist_flags, ImPlotHistogramFlags_NoOutliers);
  644. }
  645. static NormalDistribution<10000> dist(mu, sigma);
  646. static double x[100];
  647. static double y[100];
  648. if (hist_flags & ImPlotHistogramFlags_Density) {
  649. for (int i = 0; i < 100; ++i) {
  650. x[i] = -3 + 16 * (double)i/99.0;
  651. y[i] = exp( - (x[i]-mu)*(x[i]-mu) / (2*sigma*sigma)) / (sigma * sqrt(2*3.141592653589793238));
  652. }
  653. if (hist_flags & ImPlotHistogramFlags_Cumulative) {
  654. for (int i = 1; i < 100; ++i)
  655. y[i] += y[i-1];
  656. for (int i = 0; i < 100; ++i)
  657. y[i] /= y[99];
  658. }
  659. }
  660. if (ImPlot::BeginPlot("##Histograms")) {
  661. ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_AutoFit,ImPlotAxisFlags_AutoFit);
  662. ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL,0.5f);
  663. ImPlot::PlotHistogram("Empirical", dist.Data, 10000, bins, 1.0, range ? ImPlotRange(rmin,rmax) : ImPlotRange(), hist_flags);
  664. if ((hist_flags & ImPlotHistogramFlags_Density) && !(hist_flags & ImPlotHistogramFlags_NoOutliers)) {
  665. if (hist_flags & ImPlotHistogramFlags_Horizontal)
  666. ImPlot::PlotLine("Theoretical",y,x,100);
  667. else
  668. ImPlot::PlotLine("Theoretical",x,y,100);
  669. }
  670. ImPlot::EndPlot();
  671. }
  672. }
  673. //-----------------------------------------------------------------------------
  674. void Demo_Histogram2D() {
  675. static int count = 50000;
  676. static int xybins[2] = {100,100};
  677. static ImPlotHistogramFlags hist_flags = 0;
  678. ImGui::SliderInt("Count",&count,100,100000);
  679. ImGui::SliderInt2("Bins",xybins,1,500);
  680. ImGui::SameLine();
  681. ImGui::CheckboxFlags("Density", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Density);
  682. static NormalDistribution<100000> dist1(1, 2);
  683. static NormalDistribution<100000> dist2(1, 1);
  684. double max_count = 0;
  685. ImPlotAxisFlags flags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Foreground;
  686. ImPlot::PushColormap("Hot");
  687. if (ImPlot::BeginPlot("##Hist2D",ImVec2(ImGui::GetContentRegionAvail().x-100-ImGui::GetStyle().ItemSpacing.x,0))) {
  688. ImPlot::SetupAxes(nullptr, nullptr, flags, flags);
  689. ImPlot::SetupAxesLimits(-6,6,-6,6);
  690. max_count = ImPlot::PlotHistogram2D("Hist2D",dist1.Data,dist2.Data,count,xybins[0],xybins[1],ImPlotRect(-6,6,-6,6), hist_flags);
  691. ImPlot::EndPlot();
  692. }
  693. ImGui::SameLine();
  694. ImPlot::ColormapScale(hist_flags & ImPlotHistogramFlags_Density ? "Density" : "Count",0,max_count,ImVec2(100,0));
  695. ImPlot::PopColormap();
  696. }
  697. //-----------------------------------------------------------------------------
  698. void Demo_DigitalPlots() {
  699. ImGui::BulletText("Digital plots do not respond to Y drag and zoom, so that");
  700. ImGui::Indent();
  701. ImGui::Text("you can drag analog plots over the rising/falling digital edge.");
  702. ImGui::Unindent();
  703. static bool paused = false;
  704. static ScrollingBuffer dataDigital[2];
  705. static ScrollingBuffer dataAnalog[2];
  706. static bool showDigital[2] = {true, false};
  707. static bool showAnalog[2] = {true, false};
  708. char label[32];
  709. ImGui::Checkbox("digital_0", &showDigital[0]); ImGui::SameLine();
  710. ImGui::Checkbox("digital_1", &showDigital[1]); ImGui::SameLine();
  711. ImGui::Checkbox("analog_0", &showAnalog[0]); ImGui::SameLine();
  712. ImGui::Checkbox("analog_1", &showAnalog[1]);
  713. static float t = 0;
  714. if (!paused) {
  715. t += ImGui::GetIO().DeltaTime;
  716. //digital signal values
  717. if (showDigital[0])
  718. dataDigital[0].AddPoint(t, sinf(2*t) > 0.45);
  719. if (showDigital[1])
  720. dataDigital[1].AddPoint(t, sinf(2*t) < 0.45);
  721. //Analog signal values
  722. if (showAnalog[0])
  723. dataAnalog[0].AddPoint(t, sinf(2*t));
  724. if (showAnalog[1])
  725. dataAnalog[1].AddPoint(t, cosf(2*t));
  726. }
  727. if (ImPlot::BeginPlot("##Digital")) {
  728. ImPlot::SetupAxisLimits(ImAxis_X1, t - 10.0, t, paused ? ImGuiCond_Once : ImGuiCond_Always);
  729. ImPlot::SetupAxisLimits(ImAxis_Y1, -1, 1);
  730. for (int i = 0; i < 2; ++i) {
  731. if (showDigital[i] && dataDigital[i].Data.size() > 0) {
  732. snprintf(label, sizeof(label), "digital_%d", i);
  733. ImPlot::PlotDigital(label, &dataDigital[i].Data[0].x, &dataDigital[i].Data[0].y, dataDigital[i].Data.size(), 0, dataDigital[i].Offset, 2 * sizeof(float));
  734. }
  735. }
  736. for (int i = 0; i < 2; ++i) {
  737. if (showAnalog[i]) {
  738. snprintf(label, sizeof(label), "analog_%d", i);
  739. if (dataAnalog[i].Data.size() > 0)
  740. ImPlot::PlotLine(label, &dataAnalog[i].Data[0].x, &dataAnalog[i].Data[0].y, dataAnalog[i].Data.size(), 0, dataAnalog[i].Offset, 2 * sizeof(float));
  741. }
  742. }
  743. ImPlot::EndPlot();
  744. }
  745. }
  746. //-----------------------------------------------------------------------------
  747. void Demo_Images() {
  748. ImGui::BulletText("Below we are displaying the font texture, which is the only texture we have\naccess to in this demo.");
  749. ImGui::BulletText("Use the 'ImTextureID' type as storage to pass pointers or identifiers to your\nown texture data.");
  750. ImGui::BulletText("See ImGui Wiki page 'Image Loading and Displaying Examples'.");
  751. static ImVec2 bmin(0,0);
  752. static ImVec2 bmax(1,1);
  753. static ImVec2 uv0(0,0);
  754. static ImVec2 uv1(1,1);
  755. static ImVec4 tint(1,1,1,1);
  756. ImGui::SliderFloat2("Min", &bmin.x, -2, 2, "%.1f");
  757. ImGui::SliderFloat2("Max", &bmax.x, -2, 2, "%.1f");
  758. ImGui::SliderFloat2("UV0", &uv0.x, -2, 2, "%.1f");
  759. ImGui::SliderFloat2("UV1", &uv1.x, -2, 2, "%.1f");
  760. ImGui::ColorEdit4("Tint",&tint.x);
  761. if (ImPlot::BeginPlot("##image")) {
  762. #ifdef IMGUI_HAS_TEXTURES
  763. // We use the font atlas ImTextureRef for this demo, but in your real code when you submit
  764. // an image that you have loaded yourself, you would normally have a ImTextureID which works
  765. // just as well (as ImTextureRef can be constructed from ImTextureID).
  766. ImPlot::PlotImage("my image", ImGui::GetIO().Fonts->TexRef, bmin, bmax, uv0, uv1, tint);
  767. #else
  768. ImPlot::PlotImage("my image", ImGui::GetIO().Fonts->TexID, bmin, bmax, uv0, uv1, tint);
  769. #endif
  770. ImPlot::EndPlot();
  771. }
  772. }
  773. //-----------------------------------------------------------------------------
  774. void Demo_RealtimePlots() {
  775. ImGui::BulletText("Move your mouse to change the data!");
  776. ImGui::BulletText("This example assumes 60 FPS. Higher FPS requires larger buffer size.");
  777. static ScrollingBuffer sdata1, sdata2;
  778. static RollingBuffer rdata1, rdata2;
  779. ImVec2 mouse = ImGui::GetMousePos();
  780. static float t = 0;
  781. t += ImGui::GetIO().DeltaTime;
  782. sdata1.AddPoint(t, mouse.x * 0.0005f);
  783. rdata1.AddPoint(t, mouse.x * 0.0005f);
  784. sdata2.AddPoint(t, mouse.y * 0.0005f);
  785. rdata2.AddPoint(t, mouse.y * 0.0005f);
  786. static float history = 10.0f;
  787. ImGui::SliderFloat("History",&history,1,30,"%.1f s");
  788. rdata1.Span = history;
  789. rdata2.Span = history;
  790. static ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels;
  791. if (ImPlot::BeginPlot("##Scrolling", ImVec2(-1,150))) {
  792. ImPlot::SetupAxes(nullptr, nullptr, flags, flags);
  793. ImPlot::SetupAxisLimits(ImAxis_X1,t - history, t, ImGuiCond_Always);
  794. ImPlot::SetupAxisLimits(ImAxis_Y1,0,1);
  795. ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL,0.5f);
  796. ImPlot::PlotShaded("Mouse X", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), -INFINITY, 0, sdata1.Offset, 2 * sizeof(float));
  797. ImPlot::PlotLine("Mouse Y", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), 0, sdata2.Offset, 2*sizeof(float));
  798. ImPlot::EndPlot();
  799. }
  800. if (ImPlot::BeginPlot("##Rolling", ImVec2(-1,150))) {
  801. ImPlot::SetupAxes(nullptr, nullptr, flags, flags);
  802. ImPlot::SetupAxisLimits(ImAxis_X1,0,history, ImGuiCond_Always);
  803. ImPlot::SetupAxisLimits(ImAxis_Y1,0,1);
  804. ImPlot::PlotLine("Mouse X", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 0, 2 * sizeof(float));
  805. ImPlot::PlotLine("Mouse Y", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 0, 2 * sizeof(float));
  806. ImPlot::EndPlot();
  807. }
  808. }
  809. //-----------------------------------------------------------------------------
  810. void Demo_MarkersAndText() {
  811. static float mk_size = ImPlot::GetStyle().MarkerSize;
  812. static float mk_weight = ImPlot::GetStyle().MarkerWeight;
  813. ImGui::DragFloat("Marker Size",&mk_size,0.1f,2.0f,10.0f,"%.2f px");
  814. ImGui::DragFloat("Marker Weight", &mk_weight,0.05f,0.5f,3.0f,"%.2f px");
  815. if (ImPlot::BeginPlot("##MarkerStyles", ImVec2(-1,0), ImPlotFlags_CanvasOnly)) {
  816. ImPlot::SetupAxes(nullptr, nullptr, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations);
  817. ImPlot::SetupAxesLimits(0, 10, 0, 12);
  818. ImS8 xs[2] = {1,4};
  819. ImS8 ys[2] = {10,11};
  820. // filled markers
  821. for (int m = 0; m < ImPlotMarker_COUNT; ++m) {
  822. ImGui::PushID(m);
  823. ImPlot::SetNextMarkerStyle(m, mk_size, IMPLOT_AUTO_COL, mk_weight);
  824. ImPlot::PlotLine("##Filled", xs, ys, 2);
  825. ImGui::PopID();
  826. ys[0]--; ys[1]--;
  827. }
  828. xs[0] = 6; xs[1] = 9; ys[0] = 10; ys[1] = 11;
  829. // open markers
  830. for (int m = 0; m < ImPlotMarker_COUNT; ++m) {
  831. ImGui::PushID(m);
  832. ImPlot::SetNextMarkerStyle(m, mk_size, ImVec4(0,0,0,0), mk_weight);
  833. ImPlot::PlotLine("##Open", xs, ys, 2);
  834. ImGui::PopID();
  835. ys[0]--; ys[1]--;
  836. }
  837. ImPlot::PlotText("Filled Markers", 2.5f, 6.0f);
  838. ImPlot::PlotText("Open Markers", 7.5f, 6.0f);
  839. ImPlot::PushStyleColor(ImPlotCol_InlayText, ImVec4(1,0,1,1));
  840. ImPlot::PlotText("Vertical Text", 5.0f, 6.0f, ImVec2(0,0), ImPlotTextFlags_Vertical);
  841. ImPlot::PopStyleColor();
  842. ImPlot::EndPlot();
  843. }
  844. }
  845. //-----------------------------------------------------------------------------
  846. void Demo_NaNValues() {
  847. static bool include_nan = true;
  848. static ImPlotLineFlags flags = 0;
  849. float data1[5] = {0.0f,0.25f,0.5f,0.75f,1.0f};
  850. float data2[5] = {0.0f,0.25f,0.5f,0.75f,1.0f};
  851. if (include_nan)
  852. data1[2] = NAN;
  853. ImGui::Checkbox("Include NaN",&include_nan);
  854. ImGui::SameLine();
  855. ImGui::CheckboxFlags("Skip NaN", (unsigned int*)&flags, ImPlotLineFlags_SkipNaN);
  856. if (ImPlot::BeginPlot("##NaNValues")) {
  857. ImPlot::SetNextMarkerStyle(ImPlotMarker_Square);
  858. ImPlot::PlotLine("line", data1, data2, 5, flags);
  859. ImPlot::PlotBars("bars", data1, 5);
  860. ImPlot::EndPlot();
  861. }
  862. }
  863. //-----------------------------------------------------------------------------
  864. void Demo_LogScale() {
  865. static double xs[1001], ys1[1001], ys2[1001], ys3[1001];
  866. for (int i = 0; i < 1001; ++i) {
  867. xs[i] = i*0.1f;
  868. ys1[i] = sin(xs[i]) + 1;
  869. ys2[i] = log(xs[i]);
  870. ys3[i] = pow(10.0, xs[i]);
  871. }
  872. if (ImPlot::BeginPlot("Log Plot", ImVec2(-1,0))) {
  873. ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Log10);
  874. ImPlot::SetupAxesLimits(0.1, 100, 0, 10);
  875. ImPlot::PlotLine("f(x) = x", xs, xs, 1001);
  876. ImPlot::PlotLine("f(x) = sin(x)+1", xs, ys1, 1001);
  877. ImPlot::PlotLine("f(x) = log(x)", xs, ys2, 1001);
  878. ImPlot::PlotLine("f(x) = 10^x", xs, ys3, 21);
  879. ImPlot::EndPlot();
  880. }
  881. }
  882. //-----------------------------------------------------------------------------
  883. void Demo_SymmetricLogScale() {
  884. static double xs[1001], ys1[1001], ys2[1001];
  885. for (int i = 0; i < 1001; ++i) {
  886. xs[i] = i*0.1f-50;
  887. ys1[i] = sin(xs[i]);
  888. ys2[i] = i*0.002 - 1;
  889. }
  890. if (ImPlot::BeginPlot("SymLog Plot", ImVec2(-1,0))) {
  891. ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_SymLog);
  892. ImPlot::PlotLine("f(x) = a*x+b",xs,ys2,1001);
  893. ImPlot::PlotLine("f(x) = sin(x)",xs,ys1,1001);
  894. ImPlot::EndPlot();
  895. }
  896. }
  897. //-----------------------------------------------------------------------------
  898. void Demo_TimeScale() {
  899. static double t_min = 1609459200; // 01/01/2021 @ 12:00:00am (UTC)
  900. static double t_max = 1640995200; // 01/01/2022 @ 12:00:00am (UTC)
  901. ImGui::BulletText("When ImPlotAxisFlags_Time is enabled on the X-Axis, values are interpreted as\n"
  902. "UNIX timestamps in seconds and axis labels are formated as date/time.");
  903. ImGui::BulletText("By default, labels are in UTC time but can be set to use local time instead.");
  904. ImGui::Checkbox("Local Time",&ImPlot::GetStyle().UseLocalTime);
  905. ImGui::SameLine();
  906. ImGui::Checkbox("ISO 8601",&ImPlot::GetStyle().UseISO8601);
  907. ImGui::SameLine();
  908. ImGui::Checkbox("24 Hour Clock",&ImPlot::GetStyle().Use24HourClock);
  909. static HugeTimeData* data = nullptr;
  910. if (data == nullptr) {
  911. ImGui::SameLine();
  912. if (ImGui::Button("Generate Huge Data (~500MB!)")) {
  913. static HugeTimeData sdata(t_min);
  914. data = &sdata;
  915. }
  916. }
  917. if (ImPlot::BeginPlot("##Time", ImVec2(-1,0))) {
  918. ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Time);
  919. ImPlot::SetupAxesLimits(t_min,t_max,0,1);
  920. if (data != nullptr) {
  921. // downsample our data
  922. int downsample = (int)ImPlot::GetPlotLimits().X.Size() / 1000 + 1;
  923. int start = (int)(ImPlot::GetPlotLimits().X.Min - t_min);
  924. start = start < 0 ? 0 : start > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : start;
  925. int end = (int)(ImPlot::GetPlotLimits().X.Max - t_min) + 1000;
  926. end = end < 0 ? 0 : end > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : end;
  927. int size = (end - start)/downsample;
  928. // plot it
  929. ImPlot::PlotLine("Time Series", &data->Ts[start], &data->Ys[start], size, 0, 0, sizeof(double)*downsample);
  930. }
  931. // plot time now
  932. double t_now = (double)time(nullptr);
  933. double y_now = HugeTimeData::GetY(t_now);
  934. ImPlot::PlotScatter("Now",&t_now,&y_now,1);
  935. ImPlot::Annotation(t_now,y_now,ImPlot::GetLastItemColor(),ImVec2(10,10),false,"Now");
  936. ImPlot::EndPlot();
  937. }
  938. }
  939. //-----------------------------------------------------------------------------
  940. static inline double TransformForward_Sqrt(double v, void*) {
  941. return sqrt(v);
  942. }
  943. static inline double TransformInverse_Sqrt(double v, void*) {
  944. return v*v;
  945. }
  946. void Demo_CustomScale() {
  947. static float v[100];
  948. for (int i = 0; i < 100; ++i) {
  949. v[i] = i*0.01f;
  950. }
  951. if (ImPlot::BeginPlot("Sqrt")) {
  952. ImPlot::SetupAxis(ImAxis_X1, "Linear");
  953. ImPlot::SetupAxis(ImAxis_Y1, "Sqrt");
  954. ImPlot::SetupAxisScale(ImAxis_Y1, TransformForward_Sqrt, TransformInverse_Sqrt);
  955. ImPlot::SetupAxisLimitsConstraints(ImAxis_Y1, 0, INFINITY);
  956. ImPlot::PlotLine("##data",v,v,100);
  957. ImPlot::EndPlot();
  958. }
  959. }
  960. //-----------------------------------------------------------------------------
  961. void Demo_MultipleAxes() {
  962. static float xs[1001], xs2[1001], ys1[1001], ys2[1001], ys3[1001];
  963. for (int i = 0; i < 1001; ++i) {
  964. xs[i] = (i*0.1f);
  965. xs2[i] = xs[i] + 10.0f;
  966. ys1[i] = sinf(xs[i]) * 3 + 1;
  967. ys2[i] = cosf(xs[i]) * 0.2f + 0.5f;
  968. ys3[i] = sinf(xs[i]+0.5f) * 100 + 200;
  969. }
  970. static bool x2_axis = true;
  971. static bool y2_axis = true;
  972. static bool y3_axis = true;
  973. ImGui::Checkbox("X-Axis 2", &x2_axis);
  974. ImGui::SameLine();
  975. ImGui::Checkbox("Y-Axis 2", &y2_axis);
  976. ImGui::SameLine();
  977. ImGui::Checkbox("Y-Axis 3", &y3_axis);
  978. ImGui::BulletText("You can drag axes to the opposite side of the plot.");
  979. ImGui::BulletText("Hover over legend items to see which axis they are plotted on.");
  980. if (ImPlot::BeginPlot("Multi-Axis Plot", ImVec2(-1,0))) {
  981. ImPlot::SetupAxes("X-Axis 1", "Y-Axis 1");
  982. ImPlot::SetupAxesLimits(0, 100, 0, 10);
  983. if (x2_axis) {
  984. ImPlot::SetupAxis(ImAxis_X2, "X-Axis 2",ImPlotAxisFlags_AuxDefault);
  985. ImPlot::SetupAxisLimits(ImAxis_X2, 0, 100);
  986. }
  987. if (y2_axis) {
  988. ImPlot::SetupAxis(ImAxis_Y2, "Y-Axis 2",ImPlotAxisFlags_AuxDefault);
  989. ImPlot::SetupAxisLimits(ImAxis_Y2, 0, 1);
  990. }
  991. if (y3_axis) {
  992. ImPlot::SetupAxis(ImAxis_Y3, "Y-Axis 3",ImPlotAxisFlags_AuxDefault);
  993. ImPlot::SetupAxisLimits(ImAxis_Y3, 0, 300);
  994. }
  995. ImPlot::PlotLine("f(x) = x", xs, xs, 1001);
  996. if (x2_axis) {
  997. ImPlot::SetAxes(ImAxis_X2, ImAxis_Y1);
  998. ImPlot::PlotLine("f(x) = sin(x)*3+1", xs2, ys1, 1001);
  999. }
  1000. if (y2_axis) {
  1001. ImPlot::SetAxes(ImAxis_X1, ImAxis_Y2);
  1002. ImPlot::PlotLine("f(x) = cos(x)*.2+.5", xs, ys2, 1001);
  1003. }
  1004. if (x2_axis && y3_axis) {
  1005. ImPlot::SetAxes(ImAxis_X2, ImAxis_Y3);
  1006. ImPlot::PlotLine("f(x) = sin(x+.5)*100+200 ", xs2, ys3, 1001);
  1007. }
  1008. ImPlot::EndPlot();
  1009. }
  1010. }
  1011. //-----------------------------------------------------------------------------
  1012. void Demo_LinkedAxes() {
  1013. static ImPlotRect lims(0,1,0,1);
  1014. static bool linkx = true, linky = true;
  1015. int data[2] = {0,1};
  1016. ImGui::Checkbox("Link X", &linkx);
  1017. ImGui::SameLine();
  1018. ImGui::Checkbox("Link Y", &linky);
  1019. ImGui::DragScalarN("Limits",ImGuiDataType_Double,&lims.X.Min,4,0.01f);
  1020. if (BeginAlignedPlots("AlignedGroup")) {
  1021. if (ImPlot::BeginPlot("Plot A")) {
  1022. ImPlot::SetupAxisLinks(ImAxis_X1, linkx ? &lims.X.Min : nullptr, linkx ? &lims.X.Max : nullptr);
  1023. ImPlot::SetupAxisLinks(ImAxis_Y1, linky ? &lims.Y.Min : nullptr, linky ? &lims.Y.Max : nullptr);
  1024. ImPlot::PlotLine("Line",data,2);
  1025. ImPlot::EndPlot();
  1026. }
  1027. if (ImPlot::BeginPlot("Plot B")) {
  1028. ImPlot::SetupAxisLinks(ImAxis_X1, linkx ? &lims.X.Min : nullptr, linkx ? &lims.X.Max : nullptr);
  1029. ImPlot::SetupAxisLinks(ImAxis_Y1, linky ? &lims.Y.Min : nullptr, linky ? &lims.Y.Max : nullptr);
  1030. ImPlot::PlotLine("Line",data,2);
  1031. ImPlot::EndPlot();
  1032. }
  1033. ImPlot::EndAlignedPlots();
  1034. }
  1035. }
  1036. //-----------------------------------------------------------------------------
  1037. void Demo_AxisConstraints() {
  1038. static float constraints[4] = {-10,10,1,20};
  1039. static ImPlotAxisFlags flags;
  1040. ImGui::DragFloat2("Limits Constraints", &constraints[0], 0.01f);
  1041. ImGui::DragFloat2("Zoom Constraints", &constraints[2], 0.01f);
  1042. CHECKBOX_FLAG(flags, ImPlotAxisFlags_PanStretch);
  1043. if (ImPlot::BeginPlot("##AxisConstraints",ImVec2(-1,0))) {
  1044. ImPlot::SetupAxes("X","Y",flags,flags);
  1045. ImPlot::SetupAxesLimits(-1,1,-1,1);
  1046. ImPlot::SetupAxisLimitsConstraints(ImAxis_X1,constraints[0], constraints[1]);
  1047. ImPlot::SetupAxisZoomConstraints(ImAxis_X1,constraints[2], constraints[3]);
  1048. ImPlot::SetupAxisLimitsConstraints(ImAxis_Y1,constraints[0], constraints[1]);
  1049. ImPlot::SetupAxisZoomConstraints(ImAxis_Y1,constraints[2], constraints[3]);
  1050. ImPlot::EndPlot();
  1051. }
  1052. }
  1053. //-----------------------------------------------------------------------------
  1054. void Demo_EqualAxes() {
  1055. ImGui::BulletText("Equal constraint applies to axis pairs (e.g ImAxis_X1/Y1, ImAxis_X2/Y2)");
  1056. static double xs1[360], ys1[360];
  1057. for (int i = 0; i < 360; ++i) {
  1058. double angle = i * 2 * PI / 359.0;
  1059. xs1[i] = cos(angle); ys1[i] = sin(angle);
  1060. }
  1061. float xs2[] = {-1,0,1,0,-1};
  1062. float ys2[] = {0,1,0,-1,0};
  1063. if (ImPlot::BeginPlot("##EqualAxes",ImVec2(-1,0),ImPlotFlags_Equal)) {
  1064. ImPlot::SetupAxis(ImAxis_X2, nullptr, ImPlotAxisFlags_AuxDefault);
  1065. ImPlot::SetupAxis(ImAxis_Y2, nullptr, ImPlotAxisFlags_AuxDefault);
  1066. ImPlot::PlotLine("Circle",xs1,ys1,360);
  1067. ImPlot::SetAxes(ImAxis_X2, ImAxis_Y2);
  1068. ImPlot::PlotLine("Diamond",xs2,ys2,5);
  1069. ImPlot::EndPlot();
  1070. }
  1071. }
  1072. //-----------------------------------------------------------------------------
  1073. void Demo_AutoFittingData() {
  1074. ImGui::BulletText("The Y-axis has been configured to auto-fit to only the data visible in X-axis range.");
  1075. ImGui::BulletText("Zoom and pan the X-axis. Disable Stems to see a difference in fit.");
  1076. ImGui::BulletText("If ImPlotAxisFlags_RangeFit is disabled, the axis will fit ALL data.");
  1077. static ImPlotAxisFlags xflags = ImPlotAxisFlags_None;
  1078. static ImPlotAxisFlags yflags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit;
  1079. ImGui::TextUnformatted("X: "); ImGui::SameLine();
  1080. ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine();
  1081. ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_RangeFit);
  1082. ImGui::TextUnformatted("Y: "); ImGui::SameLine();
  1083. ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine();
  1084. ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_RangeFit);
  1085. static double data[101];
  1086. srand(0);
  1087. for (int i = 0; i < 101; ++i)
  1088. data[i] = 1 + sin(i/10.0f);
  1089. if (ImPlot::BeginPlot("##DataFitting")) {
  1090. ImPlot::SetupAxes("X","Y",xflags,yflags);
  1091. ImPlot::PlotLine("Line",data,101);
  1092. ImPlot::PlotStems("Stems",data,101);
  1093. ImPlot::EndPlot();
  1094. };
  1095. }
  1096. //-----------------------------------------------------------------------------
  1097. ImPlotPoint SinewaveGetter(int i, void* data) {
  1098. float f = *(float*)data;
  1099. return ImPlotPoint(i,sinf(f*i));
  1100. }
  1101. void Demo_SubplotsSizing() {
  1102. static ImPlotSubplotFlags flags = ImPlotSubplotFlags_ShareItems|ImPlotSubplotFlags_NoLegend;
  1103. ImGui::CheckboxFlags("ImPlotSubplotFlags_NoResize", (unsigned int*)&flags, ImPlotSubplotFlags_NoResize);
  1104. ImGui::CheckboxFlags("ImPlotSubplotFlags_NoTitle", (unsigned int*)&flags, ImPlotSubplotFlags_NoTitle);
  1105. static int rows = 3;
  1106. static int cols = 3;
  1107. ImGui::SliderInt("Rows",&rows,1,5);
  1108. ImGui::SliderInt("Cols",&cols,1,5);
  1109. if (rows < 1 || cols < 1) {
  1110. ImGui::TextColored(ImVec4(1,0,0,1), "Nice try, but the number of rows and columns must be greater than 0!");
  1111. return;
  1112. }
  1113. static float rratios[] = {5,1,1,1,1,1};
  1114. static float cratios[] = {5,1,1,1,1,1};
  1115. ImGui::DragScalarN("Row Ratios",ImGuiDataType_Float,rratios,rows,0.01f,nullptr);
  1116. ImGui::DragScalarN("Col Ratios",ImGuiDataType_Float,cratios,cols,0.01f,nullptr);
  1117. if (ImPlot::BeginSubplots("My Subplots", rows, cols, ImVec2(-1,400), flags, rratios, cratios)) {
  1118. int id = 0;
  1119. for (int i = 0; i < rows*cols; ++i) {
  1120. if (ImPlot::BeginPlot("",ImVec2(),ImPlotFlags_NoLegend)) {
  1121. ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations);
  1122. float fi = 0.01f * (i+1);
  1123. if (rows*cols > 1) {
  1124. ImPlot::SetNextLineStyle(SampleColormap((float)i/(float)(rows*cols-1),ImPlotColormap_Jet));
  1125. }
  1126. char label[16];
  1127. snprintf(label, sizeof(label), "data%d", id++);
  1128. ImPlot::PlotLineG(label,SinewaveGetter,&fi,1000);
  1129. ImPlot::EndPlot();
  1130. }
  1131. }
  1132. ImPlot::EndSubplots();
  1133. }
  1134. }
  1135. //-----------------------------------------------------------------------------
  1136. void Demo_SubplotItemSharing() {
  1137. static ImPlotSubplotFlags flags = ImPlotSubplotFlags_ShareItems;
  1138. ImGui::CheckboxFlags("ImPlotSubplotFlags_ShareItems", (unsigned int*)&flags, ImPlotSubplotFlags_ShareItems);
  1139. ImGui::CheckboxFlags("ImPlotSubplotFlags_ColMajor", (unsigned int*)&flags, ImPlotSubplotFlags_ColMajor);
  1140. ImGui::BulletText("Drag and drop items from the legend onto plots (except for 'common')");
  1141. static int rows = 2;
  1142. static int cols = 3;
  1143. static int id[] = {0,1,2,3,4,5};
  1144. static int curj = -1;
  1145. if (ImPlot::BeginSubplots("##ItemSharing", rows, cols, ImVec2(-1,400), flags)) {
  1146. ImPlot::SetupLegend(ImPlotLocation_South, ImPlotLegendFlags_Sort|ImPlotLegendFlags_Horizontal);
  1147. for (int i = 0; i < rows*cols; ++i) {
  1148. if (ImPlot::BeginPlot("")) {
  1149. float fc = 0.01f;
  1150. ImPlot::PlotLineG("common",SinewaveGetter,&fc,1000);
  1151. for (int j = 0; j < 6; ++j) {
  1152. if (id[j] == i) {
  1153. char label[8];
  1154. float fj = 0.01f * (j+2);
  1155. snprintf(label, sizeof(label), "data%d", j);
  1156. ImPlot::PlotLineG(label,SinewaveGetter,&fj,1000);
  1157. if (ImPlot::BeginDragDropSourceItem(label)) {
  1158. curj = j;
  1159. ImGui::SetDragDropPayload("MY_DND",nullptr,0);
  1160. ImPlot::ItemIcon(GetLastItemColor()); ImGui::SameLine();
  1161. ImGui::TextUnformatted(label);
  1162. ImPlot::EndDragDropSource();
  1163. }
  1164. }
  1165. }
  1166. if (ImPlot::BeginDragDropTargetPlot()) {
  1167. if (ImGui::AcceptDragDropPayload("MY_DND"))
  1168. id[curj] = i;
  1169. ImPlot::EndDragDropTarget();
  1170. }
  1171. ImPlot::EndPlot();
  1172. }
  1173. }
  1174. ImPlot::EndSubplots();
  1175. }
  1176. }
  1177. //-----------------------------------------------------------------------------
  1178. void Demo_SubplotAxisLinking() {
  1179. static ImPlotSubplotFlags flags = ImPlotSubplotFlags_LinkRows | ImPlotSubplotFlags_LinkCols;
  1180. ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkRows", (unsigned int*)&flags, ImPlotSubplotFlags_LinkRows);
  1181. ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkCols", (unsigned int*)&flags, ImPlotSubplotFlags_LinkCols);
  1182. ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkAllX", (unsigned int*)&flags, ImPlotSubplotFlags_LinkAllX);
  1183. ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkAllY", (unsigned int*)&flags, ImPlotSubplotFlags_LinkAllY);
  1184. static int rows = 2;
  1185. static int cols = 2;
  1186. if (ImPlot::BeginSubplots("##AxisLinking", rows, cols, ImVec2(-1,400), flags)) {
  1187. for (int i = 0; i < rows*cols; ++i) {
  1188. if (ImPlot::BeginPlot("")) {
  1189. ImPlot::SetupAxesLimits(0,1000,-1,1);
  1190. float fc = 0.01f;
  1191. ImPlot::PlotLineG("common",SinewaveGetter,&fc,1000);
  1192. ImPlot::EndPlot();
  1193. }
  1194. }
  1195. ImPlot::EndSubplots();
  1196. }
  1197. }
  1198. //-----------------------------------------------------------------------------
  1199. void Demo_LegendOptions() {
  1200. static ImPlotLocation loc = ImPlotLocation_East;
  1201. ImGui::CheckboxFlags("North", (unsigned int*)&loc, ImPlotLocation_North); ImGui::SameLine();
  1202. ImGui::CheckboxFlags("South", (unsigned int*)&loc, ImPlotLocation_South); ImGui::SameLine();
  1203. ImGui::CheckboxFlags("West", (unsigned int*)&loc, ImPlotLocation_West); ImGui::SameLine();
  1204. ImGui::CheckboxFlags("East", (unsigned int*)&loc, ImPlotLocation_East);
  1205. static ImPlotLegendFlags flags = 0;
  1206. CHECKBOX_FLAG(flags, ImPlotLegendFlags_Horizontal);
  1207. CHECKBOX_FLAG(flags, ImPlotLegendFlags_Outside);
  1208. CHECKBOX_FLAG(flags, ImPlotLegendFlags_Sort);
  1209. ImGui::SliderFloat2("LegendPadding", (float*)&GetStyle().LegendPadding, 0.0f, 20.0f, "%.0f");
  1210. ImGui::SliderFloat2("LegendInnerPadding", (float*)&GetStyle().LegendInnerPadding, 0.0f, 10.0f, "%.0f");
  1211. ImGui::SliderFloat2("LegendSpacing", (float*)&GetStyle().LegendSpacing, 0.0f, 5.0f, "%.0f");
  1212. static int num_dummy_items = 25;
  1213. ImGui::SliderInt("Num Dummy Items (Demo Scrolling)", &num_dummy_items, 0, 100);
  1214. if (ImPlot::BeginPlot("##Legend",ImVec2(-1,0))) {
  1215. ImPlot::SetupLegend(loc, flags);
  1216. static MyImPlot::WaveData data1(0.001, 0.2, 4, 0.2);
  1217. static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.4);
  1218. static MyImPlot::WaveData data3(0.001, 0.2, 4, 0.6);
  1219. static MyImPlot::WaveData data4(0.001, 0.2, 4, 0.8);
  1220. static MyImPlot::WaveData data5(0.001, 0.2, 4, 1.0);
  1221. ImPlot::PlotLineG("Item 002", MyImPlot::SawWave, &data1, 1000); // "Item B" added to legend
  1222. ImPlot::PlotLineG("Item 001##IDText", MyImPlot::SawWave, &data2, 1000); // "Item A" added to legend, text after ## used for ID only
  1223. ImPlot::PlotLineG("##NotListed", MyImPlot::SawWave, &data3, 1000); // plotted, but not added to legend
  1224. ImPlot::PlotLineG("Item 003", MyImPlot::SawWave, &data4, 1000); // "Item C" added to legend
  1225. ImPlot::PlotLineG("Item 003", MyImPlot::SawWave, &data5, 1000); // combined with previous "Item C"
  1226. for (int i = 0; i < num_dummy_items; ++i) {
  1227. char label[16];
  1228. snprintf(label, sizeof(label), "Item %03d", i+4);
  1229. ImPlot::PlotDummy(label);
  1230. }
  1231. ImPlot::EndPlot();
  1232. }
  1233. }
  1234. //-----------------------------------------------------------------------------
  1235. void Demo_DragPoints() {
  1236. ImGui::BulletText("Click and drag each point.");
  1237. static ImPlotDragToolFlags flags = ImPlotDragToolFlags_None;
  1238. ImGui::CheckboxFlags("NoCursors", (unsigned int*)&flags, ImPlotDragToolFlags_NoCursors); ImGui::SameLine();
  1239. ImGui::CheckboxFlags("NoFit", (unsigned int*)&flags, ImPlotDragToolFlags_NoFit); ImGui::SameLine();
  1240. ImGui::CheckboxFlags("NoInput", (unsigned int*)&flags, ImPlotDragToolFlags_NoInputs);
  1241. ImPlotAxisFlags ax_flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoTickMarks;
  1242. bool clicked[4] = {false, false, false, false};
  1243. bool hovered[4] = {false, false, false, false};
  1244. bool held[4] = {false, false, false, false};
  1245. if (ImPlot::BeginPlot("##Bezier",ImVec2(-1,0),ImPlotFlags_CanvasOnly)) {
  1246. ImPlot::SetupAxes(nullptr,nullptr,ax_flags,ax_flags);
  1247. ImPlot::SetupAxesLimits(0,1,0,1);
  1248. static ImPlotPoint P[] = {ImPlotPoint(.05f,.05f), ImPlotPoint(0.2,0.4), ImPlotPoint(0.8,0.6), ImPlotPoint(.95f,.95f)};
  1249. ImPlot::DragPoint(0,&P[0].x,&P[0].y, ImVec4(0,0.9f,0,1),4,flags, &clicked[0], &hovered[0], &held[0]);
  1250. ImPlot::DragPoint(1,&P[1].x,&P[1].y, ImVec4(1,0.5f,1,1),4,flags, &clicked[1], &hovered[1], &held[1]);
  1251. ImPlot::DragPoint(2,&P[2].x,&P[2].y, ImVec4(0,0.5f,1,1),4,flags, &clicked[2], &hovered[2], &held[2]);
  1252. ImPlot::DragPoint(3,&P[3].x,&P[3].y, ImVec4(0,0.9f,0,1),4,flags, &clicked[3], &hovered[3], &held[3]);
  1253. static ImPlotPoint B[100];
  1254. for (int i = 0; i < 100; ++i) {
  1255. double t = i / 99.0;
  1256. double u = 1 - t;
  1257. double w1 = u*u*u;
  1258. double w2 = 3*u*u*t;
  1259. double w3 = 3*u*t*t;
  1260. double w4 = t*t*t;
  1261. 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);
  1262. }
  1263. ImPlot::SetNextLineStyle(ImVec4(1,0.5f,1,1),hovered[1]||held[1] ? 2.0f : 1.0f);
  1264. ImPlot::PlotLine("##h1",&P[0].x, &P[0].y, 2, 0, 0, sizeof(ImPlotPoint));
  1265. ImPlot::SetNextLineStyle(ImVec4(0,0.5f,1,1), hovered[2]||held[2] ? 2.0f : 1.0f);
  1266. ImPlot::PlotLine("##h2",&P[2].x, &P[2].y, 2, 0, 0, sizeof(ImPlotPoint));
  1267. ImPlot::SetNextLineStyle(ImVec4(0,0.9f,0,1), hovered[0]||held[0]||hovered[3]||held[3] ? 3.0f : 2.0f);
  1268. ImPlot::PlotLine("##bez",&B[0].x, &B[0].y, 100, 0, 0, sizeof(ImPlotPoint));
  1269. ImPlot::EndPlot();
  1270. }
  1271. }
  1272. //-----------------------------------------------------------------------------
  1273. void Demo_DragLines() {
  1274. ImGui::BulletText("Click and drag the horizontal and vertical lines.");
  1275. static double x1 = 0.2;
  1276. static double x2 = 0.8;
  1277. static double y1 = 0.25;
  1278. static double y2 = 0.75;
  1279. static double f = 0.1;
  1280. bool clicked = false;
  1281. bool hovered = false;
  1282. bool held = false;
  1283. static ImPlotDragToolFlags flags = ImPlotDragToolFlags_None;
  1284. ImGui::CheckboxFlags("NoCursors", (unsigned int*)&flags, ImPlotDragToolFlags_NoCursors); ImGui::SameLine();
  1285. ImGui::CheckboxFlags("NoFit", (unsigned int*)&flags, ImPlotDragToolFlags_NoFit); ImGui::SameLine();
  1286. ImGui::CheckboxFlags("NoInput", (unsigned int*)&flags, ImPlotDragToolFlags_NoInputs);
  1287. if (ImPlot::BeginPlot("##lines",ImVec2(-1,0))) {
  1288. ImPlot::SetupAxesLimits(0,1,0,1);
  1289. ImPlot::DragLineX(0,&x1,ImVec4(1,1,1,1),1,flags);
  1290. ImPlot::DragLineX(1,&x2,ImVec4(1,1,1,1),1,flags);
  1291. ImPlot::DragLineY(2,&y1,ImVec4(1,1,1,1),1,flags);
  1292. ImPlot::DragLineY(3,&y2,ImVec4(1,1,1,1),1,flags);
  1293. double xs[1000], ys[1000];
  1294. for (int i = 0; i < 1000; ++i) {
  1295. xs[i] = (x2+x1)/2+fabs(x2-x1)*(i/1000.0f - 0.5f);
  1296. ys[i] = (y1+y2)/2+fabs(y2-y1)/2*sin(f*i/10);
  1297. }
  1298. ImPlot::DragLineY(120482,&f,ImVec4(1,0.5f,1,1),1,flags, &clicked, &hovered, &held);
  1299. ImPlot::SetNextLineStyle(IMPLOT_AUTO_COL, hovered||held ? 2.0f : 1.0f);
  1300. ImPlot::PlotLine("Interactive Data", xs, ys, 1000);
  1301. ImPlot::EndPlot();
  1302. }
  1303. }
  1304. //-----------------------------------------------------------------------------
  1305. void Demo_DragRects() {
  1306. static float x_data[512];
  1307. static float y_data1[512];
  1308. static float y_data2[512];
  1309. static float y_data3[512];
  1310. static float sampling_freq = 44100;
  1311. static float freq = 500;
  1312. bool clicked = false;
  1313. bool hovered = false;
  1314. bool held = false;
  1315. for (size_t i = 0; i < 512; ++i) {
  1316. const float t = i / sampling_freq;
  1317. x_data[i] = t;
  1318. const float arg = 2 * 3.14f * freq * t;
  1319. y_data1[i] = sinf(arg);
  1320. y_data2[i] = y_data1[i] * -0.6f + sinf(2 * arg) * 0.4f;
  1321. y_data3[i] = y_data2[i] * -0.6f + sinf(3 * arg) * 0.4f;
  1322. }
  1323. ImGui::BulletText("Click and drag the edges, corners, and center of the rect.");
  1324. ImGui::BulletText("Double click edges to expand rect to plot extents.");
  1325. static ImPlotRect rect(0.0025,0.0045,0,0.5);
  1326. static ImPlotDragToolFlags flags = ImPlotDragToolFlags_None;
  1327. ImGui::CheckboxFlags("NoCursors", (unsigned int*)&flags, ImPlotDragToolFlags_NoCursors); ImGui::SameLine();
  1328. ImGui::CheckboxFlags("NoFit", (unsigned int*)&flags, ImPlotDragToolFlags_NoFit); ImGui::SameLine();
  1329. ImGui::CheckboxFlags("NoInput", (unsigned int*)&flags, ImPlotDragToolFlags_NoInputs);
  1330. if (ImPlot::BeginPlot("##Main",ImVec2(-1,150))) {
  1331. ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoTickLabels,ImPlotAxisFlags_NoTickLabels);
  1332. ImPlot::SetupAxesLimits(0,0.01,-1,1);
  1333. ImPlot::PlotLine("Signal 1", x_data, y_data1, 512);
  1334. ImPlot::PlotLine("Signal 2", x_data, y_data2, 512);
  1335. ImPlot::PlotLine("Signal 3", x_data, y_data3, 512);
  1336. ImPlot::DragRect(0,&rect.X.Min,&rect.Y.Min,&rect.X.Max,&rect.Y.Max,ImVec4(1,0,1,1),flags, &clicked, &hovered, &held);
  1337. ImPlot::EndPlot();
  1338. }
  1339. ImVec4 bg_col = held ? ImVec4(0.5f,0,0.5f,1) : (hovered ? ImVec4(0.25f,0,0.25f,1) : ImPlot::GetStyle().Colors[ImPlotCol_PlotBg]);
  1340. ImPlot::PushStyleColor(ImPlotCol_PlotBg, bg_col);
  1341. if (ImPlot::BeginPlot("##rect",ImVec2(-1,150), ImPlotFlags_CanvasOnly)) {
  1342. ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations);
  1343. ImPlot::SetupAxesLimits(rect.X.Min, rect.X.Max, rect.Y.Min, rect.Y.Max, ImGuiCond_Always);
  1344. ImPlot::PlotLine("Signal 1", x_data, y_data1, 512);
  1345. ImPlot::PlotLine("Signal 2", x_data, y_data2, 512);
  1346. ImPlot::PlotLine("Signal 3", x_data, y_data3, 512);
  1347. ImPlot::EndPlot();
  1348. }
  1349. ImPlot::PopStyleColor();
  1350. ImGui::Text("Rect is %sclicked, %shovered, %sheld", clicked ? "" : "not ", hovered ? "" : "not ", held ? "" : "not ");
  1351. }
  1352. //-----------------------------------------------------------------------------
  1353. ImPlotPoint FindCentroid(const ImVector<ImPlotPoint>& data, const ImPlotRect& bounds, int& cnt) {
  1354. cnt = 0;
  1355. ImPlotPoint avg;
  1356. ImPlotRect bounds_fixed;
  1357. bounds_fixed.X.Min = bounds.X.Min < bounds.X.Max ? bounds.X.Min : bounds.X.Max;
  1358. bounds_fixed.X.Max = bounds.X.Min < bounds.X.Max ? bounds.X.Max : bounds.X.Min;
  1359. bounds_fixed.Y.Min = bounds.Y.Min < bounds.Y.Max ? bounds.Y.Min : bounds.Y.Max;
  1360. bounds_fixed.Y.Max = bounds.Y.Min < bounds.Y.Max ? bounds.Y.Max : bounds.Y.Min;
  1361. for (int i = 0; i < data.size(); ++i) {
  1362. if (bounds_fixed.Contains(data[i].x, data[i].y)) {
  1363. avg.x += data[i].x;
  1364. avg.y += data[i].y;
  1365. cnt++;
  1366. }
  1367. }
  1368. if (cnt > 0) {
  1369. avg.x = avg.x / cnt;
  1370. avg.y = avg.y / cnt;
  1371. }
  1372. return avg;
  1373. }
  1374. //-----------------------------------------------------------------------------
  1375. void Demo_Querying() {
  1376. static ImVector<ImPlotPoint> data;
  1377. static ImVector<ImPlotRect> rects;
  1378. static ImPlotRect limits, select;
  1379. static bool init = true;
  1380. if (init) {
  1381. for (int i = 0; i < 50; ++i)
  1382. {
  1383. double x = RandomRange(0.1, 0.9);
  1384. double y = RandomRange(0.1, 0.9);
  1385. data.push_back(ImPlotPoint(x,y));
  1386. }
  1387. init = false;
  1388. }
  1389. ImGui::BulletText("Box select and left click mouse to create a new query rect.");
  1390. ImGui::BulletText("Ctrl + click in the plot area to draw points.");
  1391. if (ImGui::Button("Clear Queries"))
  1392. rects.shrink(0);
  1393. if (ImPlot::BeginPlot("##Centroid")) {
  1394. ImPlot::SetupAxesLimits(0,1,0,1);
  1395. if (ImPlot::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl) {
  1396. ImPlotPoint pt = ImPlot::GetPlotMousePos();
  1397. data.push_back(pt);
  1398. }
  1399. ImPlot::PlotScatter("Points", &data[0].x, &data[0].y, data.size(), 0, 0, 2 * sizeof(double));
  1400. if (ImPlot::IsPlotSelected()) {
  1401. select = ImPlot::GetPlotSelection();
  1402. int cnt;
  1403. ImPlotPoint centroid = FindCentroid(data,select,cnt);
  1404. if (cnt > 0) {
  1405. ImPlot::SetNextMarkerStyle(ImPlotMarker_Square,6);
  1406. ImPlot::PlotScatter("Centroid", &centroid.x, &centroid.y, 1);
  1407. }
  1408. if (ImGui::IsMouseClicked(ImPlot::GetInputMap().SelectCancel)) {
  1409. CancelPlotSelection();
  1410. rects.push_back(select);
  1411. }
  1412. }
  1413. for (int i = 0; i < rects.size(); ++i) {
  1414. int cnt;
  1415. ImPlotPoint centroid = FindCentroid(data,rects[i],cnt);
  1416. if (cnt > 0) {
  1417. ImPlot::SetNextMarkerStyle(ImPlotMarker_Square,6);
  1418. ImPlot::PlotScatter("Centroid", &centroid.x, &centroid.y, 1);
  1419. }
  1420. ImPlot::DragRect(i,&rects[i].X.Min,&rects[i].Y.Min,&rects[i].X.Max,&rects[i].Y.Max,ImVec4(1,0,1,1));
  1421. }
  1422. limits = ImPlot::GetPlotLimits();
  1423. ImPlot::EndPlot();
  1424. }
  1425. }
  1426. //-----------------------------------------------------------------------------
  1427. void Demo_Annotations() {
  1428. static bool clamp = false;
  1429. ImGui::Checkbox("Clamp",&clamp);
  1430. if (ImPlot::BeginPlot("##Annotations")) {
  1431. ImPlot::SetupAxesLimits(0,2,0,1);
  1432. static float p[] = {0.25f, 0.25f, 0.75f, 0.75f, 0.25f};
  1433. ImPlot::PlotScatter("##Points",&p[0],&p[1],4);
  1434. ImVec4 col = GetLastItemColor();
  1435. ImPlot::Annotation(0.25,0.25,col,ImVec2(-15,15),clamp,"BL");
  1436. ImPlot::Annotation(0.75,0.25,col,ImVec2(15,15),clamp,"BR");
  1437. ImPlot::Annotation(0.75,0.75,col,ImVec2(15,-15),clamp,"TR");
  1438. ImPlot::Annotation(0.25,0.75,col,ImVec2(-15,-15),clamp,"TL");
  1439. ImPlot::Annotation(0.5,0.5,col,ImVec2(0,0),clamp,"Center");
  1440. ImPlot::Annotation(1.25,0.75,ImVec4(0,1,0,1),ImVec2(0,0),clamp);
  1441. float bx[] = {1.2f,1.5f,1.8f};
  1442. float by[] = {0.25f, 0.5f, 0.75f};
  1443. ImPlot::PlotBars("##Bars",bx,by,3,0.2);
  1444. for (int i = 0; i < 3; ++i)
  1445. ImPlot::Annotation(bx[i],by[i],ImVec4(0,0,0,0),ImVec2(0,-5),clamp,"B[%d]=%.2f",i,by[i]);
  1446. ImPlot::EndPlot();
  1447. }
  1448. }
  1449. //-----------------------------------------------------------------------------
  1450. void Demo_Tags() {
  1451. static bool show = true;
  1452. ImGui::Checkbox("Show Tags",&show);
  1453. if (ImPlot::BeginPlot("##Tags")) {
  1454. ImPlot::SetupAxis(ImAxis_X2);
  1455. ImPlot::SetupAxis(ImAxis_Y2);
  1456. if (show) {
  1457. ImPlot::TagX(0.25, ImVec4(1,1,0,1));
  1458. ImPlot::TagY(0.75, ImVec4(1,1,0,1));
  1459. static double drag_tag = 0.25;
  1460. ImPlot::DragLineY(0,&drag_tag,ImVec4(1,0,0,1),1,ImPlotDragToolFlags_NoFit);
  1461. ImPlot::TagY(drag_tag, ImVec4(1,0,0,1), "Drag");
  1462. SetAxes(ImAxis_X2, ImAxis_Y2);
  1463. ImPlot::TagX(0.5, ImVec4(0,1,1,1), "%s", "MyTag");
  1464. ImPlot::TagY(0.5, ImVec4(0,1,1,1), "Tag: %d", 42);
  1465. }
  1466. ImPlot::EndPlot();
  1467. }
  1468. }
  1469. //-----------------------------------------------------------------------------
  1470. void Demo_DragAndDrop() {
  1471. ImGui::BulletText("Drag/drop items from the left column.");
  1472. ImGui::BulletText("Drag/drop items between plots.");
  1473. ImGui::Indent();
  1474. ImGui::BulletText("Plot 1 Targets: Plot, Y-Axes, Legend");
  1475. ImGui::BulletText("Plot 1 Sources: Legend Item Labels");
  1476. ImGui::BulletText("Plot 2 Targets: Plot, X-Axis, Y-Axis");
  1477. ImGui::BulletText("Plot 2 Sources: Plot, X-Axis, Y-Axis (hold Ctrl)");
  1478. ImGui::Unindent();
  1479. // convenience struct to manage DND items; do this however you like
  1480. struct MyDndItem {
  1481. int Idx;
  1482. int Plt;
  1483. ImAxis Yax;
  1484. char Label[16];
  1485. ImVector<ImVec2> Data;
  1486. ImVec4 Color;
  1487. MyDndItem() {
  1488. static int i = 0;
  1489. Idx = i++;
  1490. Plt = 0;
  1491. Yax = ImAxis_Y1;
  1492. snprintf(Label, sizeof(Label), "%02d Hz", Idx+1);
  1493. Color = RandomColor();
  1494. Data.reserve(1001);
  1495. for (int k = 0; k < 1001; ++k) {
  1496. float t = k * 1.0f / 999;
  1497. Data.push_back(ImVec2(t, 0.5f + 0.5f * sinf(2*3.14f*t*(Idx+1))));
  1498. }
  1499. }
  1500. void Reset() { Plt = 0; Yax = ImAxis_Y1; }
  1501. };
  1502. const int k_dnd = 20;
  1503. static MyDndItem dnd[k_dnd];
  1504. static MyDndItem* dndx = nullptr; // for plot 2
  1505. static MyDndItem* dndy = nullptr; // for plot 2
  1506. // child window to serve as initial source for our DND items
  1507. ImGui::BeginChild("DND_LEFT",ImVec2(100,400));
  1508. if (ImGui::Button("Reset Data")) {
  1509. for (int k = 0; k < k_dnd; ++k)
  1510. dnd[k].Reset();
  1511. dndx = dndy = nullptr;
  1512. }
  1513. for (int k = 0; k < k_dnd; ++k) {
  1514. if (dnd[k].Plt > 0)
  1515. continue;
  1516. ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine();
  1517. ImGui::Selectable(dnd[k].Label, false, 0, ImVec2(100, 0));
  1518. if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
  1519. ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int));
  1520. ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine();
  1521. ImGui::TextUnformatted(dnd[k].Label);
  1522. ImGui::EndDragDropSource();
  1523. }
  1524. }
  1525. ImGui::EndChild();
  1526. if (ImGui::BeginDragDropTarget()) {
  1527. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1528. int i = *(int*)payload->Data; dnd[i].Reset();
  1529. }
  1530. ImGui::EndDragDropTarget();
  1531. }
  1532. ImGui::SameLine();
  1533. ImGui::BeginChild("DND_RIGHT",ImVec2(-1,400));
  1534. // plot 1 (time series)
  1535. ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoHighlight;
  1536. if (ImPlot::BeginPlot("##DND1", ImVec2(-1,195))) {
  1537. ImPlot::SetupAxis(ImAxis_X1, nullptr, flags|ImPlotAxisFlags_Lock);
  1538. ImPlot::SetupAxis(ImAxis_Y1, "[drop here]", flags);
  1539. ImPlot::SetupAxis(ImAxis_Y2, "[drop here]", flags|ImPlotAxisFlags_Opposite);
  1540. ImPlot::SetupAxis(ImAxis_Y3, "[drop here]", flags|ImPlotAxisFlags_Opposite);
  1541. for (int k = 0; k < k_dnd; ++k) {
  1542. if (dnd[k].Plt == 1 && dnd[k].Data.size() > 0) {
  1543. ImPlot::SetAxis(dnd[k].Yax);
  1544. ImPlot::SetNextLineStyle(dnd[k].Color);
  1545. ImPlot::PlotLine(dnd[k].Label, &dnd[k].Data[0].x, &dnd[k].Data[0].y, dnd[k].Data.size(), 0, 0, 2 * sizeof(float));
  1546. // allow legend item labels to be DND sources
  1547. if (ImPlot::BeginDragDropSourceItem(dnd[k].Label)) {
  1548. ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int));
  1549. ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine();
  1550. ImGui::TextUnformatted(dnd[k].Label);
  1551. ImPlot::EndDragDropSource();
  1552. }
  1553. }
  1554. }
  1555. // allow the main plot area to be a DND target
  1556. if (ImPlot::BeginDragDropTargetPlot()) {
  1557. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1558. int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = ImAxis_Y1;
  1559. }
  1560. ImPlot::EndDragDropTarget();
  1561. }
  1562. // allow each y-axis to be a DND target
  1563. for (int y = ImAxis_Y1; y <= ImAxis_Y3; ++y) {
  1564. if (ImPlot::BeginDragDropTargetAxis(y)) {
  1565. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1566. int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = y;
  1567. }
  1568. ImPlot::EndDragDropTarget();
  1569. }
  1570. }
  1571. // allow the legend to be a DND target
  1572. if (ImPlot::BeginDragDropTargetLegend()) {
  1573. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1574. int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = ImAxis_Y1;
  1575. }
  1576. ImPlot::EndDragDropTarget();
  1577. }
  1578. ImPlot::EndPlot();
  1579. }
  1580. // plot 2 (Lissajous)
  1581. if (ImPlot::BeginPlot("##DND2", ImVec2(-1,195))) {
  1582. ImPlot::PushStyleColor(ImPlotCol_AxisBg, dndx != nullptr ? dndx->Color : ImPlot::GetStyle().Colors[ImPlotCol_AxisBg]);
  1583. ImPlot::SetupAxis(ImAxis_X1, dndx == nullptr ? "[drop here]" : dndx->Label, flags);
  1584. ImPlot::PushStyleColor(ImPlotCol_AxisBg, dndy != nullptr ? dndy->Color : ImPlot::GetStyle().Colors[ImPlotCol_AxisBg]);
  1585. ImPlot::SetupAxis(ImAxis_Y1, dndy == nullptr ? "[drop here]" : dndy->Label, flags);
  1586. ImPlot::PopStyleColor(2);
  1587. if (dndx != nullptr && dndy != nullptr) {
  1588. 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);
  1589. ImPlot::SetNextLineStyle(mixed);
  1590. ImPlot::PlotLine("##dndxy", &dndx->Data[0].y, &dndy->Data[0].y, dndx->Data.size(), 0, 0, 2 * sizeof(float));
  1591. }
  1592. // allow the x-axis to be a DND target
  1593. if (ImPlot::BeginDragDropTargetAxis(ImAxis_X1)) {
  1594. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1595. int i = *(int*)payload->Data; dndx = &dnd[i];
  1596. }
  1597. ImPlot::EndDragDropTarget();
  1598. }
  1599. // allow the x-axis to be a DND source
  1600. if (dndx != nullptr && ImPlot::BeginDragDropSourceAxis(ImAxis_X1)) {
  1601. ImGui::SetDragDropPayload("MY_DND", &dndx->Idx, sizeof(int));
  1602. ImPlot::ItemIcon(dndx->Color); ImGui::SameLine();
  1603. ImGui::TextUnformatted(dndx->Label);
  1604. ImPlot::EndDragDropSource();
  1605. }
  1606. // allow the y-axis to be a DND target
  1607. if (ImPlot::BeginDragDropTargetAxis(ImAxis_Y1)) {
  1608. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1609. int i = *(int*)payload->Data; dndy = &dnd[i];
  1610. }
  1611. ImPlot::EndDragDropTarget();
  1612. }
  1613. // allow the y-axis to be a DND source
  1614. if (dndy != nullptr && ImPlot::BeginDragDropSourceAxis(ImAxis_Y1)) {
  1615. ImGui::SetDragDropPayload("MY_DND", &dndy->Idx, sizeof(int));
  1616. ImPlot::ItemIcon(dndy->Color); ImGui::SameLine();
  1617. ImGui::TextUnformatted(dndy->Label);
  1618. ImPlot::EndDragDropSource();
  1619. }
  1620. // allow the plot area to be a DND target
  1621. if (ImPlot::BeginDragDropTargetPlot()) {
  1622. if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) {
  1623. int i = *(int*)payload->Data; dndx = dndy = &dnd[i];
  1624. }
  1625. }
  1626. // allow the plot area to be a DND source
  1627. if (ImPlot::BeginDragDropSourcePlot()) {
  1628. ImGui::TextUnformatted("Yes, you can\ndrag this!");
  1629. ImPlot::EndDragDropSource();
  1630. }
  1631. ImPlot::EndPlot();
  1632. }
  1633. ImGui::EndChild();
  1634. }
  1635. //-----------------------------------------------------------------------------
  1636. void Demo_Tables() {
  1637. #ifdef IMGUI_HAS_TABLE
  1638. static ImGuiTableFlags flags = ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV |
  1639. ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable;
  1640. static bool anim = true;
  1641. static int offset = 0;
  1642. ImGui::BulletText("Plots can be used inside of ImGui tables as another means of creating subplots.");
  1643. ImGui::Checkbox("Animate",&anim);
  1644. if (anim)
  1645. offset = (offset + 1) % 100;
  1646. if (ImGui::BeginTable("##table", 3, flags, ImVec2(-1,0))) {
  1647. ImGui::TableSetupColumn("Electrode", ImGuiTableColumnFlags_WidthFixed, 75.0f);
  1648. ImGui::TableSetupColumn("Voltage", ImGuiTableColumnFlags_WidthFixed, 75.0f);
  1649. ImGui::TableSetupColumn("EMG Signal");
  1650. ImGui::TableHeadersRow();
  1651. ImPlot::PushColormap(ImPlotColormap_Cool);
  1652. for (int row = 0; row < 10; row++) {
  1653. ImGui::TableNextRow();
  1654. static float data[100];
  1655. srand(row);
  1656. for (int i = 0; i < 100; ++i)
  1657. data[i] = RandomRange(0.0f,10.0f);
  1658. ImGui::TableSetColumnIndex(0);
  1659. ImGui::Text("EMG %d", row);
  1660. ImGui::TableSetColumnIndex(1);
  1661. ImGui::Text("%.3f V", data[offset]);
  1662. ImGui::TableSetColumnIndex(2);
  1663. ImGui::PushID(row);
  1664. MyImPlot::Sparkline("##spark",data,100,0,11.0f,offset,ImPlot::GetColormapColor(row),ImVec2(-1, 35));
  1665. ImGui::PopID();
  1666. }
  1667. ImPlot::PopColormap();
  1668. ImGui::EndTable();
  1669. }
  1670. #else
  1671. ImGui::BulletText("You need to merge the ImGui 'tables' branch for this section.");
  1672. #endif
  1673. }
  1674. //-----------------------------------------------------------------------------
  1675. void Demo_OffsetAndStride() {
  1676. static const int k_circles = 11;
  1677. static const int k_points_per = 50;
  1678. static const int k_size = 2 * k_points_per * k_circles;
  1679. static double interleaved_data[k_size];
  1680. for (int p = 0; p < k_points_per; ++p) {
  1681. for (int c = 0; c < k_circles; ++c) {
  1682. double r = (double)c / (k_circles - 1) * 0.2 + 0.2;
  1683. interleaved_data[p*2*k_circles + 2*c + 0] = 0.5 + r * cos((double)p/k_points_per * 6.28);
  1684. interleaved_data[p*2*k_circles + 2*c + 1] = 0.5 + r * sin((double)p/k_points_per * 6.28);
  1685. }
  1686. }
  1687. static int offset = 0;
  1688. ImGui::BulletText("Offsetting is useful for realtime plots (see above) and circular buffers.");
  1689. ImGui::BulletText("Striding is useful for interleaved data (e.g. audio) or plotting structs.");
  1690. ImGui::BulletText("Here, all circle data is stored in a single interleaved buffer:");
  1691. ImGui::BulletText("[c0.x0 c0.y0 ... cn.x0 cn.y0 c0.x1 c0.y1 ... cn.x1 cn.y1 ... cn.xm cn.ym]");
  1692. ImGui::BulletText("The offset value indicates which circle point index is considered the first.");
  1693. ImGui::BulletText("Offsets can be negative and/or larger than the actual data count.");
  1694. ImGui::SliderInt("Offset", &offset, -2*k_points_per, 2*k_points_per);
  1695. if (ImPlot::BeginPlot("##strideoffset",ImVec2(-1,0),ImPlotFlags_Equal)) {
  1696. ImPlot::PushColormap(ImPlotColormap_Jet);
  1697. char buff[32];
  1698. for (int c = 0; c < k_circles; ++c) {
  1699. snprintf(buff, sizeof(buff), "Circle %d", c);
  1700. ImPlot::PlotLine(buff, &interleaved_data[c*2 + 0], &interleaved_data[c*2 + 1], k_points_per, 0, offset, 2*k_circles*sizeof(double));
  1701. }
  1702. ImPlot::EndPlot();
  1703. ImPlot::PopColormap();
  1704. }
  1705. // offset++; uncomment for animation!
  1706. }
  1707. //-----------------------------------------------------------------------------
  1708. void Demo_CustomDataAndGetters() {
  1709. ImGui::BulletText("You can plot custom structs using the stride feature.");
  1710. ImGui::BulletText("Most plotters can also be passed a function pointer for getting data.");
  1711. ImGui::Indent();
  1712. ImGui::BulletText("You can optionally pass user data to be given to your getter function.");
  1713. ImGui::BulletText("C++ lambdas can be passed as function pointers as well!");
  1714. ImGui::Unindent();
  1715. MyImPlot::Vector2f vec2_data[2] = { MyImPlot::Vector2f(0,0), MyImPlot::Vector2f(1,1) };
  1716. if (ImPlot::BeginPlot("##Custom Data")) {
  1717. // custom structs using stride example:
  1718. ImPlot::PlotLine("Vector2f", &vec2_data[0].x, &vec2_data[0].y, 2, 0, 0, sizeof(MyImPlot::Vector2f) /* or sizeof(float) * 2 */);
  1719. // custom getter example 1:
  1720. ImPlot::PlotLineG("Spiral", MyImPlot::Spiral, nullptr, 1000);
  1721. // custom getter example 2:
  1722. static MyImPlot::WaveData data1(0.001, 0.2, 2, 0.75);
  1723. static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.25);
  1724. ImPlot::PlotLineG("Waves", MyImPlot::SineWave, &data1, 1000);
  1725. ImPlot::PlotLineG("Waves", MyImPlot::SawWave, &data2, 1000);
  1726. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
  1727. ImPlot::PlotShadedG("Waves", MyImPlot::SineWave, &data1, MyImPlot::SawWave, &data2, 1000);
  1728. ImPlot::PopStyleVar();
  1729. // you can also pass C++ lambdas:
  1730. // auto lamda = [](void* data, int idx) { ... return ImPlotPoint(x,y); };
  1731. // ImPlot::PlotLine("My Lambda", lambda, data, 1000);
  1732. ImPlot::EndPlot();
  1733. }
  1734. }
  1735. //-----------------------------------------------------------------------------
  1736. int MetricFormatter(double value, char* buff, int size, void* data) {
  1737. const char* unit = (const char*)data;
  1738. static double v[] = {1000000000,1000000,1000,1,0.001,0.000001,0.000000001};
  1739. static const char* p[] = {"G","M","k","","m","u","n"};
  1740. if (value == 0) {
  1741. return snprintf(buff,size,"0 %s", unit);
  1742. }
  1743. for (int i = 0; i < 7; ++i) {
  1744. if (fabs(value) >= v[i]) {
  1745. return snprintf(buff,size,"%g %s%s",value/v[i],p[i],unit);
  1746. }
  1747. }
  1748. return snprintf(buff,size,"%g %s%s",value/v[6],p[6],unit);
  1749. }
  1750. void Demo_TickLabels() {
  1751. static bool custom_fmt = true;
  1752. static bool custom_ticks = false;
  1753. static bool custom_labels = true;
  1754. ImGui::Checkbox("Show Custom Format", &custom_fmt);
  1755. ImGui::SameLine();
  1756. ImGui::Checkbox("Show Custom Ticks", &custom_ticks);
  1757. if (custom_ticks) {
  1758. ImGui::SameLine();
  1759. ImGui::Checkbox("Show Custom Labels", &custom_labels);
  1760. }
  1761. const double pi = 3.14;
  1762. const char* pi_str[] = {"PI"};
  1763. static double yticks[] = {100,300,700,900};
  1764. static const char* ylabels[] = {"One","Three","Seven","Nine"};
  1765. static double yticks_aux[] = {0.2,0.4,0.6};
  1766. static const char* ylabels_aux[] = {"A","B","C","D","E","F"};
  1767. if (ImPlot::BeginPlot("##Ticks")) {
  1768. ImPlot::SetupAxesLimits(2.5,5,0,1000);
  1769. ImPlot::SetupAxis(ImAxis_Y2, nullptr, ImPlotAxisFlags_AuxDefault);
  1770. ImPlot::SetupAxis(ImAxis_Y3, nullptr, ImPlotAxisFlags_AuxDefault);
  1771. if (custom_fmt) {
  1772. ImPlot::SetupAxisFormat(ImAxis_X1, "%g ms");
  1773. ImPlot::SetupAxisFormat(ImAxis_Y1, MetricFormatter, (void*)"Hz");
  1774. ImPlot::SetupAxisFormat(ImAxis_Y2, "%g dB");
  1775. ImPlot::SetupAxisFormat(ImAxis_Y3, MetricFormatter, (void*)"m");
  1776. }
  1777. if (custom_ticks) {
  1778. ImPlot::SetupAxisTicks(ImAxis_X1, &pi,1,custom_labels ? pi_str : nullptr, true);
  1779. ImPlot::SetupAxisTicks(ImAxis_Y1, yticks, 4, custom_labels ? ylabels : nullptr, false);
  1780. ImPlot::SetupAxisTicks(ImAxis_Y2, yticks_aux, 3, custom_labels ? ylabels_aux : nullptr, false);
  1781. ImPlot::SetupAxisTicks(ImAxis_Y3, 0, 1, 6, custom_labels ? ylabels_aux : nullptr, false);
  1782. }
  1783. ImPlot::EndPlot();
  1784. }
  1785. }
  1786. //-----------------------------------------------------------------------------
  1787. void Demo_CustomStyles() {
  1788. ImPlot::PushColormap(ImPlotColormap_Deep);
  1789. // normally you wouldn't change the entire style each frame
  1790. ImPlotStyle backup = ImPlot::GetStyle();
  1791. MyImPlot::StyleSeaborn();
  1792. if (ImPlot::BeginPlot("seaborn style")) {
  1793. ImPlot::SetupAxes( "x-axis", "y-axis");
  1794. ImPlot::SetupAxesLimits(-0.5f, 9.5f, 0, 10);
  1795. unsigned int lin[10] = {8,8,9,7,8,8,8,9,7,8};
  1796. unsigned int bar[10] = {1,2,5,3,4,1,2,5,3,4};
  1797. unsigned int dot[10] = {7,6,6,7,8,5,6,5,8,7};
  1798. ImPlot::PlotBars("Bars", bar, 10, 0.5f);
  1799. ImPlot::PlotLine("Line", lin, 10);
  1800. ImPlot::NextColormapColor(); // skip green
  1801. ImPlot::PlotScatter("Scatter", dot, 10);
  1802. ImPlot::EndPlot();
  1803. }
  1804. ImPlot::GetStyle() = backup;
  1805. ImPlot::PopColormap();
  1806. }
  1807. //-----------------------------------------------------------------------------
  1808. void Demo_CustomRendering() {
  1809. if (ImPlot::BeginPlot("##CustomRend")) {
  1810. ImVec2 cntr = ImPlot::PlotToPixels(ImPlotPoint(0.5f, 0.5f));
  1811. ImVec2 rmin = ImPlot::PlotToPixels(ImPlotPoint(0.25f, 0.75f));
  1812. ImVec2 rmax = ImPlot::PlotToPixels(ImPlotPoint(0.75f, 0.25f));
  1813. ImPlot::PushPlotClipRect();
  1814. ImPlot::GetPlotDrawList()->AddCircleFilled(cntr,20,IM_COL32(255,255,0,255),20);
  1815. ImPlot::GetPlotDrawList()->AddRect(rmin, rmax, IM_COL32(128,0,255,255));
  1816. ImPlot::PopPlotClipRect();
  1817. ImPlot::EndPlot();
  1818. }
  1819. }
  1820. //-----------------------------------------------------------------------------
  1821. void Demo_LegendPopups() {
  1822. ImGui::BulletText("You can implement legend context menus to inject per-item controls and widgets.");
  1823. ImGui::BulletText("Right click the legend label/icon to edit custom item attributes.");
  1824. static float frequency = 0.1f;
  1825. static float amplitude = 0.5f;
  1826. static ImVec4 color = ImVec4(1,1,0,1);
  1827. static float alpha = 1.0f;
  1828. static bool line = false;
  1829. static float thickness = 1;
  1830. static bool markers = false;
  1831. static bool shaded = false;
  1832. static float vals[101];
  1833. for (int i = 0; i < 101; ++i)
  1834. vals[i] = amplitude * sinf(frequency * i);
  1835. if (ImPlot::BeginPlot("Right Click the Legend")) {
  1836. ImPlot::SetupAxesLimits(0,100,-1,1);
  1837. // rendering logic
  1838. ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha);
  1839. if (!line) {
  1840. ImPlot::SetNextFillStyle(color);
  1841. ImPlot::PlotBars("Right Click Me", vals, 101);
  1842. }
  1843. else {
  1844. if (markers) ImPlot::SetNextMarkerStyle(ImPlotMarker_Square);
  1845. ImPlot::SetNextLineStyle(color, thickness);
  1846. ImPlot::PlotLine("Right Click Me", vals, 101);
  1847. if (shaded) ImPlot::PlotShaded("Right Click Me",vals,101);
  1848. }
  1849. ImPlot::PopStyleVar();
  1850. // custom legend context menu
  1851. if (ImPlot::BeginLegendPopup("Right Click Me")) {
  1852. ImGui::SliderFloat("Frequency",&frequency,0,1,"%0.2f");
  1853. ImGui::SliderFloat("Amplitude",&amplitude,0,1,"%0.2f");
  1854. ImGui::Separator();
  1855. ImGui::ColorEdit3("Color",&color.x);
  1856. ImGui::SliderFloat("Transparency",&alpha,0,1,"%.2f");
  1857. ImGui::Checkbox("Line Plot", &line);
  1858. if (line) {
  1859. ImGui::SliderFloat("Thickness", &thickness, 0, 5);
  1860. ImGui::Checkbox("Markers", &markers);
  1861. ImGui::Checkbox("Shaded",&shaded);
  1862. }
  1863. ImPlot::EndLegendPopup();
  1864. }
  1865. ImPlot::EndPlot();
  1866. }
  1867. }
  1868. //-----------------------------------------------------------------------------
  1869. void Demo_ColormapWidgets() {
  1870. static int cmap = ImPlotColormap_Viridis;
  1871. if (ImPlot::ColormapButton("Button",ImVec2(0,0),cmap)) {
  1872. cmap = (cmap + 1) % ImPlot::GetColormapCount();
  1873. }
  1874. static float t = 0.5f;
  1875. static ImVec4 col;
  1876. ImGui::ColorButton("##Display",col,ImGuiColorEditFlags_NoInputs);
  1877. ImGui::SameLine();
  1878. ImPlot::ColormapSlider("Slider", &t, &col, "%.3f", cmap);
  1879. ImPlot::ColormapIcon(cmap); ImGui::SameLine(); ImGui::Text("Icon");
  1880. static ImPlotColormapScaleFlags flags = 0;
  1881. static float scale[2] = {0, 100};
  1882. ImPlot::ColormapScale("Scale",scale[0],scale[1],ImVec2(0,0),"%g dB",flags,cmap);
  1883. ImGui::InputFloat2("Scale",scale);
  1884. CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_NoLabel);
  1885. CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Opposite);
  1886. CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Invert);
  1887. }
  1888. //-----------------------------------------------------------------------------
  1889. void Demo_CustomPlottersAndTooltips() {
  1890. ImGui::BulletText("You can create custom plotters or extend ImPlot using implot_internal.h.");
  1891. 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};
  1892. 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};
  1893. 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};
  1894. 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};
  1895. 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};
  1896. static bool tooltip = true;
  1897. ImGui::Checkbox("Show Tooltip", &tooltip);
  1898. ImGui::SameLine();
  1899. static ImVec4 bullCol = ImVec4(0.000f, 1.000f, 0.441f, 1.000f);
  1900. static ImVec4 bearCol = ImVec4(0.853f, 0.050f, 0.310f, 1.000f);
  1901. ImGui::SameLine(); ImGui::ColorEdit4("##Bull", &bullCol.x, ImGuiColorEditFlags_NoInputs);
  1902. ImGui::SameLine(); ImGui::ColorEdit4("##Bear", &bearCol.x, ImGuiColorEditFlags_NoInputs);
  1903. ImPlot::GetStyle().UseLocalTime = false;
  1904. if (ImPlot::BeginPlot("Candlestick Chart",ImVec2(-1,0))) {
  1905. ImPlot::SetupAxes(nullptr,nullptr,0,ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit);
  1906. ImPlot::SetupAxesLimits(1546300800, 1571961600, 1250, 1600);
  1907. ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Time);
  1908. ImPlot::SetupAxisLimitsConstraints(ImAxis_X1, 1546300800, 1571961600);
  1909. ImPlot::SetupAxisZoomConstraints(ImAxis_X1, 60*60*24*14, 1571961600-1546300800);
  1910. ImPlot::SetupAxisFormat(ImAxis_Y1, "$%.0f");
  1911. MyImPlot::PlotCandlestick("GOOGL",dates, opens, closes, lows, highs, 218, tooltip, 0.25f, bullCol, bearCol);
  1912. ImPlot::EndPlot();
  1913. }
  1914. }
  1915. //-----------------------------------------------------------------------------
  1916. // DEMO WINDOW
  1917. //-----------------------------------------------------------------------------
  1918. void DemoHeader(const char* label, void(*demo)()) {
  1919. if (ImGui::TreeNodeEx(label)) {
  1920. demo();
  1921. ImGui::TreePop();
  1922. }
  1923. }
  1924. void ShowDemoWindow(bool* p_open) {
  1925. static bool show_implot_metrics = false;
  1926. static bool show_implot_style_editor = false;
  1927. static bool show_imgui_metrics = false;
  1928. static bool show_imgui_style_editor = false;
  1929. static bool show_imgui_demo = false;
  1930. if (show_implot_metrics) {
  1931. ImPlot::ShowMetricsWindow(&show_implot_metrics);
  1932. }
  1933. if (show_implot_style_editor) {
  1934. ImGui::SetNextWindowSize(ImVec2(415,762), ImGuiCond_Appearing);
  1935. ImGui::Begin("Style Editor (ImPlot)", &show_implot_style_editor);
  1936. ImPlot::ShowStyleEditor();
  1937. ImGui::End();
  1938. }
  1939. if (show_imgui_style_editor) {
  1940. ImGui::Begin("Style Editor (ImGui)", &show_imgui_style_editor);
  1941. ImGui::ShowStyleEditor();
  1942. ImGui::End();
  1943. }
  1944. if (show_imgui_metrics) {
  1945. ImGui::ShowMetricsWindow(&show_imgui_metrics);
  1946. }
  1947. if (show_imgui_demo) {
  1948. ImGui::ShowDemoWindow(&show_imgui_demo);
  1949. }
  1950. ImGui::SetNextWindowPos(ImVec2(50, 50), ImGuiCond_FirstUseEver);
  1951. ImGui::SetNextWindowSize(ImVec2(600, 750), ImGuiCond_FirstUseEver);
  1952. ImGui::Begin("ImPlot Demo", p_open, ImGuiWindowFlags_MenuBar);
  1953. if (ImGui::BeginMenuBar()) {
  1954. if (ImGui::BeginMenu("Tools")) {
  1955. ImGui::MenuItem("Metrics", nullptr, &show_implot_metrics);
  1956. ImGui::MenuItem("Style Editor", nullptr, &show_implot_style_editor);
  1957. ImGui::Separator();
  1958. ImGui::MenuItem("ImGui Metrics", nullptr, &show_imgui_metrics);
  1959. ImGui::MenuItem("ImGui Style Editor", nullptr, &show_imgui_style_editor);
  1960. ImGui::MenuItem("ImGui Demo", nullptr, &show_imgui_demo);
  1961. ImGui::EndMenu();
  1962. }
  1963. ImGui::EndMenuBar();
  1964. }
  1965. //-------------------------------------------------------------------------
  1966. ImGui::Text("ImPlot says hello. (%s)", IMPLOT_VERSION);
  1967. // display warning about 16-bit indices
  1968. static bool showWarning = sizeof(ImDrawIdx)*8 == 16 && (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) == false;
  1969. if (showWarning) {
  1970. ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1,1,0,1));
  1971. ImGui::TextWrapped("WARNING: ImDrawIdx is 16-bit and ImGuiBackendFlags_RendererHasVtxOffset is false. Expect visual glitches and artifacts! See README for more information.");
  1972. ImGui::PopStyleColor();
  1973. }
  1974. ImGui::Spacing();
  1975. if (ImGui::BeginTabBar("ImPlotDemoTabs")) {
  1976. if (ImGui::BeginTabItem("Plots")) {
  1977. DemoHeader("Line Plots", Demo_LinePlots);
  1978. DemoHeader("Filled Line Plots", Demo_FilledLinePlots);
  1979. DemoHeader("Shaded Plots##", Demo_ShadedPlots);
  1980. DemoHeader("Scatter Plots", Demo_ScatterPlots);
  1981. DemoHeader("Realtime Plots", Demo_RealtimePlots);
  1982. DemoHeader("Stairstep Plots", Demo_StairstepPlots);
  1983. DemoHeader("Bar Plots", Demo_BarPlots);
  1984. DemoHeader("Bar Groups", Demo_BarGroups);
  1985. DemoHeader("Bar Stacks", Demo_BarStacks);
  1986. DemoHeader("Error Bars", Demo_ErrorBars);
  1987. DemoHeader("Stem Plots##", Demo_StemPlots);
  1988. DemoHeader("Infinite Lines", Demo_InfiniteLines);
  1989. DemoHeader("Pie Charts", Demo_PieCharts);
  1990. DemoHeader("Heatmaps", Demo_Heatmaps);
  1991. DemoHeader("Histogram", Demo_Histogram);
  1992. DemoHeader("Histogram 2D", Demo_Histogram2D);
  1993. DemoHeader("Digital Plots", Demo_DigitalPlots);
  1994. DemoHeader("Images", Demo_Images);
  1995. DemoHeader("Markers and Text", Demo_MarkersAndText);
  1996. DemoHeader("NaN Values", Demo_NaNValues);
  1997. ImGui::EndTabItem();
  1998. }
  1999. if (ImGui::BeginTabItem("Subplots")) {
  2000. DemoHeader("Sizing", Demo_SubplotsSizing);
  2001. DemoHeader("Item Sharing", Demo_SubplotItemSharing);
  2002. DemoHeader("Axis Linking", Demo_SubplotAxisLinking);
  2003. DemoHeader("Tables", Demo_Tables);
  2004. ImGui::EndTabItem();
  2005. }
  2006. if (ImGui::BeginTabItem("Axes")) {
  2007. DemoHeader("Log Scale", Demo_LogScale);
  2008. DemoHeader("Symmetric Log Scale", Demo_SymmetricLogScale);
  2009. DemoHeader("Time Scale", Demo_TimeScale);
  2010. DemoHeader("Custom Scale", Demo_CustomScale);
  2011. DemoHeader("Multiple Axes", Demo_MultipleAxes);
  2012. DemoHeader("Tick Labels", Demo_TickLabels);
  2013. DemoHeader("Linked Axes", Demo_LinkedAxes);
  2014. DemoHeader("Axis Constraints", Demo_AxisConstraints);
  2015. DemoHeader("Equal Axes", Demo_EqualAxes);
  2016. DemoHeader("Auto-Fitting Data", Demo_AutoFittingData);
  2017. ImGui::EndTabItem();
  2018. }
  2019. if (ImGui::BeginTabItem("Tools")) {
  2020. DemoHeader("Offset and Stride", Demo_OffsetAndStride);
  2021. DemoHeader("Drag Points", Demo_DragPoints);
  2022. DemoHeader("Drag Lines", Demo_DragLines);
  2023. DemoHeader("Drag Rects", Demo_DragRects);
  2024. DemoHeader("Querying", Demo_Querying);
  2025. DemoHeader("Annotations", Demo_Annotations);
  2026. DemoHeader("Tags", Demo_Tags);
  2027. DemoHeader("Drag and Drop", Demo_DragAndDrop);
  2028. DemoHeader("Legend Options", Demo_LegendOptions);
  2029. DemoHeader("Legend Popups", Demo_LegendPopups);
  2030. DemoHeader("Colormap Widgets", Demo_ColormapWidgets);
  2031. ImGui::EndTabItem();
  2032. }
  2033. if (ImGui::BeginTabItem("Custom")) {
  2034. DemoHeader("Custom Styles", Demo_CustomStyles);
  2035. DemoHeader("Custom Data and Getters", Demo_CustomDataAndGetters);
  2036. DemoHeader("Custom Rendering", Demo_CustomRendering);
  2037. DemoHeader("Custom Plotters and Tooltips", Demo_CustomPlottersAndTooltips);
  2038. ImGui::EndTabItem();
  2039. }
  2040. if (ImGui::BeginTabItem("Config")) {
  2041. Demo_Config();
  2042. ImGui::EndTabItem();
  2043. }
  2044. if (ImGui::BeginTabItem("Help")) {
  2045. Demo_Help();
  2046. ImGui::EndTabItem();
  2047. }
  2048. ImGui::EndTabBar();
  2049. }
  2050. ImGui::End();
  2051. }
  2052. } // namespace ImPlot
  2053. namespace MyImPlot {
  2054. ImPlotPoint SineWave(int idx, void* data) {
  2055. WaveData* wd = (WaveData*)data;
  2056. double x = idx * wd->X;
  2057. return ImPlotPoint(x, wd->Offset + wd->Amp * sin(2 * 3.14 * wd->Freq * x));
  2058. }
  2059. ImPlotPoint SawWave(int idx, void* data) {
  2060. WaveData* wd = (WaveData*)data;
  2061. double x = idx * wd->X;
  2062. return ImPlotPoint(x, wd->Offset + wd->Amp * (-2 / 3.14 * atan(cos(3.14 * wd->Freq * x) / sin(3.14 * wd->Freq * x))));
  2063. }
  2064. ImPlotPoint Spiral(int idx, void*) {
  2065. float r = 0.9f; // outer radius
  2066. float a = 0; // inner radius
  2067. float b = 0.05f; // increment per rev
  2068. float n = (r - a) / b; // number of revolutions
  2069. double th = 2 * n * 3.14; // angle
  2070. float Th = float(th * idx / (1000 - 1));
  2071. return ImPlotPoint(0.5f+(a + b*Th / (2.0f * (float) 3.14))*cos(Th),
  2072. 0.5f + (a + b*Th / (2.0f * (float)3.14))*sin(Th));
  2073. }
  2074. void Sparkline(const char* id, const float* values, int count, float min_v, float max_v, int offset, const ImVec4& col, const ImVec2& size) {
  2075. ImPlot::PushStyleVar(ImPlotStyleVar_PlotPadding, ImVec2(0,0));
  2076. if (ImPlot::BeginPlot(id,size,ImPlotFlags_CanvasOnly)) {
  2077. ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations);
  2078. ImPlot::SetupAxesLimits(0, count - 1, min_v, max_v, ImGuiCond_Always);
  2079. ImPlot::SetNextLineStyle(col);
  2080. ImPlot::SetNextFillStyle(col, 0.25);
  2081. ImPlot::PlotLine(id, values, count, 1, 0, ImPlotLineFlags_Shaded, offset);
  2082. ImPlot::EndPlot();
  2083. }
  2084. ImPlot::PopStyleVar();
  2085. }
  2086. void StyleSeaborn() {
  2087. ImPlotStyle& style = ImPlot::GetStyle();
  2088. ImVec4* colors = style.Colors;
  2089. colors[ImPlotCol_Line] = IMPLOT_AUTO_COL;
  2090. colors[ImPlotCol_Fill] = IMPLOT_AUTO_COL;
  2091. colors[ImPlotCol_MarkerOutline] = IMPLOT_AUTO_COL;
  2092. colors[ImPlotCol_MarkerFill] = IMPLOT_AUTO_COL;
  2093. colors[ImPlotCol_ErrorBar] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  2094. colors[ImPlotCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  2095. colors[ImPlotCol_PlotBg] = ImVec4(0.92f, 0.92f, 0.95f, 1.00f);
  2096. colors[ImPlotCol_PlotBorder] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
  2097. colors[ImPlotCol_LegendBg] = ImVec4(0.92f, 0.92f, 0.95f, 1.00f);
  2098. colors[ImPlotCol_LegendBorder] = ImVec4(0.80f, 0.81f, 0.85f, 1.00f);
  2099. colors[ImPlotCol_LegendText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  2100. colors[ImPlotCol_TitleText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  2101. colors[ImPlotCol_InlayText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  2102. colors[ImPlotCol_AxisText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  2103. colors[ImPlotCol_AxisGrid] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  2104. colors[ImPlotCol_AxisBgHovered] = ImVec4(0.92f, 0.92f, 0.95f, 1.00f);
  2105. colors[ImPlotCol_AxisBgActive] = ImVec4(0.92f, 0.92f, 0.95f, 0.75f);
  2106. colors[ImPlotCol_Selection] = ImVec4(1.00f, 0.65f, 0.00f, 1.00f);
  2107. colors[ImPlotCol_Crosshairs] = ImVec4(0.23f, 0.10f, 0.64f, 0.50f);
  2108. style.LineWeight = 1.5;
  2109. style.Marker = ImPlotMarker_None;
  2110. style.MarkerSize = 4;
  2111. style.MarkerWeight = 1;
  2112. style.FillAlpha = 1.0f;
  2113. style.ErrorBarSize = 5;
  2114. style.ErrorBarWeight = 1.5f;
  2115. style.DigitalBitHeight = 8;
  2116. style.DigitalBitGap = 4;
  2117. style.PlotBorderSize = 0;
  2118. style.MinorAlpha = 1.0f;
  2119. style.MajorTickLen = ImVec2(0,0);
  2120. style.MinorTickLen = ImVec2(0,0);
  2121. style.MajorTickSize = ImVec2(0,0);
  2122. style.MinorTickSize = ImVec2(0,0);
  2123. style.MajorGridSize = ImVec2(1.2f,1.2f);
  2124. style.MinorGridSize = ImVec2(1.2f,1.2f);
  2125. style.PlotPadding = ImVec2(12,12);
  2126. style.LabelPadding = ImVec2(5,5);
  2127. style.LegendPadding = ImVec2(5,5);
  2128. style.MousePosPadding = ImVec2(5,5);
  2129. style.PlotMinSize = ImVec2(300,225);
  2130. }
  2131. } // namespaece MyImPlot
  2132. // WARNING:
  2133. //
  2134. // You can use "implot_internal.h" to build custom plotting fuctions or extend ImPlot.
  2135. // However, note that forward compatibility of this file is not guaranteed and the
  2136. // internal API is subject to change. At some point we hope to bring more of this
  2137. // into the public API and expose the necessary building blocks to fully support
  2138. // custom plotters. For now, proceed at your own risk!
  2139. #include "implot_internal.h"
  2140. namespace MyImPlot {
  2141. template <typename T>
  2142. int BinarySearch(const T* arr, int l, int r, T x) {
  2143. if (r >= l) {
  2144. int mid = l + (r - l) / 2;
  2145. if (arr[mid] == x)
  2146. return mid;
  2147. if (arr[mid] > x)
  2148. return BinarySearch(arr, l, mid - 1, x);
  2149. return BinarySearch(arr, mid + 1, r, x);
  2150. }
  2151. return -1;
  2152. }
  2153. 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) {
  2154. // get ImGui window DrawList
  2155. ImDrawList* draw_list = ImPlot::GetPlotDrawList();
  2156. // calc real value width
  2157. double half_width = count > 1 ? (xs[1] - xs[0]) * width_percent : width_percent;
  2158. // custom tool
  2159. if (ImPlot::IsPlotHovered() && tooltip) {
  2160. ImPlotPoint mouse = ImPlot::GetPlotMousePos();
  2161. mouse.x = ImPlot::RoundTime(ImPlotTime::FromDouble(mouse.x), ImPlotTimeUnit_Day).ToDouble();
  2162. float tool_l = ImPlot::PlotToPixels(mouse.x - half_width * 1.5, mouse.y).x;
  2163. float tool_r = ImPlot::PlotToPixels(mouse.x + half_width * 1.5, mouse.y).x;
  2164. float tool_t = ImPlot::GetPlotPos().y;
  2165. float tool_b = tool_t + ImPlot::GetPlotSize().y;
  2166. ImPlot::PushPlotClipRect();
  2167. draw_list->AddRectFilled(ImVec2(tool_l, tool_t), ImVec2(tool_r, tool_b), IM_COL32(128,128,128,64));
  2168. ImPlot::PopPlotClipRect();
  2169. // find mouse location index
  2170. int idx = BinarySearch(xs, 0, count - 1, mouse.x);
  2171. // render tool tip (won't be affected by plot clip rect)
  2172. if (idx != -1) {
  2173. ImGui::BeginTooltip();
  2174. char buff[32];
  2175. ImPlot::FormatDate(ImPlotTime::FromDouble(xs[idx]),buff,32,ImPlotDateFmt_DayMoYr,ImPlot::GetStyle().UseISO8601);
  2176. ImGui::Text("Day: %s", buff);
  2177. ImGui::Text("Open: $%.2f", opens[idx]);
  2178. ImGui::Text("Close: $%.2f", closes[idx]);
  2179. ImGui::Text("Low: $%.2f", lows[idx]);
  2180. ImGui::Text("High: $%.2f", highs[idx]);
  2181. ImGui::EndTooltip();
  2182. }
  2183. }
  2184. // begin plot item
  2185. if (ImPlot::BeginItem(label_id)) {
  2186. // override legend icon color
  2187. ImPlot::GetCurrentItem()->Color = IM_COL32(64,64,64,255);
  2188. // fit data if requested
  2189. if (ImPlot::FitThisFrame()) {
  2190. for (int i = 0; i < count; ++i) {
  2191. ImPlot::FitPoint(ImPlotPoint(xs[i], lows[i]));
  2192. ImPlot::FitPoint(ImPlotPoint(xs[i], highs[i]));
  2193. }
  2194. }
  2195. // render data
  2196. for (int i = 0; i < count; ++i) {
  2197. ImVec2 open_pos = ImPlot::PlotToPixels(xs[i] - half_width, opens[i]);
  2198. ImVec2 close_pos = ImPlot::PlotToPixels(xs[i] + half_width, closes[i]);
  2199. ImVec2 low_pos = ImPlot::PlotToPixels(xs[i], lows[i]);
  2200. ImVec2 high_pos = ImPlot::PlotToPixels(xs[i], highs[i]);
  2201. ImU32 color = ImGui::GetColorU32(opens[i] > closes[i] ? bearCol : bullCol);
  2202. draw_list->AddLine(low_pos, high_pos, color);
  2203. draw_list->AddRectFilled(open_pos, close_pos, color);
  2204. }
  2205. // end plot item
  2206. ImPlot::EndItem();
  2207. }
  2208. }
  2209. } // namespace MyImplot
  2210. #else
  2211. void ImPlot::ShowDemoWindow(bool* p_open) {}
  2212. #endif
  2213. #endif // #ifndef IMGUI_DISABLE