From 8c1bbf4d8d528edae47ec840ff1d5272a627d745 Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Wed, 7 Jul 2021 21:06:15 -0700 Subject: [PATCH] Subplots (#203) * SubPlots: Y axis padding over multiple plots * Align Plots Group new signature ID, ImPool to store padding data, demo * Align plots orientation (vertically, horizontally) vertical will align Y axis, horizontal will align X axis. *signature changed * ImPlotOrientation used as flag for 2D grids of aligned plots https://github.com/epezent/implot/pull/144#issuecomment-725849368 * AlignPlots updates to merge with v.0.9 * Sync to v0.9 20210127 * subplots proto * make link flags work * stuff * add multi-line centered titles * subplots work * flag ideas * better subplot positioning * resizable subplots * subplot shared items * subplot ratios * some cleanup and refactor * some cleanup and refactor * refactors and demo reorganization * context menus...almost done! * context menus, bug fixes * active id * make implot use ButtonBehavior throughout * bug fixes * more bug fixes * tweaks * fix id issue * finish work on subplots Co-authored-by: ozlb --- README.md | 1 + implot.cpp | 1717 +++++++++++++++++--------- implot.h | 119 +- implot_demo.cpp | 2989 ++++++++++++++++++++++++--------------------- implot_internal.h | 215 +++- implot_items.cpp | 43 +- 6 files changed, 3057 insertions(+), 2027 deletions(-) diff --git a/README.md b/README.md index 66c4503..b33f78a 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ ImPlot is an immediate mode, GPU accelerated plotting library for [Dear ImGui](h - and more likely to come - mix/match multiple plot items on a single plot - configurable axes ranges and scaling (linear/log) +- subplots - time formatted x-axes (US formatted or ISO 8601) - reversible and lockable axes - up to three independent y-axes diff --git a/implot.cpp b/implot.cpp index 837edee..54d0938 100644 --- a/implot.cpp +++ b/implot.cpp @@ -31,43 +31,43 @@ Below is a change-log of API breaking changes only. If you are using one of the When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all implot files. You can read releases logs https://github.com/epezent/implot/releases for more details. -- 2021/03/08 (0.9) - SetColormap and PushColormap(ImVec4*) were removed. Use AddColormap for custom colormap support. LerpColormap was changed to SampleColormap. - ShowColormapScale was changed to ColormapScale and requires additional arguments. -- 2021/03/07 (0.9) - The signature of ShowColormapScale was modified to accept a ImVec2 size. -- 2021/02/28 (0.9) - BeginLegendDragDropSource was changed to BeginDragDropSourceItem with a number of other drag and drop improvements. -- 2021/01/18 (0.9) - The default behavior for opening context menus was change from double right-click to single right-click. ImPlotInputMap and related functions were moved - to implot_internal.h due to its immaturity. -- 2020/10/16 (0.8) - ImPlotStyleVar_InfoPadding was changed to ImPlotStyleVar_MousePosPadding -- 2020/09/10 (0.8) - The single array versions of PlotLine, PlotScatter, PlotStems, and PlotShaded were given additional arguments for x-scale and x0. -- 2020/09/07 (0.8) - Plotting functions which accept a custom getter function pointer have been post-fixed with a G (e.g. PlotLineG) -- 2020/09/06 (0.7) - Several flags under ImPlotFlags and ImPlotAxisFlags were inverted (e.g. ImPlotFlags_Legend -> ImPlotFlags_NoLegend) so that the default flagset - is simply 0. This more closely matches ImGui's style and makes it easier to enable non-default but commonly used flags (e.g. ImPlotAxisFlags_Time). -- 2020/08/28 (0.5) - ImPlotMarker_ can no longer be combined with bitwise OR, |. This features caused unecessary slow-down, and almost no one used it. -- 2020/08/25 (0.5) - ImPlotAxisFlags_Scientific was removed. Logarithmic axes automatically uses scientific notation. -- 2020/08/17 (0.5) - PlotText was changed so that text is centered horizontally and vertically about the desired point. -- 2020/08/16 (0.5) - An ImPlotContext must be explicitly created and destroyed now with `CreateContext` and `DestroyContext`. Previously, the context was statically initialized in this source file. -- 2020/06/13 (0.4) - The flags `ImPlotAxisFlag_Adaptive` and `ImPlotFlags_Cull` were removed. Both are now done internally by default. -- 2020/06/03 (0.3) - The signature and behavior of PlotPieChart was changed so that data with sum less than 1 can optionally be normalized. The label format can now be specified as well. -- 2020/06/01 (0.3) - SetPalette was changed to `SetColormap` for consistency with other plotting libraries. `RestorePalette` was removed. Use `SetColormap(ImPlotColormap_Default)`. -- 2020/05/31 (0.3) - Plot functions taking custom ImVec2* getters were removed. Use the ImPlotPoint* getter versions instead. -- 2020/05/29 (0.3) - The signature of ImPlotLimits::Contains was changed to take two doubles instead of ImVec2 -- 2020/05/16 (0.2) - All plotting functions were reverted to being prefixed with "Plot" to maintain a consistent VerbNoun style. `Plot` was split into `PlotLine` - and `PlotScatter` (however, `PlotLine` can still be used to plot scatter points as `Plot` did before.). `Bar` is not `PlotBars`, to indicate - that multiple bars will be plotted. -- 2020/05/13 (0.2) - `ImMarker` was change to `ImPlotMarker` and `ImAxisFlags` was changed to `ImPlotAxisFlags`. -- 2020/05/11 (0.2) - `ImPlotFlags_Selection` was changed to `ImPlotFlags_BoxSelect` -- 2020/05/11 (0.2) - The namespace ImGui:: was replaced with ImPlot::. As a result, the following additional changes were made: - - Functions that were prefixed or decorated with the word "Plot" have been truncated. E.g., `ImGui::PlotBars` is now just `ImPlot::Bar`. - It should be fairly obvious what was what. - - Some functions have been given names that would have otherwise collided with the ImGui namespace. This has been done to maintain a consistent - style with ImGui. E.g., 'ImGui::PushPlotStyleVar` is now 'ImPlot::PushStyleVar'. -- 2020/05/10 (0.2) - The following function/struct names were changes: - - ImPlotRange -> ImPlotLimits - - GetPlotRange() -> GetPlotLimits() - - SetNextPlotRange -> SetNextPlotLimits - - SetNextPlotRangeX -> SetNextPlotLimitsX - - SetNextPlotRangeY -> SetNextPlotLimitsY -- 2020/05/10 (0.2) - Plot queries are pixel based by default. Query rects that maintain relative plot position have been removed. This was done to support multi-y-axis. +- 2021/03/08 (0.9) - SetColormap and PushColormap(ImVec4*) were removed. Use AddColormap for custom colormap support. LerpColormap was changed to SampleColormap. + ShowColormapScale was changed to ColormapScale and requires additional arguments. +- 2021/03/07 (0.9) - The signature of ShowColormapScale was modified to accept a ImVec2 size. +- 2021/02/28 (0.9) - BeginLegendDragDropSource was changed to BeginDragDropSourceItem with a number of other drag and drop improvements. +- 2021/01/18 (0.9) - The default behavior for opening context menus was change from double right-click to single right-click. ImPlotInputMap and related functions were moved + to implot_internal.h due to its immaturity. +- 2020/10/16 (0.8) - ImPlotStyleVar_InfoPadding was changed to ImPlotStyleVar_MousePosPadding +- 2020/09/10 (0.8) - The single array versions of PlotLine, PlotScatter, PlotStems, and PlotShaded were given additional arguments for x-scale and x0. +- 2020/09/07 (0.8) - Plotting functions which accept a custom getter function pointer have been post-fixed with a G (e.g. PlotLineG) +- 2020/09/06 (0.7) - Several flags under ImPlotFlags and ImPlotAxisFlags were inverted (e.g. ImPlotFlags_Legend -> ImPlotFlags_NoLegend) so that the default flagset + is simply 0. This more closely matches ImGui's style and makes it easier to enable non-default but commonly used flags (e.g. ImPlotAxisFlags_Time). +- 2020/08/28 (0.5) - ImPlotMarker_ can no longer be combined with bitwise OR, |. This features caused unecessary slow-down, and almost no one used it. +- 2020/08/25 (0.5) - ImPlotAxisFlags_Scientific was removed. Logarithmic axes automatically uses scientific notation. +- 2020/08/17 (0.5) - PlotText was changed so that text is centered horizontally and vertically about the desired point. +- 2020/08/16 (0.5) - An ImPlotContext must be explicitly created and destroyed now with `CreateContext` and `DestroyContext`. Previously, the context was statically initialized in this source file. +- 2020/06/13 (0.4) - The flags `ImPlotAxisFlag_Adaptive` and `ImPlotFlags_Cull` were removed. Both are now done internally by default. +- 2020/06/03 (0.3) - The signature and behavior of PlotPieChart was changed so that data with sum less than 1 can optionally be normalized. The label format can now be specified as well. +- 2020/06/01 (0.3) - SetPalette was changed to `SetColormap` for consistency with other plotting libraries. `RestorePalette` was removed. Use `SetColormap(ImPlotColormap_Default)`. +- 2020/05/31 (0.3) - Plot functions taking custom ImVec2* getters were removed. Use the ImPlotPoint* getter versions instead. +- 2020/05/29 (0.3) - The signature of ImPlotLimits::Contains was changed to take two doubles instead of ImVec2 +- 2020/05/16 (0.2) - All plotting functions were reverted to being prefixed with "Plot" to maintain a consistent VerbNoun style. `Plot` was split into `PlotLine` + and `PlotScatter` (however, `PlotLine` can still be used to plot scatter points as `Plot` did before.). `Bar` is not `PlotBars`, to indicate + that multiple bars will be plotted. +- 2020/05/13 (0.2) - `ImMarker` was change to `ImPlotMarker` and `ImAxisFlags` was changed to `ImPlotAxisFlags`. +- 2020/05/11 (0.2) - `ImPlotFlags_Selection` was changed to `ImPlotFlags_BoxSelect` +- 2020/05/11 (0.2) - The namespace ImGui:: was replaced with ImPlot::. As a result, the following additional changes were made: + - Functions that were prefixed or decorated with the word "Plot" have been truncated. E.g., `ImGui::PlotBars` is now just `ImPlot::Bar`. + It should be fairly obvious what was what. + - Some functions have been given names that would have otherwise collided with the ImGui namespace. This has been done to maintain a consistent + style with ImGui. E.g., 'ImGui::PushPlotStyleVar` is now 'ImPlot::PushStyleVar'. +- 2020/05/10 (0.2) - The following function/struct names were changes: + - ImPlotRange -> ImPlotLimits + - GetPlotRange() -> GetPlotLimits() + - SetNextPlotRange -> SetNextPlotLimits + - SetNextPlotRangeX -> SetNextPlotLimitsX + - SetNextPlotRangeY -> SetNextPlotLimitsY +- 2020/05/10 (0.2) - Plot queries are pixel based by default. Query rects that maintain relative plot position have been removed. This was done to support multi-y-axis. */ @@ -139,7 +139,7 @@ ImPlotStyle::ImPlotStyle() { AnnotationPadding = ImVec2(2,2); FitPadding = ImVec2(0,0); PlotDefaultSize = ImVec2(400,300); - PlotMinSize = ImVec2(300,225); + PlotMinSize = ImVec2(200,150); ImPlot::StyleColorsAuto(this); @@ -151,17 +151,6 @@ ImPlotStyle::ImPlotStyle() { UseISO8601 = false; } -ImPlotItem* ImPlotPlot::GetLegendItem(int i) { - return Items.GetByIndex(LegendData.Indices[i]); -} - -const char* ImPlotPlot::GetLegendLabel(int i) { - ImPlotItem* item = GetLegendItem(i); - IM_ASSERT(item != NULL); - IM_ASSERT(item->NameOffset != -1 && item->NameOffset < LegendData.Labels.Buf.Size); - return LegendData.Labels.Buf.Data + item->NameOffset; -} - //----------------------------------------------------------------------------- // Style //----------------------------------------------------------------------------- @@ -339,6 +328,21 @@ void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char *te DrawList->PrimUnreserve(chars_skp*6, chars_skp*4); } +void AddTextCentered(ImDrawList* DrawList, ImVec2 top_center, ImU32 col, const char* text_begin, const char* text_end) { + float txt_ht = ImGui::GetTextLineHeight(); + const char* title_end = ImGui::FindRenderedTextEnd(text_begin, text_end); + ImVec2 text_size; + float y = 0; + while (const char* tmp = (const char*)memchr(text_begin, '\n', title_end-text_begin)) { + text_size = ImGui::CalcTextSize(text_begin,tmp,true); + DrawList->AddText(ImVec2(top_center.x - text_size.x * 0.5f, top_center.y+y),col,text_begin,tmp); + text_begin = tmp + 1; + y += txt_ht; + } + text_size = ImGui::CalcTextSize(text_begin,title_end,true); + DrawList->AddText(ImVec2(top_center.x - text_size.x * 0.5f, top_center.y+y),col,text_begin,title_end); +} + double NiceNum(double x, bool round) { double f; /* fractional part of x */ double nf; /* nice, rounded fraction */ @@ -400,7 +404,9 @@ void SetCurrentContext(ImPlotContext* ctx) { #define IM_RGB(r,g,b) IM_COL32(r,g,b,255) void Initialize(ImPlotContext* ctx) { - Reset(ctx); + ResetCtxForNextPlot(ctx); + ResetCtxForNextAlignedPlots(ctx); + ResetCtxForNextSubplot(ctx); const ImU32 Deep[] = {4289753676, 4283598045, 4285048917, 4283584196, 4289950337, 4284512403, 4291005402, 4287401100, 4285839820, 4291671396 }; const ImU32 Dark[] = {4280031972, 4290281015, 4283084621, 4288892568, 4278222847, 4281597951, 4280833702, 4290740727, 4288256409 }; @@ -438,7 +444,7 @@ void Initialize(ImPlotContext* ctx) { } -void Reset(ImPlotContext* ctx) { +void ResetCtxForNextPlot(ImPlotContext* ctx) { // end child window if it was made if (ctx->ChildWindowMade) ImGui::EndChild(); @@ -446,8 +452,6 @@ void Reset(ImPlotContext* ctx) { // reset the next plot/item data ctx->NextPlotData.Reset(); ctx->NextItemData.Reset(); - // reset items count - ctx->VisibleItemCount = 0; // reset ticks/labels ctx->XTicks.Reset(); for (int i = 0; i < 3; ++i) @@ -473,6 +477,17 @@ void Reset(ImPlotContext* ctx) { ctx->PreviousItem = NULL; } +void ResetCtxForNextAlignedPlots(ImPlotContext* ctx) { + ctx->CurrentAlignmentH = NULL; + ctx->CurrentAlignmentV = NULL; +} + +void ResetCtxForNextSubplot(ImPlotContext* ctx) { + ctx->CurrentSubplot = NULL; + ctx->CurrentAlignmentH = NULL; + ctx->CurrentAlignmentV = NULL; +} + //----------------------------------------------------------------------------- // Plot Utils //----------------------------------------------------------------------------- @@ -489,6 +504,7 @@ ImPlotPlot* GetCurrentPlot() { void BustPlotCache() { GImPlot->Plots.Clear(); + GImPlot->Subplots.Clear(); } void PushLinkedAxis(ImPlotAxis& axis) { @@ -593,16 +609,16 @@ ImVec2 GetLocationPos(const ImRect& outer_rect, const ImVec2& inner_size, ImPlot return pos; } -ImVec2 CalcLegendSize(ImPlotPlot& plot, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orn) { +ImVec2 CalcLegendSize(ImPlotItemGroup& items, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orn) { // vars - const int nItems = plot.GetLegendCount(); + const int nItems = items.GetLegendCount(); const float txt_ht = ImGui::GetTextLineHeight(); const float icon_size = txt_ht; // get label max width float max_label_width = 0; float sum_label_width = 0; for (int i = 0; i < nItems; ++i) { - const char* label = plot.GetLegendLabel(i); + const char* label = items.GetLegendLabel(i); const float label_width = ImGui::CalcTextSize(label, NULL, true).x; max_label_width = label_width > max_label_width ? label_width : max_label_width; sum_label_width += label_width; @@ -614,7 +630,7 @@ ImVec2 CalcLegendSize(ImPlotPlot& plot, const ImVec2& pad, const ImVec2& spacing return legend_size; } -void ShowLegendEntries(ImPlotPlot& plot, const ImRect& legend_bb, bool interactable, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orn, ImDrawList& DrawList) { +bool ShowLegendEntries(ImPlotItemGroup& items, const ImRect& legend_bb, bool hovered, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orn, ImDrawList& DrawList) { ImGuiIO& IO = ImGui::GetIO(); // vars const float txt_ht = ImGui::GetTextLineHeight(); @@ -624,9 +640,10 @@ void ShowLegendEntries(ImPlotPlot& plot, const ImRect& legend_bb, bool interacta ImU32 col_txt_dis = ImAlphaU32(col_txt, 0.25f); // render each legend item float sum_label_width = 0; - for (int i = 0; i < plot.GetLegendCount(); ++i) { - ImPlotItem* item = plot.GetLegendItem(i); - const char* label = plot.GetLegendLabel(i); + bool any_item_hovered = false; + for (int i = 0; i < items.GetLegendCount(); ++i) { + ImPlotItem* item = items.GetLegendItem(i); + const char* label = items.GetLegendLabel(i); const float label_width = ImGui::CalcTextSize(label, NULL, true).x; const ImVec2 top_left = orn == ImPlotOrientation_Vertical ? legend_bb.Min + pad + ImVec2(0, i * (txt_ht + spacing.y)) : @@ -638,31 +655,40 @@ void ShowLegendEntries(ImPlotPlot& plot, const ImRect& legend_bb, bool interacta ImRect label_bb; label_bb.Min = top_left; label_bb.Max = top_left + ImVec2(label_width + icon_size, icon_size); - ImU32 col_hl_txt; + ImU32 col_txt_hl; ImU32 col_item = ImAlphaU32(item->Color,1); - if (interactable && (icon_bb.Contains(IO.MousePos) || label_bb.Contains(IO.MousePos))) { + + // ImGui::ItemAdd(icon_bb, item->ID, &icon_bb); + // ImGui::KeepAliveID(item->ID); + + bool icon_hov = false; + bool icon_hld = false; + bool icon_clk = ImGui::ButtonBehavior(icon_bb, item->ID, &icon_hov, &icon_hld); + if (icon_clk) + item->Show = !item->Show; + + if (icon_hov || label_bb.Contains(IO.MousePos)) { item->LegendHovered = true; - col_hl_txt = ImMixU32(col_txt, col_item, 64); + col_txt_hl = ImMixU32(col_txt, col_item, 64); + any_item_hovered = true; } else { - // item->LegendHovered = false; - col_hl_txt = ImGui::GetColorU32(col_txt); + col_txt_hl = ImGui::GetColorU32(col_txt); } - ImU32 iconColor; - if (interactable && icon_bb.Contains(IO.MousePos)) { - ImU32 col_alpha = ImAlphaU32(col_item,0.5f); - iconColor = item->Show ? col_alpha : ImGui::GetColorU32(ImGuiCol_TextDisabled, 0.5f); - if (IO.MouseClicked[0]) - item->Show = !item->Show; - } - else { - iconColor = item->Show ? col_item : col_txt_dis; - } - DrawList.AddRectFilled(icon_bb.Min, icon_bb.Max, iconColor, 1); + ImU32 col_icon; + if (icon_hld) + col_icon = item->Show ? ImAlphaU32(col_item,0.5f) : ImGui::GetColorU32(ImGuiCol_TextDisabled, 0.5f); + else if (icon_hov) + col_icon = item->Show ? ImAlphaU32(col_item,0.75f) : ImGui::GetColorU32(ImGuiCol_TextDisabled, 0.75f); + else + col_icon = item->Show ? col_item : col_txt_dis; + + DrawList.AddRectFilled(icon_bb.Min, icon_bb.Max, col_icon, 1); const char* text_display_end = ImGui::FindRenderedTextEnd(label, NULL); if (label != text_display_end) - DrawList.AddText(top_left + ImVec2(icon_size, 0), item->Show ? col_hl_txt : col_txt_dis, label, text_display_end); + DrawList.AddText(top_left + ImVec2(icon_size, 0), item->Show ? col_txt_hl : col_txt_dis, label, text_display_end); } + return hovered && !any_item_hovered; } //----------------------------------------------------------------------------- @@ -1319,301 +1345,14 @@ static inline void RenderSelectionRect(ImDrawList& DrawList, const ImVec2& p_min } //----------------------------------------------------------------------------- -// BeginPlot() +// Input Handling //----------------------------------------------------------------------------- -bool BeginPlot(const char* title, const char* x_label, const char* y1_label, const ImVec2& size, - ImPlotFlags flags, ImPlotAxisFlags x_flags, ImPlotAxisFlags y1_flags, ImPlotAxisFlags y2_flags, ImPlotAxisFlags y3_flags, - const char* y2_label, const char* y3_label) -{ - IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?"); +void HandlePlotInput(ImPlotPlot& plot) { + + ImGuiContext& G = *GImGui; ImPlotContext& gp = *GImPlot; - IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "Mismatched BeginPlot()/EndPlot()!"); - IM_ASSERT_USER_ERROR(!(ImHasFlag(x_flags, ImPlotAxisFlags_Time) && ImHasFlag(x_flags, ImPlotAxisFlags_LogScale)), "ImPlotAxisFlags_Time and ImPlotAxisFlags_LogScale cannot be enabled at the same time!"); - IM_ASSERT_USER_ERROR(!ImHasFlag(y1_flags, ImPlotAxisFlags_Time), "Y axes cannot display time formatted labels!"); - - // FRONT MATTER ----------------------------------------------------------- - - ImGuiContext &G = *GImGui; - ImGuiWindow * Window = G.CurrentWindow; - if (Window->SkipItems) { - Reset(GImPlot); - return false; - } - - const ImGuiID ID = Window->GetID(title); - const ImGuiStyle &Style = G.Style; - const ImGuiIO & IO = ImGui::GetIO(); - - bool just_created = gp.Plots.GetByKey(ID) == NULL; - gp.CurrentPlot = gp.Plots.GetOrAddByKey(ID); - gp.CurrentPlot->ID = ID; - ImPlotPlot &plot = *gp.CurrentPlot; - - plot.CurrentYAxis = 0; - - if (just_created) { - plot.Flags = flags; - plot.XAxis.Flags = x_flags; - plot.YAxis[0].Flags = y1_flags; - plot.YAxis[1].Flags = y2_flags; - plot.YAxis[2].Flags = y3_flags; - } - else { - // TODO: Check which individual flags changed, and only reset those! - // There's probably an easy bit mask trick I'm not aware of. - if (flags != plot.PreviousFlags) - plot.Flags = flags; - if (x_flags != plot.XAxis.PreviousFlags) - plot.XAxis.Flags = x_flags; - if (y1_flags != plot.YAxis[0].PreviousFlags) - plot.YAxis[0].Flags = y1_flags; - if (y2_flags != plot.YAxis[1].PreviousFlags) - plot.YAxis[1].Flags = y2_flags; - if (y3_flags != plot.YAxis[2].PreviousFlags) - plot.YAxis[2].Flags = y3_flags; - } - - plot.PreviousFlags = flags; - plot.XAxis.PreviousFlags = x_flags; - plot.YAxis[0].PreviousFlags = y1_flags; - plot.YAxis[1].PreviousFlags = y2_flags; - plot.YAxis[2].PreviousFlags = y3_flags; - - // capture scroll with a child region - if (!ImHasFlag(plot.Flags, ImPlotFlags_NoChild)) { - ImGui::BeginChild(title, ImVec2(size.x == 0 ? gp.Style.PlotDefaultSize.x : size.x, size.y == 0 ? gp.Style.PlotDefaultSize.y : size.y), false, ImGuiWindowFlags_NoScrollbar); - Window = ImGui::GetCurrentWindow(); - Window->ScrollMax.y = 1.0f; - gp.ChildWindowMade = true; - } - else { - gp.ChildWindowMade = false; - } - - ImDrawList &DrawList = *Window->DrawList; - - // NextPlotData ----------------------------------------------------------- - - // linked axes - plot.XAxis.LinkedMin = gp.NextPlotData.LinkedXmin; - plot.XAxis.LinkedMax = gp.NextPlotData.LinkedXmax; - PullLinkedAxis(plot.XAxis); - for (int i = 0; i < IMPLOT_Y_AXES; ++i) { - plot.YAxis[i].LinkedMin = gp.NextPlotData.LinkedYmin[i]; - plot.YAxis[i].LinkedMax = gp.NextPlotData.LinkedYmax[i]; - PullLinkedAxis(plot.YAxis[i]); - } - - if (gp.NextPlotData.HasXRange) { - if (!plot.Initialized || gp.NextPlotData.XRangeCond == ImGuiCond_Always) - plot.XAxis.SetRange(gp.NextPlotData.XRange); - } - - for (int i = 0; i < IMPLOT_Y_AXES; i++) { - if (gp.NextPlotData.HasYRange[i]) { - if (!plot.Initialized || gp.NextPlotData.YRangeCond[i] == ImGuiCond_Always) - plot.YAxis[i].SetRange(gp.NextPlotData.YRange[i]); - } - } - - // Initialization ------------------------------------------------------------ - - if (!plot.Initialized) { - if (!ImHasFlag(plot.XAxis.Flags,ImPlotAxisFlags_NoInitialFit) && !gp.NextPlotData.HasXRange && !gp.NextPlotData.LinkedXmin && !gp.NextPlotData.LinkedXmax) - gp.FitThisFrame = gp.FitX = true; - for (int i = 0; i < IMPLOT_Y_AXES; ++i) { - if (!ImHasFlag(plot.YAxis[i].Flags,ImPlotAxisFlags_NoInitialFit) && !gp.NextPlotData.HasYRange[i] && !gp.NextPlotData.LinkedYmin[i] && !gp.NextPlotData.LinkedYmax[i]) - gp.FitThisFrame = gp.FitY[i] = true; - } - } - - // AXIS STATES ------------------------------------------------------------ - plot.XAxis.HasRange = gp.NextPlotData.HasXRange; plot.XAxis.RangeCond = gp.NextPlotData.XRangeCond; plot.XAxis.Present = true; - plot.YAxis[0].HasRange = gp.NextPlotData.HasYRange[0]; plot.YAxis[0].RangeCond = gp.NextPlotData.YRangeCond[0]; plot.YAxis[0].Present = true; - plot.YAxis[1].HasRange = gp.NextPlotData.HasYRange[1]; plot.YAxis[1].RangeCond = gp.NextPlotData.YRangeCond[1]; plot.YAxis[1].Present = ImHasFlag(plot.Flags, ImPlotFlags_YAxis2); - plot.YAxis[2].HasRange = gp.NextPlotData.HasYRange[2]; plot.YAxis[2].RangeCond = gp.NextPlotData.YRangeCond[2]; plot.YAxis[2].Present = ImHasFlag(plot.Flags, ImPlotFlags_YAxis3); - - for (int i = 0; i < IMPLOT_Y_AXES; ++i) { - if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) - gp.Scales[i] = ImPlotScale_LinLin; - else if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) - gp.Scales[i] = ImPlotScale_LogLin; - else if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) - gp.Scales[i] = ImPlotScale_LinLog; - else if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) - gp.Scales[i] = ImPlotScale_LogLog; - } - - // constraints - plot.XAxis.Constrain(); - for (int i = 0; i < IMPLOT_Y_AXES; ++i) - plot.YAxis[i].Constrain(); - - // constrain equal axes for primary x and y if not approximately equal - // constrains x to y since x pixel size depends on y labels width, and causes feedback loops in opposite case - if (ImHasFlag(plot.Flags, ImPlotFlags_Equal)) { - double xar = plot.XAxis.GetAspect(); - double yar = plot.YAxis[0].GetAspect(); - // edge case: user has set x range this frame, so fit y to x so that we honor their request for x range - // NB: because of feedback across several frames, the user's x request may not be perfectly honored - if (gp.NextPlotData.HasXRange) { - plot.YAxis[0].SetAspect(xar); - } - else { - if (!ImAlmostEqual(xar,yar) && !plot.YAxis[0].IsInputLocked()) - plot.XAxis.SetAspect(yar); - } - } - - // AXIS COLORS ----------------------------------------------------------------- - - UpdateAxisColors(ImPlotCol_XAxis, &plot.XAxis); - UpdateAxisColors(ImPlotCol_YAxis, &plot.YAxis[0]); - UpdateAxisColors(ImPlotCol_YAxis2, &plot.YAxis[1]); - UpdateAxisColors(ImPlotCol_YAxis3, &plot.YAxis[2]); - - // BB, PADDING, HOVER ----------------------------------------------------------- - - // frame - ImVec2 frame_size = ImGui::CalcItemSize(size, gp.Style.PlotDefaultSize.x, gp.Style.PlotDefaultSize.y); - if (frame_size.x < gp.Style.PlotMinSize.x && size.x < 0.0f) - frame_size.x = gp.Style.PlotMinSize.x; - if (frame_size.y < gp.Style.PlotMinSize.y && size.y < 0.0f) - frame_size.y = gp.Style.PlotMinSize.y; - plot.FrameRect = ImRect(Window->DC.CursorPos, Window->DC.CursorPos + frame_size); - ImGui::ItemSize(plot.FrameRect); - if (!ImGui::ItemAdd(plot.FrameRect, ID, &plot.FrameRect)) { - Reset(GImPlot); - return false; - } - plot.FrameHovered = ImGui::ItemHoverable(plot.FrameRect, ID); - if (G.HoveredIdPreviousFrame != 0 && G.HoveredIdPreviousFrame != ID) - plot.FrameHovered = false; - ImGui::SetItemAllowOverlap(); - ImGui::RenderFrame(plot.FrameRect.Min, plot.FrameRect.Max, GetStyleColorU32(ImPlotCol_FrameBg), true, Style.FrameRounding); - - // canvas/axes bb - plot.CanvasRect = ImRect(plot.FrameRect.Min + gp.Style.PlotPadding, plot.FrameRect.Max - gp.Style.PlotPadding); - plot.AxesRect = plot.FrameRect; - - // outside legend adjustments - if (!ImHasFlag(plot.Flags, ImPlotFlags_NoLegend) && plot.GetLegendCount() > 0 && plot.LegendOutside) { - const ImVec2 legend_size = CalcLegendSize(plot, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, plot.LegendOrientation); - const bool west = ImHasFlag(plot.LegendLocation, ImPlotLocation_West) && !ImHasFlag(plot.LegendLocation, ImPlotLocation_East); - const bool east = ImHasFlag(plot.LegendLocation, ImPlotLocation_East) && !ImHasFlag(plot.LegendLocation, ImPlotLocation_West); - const bool north = ImHasFlag(plot.LegendLocation, ImPlotLocation_North) && !ImHasFlag(plot.LegendLocation, ImPlotLocation_South); - const bool south = ImHasFlag(plot.LegendLocation, ImPlotLocation_South) && !ImHasFlag(plot.LegendLocation, ImPlotLocation_North); - const bool horz = plot.LegendOrientation == ImPlotOrientation_Horizontal; - if ((west && !horz) || (west && horz && !north && !south)) { - plot.CanvasRect.Min.x += (legend_size.x + gp.Style.LegendPadding.x); - plot.AxesRect.Min.x += (legend_size.x + gp.Style.PlotPadding.x); - } - if ((east && !horz) || (east && horz && !north && !south)) { - plot.CanvasRect.Max.x -= (legend_size.x + gp.Style.LegendPadding.x); - plot.AxesRect.Max.x -= (legend_size.x + gp.Style.PlotPadding.x); - } - if ((north && horz) || (north && !horz && !west && !east)) { - plot.CanvasRect.Min.y += (legend_size.y + gp.Style.LegendPadding.y); - plot.AxesRect.Min.y += (legend_size.y + gp.Style.PlotPadding.y); - } - if ((south && horz) || (south && !horz && !west && !east)) { - plot.CanvasRect.Max.y -= (legend_size.y + gp.Style.LegendPadding.y); - plot.AxesRect.Max.y -= (legend_size.y + gp.Style.PlotPadding.y); - } - } - - gp.RenderX = (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoGridLines) || - !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickMarks) || - !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickLabels)); - for (int i = 0; i < IMPLOT_Y_AXES; i++) { - gp.RenderY[i] = plot.YAxis[i].Present && - (!ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoGridLines) || - !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickMarks) || - !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickLabels)); - } - - // plot bb - - // (1) calc top/bot padding and plot height - ImVec2 title_size(0.0f, 0.0f); - const float txt_height = ImGui::GetTextLineHeight(); - if (!ImHasFlag(plot.Flags, ImPlotFlags_NoTitle)){ - title_size = ImGui::CalcTextSize(title, NULL, true); - } - - const bool show_x_label = x_label && !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoLabel); - - const float pad_top = title_size.x > 0.0f ? txt_height + gp.Style.LabelPadding.y : 0; - const float pad_bot = (plot.XAxis.IsLabeled() ? ImMax(txt_height, gp.XTicks.MaxHeight) + gp.Style.LabelPadding.y + (plot.XAxis.IsTime() ? txt_height + gp.Style.LabelPadding.y : 0) : 0) - + (show_x_label ? txt_height + gp.Style.LabelPadding.y : 0); - - const float plot_height = plot.CanvasRect.GetHeight() - pad_top - pad_bot; - - // (2) get y tick labels (needed for left/right pad) - for (int i = 0; i < IMPLOT_Y_AXES; i++) { - if (gp.RenderY[i] && gp.NextPlotData.ShowDefaultTicksY[i]) { - if (ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) - AddTicksLogarithmic(plot.YAxis[i].Range, plot_height, ImPlotOrientation_Vertical, gp.YTicks[i], GetFormatY(i)); - else - AddTicksDefault(plot.YAxis[i].Range, plot_height, ImPlotOrientation_Vertical, gp.YTicks[i], GetFormatY(i)); - } - } - - // (3) calc left/right pad - const bool show_y1_label = y1_label && !ImHasFlag(plot.YAxis[0].Flags, ImPlotAxisFlags_NoLabel); - const bool show_y2_label = y2_label && !ImHasFlag(plot.YAxis[1].Flags, ImPlotAxisFlags_NoLabel); - const bool show_y3_label = y3_label && !ImHasFlag(plot.YAxis[2].Flags, ImPlotAxisFlags_NoLabel); - - const float pad_left = (show_y1_label ? txt_height + gp.Style.LabelPadding.x : 0) - + (plot.YAxis[0].IsLabeled() ? gp.YTicks[0].MaxWidth + gp.Style.LabelPadding.x : 0); - const float pad_right = ((plot.YAxis[1].Present && plot.YAxis[1].IsLabeled()) ? gp.YTicks[1].MaxWidth + gp.Style.LabelPadding.x : 0) - + ((plot.YAxis[1].Present && show_y2_label) ? txt_height + gp.Style.LabelPadding.x : 0) - + ((plot.YAxis[1].Present && plot.YAxis[2].Present) ? gp.Style.LabelPadding.x + gp.Style.MinorTickLen.y : 0) - + ((plot.YAxis[2].Present && plot.YAxis[2].IsLabeled()) ? gp.YTicks[2].MaxWidth + gp.Style.LabelPadding.x : 0) - + ((plot.YAxis[2].Present && show_y3_label) ? txt_height + gp.Style.LabelPadding.x : 0); - - const float plot_width = plot.CanvasRect.GetWidth() - pad_left - pad_right; - - // (4) get x ticks - if (gp.RenderX && gp.NextPlotData.ShowDefaultTicksX) { - if (plot.XAxis.IsTime()) - AddTicksTime(plot.XAxis.Range, plot_width, gp.XTicks); - else if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale)) - AddTicksLogarithmic(plot.XAxis.Range, plot_width, ImPlotOrientation_Horizontal, gp.XTicks, GetFormatX()); - else - AddTicksDefault(plot.XAxis.Range, plot_width, ImPlotOrientation_Horizontal, gp.XTicks, GetFormatX()); - } - - // (5) calc plot bb - plot.PlotRect = ImRect(plot.CanvasRect.Min + ImVec2(pad_left, pad_top), plot.CanvasRect.Max - ImVec2(pad_right, pad_bot)); - plot.PlotHovered = plot.FrameHovered && plot.PlotRect.Contains(IO.MousePos); - - // x axis region bb and hover - plot.XAxis.HoverRect = ImRect(plot.PlotRect.GetBL(), ImVec2(plot.PlotRect.Max.x, plot.AxesRect.Max.y)); - plot.XAxis.ExtHovered = plot.XAxis.HoverRect.Contains(IO.MousePos); - plot.XAxis.AllHovered = plot.XAxis.ExtHovered || plot.PlotHovered; - - // axis label reference - gp.YAxisReference[0] = plot.PlotRect.Min.x; - gp.YAxisReference[1] = plot.PlotRect.Max.x; - gp.YAxisReference[2] = !plot.YAxis[1].Present ? plot.PlotRect.Max.x : gp.YAxisReference[1] - + (plot.YAxis[1].IsLabeled() ? gp.Style.LabelPadding.x + gp.YTicks[1].MaxWidth : 0) - + (show_y2_label ? txt_height + gp.Style.LabelPadding.x : 0) - + gp.Style.LabelPadding.x + gp.Style.MinorTickLen.y; - - // y axis regions bb and hover - plot.YAxis[0].HoverRect = ImRect(ImVec2(plot.AxesRect.Min.x, plot.PlotRect.Min.y), ImVec2(plot.PlotRect.Min.x, plot.PlotRect.Max.y)); - plot.YAxis[1].HoverRect = plot.YAxis[2].Present - ? ImRect(plot.PlotRect.GetTR(), ImVec2(gp.YAxisReference[2], plot.PlotRect.Max.y)) - : ImRect(plot.PlotRect.GetTR(), ImVec2(plot.AxesRect.Max.x, plot.PlotRect.Max.y)); - - plot.YAxis[2].HoverRect = ImRect(ImVec2(gp.YAxisReference[2], plot.PlotRect.Min.y), ImVec2(plot.AxesRect.Max.x, plot.PlotRect.Max.y)); - - for (int i = 0; i < IMPLOT_Y_AXES; ++i) { - plot.YAxis[i].ExtHovered = plot.YAxis[i].Present && plot.YAxis[i].HoverRect.Contains(IO.MousePos); - plot.YAxis[i].AllHovered = plot.YAxis[i].ExtHovered || plot.PlotHovered; - } + ImGuiIO& IO = ImGui::GetIO(); const bool any_hov_y_axis_region = plot.YAxis[0].AllHovered || plot.YAxis[1].AllHovered || plot.YAxis[2].AllHovered; @@ -1625,12 +1364,8 @@ bool BeginPlot(const char* title, const char* x_label, const char* y1_label, con hov_query = bb_query.Contains(IO.MousePos); } - // AXIS ASPECT RATIOS - plot.XAxis.Pixels = plot.PlotRect.GetWidth(); - for (int i = 0; i < IMPLOT_Y_AXES; ++i) - plot.YAxis[i].Pixels = plot.PlotRect.GetHeight(); - // QUERY DRAG ------------------------------------------------------------- + if (plot.DraggingQuery && (IO.MouseReleased[gp.InputMap.PanButton] || !IO.MouseDown[gp.InputMap.PanButton])) { plot.DraggingQuery = false; } @@ -1639,7 +1374,7 @@ bool BeginPlot(const char* title, const char* x_label, const char* y1_label, con plot.QueryRect.Min += IO.MouseDelta; plot.QueryRect.Max += IO.MouseDelta; } - if (plot.PlotHovered && hov_query && !plot.DraggingQuery && !plot.Selecting && !plot.LegendHovered) { + if (plot.PlotHovered && hov_query && !plot.DraggingQuery && !plot.Selecting && !plot.Items.Legend.Hovered) { ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll); const bool any_y_dragging = plot.YAxis[0].Dragging || plot.YAxis[1].Dragging || plot.YAxis[2].Dragging; if (IO.MouseDown[gp.InputMap.PanButton] && !plot.XAxis.Dragging && !any_y_dragging) { @@ -1724,7 +1459,7 @@ bool BeginPlot(const char* title, const char* x_label, const char* y1_label, con } } // start drag - if (!drag_in_progress && plot.FrameHovered && IO.MouseClicked[gp.InputMap.PanButton] && ImHasFlag(IO.KeyMods, gp.InputMap.PanMod) && !plot.Selecting && !plot.LegendHovered && !hov_query && !plot.DraggingQuery) { + if (!drag_in_progress && plot.FrameHovered && IO.MouseClicked[gp.InputMap.PanButton] && ImHasFlag(IO.KeyMods, gp.InputMap.PanMod) && !plot.Selecting && !plot.Items.Legend.Hovered && !hov_query && !plot.DraggingQuery) { if (plot.XAxis.AllHovered) { plot.XAxis.Dragging = true; } @@ -1890,7 +1625,7 @@ bool BeginPlot(const char* title, const char* x_label, const char* y1_label, con // FIT ----------------------------------------------------------- // fit from double click - if ( IO.MouseDoubleClicked[gp.InputMap.FitButton] && plot.FrameHovered && (plot.XAxis.AllHovered || any_hov_y_axis_region) && !plot.LegendHovered && !hov_query ) { + if ( IO.MouseDoubleClicked[gp.InputMap.FitButton] && plot.FrameHovered && (plot.XAxis.AllHovered || any_hov_y_axis_region) && !plot.Items.Legend.Hovered && !hov_query ) { gp.FitThisFrame = true; gp.FitX = plot.XAxis.AllHovered; for (int i = 0; i < IMPLOT_Y_AXES; i++) @@ -1913,105 +1648,6 @@ bool BeginPlot(const char* title, const char* x_label, const char* y1_label, con // focus window if ((IO.MouseClicked[0] || IO.MouseClicked[1] || IO.MouseClicked[2]) && plot.FrameHovered) ImGui::FocusWindow(ImGui::GetCurrentWindow()); - - UpdateTransformCache(); - - // set mouse position - for (int i = 0; i < IMPLOT_Y_AXES; i++) { - gp.MousePos[i] = PixelsToPlot(IO.MousePos, i); - } - - // RENDER ----------------------------------------------------------------- - - // grid bg - DrawList.AddRectFilled(plot.PlotRect.Min, plot.PlotRect.Max, GetStyleColorU32(ImPlotCol_PlotBg)); - - // transform ticks (TODO: Move this into ImPlotTickCollection) - if (gp.RenderX) { - for (int t = 0; t < gp.XTicks.Size; t++) { - ImPlotTick *xt = &gp.XTicks.Ticks[t]; - xt->PixelPos = PlotToPixels(xt->PlotPos, 0, 0).x; - } - } - for (int i = 0; i < IMPLOT_Y_AXES; i++) { - if (gp.RenderY[i]) { - for (int t = 0; t < gp.YTicks[i].Size; t++) { - ImPlotTick *yt = &gp.YTicks[i].Ticks[t]; - yt->PixelPos = PlotToPixels(0, yt->PlotPos, i).y; - } - } - } - - // render grid (background) - PushPlotClipRect(gp.Style.PlotBorderSize == 0 ? 1.0f : 0.0f); - if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoGridLines) && !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_Foreground)) - RenderGridLinesX(DrawList, gp.XTicks, plot.PlotRect, plot.XAxis.ColorMaj, plot.XAxis.ColorMin, gp.Style.MajorGridSize.x, gp.Style.MinorGridSize.x); - for (int i = 0; i < IMPLOT_Y_AXES; i++) { - if (plot.YAxis[i].Present && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoGridLines) && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_Foreground)) - RenderGridLinesY(DrawList, gp.YTicks[i], plot.PlotRect, plot.YAxis[i].ColorMaj, plot.YAxis[i].ColorMin, gp.Style.MajorGridSize.y, gp.Style.MinorGridSize.y); - } - PopPlotClipRect(); - - // render title - if (title_size.x > 0.0f && !ImHasFlag(plot.Flags, ImPlotFlags_NoTitle)) { - ImU32 col = GetStyleColorU32(ImPlotCol_TitleText); - const char* title_end = ImGui::FindRenderedTextEnd(title); - DrawList.AddText(ImVec2(plot.CanvasRect.GetCenter().x - title_size.x * 0.5f, plot.CanvasRect.Min.y),col,title,title_end); - } - - // render axis labels - if (show_x_label) { - const ImVec2 xLabel_size = ImGui::CalcTextSize(x_label); - const ImVec2 xLabel_pos(plot.PlotRect.GetCenter().x - xLabel_size.x * 0.5f, plot.CanvasRect.Max.y - txt_height); - DrawList.AddText(xLabel_pos, plot.XAxis.ColorTxt, x_label); - } - - if (show_y1_label) { - const ImVec2 yLabel_size = CalcTextSizeVertical(y1_label); - const ImVec2 yLabel_pos(plot.CanvasRect.Min.x, plot.PlotRect.GetCenter().y + yLabel_size.y * 0.5f); - AddTextVertical(&DrawList, yLabel_pos, plot.YAxis[0].ColorTxt, y1_label); - } - - const char* y_labels[] = {y2_label, y3_label}; - for (int i = 1; i < IMPLOT_Y_AXES; i++) { - const char* current_label = y_labels[i-1]; - if (plot.YAxis[i].Present && current_label && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoLabel)) { - const ImVec2 yLabel_size = CalcTextSizeVertical(current_label); - float label_offset = (plot.YAxis[i].IsLabeled() ? gp.YTicks[i].MaxWidth + gp.Style.LabelPadding.x : 0.0f) + gp.Style.LabelPadding.x; - const ImVec2 yLabel_pos(gp.YAxisReference[i] + label_offset, plot.PlotRect.GetCenter().y + yLabel_size.y * 0.5f); - AddTextVertical(&DrawList, yLabel_pos, plot.YAxis[i].ColorTxt, current_label); - } - } - - // render tick labels - ImGui::PushClipRect(plot.FrameRect.Min, plot.FrameRect.Max, true); - if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickLabels)) { - for (int t = 0; t < gp.XTicks.Size; t++) { - ImPlotTick *xt = &gp.XTicks.Ticks[t]; - if (xt->ShowLabel && xt->PixelPos >= plot.PlotRect.Min.x - 1 && xt->PixelPos <= plot.PlotRect.Max.x + 1) - DrawList.AddText(ImVec2(xt->PixelPos - xt->LabelSize.x * 0.5f, plot.PlotRect.Max.y + gp.Style.LabelPadding.y + xt->Level * (txt_height + gp.Style.LabelPadding.y)), - xt->Major ? plot.XAxis.ColorTxt : plot.XAxis.ColorTxt, gp.XTicks.GetText(t)); - } - } - for (int i = 0; i < IMPLOT_Y_AXES; i++) { - if (plot.YAxis[i].Present && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickLabels)) { - for (int t = 0; t < gp.YTicks[i].Size; t++) { - const float x_start = gp.YAxisReference[i] + (i == 0 ? (-gp.Style.LabelPadding.x - gp.YTicks[i].Ticks[t].LabelSize.x) : gp.Style.LabelPadding.x); - ImPlotTick *yt = &gp.YTicks[i].Ticks[t]; - if (yt->ShowLabel && yt->PixelPos >= plot.PlotRect.Min.y - 1 && yt->PixelPos <= plot.PlotRect.Max.y + 1) { - ImVec2 start(x_start, yt->PixelPos - 0.5f * yt->LabelSize.y); - DrawList.AddText(start, yt->Major ? plot.YAxis[i].ColorTxt : plot.YAxis[i].ColorTxt, gp.YTicks[i].GetText(t)); - } - } - } - } - ImGui::PopClipRect(); - - // clear legend - plot.LegendData.Reset(); - // push plot ID into stack - ImGui::PushID(ID); - return true; } //----------------------------------------------------------------------------- @@ -2158,7 +1794,66 @@ void ShowAxisContextMenu(ImPlotAxis& axis, ImPlotAxis* equal_axis, bool time_all ImFlipFlag(axis.Flags, ImPlotAxisFlags_NoTickLabels); } +bool ShowLegendContextMenu(ImPlotLegendData& legend, bool visible) { + const float s = ImGui::GetFrameHeight(); + bool ret = false; + if (ImGui::Checkbox("Show",&visible)) + ret = true; + if (legend.CanGoInside) + ImGui::Checkbox("Outside", &legend.Outside); + if (ImGui::RadioButton("H", legend.Orientation == ImPlotOrientation_Horizontal)) + legend.Orientation = ImPlotOrientation_Horizontal; + ImGui::SameLine(); + if (ImGui::RadioButton("V", legend.Orientation == ImPlotOrientation_Vertical)) + legend.Orientation = ImPlotOrientation_Vertical; + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2,2)); + if (ImGui::Button("NW",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_NorthWest; } ImGui::SameLine(); + if (ImGui::Button("N", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_North; } ImGui::SameLine(); + if (ImGui::Button("NE",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_NorthEast; } + if (ImGui::Button("W", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_West; } ImGui::SameLine(); + if (ImGui::InvisibleButton("C", ImVec2(1.5f*s,s))) { } ImGui::SameLine(); + if (ImGui::Button("E", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_East; } + if (ImGui::Button("SW",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_SouthWest; } ImGui::SameLine(); + if (ImGui::Button("S", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_South; } ImGui::SameLine(); + if (ImGui::Button("SE",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_SouthEast; } + ImGui::PopStyleVar(); + return ret; +} + +void ShowSubplotsContextMenu(ImPlotSubplot& subplot) { + if ((ImGui::BeginMenu("Linking"))) { + if (ImGui::MenuItem("Link Rows",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkRows))) + ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkRows); + if (ImGui::MenuItem("Link Cols",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkCols))) + ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkCols); + if (ImGui::MenuItem("Link All X",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllX))) + ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllX); + if (ImGui::MenuItem("Link All Y",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllY))) + ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllY); + ImGui::EndMenu(); + } + // if (ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems)) { + // if ((ImGui::BeginMenu("Legend"))) { + // if (ShowLegendContextMenu(subplot.Items.Legend, !ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoLegend))) + // ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoLegend); + // ImGui::EndMenu(); + // } + // } + if ((ImGui::BeginMenu("Settings"))) { + if (ImGui::MenuItem("Title",NULL,!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoTitle))) + ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoTitle); + if (ImGui::MenuItem("Resizable",NULL,!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoResize))) + ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoResize); + if (ImGui::MenuItem("Align",NULL,!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoAlign))) + ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoAlign); + if (ImGui::MenuItem("Share Items",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems))) + ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems); + ImGui::EndMenu(); + } +} + void ShowPlotContextMenu(ImPlotPlot& plot) { + const bool owns_legend = GImPlot->CurrentItems == &plot.Items; const bool equal = ImHasFlag(plot.Flags, ImPlotFlags_Equal); if (ImGui::BeginMenu("X-Axis")) { ImGui::PushID("X"); @@ -2188,6 +1883,17 @@ void ShowPlotContextMenu(ImPlotPlot& plot) { } ImGui::Separator(); + if ((ImGui::BeginMenu("Legend"))) { + if (owns_legend) { + if (ShowLegendContextMenu(plot.Items.Legend, !ImHasFlag(plot.Flags, ImPlotFlags_NoLegend))) + ImFlipFlag(plot.Flags, ImPlotFlags_NoLegend); + } + else if (GImPlot->CurrentSubplot != NULL) { + if (ShowLegendContextMenu(GImPlot->CurrentSubplot->Items.Legend, !ImHasFlag(GImPlot->CurrentSubplot->Flags, ImPlotSubplotFlags_NoLegend))) + ImFlipFlag(GImPlot->CurrentSubplot->Flags, ImPlotSubplotFlags_NoLegend); + } + ImGui::EndMenu(); + } if ((ImGui::BeginMenu("Settings"))) { if (ImGui::MenuItem("Anti-Aliased Lines",NULL,ImHasFlag(plot.Flags, ImPlotFlags_AntiAliased))) ImFlipFlag(plot.Flags, ImPlotFlags_AntiAliased); @@ -2203,34 +1909,450 @@ void ShowPlotContextMenu(ImPlotPlot& plot) { ImFlipFlag(plot.Flags, ImPlotFlags_NoMousePos); if (ImGui::MenuItem("Crosshairs",NULL,ImHasFlag(plot.Flags, ImPlotFlags_Crosshairs))) ImFlipFlag(plot.Flags, ImPlotFlags_Crosshairs); - if ((ImGui::BeginMenu("Legend"))) { - const float s = ImGui::GetFrameHeight(); - if (ImGui::RadioButton("H", plot.LegendOrientation == ImPlotOrientation_Horizontal)) - plot.LegendOrientation = ImPlotOrientation_Horizontal; - ImGui::SameLine(); - if (ImGui::RadioButton("V", plot.LegendOrientation == ImPlotOrientation_Vertical)) - plot.LegendOrientation = ImPlotOrientation_Vertical; - ImGui::Checkbox("Outside", &plot.LegendOutside); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(1,1)); - if (ImGui::Button("##NW",ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_NorthWest; } ImGui::SameLine(); - if (ImGui::Button("##N", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_North; } ImGui::SameLine(); - if (ImGui::Button("##NE",ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_NorthEast; } - if (ImGui::Button("##W", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_West; } ImGui::SameLine(); - if (ImGui::Button("##C", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_Center; } ImGui::SameLine(); - if (ImGui::Button("##E", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_East; } - if (ImGui::Button("##SW",ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_SouthWest; } ImGui::SameLine(); - if (ImGui::Button("##S", ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_South; } ImGui::SameLine(); - if (ImGui::Button("##SE",ImVec2(1.5f*s,s))) { plot.LegendLocation = ImPlotLocation_SouthEast; } - ImGui::PopStyleVar(); - ImGui::EndMenu(); - } ImGui::EndMenu(); } - if (ImGui::MenuItem("Legend",NULL,!ImHasFlag(plot.Flags, ImPlotFlags_NoLegend))) { - ImFlipFlag(plot.Flags, ImPlotFlags_NoLegend); + if (GImPlot->CurrentSubplot != NULL && !ImHasFlag(GImPlot->CurrentPlot->Flags, ImPlotSubplotFlags_NoMenus)) { + ImGui::Separator(); + if ((ImGui::BeginMenu("Subplots"))) { + ShowSubplotsContextMenu(*GImPlot->CurrentSubplot); + ImGui::EndMenu(); + } } } +//----------------------------------------------------------------------------- +// BeginPlot() +//----------------------------------------------------------------------------- + +bool BeginPlot(const char* title, const char* x_label, const char* y1_label, const ImVec2& size, + ImPlotFlags flags, ImPlotAxisFlags x_flags, ImPlotAxisFlags y1_flags, ImPlotAxisFlags y2_flags, ImPlotAxisFlags y3_flags, + const char* y2_label, const char* y3_label) +{ + IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?"); + ImPlotContext& gp = *GImPlot; + IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "Mismatched BeginPlot()/EndPlot()!"); + IM_ASSERT_USER_ERROR(!(ImHasFlag(x_flags, ImPlotAxisFlags_Time) && ImHasFlag(x_flags, ImPlotAxisFlags_LogScale)), "ImPlotAxisFlags_Time and ImPlotAxisFlags_LogScale cannot be enabled at the same time!"); + IM_ASSERT_USER_ERROR(!ImHasFlag(y1_flags, ImPlotAxisFlags_Time), "Y axes cannot display time formatted labels!"); + + // SUBPLOT ID -------------------------------------------------------------- + + if (gp.CurrentSubplot != NULL) + ImGui::PushID(gp.CurrentSubplot->CurrentIdx); + + // FRONT MATTER ----------------------------------------------------------- + + ImGuiContext &G = *GImGui; + ImGuiWindow * Window = G.CurrentWindow; + if (Window->SkipItems && !gp.CurrentSubplot) { + ResetCtxForNextPlot(GImPlot); + return false; + } + + const ImGuiID ID = Window->GetID(title); + const ImGuiStyle &Style = G.Style; + const ImGuiIO & IO = ImGui::GetIO(); + + bool just_created = gp.Plots.GetByKey(ID) == NULL; + gp.CurrentPlot = gp.Plots.GetOrAddByKey(ID); + gp.CurrentPlot->ID = ID; + gp.CurrentPlot->Items.ID = ID; + ImPlotPlot &plot = *gp.CurrentPlot; + + plot.CurrentYAxis = 0; + + if (just_created) { + plot.Flags = flags; + plot.XAxis.Flags = x_flags; + plot.YAxis[0].Flags = y1_flags; + plot.YAxis[1].Flags = y2_flags; + plot.YAxis[2].Flags = y3_flags; + } + else { + // TODO: Check which individual flags changed, and only reset those! + // There's probably an easy bit mask trick I'm not aware of. + if (flags != plot.PreviousFlags) + plot.Flags = flags; + if (x_flags != plot.XAxis.PreviousFlags) + plot.XAxis.Flags = x_flags; + if (y1_flags != plot.YAxis[0].PreviousFlags) + plot.YAxis[0].Flags = y1_flags; + if (y2_flags != plot.YAxis[1].PreviousFlags) + plot.YAxis[1].Flags = y2_flags; + if (y3_flags != plot.YAxis[2].PreviousFlags) + plot.YAxis[2].Flags = y3_flags; + } + + plot.PreviousFlags = flags; + plot.XAxis.PreviousFlags = x_flags; + plot.YAxis[0].PreviousFlags = y1_flags; + plot.YAxis[1].PreviousFlags = y2_flags; + plot.YAxis[2].PreviousFlags = y3_flags; + + // capture scroll with a child region + if (!ImHasFlag(plot.Flags, ImPlotFlags_NoChild)) { + ImVec2 child_size; + if (gp.CurrentSubplot != NULL) + child_size = gp.CurrentSubplot->CellSize; + else + child_size = ImVec2(size.x == 0 ? gp.Style.PlotDefaultSize.x : size.x, size.y == 0 ? gp.Style.PlotDefaultSize.y : size.y); + ImGui::BeginChild(title, child_size, false, ImGuiWindowFlags_NoScrollbar); + Window = ImGui::GetCurrentWindow(); + Window->ScrollMax.y = 1.0f; + gp.ChildWindowMade = true; + } + else { + gp.ChildWindowMade = false; + } + + ImDrawList &DrawList = *Window->DrawList; + + // NextPlotData ----------------------------------------------------------- + + // linked axes + plot.XAxis.LinkedMin = gp.NextPlotData.LinkedXmin; + plot.XAxis.LinkedMax = gp.NextPlotData.LinkedXmax; + PullLinkedAxis(plot.XAxis); + for (int i = 0; i < IMPLOT_Y_AXES; ++i) { + plot.YAxis[i].LinkedMin = gp.NextPlotData.LinkedYmin[i]; + plot.YAxis[i].LinkedMax = gp.NextPlotData.LinkedYmax[i]; + PullLinkedAxis(plot.YAxis[i]); + } + + if (gp.NextPlotData.HasXRange) { + if (!plot.Initialized || gp.NextPlotData.XRangeCond == ImGuiCond_Always) + plot.XAxis.SetRange(gp.NextPlotData.XRange); + } + + for (int i = 0; i < IMPLOT_Y_AXES; i++) { + if (gp.NextPlotData.HasYRange[i]) { + if (!plot.Initialized || gp.NextPlotData.YRangeCond[i] == ImGuiCond_Always) + plot.YAxis[i].SetRange(gp.NextPlotData.YRange[i]); + } + } + + // Initialization ------------------------------------------------------------ + + if (!plot.Initialized) { + if (!ImHasFlag(plot.XAxis.Flags,ImPlotAxisFlags_NoInitialFit) && !gp.NextPlotData.HasXRange && !gp.NextPlotData.LinkedXmin && !gp.NextPlotData.LinkedXmax) + gp.FitThisFrame = gp.FitX = true; + for (int i = 0; i < IMPLOT_Y_AXES; ++i) { + if (!ImHasFlag(plot.YAxis[i].Flags,ImPlotAxisFlags_NoInitialFit) && !gp.NextPlotData.HasYRange[i] && !gp.NextPlotData.LinkedYmin[i] && !gp.NextPlotData.LinkedYmax[i]) + gp.FitThisFrame = gp.FitY[i] = true; + } + } + + // AXIS STATES ------------------------------------------------------------ + plot.XAxis.HasRange = gp.NextPlotData.HasXRange; plot.XAxis.RangeCond = gp.NextPlotData.XRangeCond; plot.XAxis.Present = true; + plot.YAxis[0].HasRange = gp.NextPlotData.HasYRange[0]; plot.YAxis[0].RangeCond = gp.NextPlotData.YRangeCond[0]; plot.YAxis[0].Present = true; + plot.YAxis[1].HasRange = gp.NextPlotData.HasYRange[1]; plot.YAxis[1].RangeCond = gp.NextPlotData.YRangeCond[1]; plot.YAxis[1].Present = ImHasFlag(plot.Flags, ImPlotFlags_YAxis2); + plot.YAxis[2].HasRange = gp.NextPlotData.HasYRange[2]; plot.YAxis[2].RangeCond = gp.NextPlotData.YRangeCond[2]; plot.YAxis[2].Present = ImHasFlag(plot.Flags, ImPlotFlags_YAxis3); + + for (int i = 0; i < IMPLOT_Y_AXES; ++i) { + if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) + gp.Scales[i] = ImPlotScale_LinLin; + else if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) + gp.Scales[i] = ImPlotScale_LogLin; + else if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) + gp.Scales[i] = ImPlotScale_LinLog; + else if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) + gp.Scales[i] = ImPlotScale_LogLog; + } + + // constraints + plot.XAxis.Constrain(); + for (int i = 0; i < IMPLOT_Y_AXES; ++i) + plot.YAxis[i].Constrain(); + + // constrain equal axes for primary x and y if not approximately equal + // constrains x to y since x pixel size depends on y labels width, and causes feedback loops in opposite case + if (ImHasFlag(plot.Flags, ImPlotFlags_Equal)) { + double xar = plot.XAxis.GetAspect(); + double yar = plot.YAxis[0].GetAspect(); + // edge case: user has set x range this frame, so fit y to x so that we honor their request for x range + // NB: because of feedback across several frames, the user's x request may not be perfectly honored + if (gp.NextPlotData.HasXRange) { + plot.YAxis[0].SetAspect(xar); + } + else { + if (!ImAlmostEqual(xar,yar) && !plot.YAxis[0].IsInputLocked()) + plot.XAxis.SetAspect(yar); + } + } + + // AXIS COLORS ----------------------------------------------------------------- + + UpdateAxisColors(ImPlotCol_XAxis, &plot.XAxis); + UpdateAxisColors(ImPlotCol_YAxis, &plot.YAxis[0]); + UpdateAxisColors(ImPlotCol_YAxis2, &plot.YAxis[1]); + UpdateAxisColors(ImPlotCol_YAxis3, &plot.YAxis[2]); + + // SIZING, BB, PADDING, HOVER ----------------------------------------------------------- + + // frame size + ImVec2 frame_size; + if (gp.CurrentSubplot != NULL) + frame_size = gp.CurrentSubplot->CellSize; + else + frame_size = ImGui::CalcItemSize(size, gp.Style.PlotDefaultSize.x, gp.Style.PlotDefaultSize.y); + + if (frame_size.x < gp.Style.PlotMinSize.x && (size.x < 0.0f || gp.CurrentSubplot != NULL)) + frame_size.x = gp.Style.PlotMinSize.x; + if (frame_size.y < gp.Style.PlotMinSize.y && (size.y < 0.0f || gp.CurrentSubplot != NULL)) + frame_size.y = gp.Style.PlotMinSize.y; + + plot.FrameRect = ImRect(Window->DC.CursorPos, Window->DC.CursorPos + frame_size); + ImGui::ItemSize(plot.FrameRect); + if (!ImGui::ItemAdd(plot.FrameRect, ID, &plot.FrameRect) && !gp.CurrentSubplot) { + ResetCtxForNextPlot(GImPlot); + return false; + } + // NB: ImGuiButtonFlags_AllowItemOverlap and SetItemAllowOverlap() required for DragLine and DragPoint + ImGui::ButtonBehavior(plot.FrameRect,plot.ID,&plot.FrameHovered,&plot.FrameHeld,ImGuiButtonFlags_AllowItemOverlap); + ImGui::SetItemAllowOverlap(); + + // canvas/axes bb + plot.CanvasRect = ImRect(plot.FrameRect.Min + gp.Style.PlotPadding, plot.FrameRect.Max - gp.Style.PlotPadding); + plot.AxesRect = plot.FrameRect; + + // outside legend adjustments + if (!ImHasFlag(plot.Flags, ImPlotFlags_NoLegend) && plot.Items.GetLegendCount() > 0 && plot.Items.Legend.Outside) { + const ImVec2 legend_size = CalcLegendSize(plot.Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, plot.Items.Legend.Orientation); + const bool west = ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_West) && !ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_East); + const bool east = ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_East) && !ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_West); + const bool north = ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_North) && !ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_South); + const bool south = ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_South) && !ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_North); + const bool horz = plot.Items.Legend.Orientation == ImPlotOrientation_Horizontal; + if ((west && !horz) || (west && horz && !north && !south)) { + plot.CanvasRect.Min.x += (legend_size.x + gp.Style.LegendPadding.x); + plot.AxesRect.Min.x += (legend_size.x + gp.Style.PlotPadding.x); + } + if ((east && !horz) || (east && horz && !north && !south)) { + plot.CanvasRect.Max.x -= (legend_size.x + gp.Style.LegendPadding.x); + plot.AxesRect.Max.x -= (legend_size.x + gp.Style.PlotPadding.x); + } + if ((north && horz) || (north && !horz && !west && !east)) { + plot.CanvasRect.Min.y += (legend_size.y + gp.Style.LegendPadding.y); + plot.AxesRect.Min.y += (legend_size.y + gp.Style.PlotPadding.y); + } + if ((south && horz) || (south && !horz && !west && !east)) { + plot.CanvasRect.Max.y -= (legend_size.y + gp.Style.LegendPadding.y); + plot.AxesRect.Max.y -= (legend_size.y + gp.Style.PlotPadding.y); + } + } + + gp.RenderX = (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoGridLines) || + !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickMarks) || + !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickLabels)); + for (int i = 0; i < IMPLOT_Y_AXES; i++) { + gp.RenderY[i] = plot.YAxis[i].Present && + (!ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoGridLines) || + !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickMarks) || + !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickLabels)); + } + + // plot bb + + // (1) calc top/bot padding and plot height + ImVec2 title_size(0.0f, 0.0f); + const float txt_height = ImGui::GetTextLineHeight(); + if (!ImHasFlag(plot.Flags, ImPlotFlags_NoTitle)){ + title_size = ImGui::CalcTextSize(title, NULL, true); + } + + const bool show_x_label = x_label && !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoLabel); + + float pad_top = title_size.x > 0.0f ? title_size.y + gp.Style.LabelPadding.y : 0; + float pad_bot = (plot.XAxis.IsLabeled() ? ImMax(txt_height, gp.XTicks.MaxHeight) + gp.Style.LabelPadding.y + (plot.XAxis.IsTime() ? txt_height + gp.Style.LabelPadding.y : 0) : 0) + + (show_x_label ? txt_height + gp.Style.LabelPadding.y : 0); + + // (1*) align plots group + if (gp.CurrentAlignmentH) + gp.CurrentAlignmentH->Update(pad_top,pad_bot); + + const float plot_height = plot.CanvasRect.GetHeight() - pad_top - pad_bot; + + // (2) get y tick labels (needed for left/right pad) + for (int i = 0; i < IMPLOT_Y_AXES; i++) { + if (gp.RenderY[i] && gp.NextPlotData.ShowDefaultTicksY[i]) { + if (ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale)) + AddTicksLogarithmic(plot.YAxis[i].Range, plot_height, ImPlotOrientation_Vertical, gp.YTicks[i], GetFormatY(i)); + else + AddTicksDefault(plot.YAxis[i].Range, plot_height, ImPlotOrientation_Vertical, gp.YTicks[i], GetFormatY(i)); + } + } + + // (3) calc left/right pad + const bool show_y1_label = y1_label && !ImHasFlag(plot.YAxis[0].Flags, ImPlotAxisFlags_NoLabel); + const bool show_y2_label = y2_label && !ImHasFlag(plot.YAxis[1].Flags, ImPlotAxisFlags_NoLabel); + const bool show_y3_label = y3_label && !ImHasFlag(plot.YAxis[2].Flags, ImPlotAxisFlags_NoLabel); + + float pad_left = (show_y1_label ? txt_height + gp.Style.LabelPadding.x : 0) + + (plot.YAxis[0].IsLabeled() ? gp.YTicks[0].MaxWidth + gp.Style.LabelPadding.x : 0); + float pad_right = ((plot.YAxis[1].Present && plot.YAxis[1].IsLabeled()) ? gp.YTicks[1].MaxWidth + gp.Style.LabelPadding.x : 0) + + ((plot.YAxis[1].Present && show_y2_label) ? txt_height + gp.Style.LabelPadding.x : 0) + + ((plot.YAxis[1].Present && plot.YAxis[2].Present) ? gp.Style.LabelPadding.x + gp.Style.MinorTickLen.y : 0) + + ((plot.YAxis[2].Present && plot.YAxis[2].IsLabeled()) ? gp.YTicks[2].MaxWidth + gp.Style.LabelPadding.x : 0) + + ((plot.YAxis[2].Present && show_y3_label) ? txt_height + gp.Style.LabelPadding.x : 0); + + // (3*) align plots group + if (gp.CurrentAlignmentV) + gp.CurrentAlignmentV->Update(pad_left,pad_right); + + const float plot_width = plot.CanvasRect.GetWidth() - pad_left - pad_right; + + // (4) get x ticks + if (gp.RenderX && gp.NextPlotData.ShowDefaultTicksX) { + if (plot.XAxis.IsTime()) + AddTicksTime(plot.XAxis.Range, plot_width, gp.XTicks); + else if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale)) + AddTicksLogarithmic(plot.XAxis.Range, plot_width, ImPlotOrientation_Horizontal, gp.XTicks, GetFormatX()); + else + AddTicksDefault(plot.XAxis.Range, plot_width, ImPlotOrientation_Horizontal, gp.XTicks, GetFormatX()); + } + + // (5) calc plot bb + plot.PlotRect = ImRect(plot.CanvasRect.Min + ImVec2(pad_left, pad_top), plot.CanvasRect.Max - ImVec2(pad_right, pad_bot)); + plot.PlotHovered = plot.FrameHovered && plot.PlotRect.Contains(IO.MousePos); + + // x axis region bb and hover + plot.XAxis.HoverRect = ImRect(plot.PlotRect.GetBL(), ImVec2(plot.PlotRect.Max.x, plot.AxesRect.Max.y)); + plot.XAxis.ExtHovered = plot.XAxis.HoverRect.Contains(IO.MousePos); + plot.XAxis.AllHovered = plot.XAxis.ExtHovered || plot.PlotHovered; + + // axis label reference + gp.YAxisReference[0] = plot.PlotRect.Min.x; + gp.YAxisReference[1] = plot.PlotRect.Max.x; + gp.YAxisReference[2] = !plot.YAxis[1].Present ? plot.PlotRect.Max.x : gp.YAxisReference[1] + + (plot.YAxis[1].IsLabeled() ? gp.Style.LabelPadding.x + gp.YTicks[1].MaxWidth : 0) + + (show_y2_label ? txt_height + gp.Style.LabelPadding.x : 0) + + gp.Style.LabelPadding.x + gp.Style.MinorTickLen.y; + + // y axis regions bb and hover + plot.YAxis[0].HoverRect = ImRect(ImVec2(plot.AxesRect.Min.x, plot.PlotRect.Min.y), ImVec2(plot.PlotRect.Min.x, plot.PlotRect.Max.y)); + plot.YAxis[1].HoverRect = plot.YAxis[2].Present + ? ImRect(plot.PlotRect.GetTR(), ImVec2(gp.YAxisReference[2], plot.PlotRect.Max.y)) + : ImRect(plot.PlotRect.GetTR(), ImVec2(plot.AxesRect.Max.x, plot.PlotRect.Max.y)); + + plot.YAxis[2].HoverRect = ImRect(ImVec2(gp.YAxisReference[2], plot.PlotRect.Min.y), ImVec2(plot.AxesRect.Max.x, plot.PlotRect.Max.y)); + + for (int i = 0; i < IMPLOT_Y_AXES; ++i) { + plot.YAxis[i].ExtHovered = plot.YAxis[i].Present && plot.YAxis[i].HoverRect.Contains(IO.MousePos); + plot.YAxis[i].AllHovered = plot.YAxis[i].ExtHovered || plot.PlotHovered; + } + + // AXIS ASPECT RATIOS + plot.XAxis.Pixels = plot.PlotRect.GetWidth(); + for (int i = 0; i < IMPLOT_Y_AXES; ++i) + plot.YAxis[i].Pixels = plot.PlotRect.GetHeight(); + + // INPUT ------------------------------------------------------------------ + HandlePlotInput(plot); + + + UpdateTransformCache(); + + // set mouse position + for (int i = 0; i < IMPLOT_Y_AXES; i++) { + gp.MousePos[i] = PixelsToPlot(IO.MousePos, i); + } + + // RENDER ----------------------------------------------------------------- + + // render frame + ImGui::RenderFrame(plot.FrameRect.Min, plot.FrameRect.Max, GetStyleColorU32(ImPlotCol_FrameBg), true, Style.FrameRounding); + + // grid bg + DrawList.AddRectFilled(plot.PlotRect.Min, plot.PlotRect.Max, GetStyleColorU32(ImPlotCol_PlotBg)); + + // transform ticks (TODO: Move this into ImPlotTickCollection) + if (gp.RenderX) { + for (int t = 0; t < gp.XTicks.Size; t++) { + ImPlotTick *xt = &gp.XTicks.Ticks[t]; + xt->PixelPos = IM_ROUND(PlotToPixels(xt->PlotPos, 0, 0).x); + } + } + for (int i = 0; i < IMPLOT_Y_AXES; i++) { + if (gp.RenderY[i]) { + for (int t = 0; t < gp.YTicks[i].Size; t++) { + ImPlotTick *yt = &gp.YTicks[i].Ticks[t]; + yt->PixelPos = IM_ROUND(PlotToPixels(0, yt->PlotPos, i).y); + } + } + } + + // render grid (background) + PushPlotClipRect(gp.Style.PlotBorderSize == 0 ? 1.0f : 0.0f); + if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoGridLines) && !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_Foreground)) + RenderGridLinesX(DrawList, gp.XTicks, plot.PlotRect, plot.XAxis.ColorMaj, plot.XAxis.ColorMin, gp.Style.MajorGridSize.x, gp.Style.MinorGridSize.x); + for (int i = 0; i < IMPLOT_Y_AXES; i++) { + if (plot.YAxis[i].Present && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoGridLines) && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_Foreground)) + RenderGridLinesY(DrawList, gp.YTicks[i], plot.PlotRect, plot.YAxis[i].ColorMaj, plot.YAxis[i].ColorMin, gp.Style.MajorGridSize.y, gp.Style.MinorGridSize.y); + } + PopPlotClipRect(); + + // render title + if (title_size.x > 0.0f && !ImHasFlag(plot.Flags, ImPlotFlags_NoTitle)) { + ImU32 col = GetStyleColorU32(ImPlotCol_TitleText); + AddTextCentered(&DrawList,ImVec2(plot.PlotRect.GetCenter().x, plot.CanvasRect.Min.y),col,title); + } + + // render axis labels + if (show_x_label) { + const ImVec2 xLabel_size = ImGui::CalcTextSize(x_label); + const ImVec2 xLabel_pos(plot.PlotRect.GetCenter().x - xLabel_size.x * 0.5f, plot.CanvasRect.Max.y - txt_height); + DrawList.AddText(xLabel_pos, plot.XAxis.ColorTxt, x_label); + } + + if (show_y1_label) { + const ImVec2 yLabel_size = CalcTextSizeVertical(y1_label); + const ImVec2 yLabel_pos(plot.CanvasRect.Min.x, plot.PlotRect.GetCenter().y + yLabel_size.y * 0.5f); + AddTextVertical(&DrawList, yLabel_pos, plot.YAxis[0].ColorTxt, y1_label); + } + + const char* y_labels[] = {y2_label, y3_label}; + for (int i = 1; i < IMPLOT_Y_AXES; i++) { + const char* current_label = y_labels[i-1]; + if (plot.YAxis[i].Present && current_label && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoLabel)) { + const ImVec2 yLabel_size = CalcTextSizeVertical(current_label); + float label_offset = (plot.YAxis[i].IsLabeled() ? gp.YTicks[i].MaxWidth + gp.Style.LabelPadding.x : 0.0f) + gp.Style.LabelPadding.x; + const ImVec2 yLabel_pos(gp.YAxisReference[i] + label_offset, plot.PlotRect.GetCenter().y + yLabel_size.y * 0.5f); + AddTextVertical(&DrawList, yLabel_pos, plot.YAxis[i].ColorTxt, current_label); + } + } + + // render tick labels + ImGui::PushClipRect(plot.FrameRect.Min, plot.FrameRect.Max, true); + if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickLabels)) { + for (int t = 0; t < gp.XTicks.Size; t++) { + ImPlotTick *xt = &gp.XTicks.Ticks[t]; + if (xt->ShowLabel && xt->PixelPos >= plot.PlotRect.Min.x - 1 && xt->PixelPos <= plot.PlotRect.Max.x + 1) + DrawList.AddText(ImVec2(xt->PixelPos - xt->LabelSize.x * 0.5f, plot.PlotRect.Max.y + gp.Style.LabelPadding.y + xt->Level * (txt_height + gp.Style.LabelPadding.y)), + plot.XAxis.ColorTxt, gp.XTicks.GetText(t)); + } + } + for (int i = 0; i < IMPLOT_Y_AXES; i++) { + if (plot.YAxis[i].Present && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickLabels)) { + for (int t = 0; t < gp.YTicks[i].Size; t++) { + const float x_start = gp.YAxisReference[i] + (i == 0 ? (-gp.Style.LabelPadding.x - gp.YTicks[i].Ticks[t].LabelSize.x) : gp.Style.LabelPadding.x); + ImPlotTick *yt = &gp.YTicks[i].Ticks[t]; + if (yt->ShowLabel && yt->PixelPos >= plot.PlotRect.Min.y - 1 && yt->PixelPos <= plot.PlotRect.Max.y + 1) { + ImVec2 start(x_start, yt->PixelPos - 0.5f * yt->LabelSize.y); + DrawList.AddText(start, plot.YAxis[i].ColorTxt, gp.YTicks[i].GetText(t)); + } + } + } + } + ImGui::PopClipRect(); + // clear legend (TODO: put elsewhere) + plot.Items.Legend.Reset(); + // setup items (or dont) + if (gp.CurrentItems == NULL) + gp.CurrentItems = &plot.Items; + // push ID to see item hashes + ImGui::PushOverrideID(gp.CurrentItems->ID); + return true; +} + //----------------------------------------------------------------------------- // EndPlot() //----------------------------------------------------------------------------- @@ -2349,7 +2471,7 @@ void EndPlot() { RenderSelectionRect(DrawList, plot.QueryRect.Min + plot.PlotRect.Min, plot.QueryRect.Max + plot.PlotRect.Min, GetStyleColorVec4(ImPlotCol_Query)); // render crosshairs - if (ImHasFlag(plot.Flags, ImPlotFlags_Crosshairs) && plot.PlotHovered && !(plot.XAxis.Dragging || any_y_dragging) && !plot.Selecting && !plot.Querying && !plot.LegendHovered) { + if (ImHasFlag(plot.Flags, ImPlotFlags_Crosshairs) && plot.PlotHovered && !(plot.XAxis.Dragging || any_y_dragging) && !plot.Selecting && !plot.Querying && !plot.Items.Legend.Hovered) { ImGui::SetMouseCursor(ImGuiMouseCursor_None); ImVec2 xy = IO.MousePos; ImVec2 h1(plot.PlotRect.Min.x, xy.y); @@ -2403,37 +2525,46 @@ void EndPlot() { PopPlotClipRect(); // reset legend hovers - plot.LegendHovered = false; - for (int i = 0; i < plot.Items.GetBufSize(); ++i) - plot.Items.GetByIndex(i)->LegendHovered = false; + plot.Items.Legend.Hovered = false; + for (int i = 0; i < plot.Items.GetItemCount(); ++i) + plot.Items.GetItemByIndex(i)->LegendHovered = false; // render legend - if (!ImHasFlag(plot.Flags, ImPlotFlags_NoLegend) && plot.GetLegendCount() > 0) { - const ImVec2 legend_size = CalcLegendSize(plot, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, plot.LegendOrientation); - const ImVec2 legend_pos = GetLocationPos(plot.LegendOutside ? plot.FrameRect : plot.PlotRect, + if (!ImHasFlag(plot.Flags, ImPlotFlags_NoLegend) && plot.Items.GetLegendCount() > 0) { + const ImVec2 legend_size = CalcLegendSize(plot.Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, plot.Items.Legend.Orientation); + const ImVec2 legend_pos = GetLocationPos(plot.Items.Legend.Outside ? plot.FrameRect : plot.PlotRect, legend_size, - plot.LegendLocation, - plot.LegendOutside ? gp.Style.PlotPadding : gp.Style.LegendPadding); - plot.LegendRect = ImRect(legend_pos, legend_pos + legend_size); + plot.Items.Legend.Location, + plot.Items.Legend.Outside ? gp.Style.PlotPadding : gp.Style.LegendPadding); + plot.Items.Legend.Rect = ImRect(legend_pos, legend_pos + legend_size); // test hover - plot.LegendHovered = plot.FrameHovered && plot.LegendRect.Contains(IO.MousePos); + plot.Items.Legend.Hovered = plot.FrameHovered && plot.Items.Legend.Rect.Contains(IO.MousePos); - if (plot.LegendOutside) + if (plot.Items.Legend.Outside) ImGui::PushClipRect(plot.FrameRect.Min, plot.FrameRect.Max, true); else PushPlotClipRect(); ImU32 col_bg = GetStyleColorU32(ImPlotCol_LegendBg); ImU32 col_bd = GetStyleColorU32(ImPlotCol_LegendBorder); - DrawList.AddRectFilled(plot.LegendRect.Min, plot.LegendRect.Max, col_bg); - DrawList.AddRect(plot.LegendRect.Min, plot.LegendRect.Max, col_bd); - ShowLegendEntries(plot, plot.LegendRect, plot.LegendHovered, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, plot.LegendOrientation, DrawList); + DrawList.AddRectFilled(plot.Items.Legend.Rect.Min, plot.Items.Legend.Rect.Max, col_bg); + DrawList.AddRect(plot.Items.Legend.Rect.Min, plot.Items.Legend.Rect.Max, col_bd); + bool legend_contextable = ShowLegendEntries(plot.Items, plot.Items.Legend.Rect, plot.Items.Legend.Hovered, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, plot.Items.Legend.Orientation, DrawList); + // main ctx menu + if (legend_contextable && !ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.ContextLocked) + ImGui::OpenPopup("##LegendContext"); ImGui::PopClipRect(); + if (ImGui::BeginPopup("##LegendContext")) { + ImGui::Text("Legend"); ImGui::Separator(); + if (ShowLegendContextMenu(plot.Items.Legend, !ImHasFlag(plot.Flags, ImPlotFlags_NoLegend))) + ImFlipFlag(plot.Flags, ImPlotFlags_NoLegend); + ImGui::EndPopup(); + } } else { - plot.LegendRect = ImRect(); + plot.Items.Legend.Rect = ImRect(); } - if (plot.LegendFlipSideNextFrame) { - plot.LegendOutside = !plot.LegendOutside; - plot.LegendFlipSideNextFrame = false; + if (plot.Items.Legend.FlipSideNextFrame) { + plot.Items.Legend.Outside = !plot.Items.Legend.Outside; + plot.Items.Legend.FlipSideNextFrame = false; } // render border @@ -2442,7 +2573,7 @@ void EndPlot() { // FIT DATA -------------------------------------------------------------- const bool axis_equal = ImHasFlag(plot.Flags, ImPlotFlags_Equal); - if (gp.FitThisFrame && (gp.VisibleItemCount > 0 || plot.Queried)) { + if (gp.FitThisFrame) { if (gp.FitX) { const double ext_size = gp.ExtentsX.Size() * 0.5; gp.ExtentsX.Min -= ext_size * gp.Style.FitPadding.x; @@ -2486,27 +2617,26 @@ void EndPlot() { // CONTEXT MENUS ----------------------------------------------------------- + ImGui::PushOverrideID(plot.ID); // main ctx menu - if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && plot.PlotHovered && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.LegendHovered && !plot.ContextLocked) + if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && plot.PlotHovered && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.Items.Legend.Hovered && !plot.ContextLocked) ImGui::OpenPopup("##PlotContext"); if (ImGui::BeginPopup("##PlotContext")) { ShowPlotContextMenu(plot); ImGui::EndPopup(); } - // x-axis ctx menu - if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && plot.FrameHovered && plot.XAxis.ExtHovered && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.LegendHovered && !plot.ContextLocked) + if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && plot.FrameHovered && plot.XAxis.ExtHovered && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.Items.Legend.Hovered && !plot.ContextLocked) ImGui::OpenPopup("##XContext"); if (ImGui::BeginPopup("##XContext")) { ImGui::Text("X-Axis"); ImGui::Separator(); ShowAxisContextMenu(plot.XAxis, ImHasFlag(plot.Flags, ImPlotFlags_Equal) ? &plot.YAxis[0] : NULL, true); ImGui::EndPopup(); } - // y-axes ctx menus for (int i = 0; i < IMPLOT_Y_AXES; ++i) { ImGui::PushID(i); - if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && plot.FrameHovered && plot.YAxis[i].ExtHovered && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.LegendHovered && !plot.ContextLocked) + if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && plot.FrameHovered && plot.YAxis[i].ExtHovered && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.Items.Legend.Hovered && !plot.ContextLocked) ImGui::OpenPopup("##YContext"); if (ImGui::BeginPopup("##YContext")) { if (i == 0) { @@ -2520,7 +2650,7 @@ void EndPlot() { } ImGui::PopID(); } - + ImGui::PopID(); // LINKED AXES ------------------------------------------------------------ @@ -2534,25 +2664,399 @@ void EndPlot() { if (plot.ContextLocked && IO.MouseReleased[gp.InputMap.BoxSelectButton]) plot.ContextLocked = false; - + // remove items + if (gp.CurrentItems == &plot.Items) + gp.CurrentItems = NULL; // reset the plot items for the next frame - for (int i = 0; i < plot.Items.GetBufSize(); ++i) { - plot.Items.GetByIndex(i)->SeenThisFrame = false; + for (int i = 0; i < plot.Items.GetItemCount(); ++i) { + plot.Items.GetItemByIndex(i)->SeenThisFrame = false; } // mark the plot as initialized, i.e. having made it through one frame completely plot.Initialized = true; - // Pop ImGui::PushID at the end of BeginPlot ImGui::PopID(); // Reset context for next plot - Reset(GImPlot); + ResetCtxForNextPlot(GImPlot); + + // setup next subplot + if (gp.CurrentSubplot != NULL) { + ImGui::PopID(); + SubplotNextCell(); + } +} + +//----------------------------------------------------------------------------- +// BEGIN/END SUBPLOT +//----------------------------------------------------------------------------- + +static const float SUBPLOT_BORDER_SIZE = 1.0f; +static const float SUBPLOT_SPLITTER_HALF_THICKNESS = 4.0f; +static const float SUBPLOT_SPLITTER_FEEDBACK_TIMER = 0.06f; + +void SubplotSetCell(int row, int col) { + ImPlotContext& gp = *GImPlot; + ImPlotSubplot& subplot = *gp.CurrentSubplot; + if (row >= subplot.Rows || col >= subplot.Cols) + return; + float xoff = 0; + float yoff = 0; + for (int c = 0; c < col; ++c) + xoff += subplot.ColRatios[c]; + for (int r = 0; r < row; ++r) + yoff += subplot.RowRatios[r]; + const ImVec2 grid_size = subplot.GridRect.GetSize(); + ImVec2 cpos = subplot.GridRect.Min + ImVec2(xoff*grid_size.x,yoff*grid_size.y); + cpos.x = IM_ROUND(cpos.x); + cpos.y = IM_ROUND(cpos.y); + ImGui::GetCurrentWindow()->DC.CursorPos = cpos; + // set cell size + subplot.CellSize.x = IM_ROUND(subplot.GridRect.GetWidth() * subplot.ColRatios[col]); + subplot.CellSize.y = IM_ROUND(subplot.GridRect.GetHeight() * subplot.RowRatios[row]); + // setup links + const bool lx = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllX); + const bool ly = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllY); + const bool lr = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkRows); + const bool lc = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkCols); + LinkNextPlotLimits(lx ? &subplot.ColLinkData[0].Min : lc ? &subplot.ColLinkData[col].Min : NULL, + lx ? &subplot.ColLinkData[0].Max : lc ? &subplot.ColLinkData[col].Max : NULL, + ly ? &subplot.RowLinkData[0].Min : lr ? &subplot.RowLinkData[row].Min : NULL, + ly ? &subplot.RowLinkData[0].Max : lr ? &subplot.RowLinkData[row].Max : NULL); + // setup alignment + if (!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoAlign)) { + gp.CurrentAlignmentH = &subplot.RowAlignmentData[row]; + gp.CurrentAlignmentV = &subplot.ColAlignmentData[col]; + } + // set idx + if (ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ColMajor)) + subplot.CurrentIdx = col * subplot.Rows + row; + else + subplot.CurrentIdx = row * subplot.Cols + col; +} + +void SubplotSetCell(int idx) { + ImPlotContext& gp = *GImPlot; + ImPlotSubplot& subplot = *gp.CurrentSubplot; + if (idx >= subplot.Rows * subplot.Cols) + return; + int row = 0, col = 0; + if (ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ColMajor)) { + row = idx % subplot.Rows; + col = idx / subplot.Rows; + } + else { + row = idx / subplot.Cols; + col = idx % subplot.Cols; + } + return SubplotSetCell(row, col); +} + +void SubplotNextCell() { + ImPlotContext& gp = *GImPlot; + ImPlotSubplot& subplot = *gp.CurrentSubplot; + SubplotSetCell(++subplot.CurrentIdx); +} + +bool BeginSubplots(const char* title, int rows, int cols, const ImVec2& size, ImPlotSubplotFlags flags, float* row_sizes, float* col_sizes) { + IM_ASSERT_USER_ERROR(rows > 0 && cols > 0, "Invalid sizing arguments!"); + IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?"); + IM_ASSERT_USER_ERROR(GImPlot->CurrentSubplot == NULL, "Mismatched BeginSubplots()/EndSubplots()!"); + ImPlotContext& gp = *GImPlot; + ImGuiContext &G = *GImGui; + ImGuiWindow * Window = G.CurrentWindow; + if (Window->SkipItems) + return false; + const ImGuiID ID = Window->GetID(title); + bool just_created = gp.Subplots.GetByKey(ID) == NULL; + gp.CurrentSubplot = gp.Subplots.GetOrAddByKey(ID); + ImPlotSubplot& subplot = *gp.CurrentSubplot; + subplot.ID = ID; + subplot.Items.ID = ID; + // push ID + ImGui::PushID(ID); + + if (just_created) + subplot.Flags = flags; + else if (flags != subplot.PreviousFlags) + subplot.Flags = flags; + subplot.PreviousFlags = flags; + + // check for change in rows and cols + if (subplot.Rows != rows || subplot.Cols != cols) { + subplot.RowAlignmentData.resize(rows); + subplot.RowLinkData.resize(rows); + subplot.RowRatios.resize(rows); + for (int r = 0; r < rows; ++r) { + subplot.RowAlignmentData[r].Reset(); + subplot.RowLinkData[r] = ImPlotRange(0,1); + subplot.RowRatios[r] = 1.0f / rows; + } + subplot.ColAlignmentData.resize(cols); + subplot.ColLinkData.resize(cols); + subplot.ColRatios.resize(cols); + for (int c = 0; c < cols; ++c) { + subplot.ColAlignmentData[c].Reset(); + subplot.ColLinkData[c] = ImPlotRange(0,1); + subplot.ColRatios[c] = 1.0f / cols; + } + } + // check incoming size requests + float row_sum = 0, col_sum = 0; + if (row_sizes != NULL) { + row_sum = ImSum(row_sizes, rows); + for (int r = 0; r < rows; ++r) + subplot.RowRatios[r] = row_sizes[r] / row_sum; + } + if (col_sizes != NULL) { + col_sum = ImSum(col_sizes, cols); + for (int c = 0; c < cols; ++c) + subplot.ColRatios[c] = col_sizes[c] / col_sum; + } + subplot.Rows = rows; + subplot.Cols = cols; + + // calc plot frame sizes + ImVec2 title_size(0.0f, 0.0f); + const float txt_height = ImGui::GetTextLineHeight(); + if (!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoTitle)) + title_size = ImGui::CalcTextSize(title, NULL, true); + const float pad_top = title_size.x > 0.0f ? title_size.y + gp.Style.LabelPadding.y : 0; + const ImVec2 half_pad = gp.Style.PlotPadding/2; + const ImVec2 frame_size = ImGui::CalcItemSize(size, gp.Style.PlotDefaultSize.x, gp.Style.PlotDefaultSize.y); + subplot.FrameRect = ImRect(Window->DC.CursorPos, Window->DC.CursorPos + frame_size); + subplot.GridRect.Min = subplot.FrameRect.Min + half_pad + ImVec2(0,pad_top); + subplot.GridRect.Max = subplot.FrameRect.Max - half_pad; + subplot.FrameHovered = subplot.FrameRect.Contains(ImGui::GetMousePos()) && ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows); + + // outside legend adjustments + const bool share_items = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems); + if (share_items) + gp.CurrentItems = &subplot.Items; + if (share_items && !ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoLegend) && subplot.Items.GetLegendCount() > 0) { + const ImVec2 legend_size = CalcLegendSize(subplot.Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, subplot.Items.Legend.Orientation); + const bool west = ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_West) && !ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_East); + const bool east = ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_East) && !ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_West); + const bool north = ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_North) && !ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_South); + const bool south = ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_South) && !ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_North); + const bool horz = subplot.Items.Legend.Orientation == ImPlotOrientation_Horizontal; + if ((west && !horz) || (west && horz && !north && !south)) + subplot.GridRect.Min.x += (legend_size.x + gp.Style.LegendPadding.x); + if ((east && !horz) || (east && horz && !north && !south)) + subplot.GridRect.Max.x -= (legend_size.x + gp.Style.LegendPadding.x); + if ((north && horz) || (north && !horz && !west && !east)) + subplot.GridRect.Min.y += (legend_size.y + gp.Style.LegendPadding.y); + if ((south && horz) || (south && !horz && !west && !east)) + subplot.GridRect.Max.y -= (legend_size.y + gp.Style.LegendPadding.y); + } + + // render single background frame + ImGui::RenderFrame(subplot.FrameRect.Min, subplot.FrameRect.Max, GetStyleColorU32(ImPlotCol_FrameBg), true, ImGui::GetStyle().FrameRounding); + // render title + if (title_size.x > 0.0f && !ImHasFlag(subplot.Flags, ImPlotFlags_NoTitle)) { + const ImU32 col = GetStyleColorU32(ImPlotCol_TitleText); + AddTextCentered(ImGui::GetWindowDrawList(),ImVec2(subplot.GridRect.GetCenter().x, subplot.GridRect.Min.y - pad_top + half_pad.y),col,title); + } + + // render splitters + if (!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoResize)) { + ImDrawList& DrawList = *ImGui::GetWindowDrawList(); + const ImU32 nrm_col = ImGui::ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_Separator]); + const ImU32 hov_col = ImGui::ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_SeparatorHovered]); + const ImU32 act_col = ImGui::ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_SeparatorActive]); + float xpos = subplot.GridRect.Min.x; + float ypos = subplot.GridRect.Min.y; + const ImVec2 mouse = ImGui::GetIO().MousePos; + int separator = 1; + // bool pass = false; + for (int r = 0; r < subplot.Rows-1; ++r) { + ypos += subplot.RowRatios[r] * subplot.GridRect.GetHeight(); + const ImGuiID sep_id = subplot.ID + separator; + ImGui::KeepAliveID(sep_id); + const ImRect sep_bb = ImRect(subplot.GridRect.Min.x, ypos-SUBPLOT_SPLITTER_HALF_THICKNESS, subplot.GridRect.Max.x, ypos+SUBPLOT_SPLITTER_HALF_THICKNESS); + bool sep_hov = false, sep_hld = false; + const bool sep_clk = ImGui::ButtonBehavior(sep_bb, sep_id, &sep_hov, &sep_hld, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick); + if ((sep_hov && G.HoveredIdTimer > SUBPLOT_SPLITTER_FEEDBACK_TIMER) || sep_hld) { + if (sep_clk && ImGui::IsMouseDoubleClicked(0)) { + float p = (subplot.RowRatios[r] + subplot.RowRatios[r+1])/2; + subplot.RowRatios[r] = subplot.RowRatios[r+1] = p; + } + if (sep_clk) { + subplot.TempSizes[0] = subplot.RowRatios[r]; + subplot.TempSizes[1] = subplot.RowRatios[r+1]; + } + if (sep_hld) { + float dp = ImGui::GetMouseDragDelta(0).y / subplot.GridRect.GetHeight(); + if (subplot.TempSizes[0] + dp > 0.1f && subplot.TempSizes[1] - dp > 0.1f) { + subplot.RowRatios[r] = subplot.TempSizes[0] + dp; + subplot.RowRatios[r+1] = subplot.TempSizes[1] - dp; + } + } + DrawList.AddLine(ImVec2(IM_ROUND(subplot.GridRect.Min.x),IM_ROUND(ypos)), + ImVec2(IM_ROUND(subplot.GridRect.Max.x),IM_ROUND(ypos)), + sep_hld ? act_col : hov_col, SUBPLOT_BORDER_SIZE); + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS); + } + separator++; + } + for (int c = 0; c < subplot.Cols-1; ++c) { + xpos += subplot.ColRatios[c] * subplot.GridRect.GetWidth(); + const ImGuiID sep_id = subplot.ID + separator; + ImGui::KeepAliveID(sep_id); + const ImRect sep_bb = ImRect(xpos-SUBPLOT_SPLITTER_HALF_THICKNESS, subplot.GridRect.Min.y, xpos+SUBPLOT_SPLITTER_HALF_THICKNESS, subplot.GridRect.Max.y); + bool sep_hov = false, sep_hld = false; + const bool sep_clk = ImGui::ButtonBehavior(sep_bb, sep_id, &sep_hov, &sep_hld, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick); + if ((sep_hov && G.HoveredIdTimer > SUBPLOT_SPLITTER_FEEDBACK_TIMER) || sep_hld) { + if (sep_clk && ImGui::IsMouseDoubleClicked(0)) { + float p = (subplot.ColRatios[c] + subplot.ColRatios[c+1])/2; + subplot.ColRatios[c] = subplot.ColRatios[c+1] = p; + } + if (sep_clk) { + subplot.TempSizes[0] = subplot.ColRatios[c]; + subplot.TempSizes[1] = subplot.ColRatios[c+1]; + } + if (sep_hld) { + float dp = ImGui::GetMouseDragDelta(0).x / subplot.GridRect.GetWidth(); + if (subplot.TempSizes[0] + dp > 0.1f && subplot.TempSizes[1] - dp > 0.1f) { + subplot.ColRatios[c] = subplot.TempSizes[0] + dp; + subplot.ColRatios[c+1] = subplot.TempSizes[1] - dp; + } + } + DrawList.AddLine(ImVec2(IM_ROUND(xpos),IM_ROUND(subplot.GridRect.Min.y)), + ImVec2(IM_ROUND(xpos),IM_ROUND(subplot.GridRect.Max.y)), + sep_hld ? act_col : hov_col, SUBPLOT_BORDER_SIZE); + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW); + } + separator++; + } + } + + // set outgoing sizes + if (row_sizes != NULL) { + for (int r = 0; r < rows; ++r) + row_sizes[r] = subplot.RowRatios[r] * row_sum; + } + if (col_sizes != NULL) { + for (int c = 0; c < cols; ++c) + col_sizes[c] = subplot.ColRatios[c] * col_sum; + } + + // push styling + PushStyleColor(ImPlotCol_FrameBg, IM_COL32_BLACK_TRANS); + PushStyleVar(ImPlotStyleVar_PlotPadding, half_pad); + PushStyleVar(ImPlotStyleVar_PlotMinSize, ImVec2(0,0)); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize,0); + + // set initial cursor pos + Window->DC.CursorPos = subplot.GridRect.Min; + // begin alignrmnts + for (int r = 0; r < subplot.Rows; ++r) + subplot.RowAlignmentData[r].Begin(); + for (int c = 0; c < subplot.Cols; ++c) + subplot.ColAlignmentData[c].Begin(); + // clear legend data + subplot.Items.Legend.Reset(); + // Setup first subplot + SubplotSetCell(0,0); + return true; +} + +void EndSubplots() { + IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?"); + IM_ASSERT_USER_ERROR(GImPlot->CurrentSubplot != NULL, "Mismatched BeginSubplots()/EndSubplots()!"); + ImPlotContext& gp = *GImPlot; + ImPlotSubplot& subplot = *GImPlot->CurrentSubplot; + // set alignments + for (int r = 0; r < subplot.Rows; ++r) + subplot.RowAlignmentData[r].End(); + for (int c = 0; c < subplot.Cols; ++c) + subplot.ColAlignmentData[c].End(); + // pop styling + PopStyleColor(); + PopStyleVar(); + PopStyleVar(); + ImGui::PopStyleVar(); + // legend + subplot.Items.Legend.Hovered = false; + for (int i = 0; i < subplot.Items.GetItemCount(); ++i) + subplot.Items.GetItemByIndex(i)->LegendHovered = false; + // render legend + const bool share_items = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems); + ImDrawList& DrawList = *ImGui::GetWindowDrawList(); + if (share_items && !ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoLegend) && subplot.Items.GetLegendCount() > 0) { + const ImVec2 legend_size = CalcLegendSize(subplot.Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, subplot.Items.Legend.Orientation); + const ImVec2 legend_pos = GetLocationPos(subplot.FrameRect, legend_size, subplot.Items.Legend.Location, gp.Style.PlotPadding); + subplot.Items.Legend.Rect = ImRect(legend_pos, legend_pos + legend_size); + subplot.Items.Legend.Hovered = subplot.FrameHovered && subplot.Items.Legend.Rect.Contains(ImGui::GetIO().MousePos); + ImGui::PushClipRect(subplot.FrameRect.Min, subplot.FrameRect.Max, true); + ImU32 col_bg = GetStyleColorU32(ImPlotCol_LegendBg); + ImU32 col_bd = GetStyleColorU32(ImPlotCol_LegendBorder); + DrawList.AddRectFilled(subplot.Items.Legend.Rect.Min, subplot.Items.Legend.Rect.Max, col_bg); + DrawList.AddRect(subplot.Items.Legend.Rect.Min, subplot.Items.Legend.Rect.Max, col_bd); + bool legend_contextable =ShowLegendEntries(subplot.Items, subplot.Items.Legend.Rect, subplot.Items.Legend.Hovered, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, subplot.Items.Legend.Orientation, DrawList); + if (legend_contextable && !ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoMenus) && ImGui::GetIO().MouseReleased[gp.InputMap.ContextMenuButton]) + ImGui::OpenPopup("##LegendContext"); + ImGui::PopClipRect(); + if (ImGui::BeginPopup("##LegendContext")) { + ImGui::Text("Legend"); ImGui::Separator(); + if (ShowLegendContextMenu(subplot.Items.Legend, !ImHasFlag(subplot.Flags, ImPlotFlags_NoLegend))) + ImFlipFlag(subplot.Flags, ImPlotFlags_NoLegend); + ImGui::EndPopup(); + } + } + else { + subplot.Items.Legend.Rect = ImRect(); + } + // remove items + if (gp.CurrentItems == &subplot.Items) + gp.CurrentItems = NULL; + // reset the plot items for the next frame (TODO: put this elswhere) + for (int i = 0; i < subplot.Items.GetItemCount(); ++i) { + subplot.Items.GetItemByIndex(i)->SeenThisFrame = false; + } + // pop id + ImGui::PopID(); + // set DC back correctly + GImGui->CurrentWindow->DC.CursorPos = subplot.FrameRect.Min; + ImGui::Dummy(subplot.FrameRect.GetSize()); + ResetCtxForNextSubplot(GImPlot); + } //----------------------------------------------------------------------------- // MISC API //----------------------------------------------------------------------------- +bool BeginAlignedPlots(const char* group_id, ImPlotOrientation orientation) { + IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?"); + IM_ASSERT_USER_ERROR(GImPlot->CurrentAlignmentH == NULL && GImPlot->CurrentAlignmentV == NULL, "Mismatched BeginAlignedPlots()/EndAlignedPlots()!"); + ImPlotContext& gp = *GImPlot; + ImGuiContext &G = *GImGui; + ImGuiWindow * Window = G.CurrentWindow; + if (Window->SkipItems) + return false; + const ImGuiID ID = Window->GetID(group_id); + ImPlotAlignmentData* alignment = gp.AlignmentData.GetOrAddByKey(ID); + if (orientation == ImPlotOrientation_Horizontal) + gp.CurrentAlignmentH = alignment; + if (orientation == ImPlotOrientation_Vertical) + gp.CurrentAlignmentV = alignment; + if (alignment->Orientation != orientation) + alignment->Reset(); + alignment->Orientation = orientation; + alignment->Begin(); + return true; +} + +void EndAlignedPlots() { + IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?"); + IM_ASSERT_USER_ERROR(GImPlot->CurrentAlignmentH != NULL || GImPlot->CurrentAlignmentV != NULL, "Mismatched BeginAlignedPlots()/EndAlignedPlots()!"); + ImPlotContext& gp = *GImPlot; + ImPlotAlignmentData* alignment = gp.CurrentAlignmentH != NULL ? gp.CurrentAlignmentH : (gp.CurrentAlignmentV != NULL ? gp.CurrentAlignmentV : NULL); + if (alignment) + alignment->End(); + ResetCtxForNextAlignedPlots(GImPlot); +} + ImPlotInputMap& GetInputMap() { return GImPlot->InputMap; } @@ -2986,6 +3490,7 @@ bool DragPoint(const char* id, double* x, double* y, bool show_label, const ImVe #define IMPLOT_ID_LEG 10030911 #define IMPLOT_ID_XAX 10030912 #define IMPLOT_ID_YAX 10030913 +#define IMPLOT_ID_ITM 10030914 bool BeginDragDropTargetEx(int id, const ImRect& rect) { ImGuiContext& G = *GImGui; @@ -3009,8 +3514,7 @@ bool BeginDragDropTargetY(ImPlotYAxis axis) { } bool BeginDragDropTargetLegend() { - return !ImHasFlag(GImPlot->CurrentPlot->Flags,ImPlotFlags_NoLegend) && - BeginDragDropTargetEx(IMPLOT_ID_LEG, GImPlot->CurrentPlot->LegendRect); + return BeginDragDropTargetEx(IMPLOT_ID_LEG, GImPlot->CurrentItems->Legend.Rect); } void EndDragDropTarget() { @@ -3064,8 +3568,10 @@ bool BeginDragDropSourceEx(ImGuiID source_id, bool is_hovered, ImGuiDragDropFlag tooltip_window->HiddenFramesCanSkipItems = 1; } } + return true; } + return false; } @@ -3098,11 +3604,12 @@ bool BeginDragDropSourceY(ImPlotYAxis axis, ImGuiKeyModFlags key_mods, ImGuiDrag bool BeginDragDropSourceItem(const char* label_id, ImGuiDragDropFlags flags) { ImPlotContext& gp = *GImPlot; - IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "BeginDragDropSourceItem() needs to be called between BeginPlot() and EndPlot()!"); - ImGuiID source_id = ImGui::GetID(label_id); - ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(source_id); + IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "BeginDragDropSourceItem() needs to be called within an itemized context!"); + ImGuiID item_id = ImGui::GetIDWithSeed(label_id, NULL, gp.CurrentItems->ID); + ImPlotItem* item = gp.CurrentItems->GetItem(item_id); bool is_hovered = item && item->LegendHovered; - return BeginDragDropSourceEx(source_id, is_hovered, flags, ImGuiKeyModFlags_None); + ImGuiID temp_id = ImGui::GetIDWithSeed("dnd",NULL,item->ID); // total hack + return BeginDragDropSourceEx(temp_id, is_hovered, flags, ImGuiKeyModFlags_None); } void EndDragDropSource() { @@ -3138,11 +3645,11 @@ void ColormapIcon(ImPlotColormap cmap) { void SetLegendLocation(ImPlotLocation location, ImPlotOrientation orientation, bool outside) { ImPlotContext& gp = *GImPlot; - IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "SetLegendLocation() needs to be called between BeginPlot() and EndPlot()!"); - gp.CurrentPlot->LegendLocation = location; - gp.CurrentPlot->LegendOrientation = orientation; - if (gp.CurrentPlot->LegendOutside != outside) - gp.CurrentPlot->LegendFlipSideNextFrame = true; + IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "SetLegendLocation() needs to be called within an itemized context!"); + gp.CurrentItems->Legend.Location = location; + gp.CurrentItems->Legend.Orientation = orientation; + if (gp.CurrentItems->Legend.Outside != outside) + gp.CurrentItems->Legend.FlipSideNextFrame = true; } void SetMousePosLocation(ImPlotLocation location) { @@ -3153,21 +3660,21 @@ void SetMousePosLocation(ImPlotLocation location) { bool IsLegendEntryHovered(const char* label_id) { ImPlotContext& gp = *GImPlot; - IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotItemHighlight() needs to be called between BeginPlot() and EndPlot()!"); - ImGuiID id = ImGui::GetID(label_id); - ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id); + IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "IsPlotItemHighlight() needs to be called within an itemized context!"); + ImGuiID id = ImGui::GetIDWithSeed(label_id, NULL, gp.CurrentItems->ID); + ImPlotItem* item = gp.CurrentItems->GetItem(id); return item && item->LegendHovered; } bool BeginLegendPopup(const char* label_id, ImGuiMouseButton mouse_button) { ImPlotContext& gp = *GImPlot; - IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "BeginLegendPopup() needs to be called between BeginPlot() and EndPlot()!"); + IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "BeginLegendPopup() needs to be called within an itemized context!"); ImGuiWindow* window = GImGui->CurrentWindow; if (window->SkipItems) return false; - ImGuiID id = ImGui::GetID(label_id); + ImGuiID id = ImGui::GetIDWithSeed(label_id, NULL, gp.CurrentItems->ID); if (ImGui::IsMouseReleased(mouse_button)) { - ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id); + ImPlotItem* item = gp.CurrentItems->GetItem(id); if (item && item->LegendHovered) ImGui::OpenPopupEx(id); } @@ -3189,7 +3696,7 @@ void ShowAltLegend(const char* title_id, ImPlotOrientation orientation, const Im ImVec2 legend_size; ImVec2 default_size = gp.Style.LegendPadding * 2; if (plot != NULL) { - legend_size = CalcLegendSize(*plot, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, orientation); + legend_size = CalcLegendSize(plot->Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, orientation); default_size = legend_size + gp.Style.LegendPadding * 2; } ImVec2 frame_size = ImGui::CalcItemSize(size, default_size.x, default_size.y); @@ -3209,7 +3716,7 @@ void ShowAltLegend(const char* title_id, ImPlotOrientation orientation, const Im DrawList.AddRectFilled(legend_bb.Min, legend_bb.Max, col_bg); DrawList.AddRect(legend_bb.Min, legend_bb.Max, col_bd); // render entries - ShowLegendEntries(*plot, legend_bb, interactable, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, orientation, DrawList); + ShowLegendEntries(plot->Items, legend_bb, interactable, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, orientation, DrawList); } DrawList.PopClipRect(); } @@ -3383,10 +3890,10 @@ void PopColormap(int count) { ImU32 NextColormapColorU32() { ImPlotContext& gp = *GImPlot; - IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "NextColormapColor() needs to be called between BeginPlot() and EndPlot()!"); - int idx = gp.CurrentPlot->ColormapIdx % gp.ColormapData.GetKeyCount(gp.Style.Colormap); + IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "NextColormapColor() needs to be called between BeginPlot() and EndPlot()!"); + int idx = gp.CurrentItems->ColormapIdx % gp.ColormapData.GetKeyCount(gp.Style.Colormap); ImU32 col = gp.ColormapData.GetKeyColor(gp.Style.Colormap, idx); - gp.CurrentPlot->ColormapIdx++; + gp.CurrentItems->ColormapIdx++; return col; } @@ -3912,7 +4419,7 @@ void ShowUserGuide() { ImGui::BulletText("Click legend label icons to show/hide plot items."); } -void ShowAxisMetrics(ImPlotAxis* axis, bool show_axis_rects) { +void ShowAxisMetrics(ImPlotAxis* axis) { ImGui::Bullet(); ImGui::Text("Flags: %d", axis->Flags); ImGui::Bullet(); ImGui::Text("Range: [%f,%f]",axis->Range.Min, axis->Range.Max); ImGui::Bullet(); ImGui::Text("Pixels: %f", axis->Pixels); @@ -3924,16 +4431,16 @@ void ShowAxisMetrics(ImPlotAxis* axis, bool show_axis_rects) { ImGui::Bullet(); ImGui::Text("HasRange: %s", axis->HasRange ? "true" : "false"); ImGui::Bullet(); ImGui::Text("LinkedMin: %p", (void*)axis->LinkedMin); ImGui::Bullet(); ImGui::Text("LinkedMax: %p", (void*)axis->LinkedMax); - if (show_axis_rects) { - ImDrawList& fg = *ImGui::GetForegroundDrawList(); - fg.AddRect(axis->HoverRect.Min, axis->HoverRect.Max, IM_COL32(0,255,0,255)); - } } void ShowMetricsWindow(bool* p_popen) { static bool show_plot_rects = false; static bool show_axes_rects = false; + static bool show_canvas_rects = false; + static bool show_frame_rects = false; + static bool show_subplot_frame_rects = false; + static bool show_subplot_grid_rects = false; ImDrawList& fg = *ImGui::GetForegroundDrawList(); @@ -3950,23 +4457,54 @@ void ShowMetricsWindow(bool* p_popen) { ImGui::SameLine(); if (ImGui::Button("Bust Item Cache")) BustItemCache(); - ImGui::Checkbox("Show Plot Rects", &show_plot_rects); - ImGui::Checkbox("Show Axes Rects", &show_axes_rects); + ImGui::Checkbox("Show Frame Rects", &show_frame_rects); + ImGui::Checkbox("Show Canvas Rects",&show_canvas_rects); + ImGui::Checkbox("Show Plot Rects", &show_plot_rects); + ImGui::Checkbox("Show Axes Rects", &show_axes_rects); + ImGui::Checkbox("Show Subplot Frame Rects", &show_subplot_frame_rects); + ImGui::Checkbox("Show Subplot Grid Rects", &show_subplot_grid_rects); + ImGui::TreePop(); } const int n_plots = gp.Plots.GetBufSize(); + const int n_subplots = gp.Subplots.GetBufSize(); + // render rects + for (int p = 0; p < n_plots; ++p) { + ImPlotPlot* plot = gp.Plots.GetByIndex(p); + if (show_frame_rects) + fg.AddRect(plot->FrameRect.Min, plot->FrameRect.Max, IM_COL32(255,0,255,255)); + if (show_canvas_rects) + fg.AddRect(plot->CanvasRect.Min, plot->CanvasRect.Max, IM_COL32(0,255,255,255)); + if (show_plot_rects) + fg.AddRect(plot->PlotRect.Min, plot->PlotRect.Max, IM_COL32(255,255,0,255)); + if (show_axes_rects) { + fg.AddRect(plot->XAxis.HoverRect.Min, plot->XAxis.HoverRect.Max, IM_COL32(0,255,0,255)); + fg.AddRect(plot->YAxis[0].HoverRect.Min, plot->YAxis[0].HoverRect.Max, IM_COL32(0,255,0,255)); + if (ImHasFlag(plot->Flags, ImPlotFlags_YAxis2)) + fg.AddRect(plot->YAxis[1].HoverRect.Min, plot->YAxis[1].HoverRect.Max, IM_COL32(0,255,0,255)); + if (ImHasFlag(plot->Flags, ImPlotFlags_YAxis3)) + fg.AddRect(plot->YAxis[3].HoverRect.Min, plot->YAxis[2].HoverRect.Max, IM_COL32(0,255,0,255)); + } + } + for (int p = 0; p < n_subplots; ++p) { + ImPlotSubplot* subplot = gp.Subplots.GetByIndex(p); + if (show_subplot_frame_rects) + fg.AddRect(subplot->FrameRect.Min, subplot->FrameRect.Max, IM_COL32(255,0,0,255)); + if (show_subplot_grid_rects) + fg.AddRect(subplot->GridRect.Min, subplot->GridRect.Max, IM_COL32(0,0,255,255)); + } if (ImGui::TreeNode("Plots","Plots (%d)", n_plots)) { for (int p = 0; p < n_plots; ++p) { // plot ImPlotPlot* plot = gp.Plots.GetByIndex(p); ImGui::PushID(p); - if (ImGui::TreeNode("Plot", "Plot [ID=%u]", plot->ID)) { - int n_items = plot->Items.GetBufSize(); + if (ImGui::TreeNode("Plot", "Plot [ID=0x%08X]", plot->ID)) { + int n_items = plot->Items.GetItemCount(); if (ImGui::TreeNode("Items", "Items (%d)", n_items)) { for (int i = 0; i < n_items; ++i) { - ImPlotItem* item = plot->Items.GetByIndex(i); + ImPlotItem* item = plot->Items.GetItemByIndex(i); ImGui::PushID(i); - if (ImGui::TreeNode("Item", "Item [ID=%u]", item->ID)) { + if (ImGui::TreeNode("Item", "Item [ID=0x%08X]", item->ID)) { ImGui::Bullet(); ImGui::Checkbox("Show", &item->Show); ImGui::Bullet(); ImVec4 temp = ImGui::ColorConvertU32ToFloat4(item->Color); @@ -3974,7 +4512,7 @@ void ShowMetricsWindow(bool* p_popen) { item->Color = ImGui::ColorConvertFloat4ToU32(temp); ImGui::Bullet(); ImGui::Text("NameOffset: %d",item->NameOffset); - ImGui::Bullet(); ImGui::Text("Name: %s", item->NameOffset != -1 ? plot->LegendData.Labels.Buf.Data + item->NameOffset : "N/A"); + ImGui::Bullet(); ImGui::Text("Name: %s", item->NameOffset != -1 ? plot->Items.Legend.Labels.Buf.Data + item->NameOffset : "N/A"); ImGui::Bullet(); ImGui::Text("Hovered: %s",item->LegendHovered ? "true" : "false"); ImGui::TreePop(); } @@ -3983,22 +4521,22 @@ void ShowMetricsWindow(bool* p_popen) { ImGui::TreePop(); } if (ImGui::TreeNode("X-Axis")) { - ShowAxisMetrics(&plot->XAxis, show_axes_rects); + ShowAxisMetrics(&plot->XAxis); ImGui::TreePop(); } if (ImGui::TreeNode("Y-Axis")) { - ShowAxisMetrics(&plot->YAxis[0], show_axes_rects); + ShowAxisMetrics(&plot->YAxis[0]); ImGui::TreePop(); } if (ImHasFlag(plot->Flags, ImPlotFlags_YAxis2) && ImGui::TreeNode("Y-Axis 2")) { - ShowAxisMetrics(&plot->YAxis[1], show_axes_rects); + ShowAxisMetrics(&plot->YAxis[1]); ImGui::TreePop(); } if (ImHasFlag(plot->Flags, ImPlotFlags_YAxis3) && ImGui::TreeNode("Y-Axis 3")) { - ShowAxisMetrics(&plot->YAxis[2], show_axes_rects); + ShowAxisMetrics(&plot->YAxis[2]); ImGui::TreePop(); } - ImGui::Bullet(); ImGui::Text("Flags: %d", plot->Flags); + ImGui::Bullet(); ImGui::Text("Flags: 0x%08X", plot->Flags); ImGui::Bullet(); ImGui::Text("Initialized: %s", plot->Initialized ? "true" : "false"); ImGui::Bullet(); ImGui::Text("Selecting: %s", plot->Selecting ? "true" : "false"); ImGui::Bullet(); ImGui::Text("Selected: %s", plot->Selected ? "true" : "false"); @@ -4006,10 +4544,45 @@ void ShowMetricsWindow(bool* p_popen) { ImGui::Bullet(); ImGui::Text("Queried: %s", plot->Queried ? "true" : "false"); ImGui::Bullet(); ImGui::Text("FrameHovered: %s", plot->FrameHovered ? "true" : "false"); ImGui::Bullet(); ImGui::Text("PlotHovered: %s", plot->PlotHovered ? "true" : "false"); - ImGui::Bullet(); ImGui::Text("LegendHovered: %s", plot->LegendHovered ? "true" : "false"); + ImGui::Bullet(); ImGui::Text("LegendHovered: %s", plot->Items.Legend.Hovered ? "true" : "false"); + ImGui::TreePop(); + } + ImGui::PopID(); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Subplots","Subplots (%d)", n_subplots)) { + for (int p = 0; p < n_subplots; ++p) { + // plot + ImPlotSubplot* plot = gp.Subplots.GetByIndex(p); + ImGui::PushID(p); + if (ImGui::TreeNode("Subplot", "Subplot [ID=0x%08X]", plot->ID)) { + int n_items = plot->Items.GetItemCount(); + if (ImGui::TreeNode("Items", "Items (%d)", n_items)) { + for (int i = 0; i < n_items; ++i) { + ImPlotItem* item = plot->Items.GetItemByIndex(i); + ImGui::PushID(i); + if (ImGui::TreeNode("Item", "Item [ID=0x%08X]", item->ID)) { + ImGui::Bullet(); ImGui::Checkbox("Show", &item->Show); + ImGui::Bullet(); + ImVec4 temp = ImGui::ColorConvertU32ToFloat4(item->Color); + if (ImGui::ColorEdit4("Color",&temp.x, ImGuiColorEditFlags_NoInputs)) + item->Color = ImGui::ColorConvertFloat4ToU32(temp); + + ImGui::Bullet(); ImGui::Text("NameOffset: %d",item->NameOffset); + ImGui::Bullet(); ImGui::Text("Name: %s", item->NameOffset != -1 ? plot->Items.Legend.Labels.Buf.Data + item->NameOffset : "N/A"); + ImGui::Bullet(); ImGui::Text("Hovered: %s",item->LegendHovered ? "true" : "false"); + ImGui::TreePop(); + } + ImGui::PopID(); + } + ImGui::TreePop(); + } + ImGui::Bullet(); ImGui::Text("Flags: 0x%08X", plot->Flags); + ImGui::Bullet(); ImGui::Text("FrameHovered: %s", plot->FrameHovered ? "true" : "false"); + ImGui::Bullet(); ImGui::Text("LegendHovered: %s", plot->Items.Legend.Hovered ? "true" : "false"); ImGui::TreePop(); - if (show_plot_rects) - fg.AddRect(plot->PlotRect.Min, plot->PlotRect.Max, IM_COL32(255,255,0,255)); } ImGui::PopID(); } diff --git a/implot.h b/implot.h index e486748..830c43f 100644 --- a/implot.h +++ b/implot.h @@ -52,18 +52,19 @@ struct ImPlotContext; // ImPlot context (opaque struct, see implot_internal.h) // Enums/Flags -typedef int ImPlotFlags; // -> enum ImPlotFlags_ -typedef int ImPlotAxisFlags; // -> enum ImPlotAxisFlags_ -typedef int ImPlotCol; // -> enum ImPlotCol_ -typedef int ImPlotStyleVar; // -> enum ImPlotStyleVar_ -typedef int ImPlotMarker; // -> enum ImPlotMarker_ -typedef int ImPlotColormap; // -> enum ImPlotColormap_ -typedef int ImPlotLocation; // -> enum ImPlotLocation_ -typedef int ImPlotOrientation; // -> enum ImPlotOrientation_ -typedef int ImPlotYAxis; // -> enum ImPlotYAxis_; -typedef int ImPlotBin; // -> enum ImPlotBin_ +typedef int ImPlotFlags; // -> enum ImPlotFlags_ +typedef int ImPlotAxisFlags; // -> enum ImPlotAxisFlags_ +typedef int ImPlotSubplotFlags; // -> enum ImPlotSubplotFlags_ +typedef int ImPlotCol; // -> enum ImPlotCol_ +typedef int ImPlotStyleVar; // -> enum ImPlotStyleVar_ +typedef int ImPlotMarker; // -> enum ImPlotMarker_ +typedef int ImPlotColormap; // -> enum ImPlotColormap_ +typedef int ImPlotLocation; // -> enum ImPlotLocation_ +typedef int ImPlotOrientation; // -> enum ImPlotOrientation_ +typedef int ImPlotYAxis; // -> enum ImPlotYAxis_; +typedef int ImPlotBin; // -> enum ImPlotBin_ -// Options for plots. +// Options for plots (see BeginPlot). enum ImPlotFlags_ { ImPlotFlags_None = 0, // default ImPlotFlags_NoTitle = 1 << 0, // the plot title will not be displayed (titles are also hidden if preceeded by double hashes, e.g. "##MyPlot") @@ -82,7 +83,7 @@ enum ImPlotFlags_ { ImPlotFlags_CanvasOnly = ImPlotFlags_NoTitle | ImPlotFlags_NoLegend | ImPlotFlags_NoMenus | ImPlotFlags_NoBoxSelect | ImPlotFlags_NoMousePos }; -// Options for plot axes (X and Y). +// Options for plot axes (see BeginPlot). enum ImPlotAxisFlags_ { ImPlotAxisFlags_None = 0, // default ImPlotAxisFlags_NoLabel = 1 << 0, // the axis label will not be displayed (axis labels also hidden if the supplied string name is NULL) @@ -102,6 +103,22 @@ enum ImPlotAxisFlags_ { ImPlotAxisFlags_NoDecorations = ImPlotAxisFlags_NoLabel | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoTickMarks | ImPlotAxisFlags_NoTickLabels }; +// Options for subplots (see BeginSubplot). +enum ImPlotSubplotFlags_ { + ImPlotSubplotFlags_None = 0, // default + ImPlotSubplotFlags_NoTitle = 1 << 0, // the subplot title will not be displayed (titles are also hidden if preceeded by double hashes, e.g. "##MySubplot") + ImPlotSubplotFlags_NoLegend = 1 << 1, // the legend will not be displayed (only applicable if ImPlotSubplotFlags_ShareItems is enabled) + ImPlotSubplotFlags_NoMenus = 1 << 2, // the user will not be able to open context menus with right-click + ImPlotSubplotFlags_NoResize = 1 << 3, // resize splitters between subplot cells will be not be provided + ImPlotSubplotFlags_NoAlign = 1 << 4, // subplot edges will not be aligned vertically or horizontally + ImPlotSubplotFlags_ShareItems = 1 << 5, // items across all subplots will be shared and rendered into a single legend entry + ImPlotSubplotFlags_LinkRows = 1 << 6, // link the y-axis limits of all plots in each row (does not apply auxiliary y-axes) + ImPlotSubplotFlags_LinkCols = 1 << 7, // link the x-axis limits of all plots in each column + ImPlotSubplotFlags_LinkAllX = 1 << 8, // link the x-axis limits in every plot in the subplot + ImPlotSubplotFlags_LinkAllY = 1 << 9 , // link the y-axis limits in every plot in the subplot (does not apply to auxiliary y-axes) + ImPlotSubplotFlags_ColMajor = 1 << 10 // subplots are added in column major order instead of the default row major order +}; + // Plot styling colors. enum ImPlotCol_ { // item styling colors @@ -301,7 +318,7 @@ struct ImPlotStyle { ImVec2 AnnotationPadding; // = 2,2 text padding around annotation labels ImVec2 FitPadding; // = 0,0 additional fit padding as a percentage of the fit extents (e.g. ImVec2(0.1f,0.1f) adds 10% to the fit extents of X and Y) ImVec2 PlotDefaultSize; // = 400,300 default size used when ImVec2(0,0) is passed to BeginPlot - ImVec2 PlotMinSize; // = 300,225 minimum size plot frame can be when shrunk + ImVec2 PlotMinSize; // = 200,150 minimum size plot frame can be when shrunk // style colors ImVec4 Colors[ImPlotCol_COUNT]; // Array of styling colors. Indexable with ImPlotCol_ enums. // colormap @@ -380,6 +397,67 @@ IMPLOT_API bool BeginPlot(const char* title_id, // of an if statement conditioned on BeginPlot(). See example above. IMPLOT_API void EndPlot(); +//----------------------------------------------------------------------------- +// Begin/EndSubplots +//----------------------------------------------------------------------------- + +// Starts a subdivided plotting context. If the function returns true, +// EndSubplots() MUST be called! Call BeginPlot/EndPlot AT MOST [rows*cols] +// times in between the begining and end of the subplot context. Plots are +// added in row major order. +// +// Example: +// +// if (BeginSubplots("My Subplot",2,3,ImVec2(800,400)) { +// for (int i = 0; i < 6; ++i) { +// if (BeginPlot(...)) { +// ImPlot::PlotLine(...); +// ... +// EndPlot(); +// } +// } +// EndSubplots(); +// } +// +// Procudes: +// +// [0][1][2] +// [3][4][5] +// +// Important notes: +// +// - #title_id must be unique to the current ImGui ID scope. If you need to avoid ID +// collisions or don't want to display a title in the plot, use double hashes +// (e.g. "MyPlot##HiddenIdText" or "##NoTitle"). +// - #rows and #cols must be greater than 0. +// - #size is the size of the entire grid of subplots, not the individual plots +// - #row_ratios and #col_ratios must have AT LEAST #rows and #cols elements, +// respectively. These are the sizes of the rows and columns expressed in ratios. +// If the user adjusts the dimensions, the arrays are updated with new ratios. +// +// Important notes regarding BeginPlot from inside of BeginSubplots: +// +// - The #title_id parameter of _BeginPlot_ (see above) does NOT have to be +// unique when called inside of a subplot context. Subplot IDs are hashed +// for your convenience so you don't have call PushID or generate unique title +// strings. Simply pass an empty string to BeginPlot unless you want to title +// each subplot. +// - The #size parameter of _BeginPlot_ (see above) is ignored when inside of a +// subplot context. The actual size of the subplot will be based on the +// #size value you pass to _BeginSubplots_ and #row/#col_ratios if provided. + +IMPLOT_API bool BeginSubplots(const char* title_id, + int rows, + int cols, + const ImVec2& size, + ImPlotSubplotFlags flags = ImPlotSubplotFlags_None, + float* row_ratios = NULL, + float* col_ratios = NULL); + +// Only call EndSubplots() if BeginSubplots() returns true! Typically called at the end +// of an if statement conditioned on BeginSublots(). See example above. +IMPLOT_API void EndSubplots(); + //----------------------------------------------------------------------------- // Plot Items //----------------------------------------------------------------------------- @@ -573,6 +651,19 @@ IMPLOT_API ImPlotLimits GetPlotQuery(ImPlotYAxis y_axis = IMPLOT_AUTO); // Set the current plot query bounds. Query must be enabled with ImPlotFlags_Query. IMPLOT_API void SetPlotQuery(const ImPlotLimits& query, ImPlotYAxis y_axis = IMPLOT_AUTO); +//----------------------------------------------------------------------------- +// Algined Plots +//----------------------------------------------------------------------------- + +// Consider using Begin/EndSubplots first. They are more feature rich and +// accomplish the same behaviour by default. The functions below offer lower +// level control of plot alignment. + +// Align axis padding over multiple plots in a single row or column. If this function returns true, EndAlignedPlots() must be called. #group_id must be unique. +IMPLOT_API bool BeginAlignedPlots(const char* group_id, ImPlotOrientation orientation = ImPlotOrientation_Vertical); +// Only call EndAlignedPlots() if BeginAlignedPlots() returns true! +IMPLOT_API void EndAlignedPlots(); + //----------------------------------------------------------------------------- // Plot Tools //----------------------------------------------------------------------------- @@ -604,7 +695,7 @@ IMPLOT_API bool DragPoint(const char* id, double* x, double* y, bool show_label // The following functions MUST be called BETWEEN Begin/EndPlot! -// Set the location of the current plot's legend (default = North|West). +// Set the location of the current plot's (or subplot's) legend. IMPLOT_API void SetLegendLocation(ImPlotLocation location, ImPlotOrientation orientation = ImPlotOrientation_Vertical, bool outside = false); // Set the location of the current plot's mouse position text (default = South|East). IMPLOT_API void SetMousePosLocation(ImPlotLocation location); diff --git a/implot_demo.cpp b/implot_demo.cpp index 0a0a37a..6aa05fe 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -174,8 +174,1520 @@ struct HugeTimeData { static const int Size = 60*60*24*366; }; +//----------------------------------------------------------------------------- +// DEMOS +//----------------------------------------------------------------------------- + +void ShowDemo_Help() { + ImGui::Text("ABOUT THIS DEMO:"); + ImGui::BulletText("Sections below are demonstrating many aspects of the library."); + ImGui::BulletText("The \"Tools\" menu above gives access to: Style Editors (ImPlot/ImGui)\n" + "and Metrics (general purpose Dear ImGui debugging tool)."); + ImGui::Separator(); + ImGui::Text("PROGRAMMER GUIDE:"); + ImGui::BulletText("See the ShowDemoWindow() code in implot_demo.cpp. <- you are here!"); + ImGui::BulletText("By default, anti-aliased lines are turned OFF."); + ImGui::Indent(); + ImGui::BulletText("Software AA can be enabled globally with ImPlotStyle.AntiAliasedLines."); + ImGui::BulletText("Software AA can be enabled per plot with ImPlotFlags_AntiAliased."); + ImGui::BulletText("AA for plots can be toggled from the plot's context menu."); + ImGui::BulletText("If permitable, you are better off using hardware AA (e.g. MSAA)."); + ImGui::Unindent(); + ImGui::BulletText("If you see visual artifacts, do one of the following:"); + ImGui::Indent(); + ImGui::BulletText("Handle ImGuiBackendFlags_RendererHasVtxOffset for 16-bit indices in your backend."); + ImGui::BulletText("Or, enable 32-bit indices in imconfig.h."); + ImGui::BulletText("Your current configuration is:"); + ImGui::Indent(); + ImGui::BulletText("ImDrawIdx: %d-bit", (int)(sizeof(ImDrawIdx) * 8)); + ImGui::BulletText("ImGuiBackendFlags_RendererHasVtxOffset: %s", (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ? "True" : "False"); + ImGui::Unindent(); + ImGui::Unindent(); + ImGui::Separator(); + ImGui::Text("USER GUIDE:"); + ShowUserGuide(); +} + +//----------------------------------------------------------------------------- + +void ShowDemo_Configuration() { + ImGui::ShowFontSelector("Font"); + ImGui::ShowStyleSelector("ImGui Style"); + ImPlot::ShowStyleSelector("ImPlot Style"); + ImPlot::ShowColormapSelector("ImPlot Colormap"); + float indent = ImGui::CalcItemWidth() - ImGui::GetFrameHeight(); + ImGui::Separator(); + ImGui::Checkbox("Anti-Aliased Lines", &ImPlot::GetStyle().AntiAliasedLines); + ImGui::Separator(); + ImGui::Checkbox("Use Local Time", &ImPlot::GetStyle().UseLocalTime); + ImGui::Checkbox("Use ISO 8601", &ImPlot::GetStyle().UseISO8601); + ImGui::Checkbox("Use 24 Hour Clock", &ImPlot::GetStyle().Use24HourClock); + ImGui::Unindent(indent); +} + +//----------------------------------------------------------------------------- + +void ShowDemo_LinePlots() { + static float xs1[1001], ys1[1001]; + for (int i = 0; i < 1001; ++i) { + xs1[i] = i * 0.001f; + ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)ImGui::GetTime() / 10)); + } + static double xs2[11], ys2[11]; + for (int i = 0; i < 11; ++i) { + xs2[i] = i * 0.1f; + ys2[i] = xs2[i] * xs2[i]; + } + ImGui::BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help)."); + if (ImPlot::BeginPlot("Line Plot", "x", "f(x)")) { + ImPlot::PlotLine("sin(x)", xs1, ys1, 1001); + ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); + ImPlot::PlotLine("x^2", xs2, ys2, 11); + ImPlot::EndPlot(); + } +} + +//----------------------------------------------------------------------------- + +void ShowDemo_FilledLinePlots() { + static double xs1[101], ys1[101], ys2[101], ys3[101]; + srand(0); + for (int i = 0; i < 101; ++i) { + xs1[i] = (float)i; + ys1[i] = RandomRange(400.0,450.0); + ys2[i] = RandomRange(275.0,350.0); + ys3[i] = RandomRange(150.0,225.0); + } + static bool show_lines = true; + static bool show_fills = true; + static float fill_ref = 0; + static int shade_mode = 0; + ImGui::Checkbox("Lines",&show_lines); ImGui::SameLine(); + ImGui::Checkbox("Fills",&show_fills); + if (show_fills) { + ImGui::SameLine(); + if (ImGui::RadioButton("To -INF",shade_mode == 0)) + shade_mode = 0; + ImGui::SameLine(); + if (ImGui::RadioButton("To +INF",shade_mode == 1)) + shade_mode = 1; + ImGui::SameLine(); + if (ImGui::RadioButton("To Ref",shade_mode == 2)) + shade_mode = 2; + if (shade_mode == 2) { + ImGui::SameLine(); + ImGui::SetNextItemWidth(100); + ImGui::DragFloat("##Ref",&fill_ref, 1, -100, 500); + } + } + + ImPlot::SetNextPlotLimits(0,100,0,500); + if (ImPlot::BeginPlot("Stock Prices", "Days", "Price")) { + if (show_fills) { + ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); + ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref); + ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref); + ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref); + ImPlot::PopStyleVar(); + } + if (show_lines) { + ImPlot::PlotLine("Stock 1", xs1, ys1, 101); + ImPlot::PlotLine("Stock 2", xs1, ys2, 101); + ImPlot::PlotLine("Stock 3", xs1, ys3, 101); + } + ImPlot::EndPlot(); + } +} + +//----------------------------------------------------------------------------- + +void ShowDemo_ShadedPlots() { + static float xs[1001], ys[1001], ys1[1001], ys2[1001], ys3[1001], ys4[1001]; + srand(0); + for (int i = 0; i < 1001; ++i) { + xs[i] = i * 0.001f; + ys[i] = 0.25f + 0.25f * sinf(25 * xs[i]) * sinf(5 * xs[i]) + RandomRange(-0.01f, 0.01f); + ys1[i] = ys[i] + RandomRange(0.1f, 0.12f); + ys2[i] = ys[i] - RandomRange(0.1f, 0.12f); + ys3[i] = 0.75f + 0.2f * sinf(25 * xs[i]); + ys4[i] = 0.75f + 0.1f * cosf(25 * xs[i]); + } + static float alpha = 0.25f; + ImGui::DragFloat("Alpha",&alpha,0.01f,0,1); + + if (ImPlot::BeginPlot("Shaded Plots", "X-Axis", "Y-Axis")) { + ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha); + ImPlot::PlotShaded("Uncertain Data",xs,ys1,ys2,1001); + ImPlot::PlotLine("Uncertain Data", xs, ys, 1001); + ImPlot::PlotShaded("Overlapping",xs,ys3,ys4,1001); + ImPlot::PlotLine("Overlapping",xs,ys3,1001); + ImPlot::PlotLine("Overlapping",xs,ys4,1001); + ImPlot::PopStyleVar(); + ImPlot::EndPlot(); + } +} + +//----------------------------------------------------------------------------- + +void ShowDemo_ScatterPlots() { + srand(0); + static float xs1[100], ys1[100]; + for (int i = 0; i < 100; ++i) { + xs1[i] = i * 0.01f; + ys1[i] = xs1[i] + 0.1f * ((float)rand() / (float)RAND_MAX); + } + static float xs2[50], ys2[50]; + for (int i = 0; i < 50; i++) { + xs2[i] = 0.25f + 0.2f * ((float)rand() / (float)RAND_MAX); + ys2[i] = 0.75f + 0.2f * ((float)rand() / (float)RAND_MAX); + } + + if (ImPlot::BeginPlot("Scatter Plot", NULL, NULL)) { + ImPlot::PlotScatter("Data 1", xs1, ys1, 100); + ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); + ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, ImVec4(0,1,0,0.5f), IMPLOT_AUTO, ImVec4(0,1,0,1)); + ImPlot::PlotScatter("Data 2", xs2, ys2, 50); + ImPlot::PopStyleVar(); + ImPlot::EndPlot(); + } +} + +//----------------------------------------------------------------------------- + +void ShowDemo_StairstepPlots() { + static float ys1[101], ys2[101]; + for (int i = 0; i < 101; ++i) { + ys1[i] = 0.5f + 0.4f * sinf(50 * i * 0.01f); + ys2[i] = 0.5f + 0.2f * sinf(25 * i * 0.01f); + } + if (ImPlot::BeginPlot("Stairstep Plot", "x", "f(x)")) { + ImPlot::PlotStairs("Signal 1", ys1, 101, 0.01f); + ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 2.0f); + ImPlot::PlotStairs("Signal 2", ys2, 101, 0.01f); + ImPlot::EndPlot(); + } +} + +//----------------------------------------------------------------------------- + +void ShowDemo_BarPlots() { + static bool horz = false; + static ImS8 midtm[10] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90}; + static ImS16 final[10] = {80, 62, 56, 99, 55, 78, 88, 78, 90, 100}; + static ImS32 grade[10] = {80, 69, 52, 92, 72, 78, 75, 76, 89, 95}; + + static const char* labels[] = {"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10"}; + static const double positions[] = {0,1,2,3,4,5,6,7,8,9}; + + ImGui::Checkbox("Horizontal",&horz); + + if (horz) { + ImPlot::SetNextPlotLimits(0, 110, -0.5, 9.5, ImGuiCond_Always); + ImPlot::SetNextPlotTicksY(positions, 10, labels); + } + else { + ImPlot::SetNextPlotLimits(-0.5, 9.5, 0, 110, ImGuiCond_Always); + ImPlot::SetNextPlotTicksX(positions, 10, labels); + } + if (ImPlot::BeginPlot("Bar Plot", horz ? "Score" : "Student", horz ? "Student" : "Score", + ImVec2(-1,0), 0, 0, horz ? ImPlotAxisFlags_Invert : 0)) + { + if (horz) { + ImPlot::SetLegendLocation(ImPlotLocation_West, ImPlotOrientation_Vertical); + ImPlot::PlotBarsH("Midterm Exam", midtm, 10, 0.2, -0.2); + ImPlot::PlotBarsH("Final Exam", final, 10, 0.2, 0); + ImPlot::PlotBarsH("Course Grade", grade, 10, 0.2, 0.2); + } + else { + ImPlot::SetLegendLocation(ImPlotLocation_South, ImPlotOrientation_Horizontal); + ImPlot::PlotBars("Midterm Exam", midtm, 10, 0.2, -0.2); + ImPlot::PlotBars("Final Exam", final, 10, 0.2, 0); + ImPlot::PlotBars("Course Grade", grade, 10, 0.2, 0.2); + } + ImPlot::EndPlot(); + } +} + +//----------------------------------------------------------------------------- + +void ShowDemo_ErrorBars() { + static float xs[5] = {1,2,3,4,5}; + static float bar[5] = {1,2,5,3,4}; + static float lin1[5] = {8,8,9,7,8}; + static float lin2[5] = {6,7,6,9,6}; + static float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f}; + static float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f}; + static float err3[5] = {0.09f, 0.14f, 0.09f, 0.12f, 0.16f}; + static float err4[5] = {0.02f, 0.08f, 0.15f, 0.05f, 0.2f}; + + + ImPlot::SetNextPlotLimits(0, 6, 0, 10); + if (ImPlot::BeginPlot("##ErrorBars",NULL,NULL)) { + + ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f); + ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5); + + ImPlot::SetNextErrorBarStyle(ImPlot::GetColormapColor(1), 0); + ImPlot::PlotErrorBars("Line", xs, lin1, err1, err2, 5); + ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); + ImPlot::PlotLine("Line", xs, lin1, 5); + + ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(2)); + ImPlot::PlotErrorBars("Scatter", xs, lin2, err2, 5); + ImPlot::PlotErrorBarsH("Scatter", xs, lin2, err3, err4, 5); + ImPlot::PopStyleColor(); + ImPlot::PlotScatter("Scatter", xs, lin2, 5); + + ImPlot::EndPlot(); + } +} + +void ShowDemo_StemPlots() { + static double xs[51], ys1[51], ys2[51]; + for (int i = 0; i < 51; ++i) { + xs[i] = i * 0.02; + ys1[i] = 1.0 + 0.5 * sin(25*xs[i])*cos(2*xs[i]); + ys2[i] = 0.5 + 0.25 * sin(10*xs[i]) * sin(xs[i]); + } + ImPlot::SetNextPlotLimits(0,1,0,1.6); + if (ImPlot::BeginPlot("Stem Plots")) { + ImPlot::PlotStems("Stems 1",xs,ys1,51); + ImPlot::SetNextLineStyle(ImVec4(1,0.5f,0,0.75f)); + ImPlot::SetNextMarkerStyle(ImPlotMarker_Square,5,ImVec4(1,0.5f,0,0.25f)); + ImPlot::PlotStems("Stems 2", xs, ys2,51); + ImPlot::EndPlot(); + } +} + +void ShowDemo_InfiniteLines() { + static double vals[] = {0.25, 0.5, 0.75}; + if (ImPlot::BeginPlot("##Infinite",0,0,ImVec2(-1,0),0,ImPlotAxisFlags_NoInitialFit,ImPlotAxisFlags_NoInitialFit)) { + ImPlot::PlotVLines("VLines",vals,3); + ImPlot::PlotHLines("HLines",vals,3); + ImPlot::EndPlot(); + } +} + +void ShowDemo_PieCharts() { + static const char* labels1[] = {"Frogs","Hogs","Dogs","Logs"}; + static float data1[] = {0.15f, 0.30f, 0.2f, 0.05f}; + static bool normalize = false; + ImGui::SetNextItemWidth(250); + ImGui::DragFloat4("Values", data1, 0.01f, 0, 1); + if ((data1[0] + data1[1] + data1[2] + data1[3]) < 1) { + ImGui::SameLine(); + ImGui::Checkbox("Normalize", &normalize); + } + + ImPlot::SetNextPlotLimits(0,1,0,1,ImGuiCond_Always); + if (ImPlot::BeginPlot("##Pie1", NULL, NULL, ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMousePos, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) { + ImPlot::PlotPieChart(labels1, data1, 4, 0.5, 0.5, 0.4, normalize, "%.2f"); + ImPlot::EndPlot(); + } + + ImGui::SameLine(); + + static const char* labels2[] = {"A","B","C","D","E"}; + static int data2[] = {1,1,2,3,5}; + + ImPlot::PushColormap(ImPlotColormap_Pastel); + ImPlot::SetNextPlotLimits(0,1,0,1,ImGuiCond_Always); + if (ImPlot::BeginPlot("##Pie2", NULL, NULL, ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMousePos, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) { + ImPlot::PlotPieChart(labels2, data2, 5, 0.5, 0.5, 0.4, true, "%.0f", 180); + ImPlot::EndPlot(); + } + ImPlot::PopColormap(); +} + +void ShowDemo_Heatmaps() { + static float values1[7][7] = {{0.8f, 2.4f, 2.5f, 3.9f, 0.0f, 4.0f, 0.0f}, + {2.4f, 0.0f, 4.0f, 1.0f, 2.7f, 0.0f, 0.0f}, + {1.1f, 2.4f, 0.8f, 4.3f, 1.9f, 4.4f, 0.0f}, + {0.6f, 0.0f, 0.3f, 0.0f, 3.1f, 0.0f, 0.0f}, + {0.7f, 1.7f, 0.6f, 2.6f, 2.2f, 6.2f, 0.0f}, + {1.3f, 1.2f, 0.0f, 0.0f, 0.0f, 3.2f, 5.1f}, + {0.1f, 2.0f, 0.0f, 1.4f, 0.0f, 1.9f, 6.3f}}; + static float scale_min = 0; + static float scale_max = 6.3f; + static const char* xlabels[] = {"C1","C2","C3","C4","C5","C6","C7"}; + static const char* ylabels[] = {"R1","R2","R3","R4","R5","R6","R7"}; + + static ImPlotColormap map = ImPlotColormap_Viridis; + if (ImPlot::ColormapButton(ImPlot::GetColormapName(map),ImVec2(225,0),map)) { + map = (map + 1) % ImPlot::GetColormapCount(); + // We bust the color cache of our plots so that item colors will + // resample the new colormap in the event that they have already + // been created. See documentation in implot.h. + BustColorCache("##Heatmap1"); + BustColorCache("##Heatmap2"); + } + + ImGui::SameLine(); + ImGui::LabelText("##Colormap Index", "%s", "Change Colormap"); + ImGui::SetNextItemWidth(225); + ImGui::DragFloatRange2("Min / Max",&scale_min, &scale_max, 0.01f, -20, 20); + static ImPlotAxisFlags axes_flags = ImPlotAxisFlags_Lock | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoTickMarks; + + ImPlot::PushColormap(map); + SetNextPlotTicksX(0 + 1.0/14.0, 1 - 1.0/14.0, 7, xlabels); + SetNextPlotTicksY(1 - 1.0/14.0, 0 + 1.0/14.0, 7, ylabels); + if (ImPlot::BeginPlot("##Heatmap1",NULL,NULL,ImVec2(225,225),ImPlotFlags_NoLegend|ImPlotFlags_NoMousePos,axes_flags,axes_flags)) { + ImPlot::PlotHeatmap("heat",values1[0],7,7,scale_min,scale_max); + ImPlot::EndPlot(); + } + ImGui::SameLine(); + ImPlot::ColormapScale("##HeatScale",scale_min, scale_max, ImVec2(60,225)); + + ImGui::SameLine(); + + const int size = 200; + static double values2[size*size]; + srand((unsigned int)(ImGui::GetTime()*1000000)); + for (int i = 0; i < size*size; ++i) + values2[i] = RandomRange(0.0,1.0); + + ImPlot::SetNextPlotLimits(-1,1,-1,1); + if (ImPlot::BeginPlot("##Heatmap2",NULL,NULL,ImVec2(225,225),0,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations)) { + ImPlot::PlotHeatmap("heat1",values2,size,size,0,1,NULL); + ImPlot::PlotHeatmap("heat2",values2,size,size,0,1,NULL, ImPlotPoint(-1,-1), ImPlotPoint(0,0)); + ImPlot::EndPlot(); + } + ImPlot::PopColormap(); + +} + +void ShowDemo_Histogram() { + static int bins = 50; + static bool cumulative = false; + static bool density = true; + static bool outliers = true; + static double mu = 5; + static double sigma = 2; + + ImGui::SetNextItemWidth(200); + if (ImGui::RadioButton("Sqrt",bins==ImPlotBin_Sqrt)) { bins = ImPlotBin_Sqrt; } ImGui::SameLine(); + if (ImGui::RadioButton("Sturges",bins==ImPlotBin_Sturges)) { bins = ImPlotBin_Sturges; } ImGui::SameLine(); + if (ImGui::RadioButton("Rice",bins==ImPlotBin_Rice)) { bins = ImPlotBin_Rice; } ImGui::SameLine(); + if (ImGui::RadioButton("Scott",bins==ImPlotBin_Scott)) { bins = ImPlotBin_Scott; } ImGui::SameLine(); + if (ImGui::RadioButton("N Bins",bins>=0)) bins = 50; + if (bins>=0) { + ImGui::SameLine(); + ImGui::SetNextItemWidth(200); + ImGui::SliderInt("##Bins", &bins, 1, 100); + } + if (ImGui::Checkbox("Density", &density)) + ImPlot::FitNextPlotAxes(); + ImGui::SameLine(); + if (ImGui::Checkbox("Cumulative", &cumulative)) + ImPlot::FitNextPlotAxes(); + ImGui::SameLine(); + static bool range = false; + ImGui::Checkbox("Range", &range); + static float rmin = -3; + static float rmax = 13; + if (range) { + ImGui::SameLine(); + ImGui::SetNextItemWidth(200); + ImGui::DragFloat2("##Range",&rmin,0.1f,-3,13); + ImGui::SameLine(); + ImGui::Checkbox("Outliers",&outliers); + } + + static NormalDistribution<10000> dist(mu, sigma); + static double x[100]; + static double y[100]; + if (density) { + for (int i = 0; i < 100; ++i) { + x[i] = -3 + 16 * (double)i/99.0; + y[i] = exp( - (x[i]-mu)*(x[i]-mu) / (2*sigma*sigma)) / (sigma * sqrt(2*3.141592653589793238)); + } + if (cumulative) { + for (int i = 1; i < 100; ++i) + y[i] += y[i-1]; + for (int i = 0; i < 100; ++i) + y[i] /= y[99]; + } + } + + if (ImPlot::BeginPlot("##Histograms")) { + ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.5f); + ImPlot::PlotHistogram("Empirical", dist.Data, 10000, bins, cumulative, density, range ? ImPlotRange(rmin,rmax) : ImPlotRange(), outliers); + if (density && outliers) + ImPlot::PlotLine("Theoretical",x,y,100); + ImPlot::EndPlot(); + } +} + +void ShowDemo_Histogram2D() { + static int count = 500000; + static int xybins[2] = {200,200}; + static bool density2 = false; + ImGui::SliderInt("Count",&count,100,500000); + ImGui::SliderInt2("Bins",xybins,1,500); + ImGui::SameLine(); + ImGui::Checkbox("Density##2",&density2); + static NormalDistribution<500000> dist1(1, 2); + static NormalDistribution<500000> dist2(1, 1); + double max_count = 0; + ImPlotAxisFlags flags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Foreground; + ImPlot::PushColormap("Hot"); + ImPlot::SetNextPlotLimits(-6,6,-6,6); + if (ImPlot::BeginPlot("##Hist2D",0,0,ImVec2(ImGui::GetContentRegionAvail().x-100-ImGui::GetStyle().ItemSpacing.x,0),0,flags,flags)) { + max_count = ImPlot::PlotHistogram2D("Hist2D",dist1.Data,dist2.Data,count,xybins[0],xybins[1],density2,ImPlotLimits(-6,6,-6,6)); + ImPlot::EndPlot(); + } + ImGui::SameLine(); + ImPlot::ColormapScale(density2 ? "Density" : "Count",0,max_count,ImVec2(100,0)); + ImPlot::PopColormap(); +} + +void ShowDemo_DigitalPlots() { + ImGui::BulletText("Digital plots do not respond to Y drag and zoom, so that"); + ImGui::Indent(); + ImGui::Text("you can drag analog plots over the rising/falling digital edge."); + ImGui::Unindent(); + + static bool paused = false; + static ScrollingBuffer dataDigital[2]; + static ScrollingBuffer dataAnalog[2]; + static bool showDigital[2] = {true, false}; + static bool showAnalog[2] = {true, false}; + + char label[32]; + ImGui::Checkbox("digital_0", &showDigital[0]); ImGui::SameLine(); + ImGui::Checkbox("digital_1", &showDigital[1]); ImGui::SameLine(); + ImGui::Checkbox("analog_0", &showAnalog[0]); ImGui::SameLine(); + ImGui::Checkbox("analog_1", &showAnalog[1]); + + static float t = 0; + if (!paused) { + t += ImGui::GetIO().DeltaTime; + //digital signal values + if (showDigital[0]) + dataDigital[0].AddPoint(t, sinf(2*t) > 0.45); + if (showDigital[1]) + dataDigital[1].AddPoint(t, sinf(2*t) < 0.45); + //Analog signal values + if (showAnalog[0]) + dataAnalog[0].AddPoint(t, sinf(2*t)); + if (showAnalog[1]) + dataAnalog[1].AddPoint(t, cosf(2*t)); + } + ImPlot::SetNextPlotLimitsY(-1, 1); + ImPlot::SetNextPlotLimitsX(t - 10.0, t, paused ? ImGuiCond_Once : ImGuiCond_Always); + if (ImPlot::BeginPlot("##Digital")) { + for (int i = 0; i < 2; ++i) { + if (showDigital[i] && dataDigital[i].Data.size() > 0) { + sprintf(label, "digital_%d", i); + ImPlot::PlotDigital(label, &dataDigital[i].Data[0].x, &dataDigital[i].Data[0].y, dataDigital[i].Data.size(), dataDigital[i].Offset, 2 * sizeof(float)); + } + } + for (int i = 0; i < 2; ++i) { + if (showAnalog[i]) { + sprintf(label, "analog_%d", i); + if (dataAnalog[i].Data.size() > 0) + ImPlot::PlotLine(label, &dataAnalog[i].Data[0].x, &dataAnalog[i].Data[0].y, dataAnalog[i].Data.size(), dataAnalog[i].Offset, 2 * sizeof(float)); + } + } + ImPlot::EndPlot(); + } +} + +void ShowDemo_Images() { + ImGui::BulletText("Below we are displaying the font texture, which is the only texture we have\naccess to in this demo."); + ImGui::BulletText("Use the 'ImTextureID' type as storage to pass pointers or identifiers to your\nown texture data."); + ImGui::BulletText("See ImGui Wiki page 'Image Loading and Displaying Examples'."); + static ImVec2 bmin(0,0); + static ImVec2 bmax(1,1); + static ImVec2 uv0(0,0); + static ImVec2 uv1(1,1); + static ImVec4 tint(1,1,1,1); + ImGui::SliderFloat2("Min", &bmin.x, -2, 2, "%.1f"); + ImGui::SliderFloat2("Max", &bmax.x, -2, 2, "%.1f"); + ImGui::SliderFloat2("UV0", &uv0.x, -2, 2, "%.1f"); + ImGui::SliderFloat2("UV1", &uv1.x, -2, 2, "%.1f"); + ImGui::ColorEdit4("Tint",&tint.x); + if (ImPlot::BeginPlot("##image")) { + ImPlot::PlotImage("my image",ImGui::GetIO().Fonts->TexID, bmin, bmax, uv0, uv1, tint); + ImPlot::EndPlot(); + } +} + +void ShowDemo_RealtimePlots() { + ImGui::BulletText("Move your mouse to change the data!"); + ImGui::BulletText("This example assumes 60 FPS. Higher FPS requires larger buffer size."); + static ScrollingBuffer sdata1, sdata2; + static RollingBuffer rdata1, rdata2; + ImVec2 mouse = ImGui::GetMousePos(); + static float t = 0; + t += ImGui::GetIO().DeltaTime; + sdata1.AddPoint(t, mouse.x * 0.0005f); + rdata1.AddPoint(t, mouse.x * 0.0005f); + sdata2.AddPoint(t, mouse.y * 0.0005f); + rdata2.AddPoint(t, mouse.y * 0.0005f); + + static float history = 10.0f; + ImGui::SliderFloat("History",&history,1,30,"%.1f s"); + rdata1.Span = history; + rdata2.Span = history; + + static ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels; + ImPlot::SetNextPlotLimitsX(t - history, t, ImGuiCond_Always); + ImPlot::SetNextPlotLimitsY(0,1); + if (ImPlot::BeginPlot("##Scrolling", NULL, NULL, ImVec2(-1,150), 0, flags, flags)) { + ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL,0.5f); + ImPlot::PlotShaded("Mouse X", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), -INFINITY, sdata1.Offset, 2 * sizeof(float)); + ImPlot::PlotLine("Mouse Y", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), sdata2.Offset, 2*sizeof(float)); + ImPlot::EndPlot(); + } + ImPlot::SetNextPlotLimitsX(0, history, ImGuiCond_Always); + ImPlot::SetNextPlotLimitsY(0,1); + if (ImPlot::BeginPlot("##Rolling", NULL, NULL, ImVec2(-1,150), 0, flags, flags)) { + ImPlot::PlotLine("Mouse X", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 2 * sizeof(float)); + ImPlot::PlotLine("Mouse Y", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 2 * sizeof(float)); + ImPlot::EndPlot(); + } +} + +void ShowDemo_MarkersAndText() { + static float mk_size = ImPlot::GetStyle().MarkerSize; + static float mk_weight = ImPlot::GetStyle().MarkerWeight; + ImGui::DragFloat("Marker Size",&mk_size,0.1f,2.0f,10.0f,"%.2f px"); + ImGui::DragFloat("Marker Weight", &mk_weight,0.05f,0.5f,3.0f,"%.2f px"); + + ImPlot::SetNextPlotLimits(0, 10, 0, 12); + if (ImPlot::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,0), ImPlotFlags_CanvasOnly, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) { + ImS8 xs[2] = {1,4}; + ImS8 ys[2] = {10,11}; + + // filled markers + for (int m = 0; m < ImPlotMarker_COUNT; ++m) { + ImGui::PushID(m); + ImPlot::SetNextMarkerStyle(m, mk_size, IMPLOT_AUTO_COL, mk_weight); + ImPlot::PlotLine("##Filled", xs, ys, 2); + ImGui::PopID(); + ys[0]--; ys[1]--; + } + xs[0] = 6; xs[1] = 9; ys[0] = 10; ys[1] = 11; + // open markers + for (int m = 0; m < ImPlotMarker_COUNT; ++m) { + ImGui::PushID(m); + ImPlot::SetNextMarkerStyle(m, mk_size, ImVec4(0,0,0,0), mk_weight); + ImPlot::PlotLine("##Open", xs, ys, 2); + ImGui::PopID(); + ys[0]--; ys[1]--; + } + + ImPlot::PlotText("Filled Markers", 2.5f, 6.0f); + ImPlot::PlotText("Open Markers", 7.5f, 6.0f); + + ImPlot::PushStyleColor(ImPlotCol_InlayText, ImVec4(1,0,1,1)); + ImPlot::PlotText("Vertical Text", 5.0f, 6.0f, true); + ImPlot::PopStyleColor(); + + ImPlot::EndPlot(); + } +} + +void ShowDemo_LogAxes() { + static double xs[1001], ys1[1001], ys2[1001], ys3[1001]; + for (int i = 0; i < 1001; ++i) { + xs[i] = i*0.1f; + ys1[i] = sin(xs[i]) + 1; + ys2[i] = log(xs[i]); + ys3[i] = pow(10.0, xs[i]); + } + ImGui::BulletText("Open the plot context menu (right click) to change scales."); + + ImPlot::SetNextPlotLimits(0.1, 100, 0, 10); + if (ImPlot::BeginPlot("Log Plot", NULL, NULL, ImVec2(-1,0), 0, ImPlotAxisFlags_LogScale )) { + ImPlot::PlotLine("f(x) = x", xs, xs, 1001); + ImPlot::PlotLine("f(x) = sin(x)+1", xs, ys1, 1001); + ImPlot::PlotLine("f(x) = log(x)", xs, ys2, 1001); + ImPlot::PlotLine("f(x) = 10^x", xs, ys3, 21); + ImPlot::EndPlot(); + } +} + +void ShowDemo_TimeAxes() { + + static double t_min = 1609459200; // 01/01/2021 @ 12:00:00am (UTC) + static double t_max = 1640995200; // 01/01/2022 @ 12:00:00am (UTC) + + ImGui::BulletText("When ImPlotAxisFlags_Time is enabled on the X-Axis, values are interpreted as\n" + "UNIX timestamps in seconds and axis labels are formated as date/time."); + ImGui::BulletText("By default, labels are in UTC time but can be set to use local time instead."); + + ImGui::Checkbox("Local Time",&ImPlot::GetStyle().UseLocalTime); + ImGui::SameLine(); + ImGui::Checkbox("ISO 8601",&ImPlot::GetStyle().UseISO8601); + ImGui::SameLine(); + ImGui::Checkbox("24 Hour Clock",&ImPlot::GetStyle().Use24HourClock); + + static HugeTimeData* data = NULL; + if (data == NULL) { + ImGui::SameLine(); + if (ImGui::Button("Generate Huge Data (~500MB!)")) { + static HugeTimeData sdata(t_min); + data = &sdata; + } + } + + ImPlot::SetNextPlotLimits(t_min,t_max,0,1); + if (ImPlot::BeginPlot("##Time", NULL, NULL, ImVec2(-1,0), 0, ImPlotAxisFlags_Time)) { + if (data != NULL) { + // downsample our data + int downsample = (int)ImPlot::GetPlotLimits().X.Size() / 1000 + 1; + int start = (int)(ImPlot::GetPlotLimits().X.Min - t_min); + start = start < 0 ? 0 : start > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : start; + int end = (int)(ImPlot::GetPlotLimits().X.Max - t_min) + 1000; + end = end < 0 ? 0 : end > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : end; + int size = (end - start)/downsample; + // plot it + ImPlot::PlotLine("Time Series", &data->Ts[start], &data->Ys[start], size, 0, sizeof(double)*downsample); + } + // plot time now + double t_now = (double)time(0); + double y_now = HugeTimeData::GetY(t_now); + ImPlot::PlotScatter("Now",&t_now,&y_now,1); + ImPlot::Annotate(t_now,y_now,ImVec2(10,10),ImPlot::GetLastItemColor(),"Now"); + ImPlot::EndPlot(); + } +} + +void ShowDemo_MultipleYAxes() { + static float xs[1001], xs2[1001], ys1[1001], ys2[1001], ys3[1001]; + for (int i = 0; i < 1001; ++i) { + xs[i] = (i*0.1f); + ys1[i] = sinf(xs[i]) * 3 + 1; + ys2[i] = cosf(xs[i]) * 0.2f + 0.5f; + ys3[i] = sinf(xs[i]+0.5f) * 100 + 200; + xs2[i] = xs[i] + 10.0f; + } + static bool y2_axis = true; + static bool y3_axis = true; + ImGui::Checkbox("Y-Axis 2", &y2_axis); + ImGui::SameLine(); + ImGui::Checkbox("Y-Axis 3", &y3_axis); + ImGui::SameLine(); + + // you can fit axes programatically + ImGui::SameLine(); if (ImGui::Button("Fit X")) ImPlot::FitNextPlotAxes(true, false, false, false); + ImGui::SameLine(); if (ImGui::Button("Fit Y")) ImPlot::FitNextPlotAxes(false, true, false, false); + ImGui::SameLine(); if (ImGui::Button("Fit Y2")) ImPlot::FitNextPlotAxes(false, false, true, false); + ImGui::SameLine(); if (ImGui::Button("Fit Y3")) ImPlot::FitNextPlotAxes(false, false, false, true); + + ImPlot::SetNextPlotLimits(0.1, 100, 0, 10); + ImPlot::SetNextPlotLimitsY(0, 1, ImGuiCond_Once, 1); + ImPlot::SetNextPlotLimitsY(0, 300, ImGuiCond_Once, 2); + if (ImPlot::BeginPlot("Multi-Axis Plot", NULL, "Y-Axis 1", ImVec2(-1,0), + (y2_axis ? ImPlotFlags_YAxis2 : 0) | + (y3_axis ? ImPlotFlags_YAxis3 : 0), + ImPlotAxisFlags_None, ImPlotAxisFlags_None, ImPlotAxisFlags_NoGridLines, ImPlotAxisFlags_NoGridLines, + "Y-Axis 2", "Y-Axis 3")) { + ImPlot::PlotLine("f(x) = x", xs, xs, 1001); + ImPlot::PlotLine("f(x) = sin(x)*3+1", xs, ys1, 1001); + if (y2_axis) { + ImPlot::SetPlotYAxis(ImPlotYAxis_2); + ImPlot::PlotLine("f(x) = cos(x)*.2+.5 (Y2)", xs, ys2, 1001); + } + if (y3_axis) { + ImPlot::SetPlotYAxis(ImPlotYAxis_3); + ImPlot::PlotLine("f(x) = sin(x+.5)*100+200 (Y3)", xs2, ys3, 1001); + } + ImPlot::EndPlot(); + } +} + +void ShowDemo_LinkedAxes() { + static double xmin = 0, xmax = 1, ymin = 0, ymax = 1; + static bool linkx = true, linky = true; + int data[2] = {0,1}; + ImGui::Checkbox("Link X", &linkx); + ImGui::SameLine(); + ImGui::Checkbox("Link Y", &linky); + if (BeginAlignedPlots("AlignedGroup")) { + ImPlot::LinkNextPlotLimits(linkx ? &xmin : NULL , linkx ? &xmax : NULL, linky ? &ymin : NULL, linky ? &ymax : NULL); + if (ImPlot::BeginPlot("Plot A")) { + ImPlot::PlotLine("Line",data,2); + ImPlot::EndPlot(); + } + ImPlot::LinkNextPlotLimits(linkx ? &xmin : NULL , linkx ? &xmax : NULL, linky ? &ymin : NULL, linky ? &ymax : NULL); + if (ImPlot::BeginPlot("Plot B")) { + ImPlot::PlotLine("Line",data,2); + ImPlot::EndPlot(); + } + ImPlot::EndAlignedPlots(); + } +} + +void ShowDemo_EqualAxes() { + static double xs[1000], ys[1000]; + for (int i = 0; i < 1000; ++i) { + double angle = i * 2 * PI / 999.0; + xs[i] = cos(angle); ys[i] = sin(angle); + } + if (ImPlot::BeginPlot("",0,0,ImVec2(-1,0),ImPlotFlags_Equal)) { + ImPlot::PlotLine("Circle",xs,ys,1000); + ImPlot::EndPlot(); + } +} + +void ShowDemo_AutoFittingData() { + ImGui::BulletText("The Y-axis has been configured to auto-fit to only the data visible in X-axis range."); + ImGui::BulletText("Zoom and pan the X-axis. Disable Stems to see a difference in fit."); + ImGui::BulletText("If ImPlotAxisFlags_RangeFit is disabled, the axis will fit ALL data."); + + static ImPlotAxisFlags xflags = ImPlotAxisFlags_None; + static ImPlotAxisFlags yflags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit; + + ImGui::TextUnformatted("X: "); ImGui::SameLine(); + ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine(); + ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_RangeFit); + + ImGui::TextUnformatted("Y: "); ImGui::SameLine(); + ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine(); + ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_RangeFit); + + static double data[101]; + srand(0); + for (int i = 0; i < 101; ++i) + data[i] = 1 + sin(i/10.0f); + + if (ImPlot::BeginPlot("##DataFitting","X","Y",ImVec2(-1,0),0,xflags,yflags)) { + ImPlot::PlotLine("Line",data,101); + ImPlot::PlotStems("Stems",data,101); + ImPlot::EndPlot(); + }; +} + +ImPlotPoint SinewaveGetter(void* data, int i) { + float f = *(float*)data; + return ImPlotPoint(i,sinf(f*i)); +} + +void ShowDemo_SubplotsSizing() { + + static ImPlotSubplotFlags flags = ImPlotSubplotFlags_None; + ImGui::CheckboxFlags("ImPlotSubplotFlags_NoResize", (unsigned int*)&flags, ImPlotSubplotFlags_NoResize); + ImGui::CheckboxFlags("ImPlotSubplotFlags_NoTitle", (unsigned int*)&flags, ImPlotSubplotFlags_NoTitle); + + static int rows = 3; + static int cols = 3; + ImGui::SliderInt("Rows",&rows,1,5); + ImGui::SliderInt("Cols",&cols,1,5); + static float rratios[] = {5,1,1,1,1,1}; + static float cratios[] = {5,1,1,1,1,1}; + ImGui::DragScalarN("Row Ratios",ImGuiDataType_Float,rratios,rows,0.01f,0); + ImGui::DragScalarN("Col Ratios",ImGuiDataType_Float,cratios,cols,0.01f,0); + if (ImPlot::BeginSubplots("My Subplots", rows, cols, ImVec2(-1,400), flags, rratios, cratios)) { + for (int i = 0; i < rows*cols; ++i) { + if (ImPlot::BeginPlot("",NULL,NULL,ImVec2(),ImPlotFlags_NoLegend,ImPlotAxisFlags_NoTickLabels,ImPlotAxisFlags_NoTickLabels)) { + char buffer[8]; + float fi = 0.01f * (i+1); + sprintf(buffer, "data%d", i); + if (i == 0) + ImPlot::SetNextLineStyle(ImVec4(0,1,0,1)); + ImPlot::PlotLineG(buffer,SinewaveGetter,&fi,1000); + ImPlot::EndPlot(); + } + } + ImPlot::EndSubplots(); + } +} + +void ShowDemo_SubplotItemSharing() { + static ImPlotSubplotFlags flags = ImPlotSubplotFlags_ShareItems; + ImGui::CheckboxFlags("ImPlotSubplotFlags_ShareItems", (unsigned int*)&flags, ImPlotSubplotFlags_ShareItems); + ImGui::CheckboxFlags("ImPlotSubplotFlags_ColMajor", (unsigned int*)&flags, ImPlotSubplotFlags_ColMajor); + static int rows = 2; + static int cols = 3; + static int id[] = {0,1,2,3,4,5}; + static int curj = -1; + if (ImPlot::BeginSubplots("##ItemSharing", rows, cols, ImVec2(-1,400), flags)) { + for (int i = 0; i < rows*cols; ++i) { + if (ImPlot::BeginPlot("")) { + float fc = 0.01f; + ImPlot::PlotLineG("common",SinewaveGetter,&fc,1000); + for (int j = 0; j < 6; ++j) { + if (id[j] == i) { + char label[8]; + float fj = 0.01f * (j+2); + sprintf(label, "data%d", j); + ImPlot::PlotLineG(label,SinewaveGetter,&fj,1000); + if (ImPlot::BeginDragDropSourceItem(label)) { + curj = j; + ImGui::SetDragDropPayload("MY_DND",NULL,0); + ImPlot::ItemIcon(GetLastItemColor()); ImGui::SameLine(); + ImGui::TextUnformatted(label); + ImPlot::EndDragDropSource(); + } + } + } + if (ImPlot::BeginDragDropTarget()) { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) + id[curj] = i; + ImPlot::EndDragDropTarget(); + } + ImPlot::EndPlot(); + } + } + ImPlot::EndSubplots(); + } +} + +void ShowDemo_SubplotAxisLinking() { + static ImPlotSubplotFlags flags = ImPlotSubplotFlags_LinkRows | ImPlotSubplotFlags_LinkCols; + ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkRows", (unsigned int*)&flags, ImPlotSubplotFlags_LinkRows); + ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkCols", (unsigned int*)&flags, ImPlotSubplotFlags_LinkCols); + ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkAllX", (unsigned int*)&flags, ImPlotSubplotFlags_LinkAllX); + ImGui::CheckboxFlags("ImPlotSubplotFlags_LinkAllY", (unsigned int*)&flags, ImPlotSubplotFlags_LinkAllY); + + static int rows = 2; + static int cols = 2; + if (ImPlot::BeginSubplots("##AxisLinking", rows, cols, ImVec2(-1,400), flags)) { + for (int i = 0; i < rows*cols; ++i) { + ImPlot::SetNextPlotLimits(0,1000,-1,1); + if (ImPlot::BeginPlot("")) { + float fc = 0.01f; + ImPlot::PlotLineG("common",SinewaveGetter,&fc,1000); + ImPlot::EndPlot(); + } + } + ImPlot::EndSubplots(); + } +} + +void ShowDemo_Querying() { + static ImVector data; + static ImPlotLimits range, query, select; + static bool init = true; + if (init) { + for (int i = 0; i < 50; ++i) + { + double x = RandomRange(0.0, 1.0); + double y = RandomRange(0.0, 1.0); + data.push_back(ImPlotPoint(x,y)); + } + init = false; + } + + ImGui::BulletText("Middle click (or Ctrl + right click) and drag to create a query rect."); + ImGui::Indent(); + ImGui::BulletText("Hold Alt to expand query horizontally."); + ImGui::BulletText("Hold Shift to expand query vertically."); + ImGui::BulletText("The query rect can be dragged after it's created."); + ImGui::Unindent(); + ImGui::BulletText("Ctrl + click in the plot area to draw points."); + + ImPlot::SetNextPlotLimits(0,1,0,1); + if (ImPlot::BeginPlot("##Centroid", NULL, NULL, ImVec2(-1,0), ImPlotFlags_Query)) { + if (ImPlot::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl) { + ImPlotPoint pt = ImPlot::GetPlotMousePos(); + data.push_back(pt); + } + if (data.size() > 0) + ImPlot::PlotScatter("Points", &data[0].x, &data[0].y, data.size(), 0, 2 * sizeof(double)); + if (ImPlot::IsPlotQueried() && data.size() > 0) { + ImPlotLimits range2 = ImPlot::GetPlotQuery(); + int cnt = 0; + ImPlotPoint avg; + for (int i = 0; i < data.size(); ++i) { + if (range2.Contains(data[i].x, data[i].y)) { + avg.x += data[i].x; + avg.y += data[i].y; + cnt++; + } + } + if (cnt > 0) { + avg.x = avg.x / cnt; + avg.y = avg.y / cnt; + ImPlot::SetNextMarkerStyle(ImPlotMarker_Square); + ImPlot::PlotScatter("Centroid", &avg.x, &avg.y, 1); + } + } + range = ImPlot::GetPlotLimits(); + query = ImPlot::GetPlotQuery(); + select = ImPlot::GetPlotSelection(); + ImPlot::EndPlot(); + } + ImGui::Text("Limits: [%g,%g,%g,%g]", range.X.Min, range.X.Max, range.Y.Min, range.Y.Max); + ImGui::Text("Query: [%g,%g,%g,%g]", query.X.Min, query.X.Max, query.Y.Min, query.Y.Max); + ImGui::Text("Selection: [%g,%g,%g,%g]", select.X.Min, select.X.Max, select.Y.Min, select.Y.Max); +} + +void ShowDemo_Views() { + static float x_data[512]; + static float y_data1[512]; + static float y_data2[512]; + static float y_data3[512]; + static float sampling_freq = 44100; + static float freq = 500; + for (size_t i = 0; i < 512; ++i) { + const float t = i / sampling_freq; + x_data[i] = t; + const float arg = 2 * 3.14f * freq * t; + y_data1[i] = sinf(arg); + y_data2[i] = y_data1[i] * -0.6f + sinf(2 * arg) * 0.4f; + y_data3[i] = y_data2[i] * -0.6f + sinf(3 * arg) * 0.4f; + } + ImGui::BulletText("Query the first plot to render a subview in the second plot (see above for controls)."); + ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels; + static bool use_selection = false; + ImGui::Checkbox("Use Box Selection",&use_selection); + bool is_viewed = false; + ImPlotLimits view; + ImPlot::SetNextPlotLimits(0,0.01,-1,1); + if (ImPlot::BeginPlot("##View1",NULL,NULL,ImVec2(-1,150), ImPlotFlags_Query, flags, flags)) { + ImPlot::PlotLine("Signal 1", x_data, y_data1, 512); + ImPlot::PlotLine("Signal 2", x_data, y_data2, 512); + ImPlot::PlotLine("Signal 3", x_data, y_data3, 512); + is_viewed = use_selection ? ImPlot::IsPlotSelected() : ImPlot::IsPlotQueried(); + view = use_selection ? ImPlot::GetPlotSelection() : ImPlot::GetPlotQuery(); + ImPlot::EndPlot(); + } + ImPlot::SetNextPlotLimits(view.X.Min, view.X.Max, view.Y.Min, view.Y.Max, ImGuiCond_Always); + if (ImPlot::BeginPlot("##View2",NULL,NULL,ImVec2(-1,150), ImPlotFlags_CanvasOnly, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) { + if (is_viewed) { + ImPlot::PlotLine("Signal 1", x_data, y_data1, 512); + ImPlot::PlotLine("Signal 2", x_data, y_data2, 512); + ImPlot::PlotLine("Signal 3", x_data, y_data3, 512); + } + ImPlot::EndPlot(); + } +} + +void ShowDemo_LegendOptions() { + static ImPlotLocation loc = ImPlotLocation_East; + static bool h = false; static bool o = true; + ImGui::CheckboxFlags("North", (unsigned int*)&loc, ImPlotLocation_North); ImGui::SameLine(); + ImGui::CheckboxFlags("South", (unsigned int*)&loc, ImPlotLocation_South); ImGui::SameLine(); + ImGui::CheckboxFlags("West", (unsigned int*)&loc, ImPlotLocation_West); ImGui::SameLine(); + ImGui::CheckboxFlags("East", (unsigned int*)&loc, ImPlotLocation_East); ImGui::SameLine(); + ImGui::Checkbox("Horizontal##2", &h); ImGui::SameLine(); + ImGui::Checkbox("Outside", &o); + + ImGui::SliderFloat2("LegendPadding", (float*)&GetStyle().LegendPadding, 0.0f, 20.0f, "%.0f"); + ImGui::SliderFloat2("LegendInnerPadding", (float*)&GetStyle().LegendInnerPadding, 0.0f, 10.0f, "%.0f"); + ImGui::SliderFloat2("LegendSpacing", (float*)&GetStyle().LegendSpacing, 0.0f, 5.0f, "%.0f"); + + if (ImPlot::BeginPlot("##Legend","x","y",ImVec2(-1,0))) { + ImPlot::SetLegendLocation(loc, h ? ImPlotOrientation_Horizontal : ImPlotOrientation_Vertical, o); + static MyImPlot::WaveData data1(0.001, 0.2, 2, 0.75); + static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.25); + static MyImPlot::WaveData data3(0.001, 0.2, 6, 0.5); + ImPlot::PlotLineG("Item 1", MyImPlot::SineWave, &data1, 1000); // "Item 1" added to legend + ImPlot::PlotLineG("Item 2##IDText", MyImPlot::SawWave, &data2, 1000); // "Item 2" added to legend, text after ## used for ID only + ImPlot::PlotLineG("##NotListed", MyImPlot::SawWave, &data3, 1000); // plotted, but not added to legend + ImPlot::PlotLineG("Item 3", MyImPlot::SineWave, &data1, 1000); // "Item 3" added to legend + ImPlot::PlotLineG("Item 3", MyImPlot::SawWave, &data2, 1000); // combined with previous "Item 3" + ImPlot::EndPlot(); + } +} + +void ShowDemo_DragLines() { + ImGui::BulletText("Click and drag the horizontal and vertical lines."); + static double x1 = 0.2; + static double x2 = 0.8; + static double y1 = 0.25; + static double y2 = 0.75; + static double f = 0.1; + static bool show_labels = true; + ImGui::Checkbox("Show Labels##1",&show_labels); + ImPlot::SetNextPlotLimits(0,1,0,1); + if (ImPlot::BeginPlot("##guides",0,0,ImVec2(-1,0),ImPlotFlags_YAxis2)) { + ImPlot::DragLineX("x1",&x1,show_labels); + ImPlot::DragLineX("x2",&x2,show_labels); + ImPlot::DragLineY("y1",&y1,show_labels); + ImPlot::DragLineY("y2",&y2,show_labels); + double xs[1000], ys[1000]; + for (int i = 0; i < 1000; ++i) { + xs[i] = (x2+x1)/2+fabs(x2-x1)*(i/1000.0f - 0.5f); + ys[i] = (y1+y2)/2+fabs(y2-y1)/2*sin(f*i/10); + } + ImPlot::PlotLine("Interactive Data", xs, ys, 1000); + ImPlot::SetPlotYAxis(ImPlotYAxis_2); + ImPlot::DragLineY("f",&f,show_labels,ImVec4(1,0.5f,1,1)); + ImPlot::EndPlot(); + } +} + +void ShowDemo_DragPoints() { + static bool show_labels = true; + ImGui::BulletText("Click and drag any point."); + ImGui::Checkbox("Show Labels##2",&show_labels); + ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoTickMarks; + ImPlot::SetNextPlotLimits(0,1,0,1); + if (ImPlot::BeginPlot("##Bezier",0,0,ImVec2(-1,0),ImPlotFlags_CanvasOnly,flags,flags)) { + static ImPlotPoint P[] = {ImPlotPoint(.05f,.05f), ImPlotPoint(0.2,0.4), ImPlotPoint(0.8,0.6), ImPlotPoint(.95f,.95f)}; + static ImPlotPoint B[100]; + for (int i = 0; i < 100; ++i) { + double t = i / 99.0; + double u = 1 - t; + double w1 = u*u*u; + double w2 = 3*u*u*t; + double w3 = 3*u*t*t; + double w4 = t*t*t; + 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); + } + ImPlot::SetNextLineStyle(ImVec4(0,0.9f,0,1), 2); + ImPlot::PlotLine("##bez",&B[0].x, &B[0].y, 100, 0, sizeof(ImPlotPoint)); + ImPlot::SetNextLineStyle(ImVec4(1,0.5f,1,1)); + ImPlot::PlotLine("##h1",&P[0].x, &P[0].y, 2, 0, sizeof(ImPlotPoint)); + ImPlot::SetNextLineStyle(ImVec4(0,0.5f,1,1)); + ImPlot::PlotLine("##h2",&P[2].x, &P[2].y, 2, 0, sizeof(ImPlotPoint)); + ImPlot::DragPoint("P0",&P[0].x,&P[0].y, show_labels, ImVec4(0,0.9f,0,1)); + ImPlot::DragPoint("P1",&P[1].x,&P[1].y, show_labels, ImVec4(1,0.5f,1,1)); + ImPlot::DragPoint("P2",&P[2].x,&P[2].y, show_labels, ImVec4(0,0.5f,1,1)); + ImPlot::DragPoint("P3",&P[3].x,&P[3].y, show_labels, ImVec4(0,0.9f,0,1)); + ImPlot::EndPlot(); + } +} + +void ShowDemo_Annotations() { + static bool clamp = false; + ImGui::Checkbox("Clamp",&clamp); + ImPlot::SetNextPlotLimits(0,2,0,1); + if (ImPlot::BeginPlot("##Annotations")) { + + static float p[] = {0.25f, 0.25f, 0.75f, 0.75f, 0.25f}; + ImPlot::PlotScatter("##Points",&p[0],&p[1],4); + ImVec4 col = GetLastItemColor(); + clamp ? ImPlot::AnnotateClamped(0.25,0.25,ImVec2(-15,15),col,"BL") : ImPlot::Annotate(0.25,0.25,ImVec2(-15,15),col,"BL"); + clamp ? ImPlot::AnnotateClamped(0.75,0.25,ImVec2(15,15),col,"BR") : ImPlot::Annotate(0.75,0.25,ImVec2(15,15),col,"BR"); + clamp ? ImPlot::AnnotateClamped(0.75,0.75,ImVec2(15,-15),col,"TR") : ImPlot::Annotate(0.75,0.75,ImVec2(15,-15),col,"TR"); + clamp ? ImPlot::AnnotateClamped(0.25,0.75,ImVec2(-15,-15),col,"TL") : ImPlot::Annotate(0.25,0.75,ImVec2(-15,-15),col,"TL"); + clamp ? ImPlot::AnnotateClamped(0.5,0.5,ImVec2(0,0),col,"Center") : ImPlot::Annotate(0.5,0.5,ImVec2(0,0),col,"Center"); + + float bx[] = {1.2f,1.5f,1.8f}; + float by[] = {0.25f, 0.5f, 0.75f}; + ImPlot::PlotBars("##Bars",bx,by,3,0.2); + for (int i = 0; i < 3; ++i) + ImPlot::Annotate(bx[i],by[i],ImVec2(0,-5),"B[%d]=%.2f",i,by[i]); + ImPlot::EndPlot(); + } +} + +void ShowDemo_DragAndDrop() { + ImGui::BulletText("Drag/drop items from the left column."); + ImGui::BulletText("Drag/drop items between plots."); + ImGui::Indent(); + ImGui::BulletText("Plot 1 Targets: Plot, Y-Axes, Legend"); + ImGui::BulletText("Plot 1 Sources: Legend Item Labels"); + ImGui::BulletText("Plot 2 Targets: Plot, X-Axis, Y-Axis"); + ImGui::BulletText("Plot 2 Sources: Plot, X-Axis, Y-Axis (hold Ctrl)"); + ImGui::Unindent(); + + // convenience struct to manage DND items; do this however you like + struct MyDndItem { + int Idx; + int Plt; + int Yax; + char Label[16]; + ImVector Data; + ImVec4 Color; + MyDndItem() { + static int i = 0; + Idx = i++; + Plt = 0; + Yax = ImPlotYAxis_1; + sprintf(Label, "%02d Hz", Idx+1); + Color = RandomColor(); + Data.reserve(1001); + for (int k = 0; k < 1001; ++k) { + float t = k * 1.0f / 999; + Data.push_back(ImVec2(t, 0.5f + 0.5f * sinf(2*3.14f*t*(Idx+1)))); + } + } + void Reset() { Plt = 0; Yax = ImPlotYAxis_1; } + }; + + const int k_dnd = 20; + static MyDndItem dnd[k_dnd]; + static MyDndItem* dndx = NULL; // for plot 2 + static MyDndItem* dndy = NULL; // for plot 2 + + // child window to serve as initial source for our DND items + ImGui::BeginChild("DND_LEFT",ImVec2(100,400)); + if (ImGui::Button("Reset Data", ImVec2(100, 0))) { + for (int k = 0; k < k_dnd; ++k) + dnd[k].Reset(); + dndx = dndy = NULL; + } + for (int k = 0; k < k_dnd; ++k) { + if (dnd[k].Plt > 0) + continue; + ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine(); + ImGui::Selectable(dnd[k].Label, false, 0, ImVec2(100, 0)); + if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) { + ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int)); + ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine(); + ImGui::TextUnformatted(dnd[k].Label); + ImGui::EndDragDropSource(); + } + } + ImGui::EndChild(); + if (ImGui::BeginDragDropTarget()) { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { + int i = *(int*)payload->Data; dnd[i].Reset(); + } + ImGui::EndDragDropTarget(); + } + + ImGui::SameLine(); + ImGui::BeginChild("DND_RIGHT",ImVec2(-1,400)); + // plot 1 (time series) + ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoGridLines; + if (ImPlot::BeginPlot("##DND1", NULL, "[drop here]", ImVec2(-1,195), ImPlotFlags_YAxis2 | ImPlotFlags_YAxis3, flags | ImPlotAxisFlags_Lock, flags, flags, flags, "[drop here]", "[drop here]")) { + for (int k = 0; k < k_dnd; ++k) { + if (dnd[k].Plt == 1 && dnd[k].Data.size() > 0) { + ImPlot::SetPlotYAxis(dnd[k].Yax); + ImPlot::SetNextLineStyle(dnd[k].Color); + static char label[32]; + sprintf(label,"%s (Y%d)", dnd[k].Label, dnd[k].Yax+1); + ImPlot::PlotLine(label, &dnd[k].Data[0].x, &dnd[k].Data[0].y, dnd[k].Data.size(), 0, 2 * sizeof(float)); + // allow legend item labels to be DND sources + if (ImPlot::BeginDragDropSourceItem(label)) { + ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int)); + ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine(); + ImGui::TextUnformatted(dnd[k].Label); + ImPlot::EndDragDropSource(); + } + } + } + // allow the main plot area to be a DND target + if (ImPlot::BeginDragDropTarget()) { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { + int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = 0; + } + ImPlot::EndDragDropTarget(); + } + // allow each y-axis to be a DND target + for (int y = 0; y < 3; ++y) { + if (ImPlot::BeginDragDropTargetY(y)) { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { + int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = y; + } + ImPlot::EndDragDropTarget(); + } + } + // allow the legend to be a DND target + if (ImPlot::BeginDragDropTargetLegend()) { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { + int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = 0; + } + ImPlot::EndDragDropTarget(); + } + ImPlot::EndPlot(); + } + // plot 2 (Lissajous) + ImPlot::PushStyleColor(ImPlotCol_XAxis, dndx == NULL ? ImPlot::GetStyle().Colors[ImPlotCol_XAxis] : dndx->Color); + ImPlot::PushStyleColor(ImPlotCol_YAxis, dndy == NULL ? ImPlot::GetStyle().Colors[ImPlotCol_YAxis] : dndy->Color); + if (ImPlot::BeginPlot("##DND2", dndx == NULL ? "[drop here]" : dndx->Label, dndy == NULL ? "[drop here]" : dndy->Label, ImVec2(-1,195), 0, flags, flags )) { + if (dndx != NULL && dndy != NULL) { + 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); + ImPlot::SetNextLineStyle(mixed); + ImPlot::PlotLine("##dndxy", &dndx->Data[0].y, &dndy->Data[0].y, dndx->Data.size(), 0, 2 * sizeof(float)); + } + // allow the x-axis to be a DND target + if (ImPlot::BeginDragDropTargetX()) { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { + int i = *(int*)payload->Data; dndx = &dnd[i]; + } + ImPlot::EndDragDropTarget(); + } + // allow the x-axis to be a DND source + if (dndx != NULL && ImPlot::BeginDragDropSourceX()) { + ImGui::SetDragDropPayload("MY_DND", &dndx->Idx, sizeof(int)); + ImPlot::ItemIcon(dndx->Color); ImGui::SameLine(); + ImGui::TextUnformatted(dndx->Label); + ImPlot::EndDragDropSource(); + } + // allow the y-axis to be a DND target + if (ImPlot::BeginDragDropTargetY()) { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { + int i = *(int*)payload->Data; dndy = &dnd[i]; + } + ImPlot::EndDragDropTarget(); + } + // allow the y-axis to be a DND source + if (dndy != NULL && ImPlot::BeginDragDropSourceY()) { + ImGui::SetDragDropPayload("MY_DND", &dndy->Idx, sizeof(int)); + ImPlot::ItemIcon(dndy->Color); ImGui::SameLine(); + ImGui::TextUnformatted(dndy->Label); + ImPlot::EndDragDropSource(); + } + // allow the plot area to be a DND target + if (ImPlot::BeginDragDropTarget()) { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { + int i = *(int*)payload->Data; dndx = dndy = &dnd[i]; + } + } + // allow the plot area to be a DND source + if (ImPlot::BeginDragDropSource()) { + ImGui::TextUnformatted("Yes, you can\ndrag this!"); + ImPlot::EndDragDropSource(); + } + ImPlot::EndPlot(); + } + ImPlot::PopStyleColor(2); + ImGui::EndChild(); +} + +void ShowDemo_Tables() { +#ifdef IMGUI_HAS_TABLE + static ImGuiTableFlags flags = ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_RowBg; + static bool anim = true; + static int offset = 0; + ImGui::BulletText("Plots can be used inside of ImGui tables as a means of creating subplots."); + ImGui::Checkbox("Animate",&anim); + if (anim) + offset = (offset + 1) % 100; + if (ImGui::BeginTable("##table", 3, flags, ImVec2(-1,0))) { + ImGui::TableSetupColumn("Electrode", ImGuiTableColumnFlags_WidthFixed, 75.0f); + ImGui::TableSetupColumn("Voltage", ImGuiTableColumnFlags_WidthFixed, 75.0f); + ImGui::TableSetupColumn("EMG Signal"); + ImGui::TableHeadersRow(); + ImPlot::PushColormap(ImPlotColormap_Cool); + for (int row = 0; row < 10; row++) { + ImGui::TableNextRow(); + static float data[100]; + srand(row); + for (int i = 0; i < 100; ++i) + data[i] = RandomRange(0.0f,10.0f); + ImGui::TableSetColumnIndex(0); + ImGui::Text("EMG %d", row); + ImGui::TableSetColumnIndex(1); + ImGui::Text("%.3f V", data[offset]); + ImGui::TableSetColumnIndex(2); + ImGui::PushID(row); + MyImPlot::Sparkline("##spark",data,100,0,11.0f,offset,ImPlot::GetColormapColor(row),ImVec2(-1, 35)); + ImGui::PopID(); + } + ImPlot::PopColormap(); + ImGui::EndTable(); + } +#else + ImGui::BulletText("You need to merge the ImGui 'tables' branch for this section."); +#endif +} + +void ShowDemo_OffsetAndStride() { + static const int k_circles = 11; + static const int k_points_per = 50; + static const int k_size = 2 * k_points_per * k_circles; + static double interleaved_data[k_size]; + for (int p = 0; p < k_points_per; ++p) { + for (int c = 0; c < k_circles; ++c) { + double r = (double)c / (k_circles - 1) * 0.2 + 0.2; + interleaved_data[p*2*k_circles + 2*c + 0] = 0.5 + r * cos((double)p/k_points_per * 6.28); + interleaved_data[p*2*k_circles + 2*c + 1] = 0.5 + r * sin((double)p/k_points_per * 6.28); + } + } + static int offset = 0; + ImGui::BulletText("Offsetting is useful for realtime plots (see above) and circular buffers."); + ImGui::BulletText("Striding is useful for interleaved data (e.g. audio) or plotting structs."); + ImGui::BulletText("Here, all circle data is stored in a single interleaved buffer:"); + ImGui::BulletText("[c0.x0 c0.y0 ... cn.x0 cn.y0 c0.x1 c0.y1 ... cn.x1 cn.y1 ... cn.xm cn.ym]"); + ImGui::BulletText("The offset value indicates which circle point index is considered the first."); + ImGui::BulletText("Offsets can be negative and/or larger than the actual data count."); + ImGui::SliderInt("Offset", &offset, -2*k_points_per, 2*k_points_per); + if (ImPlot::BeginPlot("##strideoffset",0,0,ImVec2(-1,0), ImPlotFlags_Equal)) { + ImPlot::PushColormap(ImPlotColormap_Jet); + char buff[16]; + for (int c = 0; c < k_circles; ++c) { + sprintf(buff, "Circle %d", c); + ImPlot::PlotLine(buff, &interleaved_data[c*2 + 0], &interleaved_data[c*2 + 1], k_points_per, offset, 2*k_circles*sizeof(double)); + } + ImPlot::EndPlot(); + ImPlot::PopColormap(); + } + // offset++; uncomment for animation! +} + +void ShowDemo_CustomDataAndGetters() { + ImGui::BulletText("You can plot custom structs using the stride feature."); + ImGui::BulletText("Most plotters can also be passed a function pointer for getting data."); + ImGui::Indent(); + ImGui::BulletText("You can optionally pass user data to be given to your getter function."); + ImGui::BulletText("C++ lambdas can be passed as function pointers as well!"); + ImGui::Unindent(); + + MyImPlot::Vector2f vec2_data[2] = { MyImPlot::Vector2f(0,0), MyImPlot::Vector2f(1,1) }; + + if (ImPlot::BeginPlot("##Custom Data")) { + + // custom structs using stride example: + ImPlot::PlotLine("Vector2f", &vec2_data[0].x, &vec2_data[0].y, 2, 0, sizeof(MyImPlot::Vector2f) /* or sizeof(float) * 2 */); + + // custom getter example 1: + ImPlot::PlotLineG("Spiral", MyImPlot::Spiral, NULL, 1000); + + // custom getter example 2: + static MyImPlot::WaveData data1(0.001, 0.2, 2, 0.75); + static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.25); + ImPlot::PlotLineG("Waves", MyImPlot::SineWave, &data1, 1000); + ImPlot::PlotLineG("Waves", MyImPlot::SawWave, &data2, 1000); + ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); + ImPlot::PlotShadedG("Waves", MyImPlot::SineWave, &data1, MyImPlot::SawWave, &data2, 1000); + ImPlot::PopStyleVar(); + + // you can also pass C++ lambdas: + // auto lamda = [](void* data, int idx) { ... return ImPlotPoint(x,y); }; + // ImPlot::PlotLine("My Lambda", lambda, data, 1000); + + ImPlot::EndPlot(); + } +} + +void ShowDemo_TickLabels() { + static bool custom_fmt = true; + static bool custom_ticks = false; + static bool custom_labels = true; + ImGui::Checkbox("Show Custom Format", &custom_fmt); + ImGui::SameLine(); + ImGui::Checkbox("Show Custom Ticks", &custom_ticks); + if (custom_ticks) { + ImGui::SameLine(); + ImGui::Checkbox("Show Custom Labels", &custom_labels); + } + double pi = 3.14; + const char* pi_str[] = {"PI"}; + static double yticks[] = {1,3,7,9}; + static const char* ylabels[] = {"One","Three","Seven","Nine"}; + static double yticks_aux[] = {0.2,0.4,0.6}; + static const char* ylabels_aux[] = {"A","B","C","D","E","F"}; + if (custom_fmt) { + ImPlot::SetNextPlotFormatX("%g ms"); + ImPlot::SetNextPlotFormatY("%g Hz", ImPlotYAxis_1); + ImPlot::SetNextPlotFormatY("%g dB", ImPlotYAxis_2); + ImPlot::SetNextPlotFormatY("%g km", ImPlotYAxis_3); + } + if (custom_ticks) { + ImPlot::SetNextPlotTicksX(&pi,1,custom_labels ? pi_str : NULL, true); + ImPlot::SetNextPlotTicksY(yticks, 4, custom_labels ? ylabels : NULL, ImPlotYAxis_1); + ImPlot::SetNextPlotTicksY(yticks_aux, 3, custom_labels ? ylabels_aux : NULL, false, ImPlotYAxis_2); + ImPlot::SetNextPlotTicksY(0, 1, 6, custom_labels ? ylabels_aux : NULL, false, ImPlotYAxis_3); + } + ImPlot::SetNextPlotLimits(2.5,5,0,10); + if (ImPlot::BeginPlot("##Ticks", NULL, NULL, ImVec2(-1,0), ImPlotFlags_YAxis2 | ImPlotFlags_YAxis3)) { + // nothing to see here, just the ticks + ImPlot::EndPlot(); + } +} + +void ShowDemo_CustomStyles() { + ImPlot::PushColormap(ImPlotColormap_Deep); + // normally you wouldn't change the entire style each frame + ImPlotStyle backup = ImPlot::GetStyle(); + MyImPlot::StyleSeaborn(); + ImPlot::SetNextPlotLimits(-0.5f, 9.5f, 0, 10); + if (ImPlot::BeginPlot("seaborn style", "x-axis", "y-axis")) { + unsigned int lin[10] = {8,8,9,7,8,8,8,9,7,8}; + unsigned int bar[10] = {1,2,5,3,4,1,2,5,3,4}; + unsigned int dot[10] = {7,6,6,7,8,5,6,5,8,7}; + ImPlot::PlotBars("Bars", bar, 10, 0.5f); + ImPlot::PlotLine("Line", lin, 10); + ImPlot::NextColormapColor(); // skip green + ImPlot::PlotScatter("Scatter", dot, 10); + ImPlot::EndPlot(); + } + ImPlot::GetStyle() = backup; + ImPlot::PopColormap(); +} + +void ShowDemo_CustomRendering() { + if (ImPlot::BeginPlot("##CustomRend")) { + ImVec2 cntr = ImPlot::PlotToPixels(ImPlotPoint(0.5f, 0.5f)); + ImVec2 rmin = ImPlot::PlotToPixels(ImPlotPoint(0.25f, 0.75f)); + ImVec2 rmax = ImPlot::PlotToPixels(ImPlotPoint(0.75f, 0.25f)); + ImPlot::PushPlotClipRect(); + ImPlot::GetPlotDrawList()->AddCircleFilled(cntr,20,IM_COL32(255,255,0,255),20); + ImPlot::GetPlotDrawList()->AddRect(rmin, rmax, IM_COL32(128,0,255,255)); + ImPlot::PopPlotClipRect(); + ImPlot::EndPlot(); + } +} + +void ShowDemo_LegendPopups() { + ImGui::BulletText("You can implement legend context menus to inject per-item controls and widgets."); + ImGui::BulletText("Right click the legend label/icon to edit custom item attributes."); + + static float frequency = 0.1f; + static float amplitude = 0.5f; + static ImVec4 color = ImVec4(1,1,0,1); + static float alpha = 1.0f; + static bool line = false; + static float thickness = 1; + static bool markers = false; + static bool shaded = false; + + static float vals[101]; + for (int i = 0; i < 101; ++i) + vals[i] = amplitude * sinf(frequency * i); + + ImPlot::SetNextPlotLimits(0,100,-1,1); + if (ImPlot::BeginPlot("Right Click the Legend")) { + // rendering logic + ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha); + if (!line) { + ImPlot::SetNextFillStyle(color); + ImPlot::PlotBars("Right Click Me", vals, 101); + } + else { + if (markers) ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); + ImPlot::SetNextLineStyle(color, thickness); + ImPlot::PlotLine("Right Click Me", vals, 101); + if (shaded) ImPlot::PlotShaded("Right Click Me",vals,101); + } + ImPlot::PopStyleVar(); + // custom legend context menu + if (ImPlot::BeginLegendPopup("Right Click Me")) { + ImGui::SliderFloat("Frequency",&frequency,0,1,"%0.2f"); + ImGui::SliderFloat("Amplitude",&litude,0,1,"%0.2f"); + ImGui::Separator(); + ImGui::ColorEdit3("Color",&color.x); + ImGui::SliderFloat("Transparency",&alpha,0,1,"%.2f"); + ImGui::Checkbox("Line Plot", &line); + if (line) { + ImGui::SliderFloat("Thickness", &thickness, 0, 5); + ImGui::Checkbox("Markers", &markers); + ImGui::Checkbox("Shaded",&shaded); + } + ImPlot::EndLegendPopup(); + } + ImPlot::EndPlot(); + } +} + +void ShowDemo_CustomPlottersAndTooltips() { + ImGui::BulletText("You can create custom plotters or extend ImPlot using implot_internal.h."); + 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}; + 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}; + 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}; + 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}; + 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}; + static bool tooltip = true; + ImGui::Checkbox("Show Tooltip", &tooltip); + ImGui::SameLine(); + static ImVec4 bullCol = ImVec4(0.000f, 1.000f, 0.441f, 1.000f); + static ImVec4 bearCol = ImVec4(0.853f, 0.050f, 0.310f, 1.000f); + ImGui::SameLine(); ImGui::ColorEdit4("##Bull", &bullCol.x, ImGuiColorEditFlags_NoInputs); + ImGui::SameLine(); ImGui::ColorEdit4("##Bear", &bearCol.x, ImGuiColorEditFlags_NoInputs); + ImPlot::GetStyle().UseLocalTime = false; + ImPlot::SetNextPlotFormatY("$%.0f"); + ImPlot::SetNextPlotLimits(1546300800, 1571961600, 1250, 1600); + if (ImPlot::BeginPlot("Candlestick Chart",NULL,NULL,ImVec2(-1,0),0,ImPlotAxisFlags_Time,ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit)) { + MyImPlot::PlotCandlestick("GOOGL",dates, opens, closes, lows, highs, 218, tooltip, 0.25f, bullCol, bearCol); + ImPlot::EndPlot(); + } + } + +//----------------------------------------------------------------------------- +// DEMO WINDOW +//----------------------------------------------------------------------------- + void ShowDemoWindow(bool* p_open) { - double DEMO_TIME = ImGui::GetTime(); static bool show_imgui_metrics = false; static bool show_implot_metrics = false; static bool show_imgui_style_editor = false; @@ -231,1375 +1743,114 @@ void ShowDemoWindow(bool* p_open) { ImGui::Spacing(); - if (ImGui::CollapsingHeader("Help")) { - ImGui::Text("ABOUT THIS DEMO:"); - ImGui::BulletText("Sections below are demonstrating many aspects of the library."); - ImGui::BulletText("The \"Tools\" menu above gives access to: Style Editors (ImPlot/ImGui)\n" - "and Metrics (general purpose Dear ImGui debugging tool)."); - ImGui::Separator(); - ImGui::Text("PROGRAMMER GUIDE:"); - ImGui::BulletText("See the ShowDemoWindow() code in implot_demo.cpp. <- you are here!"); - ImGui::BulletText("By default, anti-aliased lines are turned OFF."); - ImGui::Indent(); - ImGui::BulletText("Software AA can be enabled globally with ImPlotStyle.AntiAliasedLines."); - ImGui::BulletText("Software AA can be enabled per plot with ImPlotFlags_AntiAliased."); - ImGui::BulletText("AA for plots can be toggled from the plot's context menu."); - ImGui::BulletText("If permitable, you are better off using hardware AA (e.g. MSAA)."); - ImGui::Unindent(); - ImGui::BulletText("If you see visual artifacts, do one of the following:"); - ImGui::Indent(); - ImGui::BulletText("Handle ImGuiBackendFlags_RendererHasVtxOffset for 16-bit indices in your backend."); - ImGui::BulletText("Or, enable 32-bit indices in imconfig.h."); - ImGui::BulletText("Your current configuration is:"); - ImGui::Indent(); - ImGui::BulletText("ImDrawIdx: %d-bit", (int)(sizeof(ImDrawIdx) * 8)); - ImGui::BulletText("ImGuiBackendFlags_RendererHasVtxOffset: %s", (ImGui::GetIO().BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ? "True" : "False"); - ImGui::Unindent(); - ImGui::Unindent(); - ImGui::Separator(); - ImGui::Text("USER GUIDE:"); - ShowUserGuide(); + if (ImGui::BeginTabBar("ImPlotDemoTabs")) { + if (ImGui::BeginTabItem("Plots")) { + if (ImGui::CollapsingHeader("Line Plots")) + ShowDemo_LinePlots(); + if (ImGui::CollapsingHeader("Filled Line Plots")) + ShowDemo_FilledLinePlots(); + if (ImGui::CollapsingHeader("Shaded Plots##")) + ShowDemo_ShadedPlots(); + if (ImGui::CollapsingHeader("Scatter Plots")) + ShowDemo_ScatterPlots(); + if (ImGui::CollapsingHeader("Realtime Plots")) + ShowDemo_RealtimePlots(); + if (ImGui::CollapsingHeader("Stairstep Plots")) + ShowDemo_StairstepPlots(); + if (ImGui::CollapsingHeader("Bar Plots")) + ShowDemo_BarPlots(); + if (ImGui::CollapsingHeader("Error Bars")) + ShowDemo_ErrorBars(); + if (ImGui::CollapsingHeader("Stem Plots##")) + ShowDemo_StemPlots(); + if (ImGui::CollapsingHeader("Infinite Lines")) + ShowDemo_InfiniteLines(); + if (ImGui::CollapsingHeader("Pie Charts")) + ShowDemo_PieCharts(); + if (ImGui::CollapsingHeader("Heatmaps")) + ShowDemo_Heatmaps(); + if (ImGui::CollapsingHeader("Histogram")) + ShowDemo_Histogram(); + if (ImGui::CollapsingHeader("Histogram 2D")) + ShowDemo_Histogram2D(); + if (ImGui::CollapsingHeader("Digital Plots")) + ShowDemo_DigitalPlots(); + if (ImGui::CollapsingHeader("Images")) + ShowDemo_Images(); + if (ImGui::CollapsingHeader("Markers and Text")) + ShowDemo_MarkersAndText(); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Subplots")) { + if (ImGui::CollapsingHeader("Sizing")) + ShowDemo_SubplotsSizing(); + if (ImGui::CollapsingHeader("Item Sharing")) + ShowDemo_SubplotItemSharing(); + if (ImGui::CollapsingHeader("Axis Linking")) + ShowDemo_SubplotAxisLinking(); + if (ImGui::CollapsingHeader("Tables")) + ShowDemo_Tables(); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Axes")) { + if (ImGui::CollapsingHeader("Log Axes")) + ShowDemo_LogAxes(); + if (ImGui::CollapsingHeader("Time Axes")) + ShowDemo_TimeAxes(); + if (ImGui::CollapsingHeader("Multiple Y-Axes")) + ShowDemo_MultipleYAxes(); + if (ImGui::CollapsingHeader("Tick Labels")) + ShowDemo_TickLabels(); + if (ImGui::CollapsingHeader("Linked Axes")) + ShowDemo_LinkedAxes(); + if (ImGui::CollapsingHeader("Equal Axes")) + ShowDemo_EqualAxes(); + if (ImGui::CollapsingHeader("Auto-Fitting Data")) + ShowDemo_AutoFittingData(); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Tools")) { + if (ImGui::CollapsingHeader("Offset and Stride")) + ShowDemo_OffsetAndStride(); + if (ImGui::CollapsingHeader("Querying")) + ShowDemo_Querying(); + if (ImGui::CollapsingHeader("Views")) + ShowDemo_Views(); + if (ImGui::CollapsingHeader("Drag Lines")) + ShowDemo_DragLines(); + if (ImGui::CollapsingHeader("Drag Points")) + ShowDemo_DragPoints(); + if (ImGui::CollapsingHeader("Annotations")) + ShowDemo_Annotations(); + if (ImGui::CollapsingHeader("Drag and Drop")) + ShowDemo_DragAndDrop(); + if (ImGui::CollapsingHeader("Legend Options")) + ShowDemo_LegendOptions(); + if (ImGui::CollapsingHeader("Legend Popups")) + ShowDemo_LegendPopups(); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Custom")) { + if (ImGui::CollapsingHeader("Custom Styles")) + ShowDemo_CustomStyles(); + if (ImGui::CollapsingHeader("Custom Data and Getters")) + ShowDemo_CustomDataAndGetters(); + if (ImGui::CollapsingHeader("Custom Rendering")) + ShowDemo_CustomRendering(); + if (ImGui::CollapsingHeader("Custom Plotters and Tooltips")) + ShowDemo_CustomPlottersAndTooltips(); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Config")) { + ShowDemo_Configuration(); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Help")) { + ShowDemo_Help(); + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Configuration")) { - ImGui::ShowFontSelector("Font"); - ImGui::ShowStyleSelector("ImGui Style"); - ImPlot::ShowStyleSelector("ImPlot Style"); - ImPlot::ShowColormapSelector("ImPlot Colormap"); - float indent = ImGui::CalcItemWidth() - ImGui::GetFrameHeight(); - ImGui::Indent(ImGui::CalcItemWidth() - ImGui::GetFrameHeight()); - ImGui::Checkbox("Anti-Aliased Lines", &ImPlot::GetStyle().AntiAliasedLines); - ImGui::Unindent(indent); - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Line Plots")) { - static float xs1[1001], ys1[1001]; - for (int i = 0; i < 1001; ++i) { - xs1[i] = i * 0.001f; - ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)DEMO_TIME / 10)); - } - static double xs2[11], ys2[11]; - for (int i = 0; i < 11; ++i) { - xs2[i] = i * 0.1f; - ys2[i] = xs2[i] * xs2[i]; - } - ImGui::BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help)."); - if (ImPlot::BeginPlot("Line Plot", "x", "f(x)")) { - ImPlot::PlotLine("sin(x)", xs1, ys1, 1001); - ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); - ImPlot::PlotLine("x^2", xs2, ys2, 11); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Filled Line Plots")) { - static double xs1[101], ys1[101], ys2[101], ys3[101]; - srand(0); - for (int i = 0; i < 101; ++i) { - xs1[i] = (float)i; - ys1[i] = RandomRange(400.0,450.0); - ys2[i] = RandomRange(275.0,350.0); - ys3[i] = RandomRange(150.0,225.0); - } - static bool show_lines = true; - static bool show_fills = true; - static float fill_ref = 0; - static int shade_mode = 0; - ImGui::Checkbox("Lines",&show_lines); ImGui::SameLine(); - ImGui::Checkbox("Fills",&show_fills); - if (show_fills) { - ImGui::SameLine(); - if (ImGui::RadioButton("To -INF",shade_mode == 0)) - shade_mode = 0; - ImGui::SameLine(); - if (ImGui::RadioButton("To +INF",shade_mode == 1)) - shade_mode = 1; - ImGui::SameLine(); - if (ImGui::RadioButton("To Ref",shade_mode == 2)) - shade_mode = 2; - if (shade_mode == 2) { - ImGui::SameLine(); - ImGui::SetNextItemWidth(100); - ImGui::DragFloat("##Ref",&fill_ref, 1, -100, 500); - } - } - - - ImPlot::SetNextPlotLimits(0,100,0,500); - if (ImPlot::BeginPlot("Stock Prices", "Days", "Price")) { - if (show_fills) { - ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); - ImPlot::PlotShaded("Stock 1", xs1, ys1, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref); - ImPlot::PlotShaded("Stock 2", xs1, ys2, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref); - ImPlot::PlotShaded("Stock 3", xs1, ys3, 101, shade_mode == 0 ? -INFINITY : shade_mode == 1 ? INFINITY : fill_ref); - ImPlot::PopStyleVar(); - } - if (show_lines) { - ImPlot::PlotLine("Stock 1", xs1, ys1, 101); - ImPlot::PlotLine("Stock 2", xs1, ys2, 101); - ImPlot::PlotLine("Stock 3", xs1, ys3, 101); - } - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Shaded Plots##")) { - static float xs[1001], ys[1001], ys1[1001], ys2[1001], ys3[1001], ys4[1001]; - srand(0); - for (int i = 0; i < 1001; ++i) { - xs[i] = i * 0.001f; - ys[i] = 0.25f + 0.25f * sinf(25 * xs[i]) * sinf(5 * xs[i]) + RandomRange(-0.01f, 0.01f); - ys1[i] = ys[i] + RandomRange(0.1f, 0.12f); - ys2[i] = ys[i] - RandomRange(0.1f, 0.12f); - ys3[i] = 0.75f + 0.2f * sinf(25 * xs[i]); - ys4[i] = 0.75f + 0.1f * cosf(25 * xs[i]); - } - static float alpha = 0.25f; - ImGui::DragFloat("Alpha",&alpha,0.01f,0,1); - - if (ImPlot::BeginPlot("Shaded Plots", "X-Axis", "Y-Axis")) { - ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha); - ImPlot::PlotShaded("Uncertain Data",xs,ys1,ys2,1001); - ImPlot::PlotLine("Uncertain Data", xs, ys, 1001); - ImPlot::PlotShaded("Overlapping",xs,ys3,ys4,1001); - ImPlot::PlotLine("Overlapping",xs,ys3,1001); - ImPlot::PlotLine("Overlapping",xs,ys4,1001); - ImPlot::PopStyleVar(); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Scatter Plots")) { - srand(0); - static float xs1[100], ys1[100]; - for (int i = 0; i < 100; ++i) { - xs1[i] = i * 0.01f; - ys1[i] = xs1[i] + 0.1f * ((float)rand() / (float)RAND_MAX); - } - static float xs2[50], ys2[50]; - for (int i = 0; i < 50; i++) { - xs2[i] = 0.25f + 0.2f * ((float)rand() / (float)RAND_MAX); - ys2[i] = 0.75f + 0.2f * ((float)rand() / (float)RAND_MAX); - } - - if (ImPlot::BeginPlot("Scatter Plot", NULL, NULL)) { - ImPlot::PlotScatter("Data 1", xs1, ys1, 100); - ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); - ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, ImVec4(0,1,0,0.5f), IMPLOT_AUTO, ImVec4(0,1,0,1)); - ImPlot::PlotScatter("Data 2", xs2, ys2, 50); - ImPlot::PopStyleVar(); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Stairstep Plots")) { - static float ys1[101], ys2[101]; - for (int i = 0; i < 101; ++i) { - ys1[i] = 0.5f + 0.4f * sinf(50 * i * 0.01f); - ys2[i] = 0.5f + 0.2f * sinf(25 * i * 0.01f); - } - if (ImPlot::BeginPlot("Stairstep Plot", "x", "f(x)")) { - ImPlot::PlotStairs("Signal 1", ys1, 101, 0.01f); - ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 2.0f); - ImPlot::PlotStairs("Signal 2", ys2, 101, 0.01f); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Bar Plots")) { - - static bool horz = false; - static ImS8 midtm[10] = {83, 67, 23, 89, 83, 78, 91, 82, 85, 90}; - static ImS16 final[10] = {80, 62, 56, 99, 55, 78, 88, 78, 90, 100}; - static ImS32 grade[10] = {80, 69, 52, 92, 72, 78, 75, 76, 89, 95}; - - static const char* labels[] = {"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10"}; - static const double positions[] = {0,1,2,3,4,5,6,7,8,9}; - - ImGui::Checkbox("Horizontal",&horz); - - if (horz) { - ImPlot::SetNextPlotLimits(0, 110, -0.5, 9.5, ImGuiCond_Always); - ImPlot::SetNextPlotTicksY(positions, 10, labels); - } - else { - ImPlot::SetNextPlotLimits(-0.5, 9.5, 0, 110, ImGuiCond_Always); - ImPlot::SetNextPlotTicksX(positions, 10, labels); - } - if (ImPlot::BeginPlot("Bar Plot", horz ? "Score" : "Student", horz ? "Student" : "Score", - ImVec2(-1,0), 0, 0, horz ? ImPlotAxisFlags_Invert : 0)) - { - if (horz) { - ImPlot::SetLegendLocation(ImPlotLocation_West, ImPlotOrientation_Vertical); - ImPlot::PlotBarsH("Midterm Exam", midtm, 10, 0.2, -0.2); - ImPlot::PlotBarsH("Final Exam", final, 10, 0.2, 0); - ImPlot::PlotBarsH("Course Grade", grade, 10, 0.2, 0.2); - } - else { - ImPlot::SetLegendLocation(ImPlotLocation_South, ImPlotOrientation_Horizontal); - ImPlot::PlotBars("Midterm Exam", midtm, 10, 0.2, -0.2); - ImPlot::PlotBars("Final Exam", final, 10, 0.2, 0); - ImPlot::PlotBars("Course Grade", grade, 10, 0.2, 0.2); - } - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Error Bars")) { - static float xs[5] = {1,2,3,4,5}; - static float bar[5] = {1,2,5,3,4}; - static float lin1[5] = {8,8,9,7,8}; - static float lin2[5] = {6,7,6,9,6}; - static float err1[5] = {0.2f, 0.4f, 0.2f, 0.6f, 0.4f}; - static float err2[5] = {0.4f, 0.2f, 0.4f, 0.8f, 0.6f}; - static float err3[5] = {0.09f, 0.14f, 0.09f, 0.12f, 0.16f}; - static float err4[5] = {0.02f, 0.08f, 0.15f, 0.05f, 0.2f}; - - - ImPlot::SetNextPlotLimits(0, 6, 0, 10); - if (ImPlot::BeginPlot("##ErrorBars",NULL,NULL)) { - - ImPlot::PlotBars("Bar", xs, bar, 5, 0.5f); - ImPlot::PlotErrorBars("Bar", xs, bar, err1, 5); - - ImPlot::SetNextErrorBarStyle(ImPlot::GetColormapColor(1), 0); - ImPlot::PlotErrorBars("Line", xs, lin1, err1, err2, 5); - ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); - ImPlot::PlotLine("Line", xs, lin1, 5); - - ImPlot::PushStyleColor(ImPlotCol_ErrorBar, ImPlot::GetColormapColor(2)); - ImPlot::PlotErrorBars("Scatter", xs, lin2, err2, 5); - ImPlot::PlotErrorBarsH("Scatter", xs, lin2, err3, err4, 5); - ImPlot::PopStyleColor(); - ImPlot::PlotScatter("Scatter", xs, lin2, 5); - - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Stem Plots##")) { - static double xs[51], ys1[51], ys2[51]; - for (int i = 0; i < 51; ++i) { - xs[i] = i * 0.02; - ys1[i] = 1.0 + 0.5 * sin(25*xs[i])*cos(2*xs[i]); - ys2[i] = 0.5 + 0.25 * sin(10*xs[i]) * sin(xs[i]); - } - ImPlot::SetNextPlotLimits(0,1,0,1.6); - if (ImPlot::BeginPlot("Stem Plots")) { - ImPlot::PlotStems("Stems 1",xs,ys1,51); - ImPlot::SetNextLineStyle(ImVec4(1,0.5f,0,0.75f)); - ImPlot::SetNextMarkerStyle(ImPlotMarker_Square,5,ImVec4(1,0.5f,0,0.25f)); - ImPlot::PlotStems("Stems 2", xs, ys2,51); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Infinite Lines")) { - static double vals[] = {0.25, 0.5, 0.75}; - if (ImPlot::BeginPlot("##Infinite",0,0,ImVec2(-1,0),0,ImPlotAxisFlags_NoInitialFit,ImPlotAxisFlags_NoInitialFit)) { - ImPlot::PlotVLines("VLines",vals,3); - ImPlot::PlotHLines("HLines",vals,3); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Pie Charts")) { - static const char* labels1[] = {"Frogs","Hogs","Dogs","Logs"}; - static float data1[] = {0.15f, 0.30f, 0.2f, 0.05f}; - static bool normalize = false; - ImGui::SetNextItemWidth(250); - ImGui::DragFloat4("Values", data1, 0.01f, 0, 1); - if ((data1[0] + data1[1] + data1[2] + data1[3]) < 1) { - ImGui::SameLine(); - ImGui::Checkbox("Normalize", &normalize); - } - - ImPlot::SetNextPlotLimits(0,1,0,1,ImGuiCond_Always); - if (ImPlot::BeginPlot("##Pie1", NULL, NULL, ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMousePos, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) { - ImPlot::PlotPieChart(labels1, data1, 4, 0.5, 0.5, 0.4, normalize, "%.2f"); - ImPlot::EndPlot(); - } - - ImGui::SameLine(); - - static const char* labels2[] = {"A","B","C","D","E"}; - static int data2[] = {1,1,2,3,5}; - - ImPlot::PushColormap(ImPlotColormap_Pastel); - ImPlot::SetNextPlotLimits(0,1,0,1,ImGuiCond_Always); - if (ImPlot::BeginPlot("##Pie2", NULL, NULL, ImVec2(250,250), ImPlotFlags_Equal | ImPlotFlags_NoMousePos, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) { - ImPlot::PlotPieChart(labels2, data2, 5, 0.5, 0.5, 0.4, true, "%.0f", 180); - ImPlot::EndPlot(); - } - ImPlot::PopColormap(); - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Heatmaps")) { - static float values1[7][7] = {{0.8f, 2.4f, 2.5f, 3.9f, 0.0f, 4.0f, 0.0f}, - {2.4f, 0.0f, 4.0f, 1.0f, 2.7f, 0.0f, 0.0f}, - {1.1f, 2.4f, 0.8f, 4.3f, 1.9f, 4.4f, 0.0f}, - {0.6f, 0.0f, 0.3f, 0.0f, 3.1f, 0.0f, 0.0f}, - {0.7f, 1.7f, 0.6f, 2.6f, 2.2f, 6.2f, 0.0f}, - {1.3f, 1.2f, 0.0f, 0.0f, 0.0f, 3.2f, 5.1f}, - {0.1f, 2.0f, 0.0f, 1.4f, 0.0f, 1.9f, 6.3f}}; - static float scale_min = 0; - static float scale_max = 6.3f; - static const char* xlabels[] = {"C1","C2","C3","C4","C5","C6","C7"}; - static const char* ylabels[] = {"R1","R2","R3","R4","R5","R6","R7"}; - - static ImPlotColormap map = ImPlotColormap_Viridis; - if (ImPlot::ColormapButton(ImPlot::GetColormapName(map),ImVec2(225,0),map)) { - map = (map + 1) % ImPlot::GetColormapCount(); - // We bust the color cache of our plots so that item colors will - // resample the new colormap in the event that they have already - // been created. See documentation in implot.h. - BustColorCache("##Heatmap1"); - BustColorCache("##Heatmap2"); - } - - ImGui::SameLine(); - ImGui::LabelText("##Colormap Index", "%s", "Change Colormap"); - ImGui::SetNextItemWidth(225); - ImGui::DragFloatRange2("Min / Max",&scale_min, &scale_max, 0.01f, -20, 20); - static ImPlotAxisFlags axes_flags = ImPlotAxisFlags_Lock | ImPlotAxisFlags_NoGridLines | ImPlotAxisFlags_NoTickMarks; - - ImPlot::PushColormap(map); - SetNextPlotTicksX(0 + 1.0/14.0, 1 - 1.0/14.0, 7, xlabels); - SetNextPlotTicksY(1 - 1.0/14.0, 0 + 1.0/14.0, 7, ylabels); - if (ImPlot::BeginPlot("##Heatmap1",NULL,NULL,ImVec2(225,225),ImPlotFlags_NoLegend|ImPlotFlags_NoMousePos,axes_flags,axes_flags)) { - ImPlot::PlotHeatmap("heat",values1[0],7,7,scale_min,scale_max); - ImPlot::EndPlot(); - } - ImGui::SameLine(); - ImPlot::ColormapScale("##HeatScale",scale_min, scale_max, ImVec2(60,225)); - - ImGui::SameLine(); - - const int size = 200; - static double values2[size*size]; - srand((unsigned int)(DEMO_TIME*1000000)); - for (int i = 0; i < size*size; ++i) - values2[i] = RandomRange(0.0,1.0); - - ImPlot::SetNextPlotLimits(-1,1,-1,1); - if (ImPlot::BeginPlot("##Heatmap2",NULL,NULL,ImVec2(225,225),0,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations)) { - ImPlot::PlotHeatmap("heat1",values2,size,size,0,1,NULL); - ImPlot::PlotHeatmap("heat2",values2,size,size,0,1,NULL, ImPlotPoint(-1,-1), ImPlotPoint(0,0)); - ImPlot::EndPlot(); - } - ImPlot::PopColormap(); - - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Histograms")) { - static int bins = 50; - static bool cumulative = false; - static bool density = true; - static bool outliers = true; - static double mu = 5; - static double sigma = 2; - - ImGui::SetNextItemWidth(200); - if (ImGui::RadioButton("Sqrt",bins==ImPlotBin_Sqrt)) { bins = ImPlotBin_Sqrt; } ImGui::SameLine(); - if (ImGui::RadioButton("Sturges",bins==ImPlotBin_Sturges)) { bins = ImPlotBin_Sturges; } ImGui::SameLine(); - if (ImGui::RadioButton("Rice",bins==ImPlotBin_Rice)) { bins = ImPlotBin_Rice; } ImGui::SameLine(); - if (ImGui::RadioButton("Scott",bins==ImPlotBin_Scott)) { bins = ImPlotBin_Scott; } ImGui::SameLine(); - if (ImGui::RadioButton("N Bins",bins>=0)) bins = 50; - if (bins>=0) { - ImGui::SameLine(); - ImGui::SetNextItemWidth(200); - ImGui::SliderInt("##Bins", &bins, 1, 100); - } - if (ImGui::Checkbox("Density", &density)) - ImPlot::FitNextPlotAxes(); - ImGui::SameLine(); - if (ImGui::Checkbox("Cumulative", &cumulative)) - ImPlot::FitNextPlotAxes(); - ImGui::SameLine(); - static bool range = false; - ImGui::Checkbox("Range", &range); - static float rmin = -3; - static float rmax = 13; - if (range) { - ImGui::SameLine(); - ImGui::SetNextItemWidth(200); - ImGui::DragFloat2("##Range",&rmin,0.1f,-3,13); - ImGui::SameLine(); - ImGui::Checkbox("Outliers",&outliers); - } - - static NormalDistribution<10000> dist(mu, sigma); - static double x[100]; - static double y[100]; - if (density) { - for (int i = 0; i < 100; ++i) { - x[i] = -3 + 16 * (double)i/99.0; - y[i] = exp( - (x[i]-mu)*(x[i]-mu) / (2*sigma*sigma)) / (sigma * sqrt(2*3.141592653589793238)); - } - if (cumulative) { - for (int i = 1; i < 100; ++i) - y[i] += y[i-1]; - for (int i = 0; i < 100; ++i) - y[i] /= y[99]; - } - } - - if (ImPlot::BeginPlot("##Histograms")) { - ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL, 0.5f); - ImPlot::PlotHistogram("Empirical", dist.Data, 10000, bins, cumulative, density, range ? ImPlotRange(rmin,rmax) : ImPlotRange(), outliers); - if (density && outliers) - ImPlot::PlotLine("Theoretical",x,y,100); - ImPlot::EndPlot(); - } - - static int count = 500000; - static int xybins[2] = {200,200}; - static bool density2 = false; - ImGui::SliderInt("Count",&count,100,500000); - ImGui::SliderInt2("Bins",xybins,1,500); - ImGui::SameLine(); - ImGui::Checkbox("Density##2",&density2); - static NormalDistribution<500000> dist1(1, 2); - static NormalDistribution<500000> dist2(1, 1); - double max_count = 0; - ImPlotAxisFlags flags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_Foreground; - ImPlot::PushColormap("Hot"); - if (ImPlot::BeginPlot("##Hist2D",0,0,ImVec2(ImGui::GetContentRegionAvail().x-100-ImGui::GetStyle().ItemSpacing.x,0),0,flags,flags)) { - max_count = ImPlot::PlotHistogram2D("Hist2D",dist1.Data,dist2.Data,count,xybins[0],xybins[1],density2,ImPlotLimits(-6,6,-6,6)); - ImPlot::EndPlot(); - } - ImGui::SameLine(); - ImPlot::ColormapScale(density2 ? "Density" : "Count",0,max_count,ImVec2(100,0)); - ImPlot::PopColormap(); - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Digital Plots")) { - ImGui::BulletText("Digital plots do not respond to Y drag and zoom, so that"); - ImGui::Indent(); - ImGui::Text("you can drag analog plots over the rising/falling digital edge."); - ImGui::Unindent(); - - static bool paused = false; - static ScrollingBuffer dataDigital[2]; - static ScrollingBuffer dataAnalog[2]; - static bool showDigital[2] = {true, false}; - static bool showAnalog[2] = {true, false}; - - char label[32]; - ImGui::Checkbox("digital_0", &showDigital[0]); ImGui::SameLine(); - ImGui::Checkbox("digital_1", &showDigital[1]); ImGui::SameLine(); - ImGui::Checkbox("analog_0", &showAnalog[0]); ImGui::SameLine(); - ImGui::Checkbox("analog_1", &showAnalog[1]); - - static float t = 0; - if (!paused) { - t += ImGui::GetIO().DeltaTime; - //digital signal values - if (showDigital[0]) - dataDigital[0].AddPoint(t, sinf(2*t) > 0.45); - if (showDigital[1]) - dataDigital[1].AddPoint(t, sinf(2*t) < 0.45); - //Analog signal values - if (showAnalog[0]) - dataAnalog[0].AddPoint(t, sinf(2*t)); - if (showAnalog[1]) - dataAnalog[1].AddPoint(t, cosf(2*t)); - } - ImPlot::SetNextPlotLimitsY(-1, 1); - ImPlot::SetNextPlotLimitsX(t - 10.0, t, paused ? ImGuiCond_Once : ImGuiCond_Always); - if (ImPlot::BeginPlot("##Digital")) { - for (int i = 0; i < 2; ++i) { - if (showDigital[i] && dataDigital[i].Data.size() > 0) { - sprintf(label, "digital_%d", i); - ImPlot::PlotDigital(label, &dataDigital[i].Data[0].x, &dataDigital[i].Data[0].y, dataDigital[i].Data.size(), dataDigital[i].Offset, 2 * sizeof(float)); - } - } - for (int i = 0; i < 2; ++i) { - if (showAnalog[i]) { - sprintf(label, "analog_%d", i); - if (dataAnalog[i].Data.size() > 0) - ImPlot::PlotLine(label, &dataAnalog[i].Data[0].x, &dataAnalog[i].Data[0].y, dataAnalog[i].Data.size(), dataAnalog[i].Offset, 2 * sizeof(float)); - } - } - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Images")) { - ImGui::BulletText("Below we are displaying the font texture, which is the only texture we have\naccess to in this demo."); - ImGui::BulletText("Use the 'ImTextureID' type as storage to pass pointers or identifiers to your\nown texture data."); - ImGui::BulletText("See ImGui Wiki page 'Image Loading and Displaying Examples'."); - static ImVec2 bmin(0,0); - static ImVec2 bmax(1,1); - static ImVec2 uv0(0,0); - static ImVec2 uv1(1,1); - static ImVec4 tint(1,1,1,1); - ImGui::SliderFloat2("Min", &bmin.x, -2, 2, "%.1f"); - ImGui::SliderFloat2("Max", &bmax.x, -2, 2, "%.1f"); - ImGui::SliderFloat2("UV0", &uv0.x, -2, 2, "%.1f"); - ImGui::SliderFloat2("UV1", &uv1.x, -2, 2, "%.1f"); - ImGui::ColorEdit4("Tint",&tint.x); - if (ImPlot::BeginPlot("##image")) { - ImPlot::PlotImage("my image",ImGui::GetIO().Fonts->TexID, bmin, bmax, uv0, uv1, tint); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Realtime Plots")) { - ImGui::BulletText("Move your mouse to change the data!"); - ImGui::BulletText("This example assumes 60 FPS. Higher FPS requires larger buffer size."); - static ScrollingBuffer sdata1, sdata2; - static RollingBuffer rdata1, rdata2; - ImVec2 mouse = ImGui::GetMousePos(); - static float t = 0; - t += ImGui::GetIO().DeltaTime; - sdata1.AddPoint(t, mouse.x * 0.0005f); - rdata1.AddPoint(t, mouse.x * 0.0005f); - sdata2.AddPoint(t, mouse.y * 0.0005f); - rdata2.AddPoint(t, mouse.y * 0.0005f); - - static float history = 10.0f; - ImGui::SliderFloat("History",&history,1,30,"%.1f s"); - rdata1.Span = history; - rdata2.Span = history; - - static ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels; - ImPlot::SetNextPlotLimitsX(t - history, t, ImGuiCond_Always); - ImPlot::SetNextPlotLimitsY(0,1); - if (ImPlot::BeginPlot("##Scrolling", NULL, NULL, ImVec2(-1,150), 0, flags, flags)) { - ImPlot::SetNextFillStyle(IMPLOT_AUTO_COL,0.5f); - ImPlot::PlotShaded("Mouse X", &sdata1.Data[0].x, &sdata1.Data[0].y, sdata1.Data.size(), -INFINITY, sdata1.Offset, 2 * sizeof(float)); - ImPlot::PlotLine("Mouse Y", &sdata2.Data[0].x, &sdata2.Data[0].y, sdata2.Data.size(), sdata2.Offset, 2*sizeof(float)); - ImPlot::EndPlot(); - } - ImPlot::SetNextPlotLimitsX(0, history, ImGuiCond_Always); - ImPlot::SetNextPlotLimitsY(0,1); - if (ImPlot::BeginPlot("##Rolling", NULL, NULL, ImVec2(-1,150), 0, flags, flags)) { - ImPlot::PlotLine("Mouse X", &rdata1.Data[0].x, &rdata1.Data[0].y, rdata1.Data.size(), 0, 2 * sizeof(float)); - ImPlot::PlotLine("Mouse Y", &rdata2.Data[0].x, &rdata2.Data[0].y, rdata2.Data.size(), 0, 2 * sizeof(float)); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Markers and Text")) { - static float mk_size = ImPlot::GetStyle().MarkerSize; - static float mk_weight = ImPlot::GetStyle().MarkerWeight; - ImGui::DragFloat("Marker Size",&mk_size,0.1f,2.0f,10.0f,"%.2f px"); - ImGui::DragFloat("Marker Weight", &mk_weight,0.05f,0.5f,3.0f,"%.2f px"); - - ImPlot::SetNextPlotLimits(0, 10, 0, 12); - if (ImPlot::BeginPlot("##MarkerStyles", NULL, NULL, ImVec2(-1,0), ImPlotFlags_CanvasOnly, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) { - ImS8 xs[2] = {1,4}; - ImS8 ys[2] = {10,11}; - - // filled markers - for (int m = 0; m < ImPlotMarker_COUNT; ++m) { - ImGui::PushID(m); - ImPlot::SetNextMarkerStyle(m, mk_size, IMPLOT_AUTO_COL, mk_weight); - ImPlot::PlotLine("##Filled", xs, ys, 2); - ImGui::PopID(); - ys[0]--; ys[1]--; - } - xs[0] = 6; xs[1] = 9; ys[0] = 10; ys[1] = 11; - // open markers - for (int m = 0; m < ImPlotMarker_COUNT; ++m) { - ImGui::PushID(m); - ImPlot::SetNextMarkerStyle(m, mk_size, ImVec4(0,0,0,0), mk_weight); - ImPlot::PlotLine("##Open", xs, ys, 2); - ImGui::PopID(); - ys[0]--; ys[1]--; - } - - ImPlot::PlotText("Filled Markers", 2.5f, 6.0f); - ImPlot::PlotText("Open Markers", 7.5f, 6.0f); - - ImPlot::PushStyleColor(ImPlotCol_InlayText, ImVec4(1,0,1,1)); - ImPlot::PlotText("Vertical Text", 5.0f, 6.0f, true); - ImPlot::PopStyleColor(); - - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Log Scale")) { - static double xs[1001], ys1[1001], ys2[1001], ys3[1001]; - for (int i = 0; i < 1001; ++i) { - xs[i] = i*0.1f; - ys1[i] = sin(xs[i]) + 1; - ys2[i] = log(xs[i]); - ys3[i] = pow(10.0, xs[i]); - } - ImGui::BulletText("Open the plot context menu (right click) to change scales."); - - ImPlot::SetNextPlotLimits(0.1, 100, 0, 10); - if (ImPlot::BeginPlot("Log Plot", NULL, NULL, ImVec2(-1,0), 0, ImPlotAxisFlags_LogScale )) { - ImPlot::PlotLine("f(x) = x", xs, xs, 1001); - ImPlot::PlotLine("f(x) = sin(x)+1", xs, ys1, 1001); - ImPlot::PlotLine("f(x) = log(x)", xs, ys2, 1001); - ImPlot::PlotLine("f(x) = 10^x", xs, ys3, 21); - ImPlot::EndPlot(); - } - } - if (ImGui::CollapsingHeader("Time Formatted Axes")) { - - static double t_min = 1609459200; // 01/01/2021 @ 12:00:00am (UTC) - static double t_max = 1640995200; // 01/01/2022 @ 12:00:00am (UTC) - - ImGui::BulletText("When ImPlotAxisFlags_Time is enabled on the X-Axis, values are interpreted as\n" - "UNIX timestamps in seconds and axis labels are formated as date/time."); - ImGui::BulletText("By default, labels are in UTC time but can be set to use local time instead."); - - ImGui::Checkbox("Local Time",&ImPlot::GetStyle().UseLocalTime); - ImGui::SameLine(); - ImGui::Checkbox("ISO 8601",&ImPlot::GetStyle().UseISO8601); - ImGui::SameLine(); - ImGui::Checkbox("24 Hour Clock",&ImPlot::GetStyle().Use24HourClock); - - static HugeTimeData* data = NULL; - if (data == NULL) { - ImGui::SameLine(); - if (ImGui::Button("Generate Huge Data (~500MB!)")) { - static HugeTimeData sdata(t_min); - data = &sdata; - } - } - - ImPlot::SetNextPlotLimits(t_min,t_max,0,1); - if (ImPlot::BeginPlot("##Time", NULL, NULL, ImVec2(-1,0), 0, ImPlotAxisFlags_Time)) { - if (data != NULL) { - // downsample our data - int downsample = (int)ImPlot::GetPlotLimits().X.Size() / 1000 + 1; - int start = (int)(ImPlot::GetPlotLimits().X.Min - t_min); - start = start < 0 ? 0 : start > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : start; - int end = (int)(ImPlot::GetPlotLimits().X.Max - t_min) + 1000; - end = end < 0 ? 0 : end > HugeTimeData::Size - 1 ? HugeTimeData::Size - 1 : end; - int size = (end - start)/downsample; - // plot it - ImPlot::PlotLine("Time Series", &data->Ts[start], &data->Ys[start], size, 0, sizeof(double)*downsample); - } - // plot time now - double t_now = (double)time(0); - double y_now = HugeTimeData::GetY(t_now); - ImPlot::PlotScatter("Now",&t_now,&y_now,1); - ImPlot::Annotate(t_now,y_now,ImVec2(10,10),ImPlot::GetLastItemColor(),"Now"); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Multiple Y-Axes")) { - static float xs[1001], xs2[1001], ys1[1001], ys2[1001], ys3[1001]; - for (int i = 0; i < 1001; ++i) { - xs[i] = (i*0.1f); - ys1[i] = sinf(xs[i]) * 3 + 1; - ys2[i] = cosf(xs[i]) * 0.2f + 0.5f; - ys3[i] = sinf(xs[i]+0.5f) * 100 + 200; - xs2[i] = xs[i] + 10.0f; - } - static bool y2_axis = true; - static bool y3_axis = true; - ImGui::Checkbox("Y-Axis 2", &y2_axis); - ImGui::SameLine(); - ImGui::Checkbox("Y-Axis 3", &y3_axis); - ImGui::SameLine(); - - // you can fit axes programatically - ImGui::SameLine(); if (ImGui::Button("Fit X")) ImPlot::FitNextPlotAxes(true, false, false, false); - ImGui::SameLine(); if (ImGui::Button("Fit Y")) ImPlot::FitNextPlotAxes(false, true, false, false); - ImGui::SameLine(); if (ImGui::Button("Fit Y2")) ImPlot::FitNextPlotAxes(false, false, true, false); - ImGui::SameLine(); if (ImGui::Button("Fit Y3")) ImPlot::FitNextPlotAxes(false, false, false, true); - - ImPlot::SetNextPlotLimits(0.1, 100, 0, 10); - ImPlot::SetNextPlotLimitsY(0, 1, ImGuiCond_Once, 1); - ImPlot::SetNextPlotLimitsY(0, 300, ImGuiCond_Once, 2); - if (ImPlot::BeginPlot("Multi-Axis Plot", NULL, "Y-Axis 1", ImVec2(-1,0), - (y2_axis ? ImPlotFlags_YAxis2 : 0) | - (y3_axis ? ImPlotFlags_YAxis3 : 0), - ImPlotAxisFlags_None, ImPlotAxisFlags_None, ImPlotAxisFlags_NoGridLines, ImPlotAxisFlags_NoGridLines, - "Y-Axis 2", "Y-Axis 3")) { - ImPlot::PlotLine("f(x) = x", xs, xs, 1001); - ImPlot::PlotLine("f(x) = sin(x)*3+1", xs, ys1, 1001); - if (y2_axis) { - ImPlot::SetPlotYAxis(ImPlotYAxis_2); - ImPlot::PlotLine("f(x) = cos(x)*.2+.5 (Y2)", xs, ys2, 1001); - } - if (y3_axis) { - ImPlot::SetPlotYAxis(ImPlotYAxis_3); - ImPlot::PlotLine("f(x) = sin(x+.5)*100+200 (Y3)", xs2, ys3, 1001); - } - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Linked Axes")) { - static double xmin = 0, xmax = 1, ymin = 0, ymax = 1; - static bool linkx = true, linky = true; - int data[2] = {0,1}; - ImGui::Checkbox("Link X", &linkx); - ImGui::SameLine(); - ImGui::Checkbox("Link Y", &linky); - ImPlot::LinkNextPlotLimits(linkx ? &xmin : NULL , linkx ? &xmax : NULL, linky ? &ymin : NULL, linky ? &ymax : NULL); - if (ImPlot::BeginPlot("Plot A")) { - ImPlot::PlotLine("Line",data,2); - ImPlot::EndPlot(); - } - ImPlot::LinkNextPlotLimits(linkx ? &xmin : NULL , linkx ? &xmax : NULL, linky ? &ymin : NULL, linky ? &ymax : NULL); - if (ImPlot::BeginPlot("Plot B")) { - ImPlot::PlotLine("Line",data,2); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Equal Axes")) { - static double xs[1000], ys[1000]; - for (int i = 0; i < 1000; ++i) { - double angle = i * 2 * PI / 999.0; - xs[i] = cos(angle); ys[i] = sin(angle); - } - if (ImPlot::BeginPlot("",0,0,ImVec2(-1,0),ImPlotFlags_Equal)) { - ImPlot::PlotLine("Circle",xs,ys,1000); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Auto-Fitting Data")) { - - ImGui::BulletText("The Y-axis has been configured to auto-fit to only the data visible in X-axis range."); - ImGui::BulletText("Zoom and pan the X-axis. Disable Stems to see a difference in fit."); - ImGui::BulletText("If ImPlotAxisFlags_RangeFit is disabled, the axis will fit ALL data."); - - static ImPlotAxisFlags xflags = ImPlotAxisFlags_None; - static ImPlotAxisFlags yflags = ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit; - - ImGui::TextUnformatted("X: "); ImGui::SameLine(); - ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine(); - ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##X", (unsigned int*)&xflags, ImPlotAxisFlags_RangeFit); - - ImGui::TextUnformatted("Y: "); ImGui::SameLine(); - ImGui::CheckboxFlags("ImPlotAxisFlags_AutoFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_AutoFit); ImGui::SameLine(); - ImGui::CheckboxFlags("ImPlotAxisFlags_RangeFit##Y", (unsigned int*)&yflags, ImPlotAxisFlags_RangeFit); - - static double data[101]; - srand(0); - for (int i = 0; i < 101; ++i) - data[i] = 1 + sin(i/10.0f); - - if (ImPlot::BeginPlot("##DataFitting","X","Y",ImVec2(-1,0),0,xflags,yflags)) { - ImPlot::PlotLine("Line",data,101); - ImPlot::PlotStems("Stems",data,101); - ImPlot::EndPlot(); - }; - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Querying")) { - static ImVector data; - static ImPlotLimits range, query, select; - - ImGui::BulletText("Ctrl + click in the plot area to draw points."); - ImGui::BulletText("Middle click (or Ctrl + right click) and drag to create a query rect."); - ImGui::Indent(); - ImGui::BulletText("Hold Alt to expand query horizontally."); - ImGui::BulletText("Hold Shift to expand query vertically."); - ImGui::BulletText("The query rect can be dragged after it's created."); - ImGui::Unindent(); - - if (ImPlot::BeginPlot("##Drawing", NULL, NULL, ImVec2(-1,0), ImPlotFlags_Query, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) { - if (ImPlot::IsPlotHovered() && ImGui::IsMouseClicked(0) && ImGui::GetIO().KeyCtrl) { - ImPlotPoint pt = ImPlot::GetPlotMousePos(); - data.push_back(pt); - } - if (data.size() > 0) - ImPlot::PlotScatter("Points", &data[0].x, &data[0].y, data.size(), 0, 2 * sizeof(double)); - if (ImPlot::IsPlotQueried() && data.size() > 0) { - ImPlotLimits range2 = ImPlot::GetPlotQuery(); - int cnt = 0; - ImPlotPoint avg; - for (int i = 0; i < data.size(); ++i) { - if (range2.Contains(data[i].x, data[i].y)) { - avg.x += data[i].x; - avg.y += data[i].y; - cnt++; - } - } - if (cnt > 0) { - avg.x = avg.x / cnt; - avg.y = avg.y / cnt; - ImPlot::SetNextMarkerStyle(ImPlotMarker_Square); - ImPlot::PlotScatter("Average", &avg.x, &avg.y, 1); - } - } - range = ImPlot::GetPlotLimits(); - query = ImPlot::GetPlotQuery(); - select = ImPlot::GetPlotSelection(); - ImPlot::EndPlot(); - } - ImGui::Text("Limits: [%g,%g,%g,%g]", range.X.Min, range.X.Max, range.Y.Min, range.Y.Max); - ImGui::Text("Query: [%g,%g,%g,%g]", query.X.Min, query.X.Max, query.Y.Min, query.Y.Max); - ImGui::Text("Selection: [%g,%g,%g,%g]", select.X.Min, select.X.Max, select.Y.Min, select.Y.Max); - - ImGui::Separator(); - - // mimic's soulthread's imgui_plot demo - static float x_data[512]; - static float y_data1[512]; - static float y_data2[512]; - static float y_data3[512]; - static float sampling_freq = 44100; - static float freq = 500; - for (size_t i = 0; i < 512; ++i) { - const float t = i / sampling_freq; - x_data[i] = t; - const float arg = 2 * 3.14f * freq * t; - y_data1[i] = sinf(arg); - y_data2[i] = y_data1[i] * -0.6f + sinf(2 * arg) * 0.4f; - y_data3[i] = y_data2[i] * -0.6f + sinf(3 * arg) * 0.4f; - } - ImGui::BulletText("Query the first plot to render a subview in the second plot (see above for controls)."); - ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels; - static bool use_selection = false; - ImGui::Checkbox("Use Box Selection",&use_selection); - bool is_viewed = false; - ImPlotLimits view; - ImPlot::SetNextPlotLimits(0,0.01,-1,1); - if (ImPlot::BeginPlot("##View1",NULL,NULL,ImVec2(-1,150), ImPlotFlags_Query, flags, flags)) { - ImPlot::PlotLine("Signal 1", x_data, y_data1, 512); - ImPlot::PlotLine("Signal 2", x_data, y_data2, 512); - ImPlot::PlotLine("Signal 3", x_data, y_data3, 512); - is_viewed = use_selection ? ImPlot::IsPlotSelected() : ImPlot::IsPlotQueried(); - view = use_selection ? ImPlot::GetPlotSelection() : ImPlot::GetPlotQuery(); - ImPlot::EndPlot(); - } - ImPlot::SetNextPlotLimits(view.X.Min, view.X.Max, view.Y.Min, view.Y.Max, ImGuiCond_Always); - if (ImPlot::BeginPlot("##View2",NULL,NULL,ImVec2(-1,150), ImPlotFlags_CanvasOnly, ImPlotAxisFlags_NoDecorations, ImPlotAxisFlags_NoDecorations)) { - if (is_viewed) { - ImPlot::PlotLine("Signal 1", x_data, y_data1, 512); - ImPlot::PlotLine("Signal 2", x_data, y_data2, 512); - ImPlot::PlotLine("Signal 3", x_data, y_data3, 512); - } - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Legend")) { - static ImPlotLocation loc = ImPlotLocation_East; - static bool h = false; static bool o = true; - ImGui::CheckboxFlags("North", (unsigned int*)&loc, ImPlotLocation_North); ImGui::SameLine(); - ImGui::CheckboxFlags("South", (unsigned int*)&loc, ImPlotLocation_South); ImGui::SameLine(); - ImGui::CheckboxFlags("West", (unsigned int*)&loc, ImPlotLocation_West); ImGui::SameLine(); - ImGui::CheckboxFlags("East", (unsigned int*)&loc, ImPlotLocation_East); ImGui::SameLine(); - ImGui::Checkbox("Horizontal##2", &h); ImGui::SameLine(); - ImGui::Checkbox("Outside", &o); - - ImGui::SliderFloat2("LegendPadding", (float*)&GetStyle().LegendPadding, 0.0f, 20.0f, "%.0f"); - ImGui::SliderFloat2("LegendInnerPadding", (float*)&GetStyle().LegendInnerPadding, 0.0f, 10.0f, "%.0f"); - ImGui::SliderFloat2("LegendSpacing", (float*)&GetStyle().LegendSpacing, 0.0f, 5.0f, "%.0f"); - - if (ImPlot::BeginPlot("##Legend","x","y",ImVec2(-1,0))) { - ImPlot::SetLegendLocation(loc, h ? ImPlotOrientation_Horizontal : ImPlotOrientation_Vertical, o); - static MyImPlot::WaveData data1(0.001, 0.2, 2, 0.75); - static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.25); - static MyImPlot::WaveData data3(0.001, 0.2, 6, 0.5); - ImPlot::PlotLineG("Item 1", MyImPlot::SineWave, &data1, 1000); // "Item 1" added to legend - ImPlot::PlotLineG("Item 2##IDText", MyImPlot::SawWave, &data2, 1000); // "Item 2" added to legend, text after ## used for ID only - ImPlot::PlotLineG("##NotListed", MyImPlot::SawWave, &data3, 1000); // plotted, but not added to legend - ImPlot::PlotLineG("Item 3", MyImPlot::SineWave, &data1, 1000); // "Item 3" added to legend - ImPlot::PlotLineG("Item 3", MyImPlot::SawWave, &data2, 1000); // combined with previous "Item 3" - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Drag Lines and Points")) { - ImGui::BulletText("Click and drag the horizontal and vertical lines."); - static double x1 = 0.2; - static double x2 = 0.8; - static double y1 = 0.25; - static double y2 = 0.75; - static double f = 0.1; - static bool show_labels = true; - ImGui::Checkbox("Show Labels##1",&show_labels); - ImPlot::SetNextPlotLimits(0,1,0,1); - if (ImPlot::BeginPlot("##guides",0,0,ImVec2(-1,0),ImPlotFlags_YAxis2)) { - ImPlot::DragLineX("x1",&x1,show_labels); - ImPlot::DragLineX("x2",&x2,show_labels); - ImPlot::DragLineY("y1",&y1,show_labels); - ImPlot::DragLineY("y2",&y2,show_labels); - double xs[1000], ys[1000]; - for (int i = 0; i < 1000; ++i) { - xs[i] = (x2+x1)/2+fabs(x2-x1)*(i/1000.0f - 0.5f); - ys[i] = (y1+y2)/2+fabs(y2-y1)/2*sin(f*i/10); - } - ImPlot::PlotLine("Interactive Data", xs, ys, 1000); - ImPlot::SetPlotYAxis(ImPlotYAxis_2); - ImPlot::DragLineY("f",&f,show_labels,ImVec4(1,0.5f,1,1)); - ImPlot::EndPlot(); - } - ImGui::BulletText("Click and drag any point."); - ImGui::Checkbox("Show Labels##2",&show_labels); - ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoTickMarks; - ImPlot::SetNextPlotLimits(0,1,0,1); - if (ImPlot::BeginPlot("##Bezier",0,0,ImVec2(-1,0),ImPlotFlags_CanvasOnly,flags,flags)) { - static ImPlotPoint P[] = {ImPlotPoint(.05f,.05f), ImPlotPoint(0.2,0.4), ImPlotPoint(0.8,0.6), ImPlotPoint(.95f,.95f)}; - static ImPlotPoint B[100]; - for (int i = 0; i < 100; ++i) { - double t = i / 99.0; - double u = 1 - t; - double w1 = u*u*u; - double w2 = 3*u*u*t; - double w3 = 3*u*t*t; - double w4 = t*t*t; - 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); - } - ImPlot::SetNextLineStyle(ImVec4(0,0.9f,0,1), 2); - ImPlot::PlotLine("##bez",&B[0].x, &B[0].y, 100, 0, sizeof(ImPlotPoint)); - ImPlot::SetNextLineStyle(ImVec4(1,0.5f,1,1)); - ImPlot::PlotLine("##h1",&P[0].x, &P[0].y, 2, 0, sizeof(ImPlotPoint)); - ImPlot::SetNextLineStyle(ImVec4(0,0.5f,1,1)); - ImPlot::PlotLine("##h2",&P[2].x, &P[2].y, 2, 0, sizeof(ImPlotPoint)); - ImPlot::DragPoint("P0",&P[0].x,&P[0].y, show_labels, ImVec4(0,0.9f,0,1)); - ImPlot::DragPoint("P1",&P[1].x,&P[1].y, show_labels, ImVec4(1,0.5f,1,1)); - ImPlot::DragPoint("P2",&P[2].x,&P[2].y, show_labels, ImVec4(0,0.5f,1,1)); - ImPlot::DragPoint("P3",&P[3].x,&P[3].y, show_labels, ImVec4(0,0.9f,0,1)); - ImPlot::EndPlot(); - } - } - if (ImGui::CollapsingHeader("Annotations")) { - static bool clamp = false; - ImGui::Checkbox("Clamp",&clamp); - ImPlot::SetNextPlotLimits(0,2,0,1); - if (ImPlot::BeginPlot("##Annotations")) { - - static float p[] = {0.25f, 0.25f, 0.75f, 0.75f, 0.25f}; - ImPlot::PlotScatter("##Points",&p[0],&p[1],4); - ImVec4 col = GetLastItemColor(); - clamp ? ImPlot::AnnotateClamped(0.25,0.25,ImVec2(-15,15),col,"BL") : ImPlot::Annotate(0.25,0.25,ImVec2(-15,15),col,"BL"); - clamp ? ImPlot::AnnotateClamped(0.75,0.25,ImVec2(15,15),col,"BR") : ImPlot::Annotate(0.75,0.25,ImVec2(15,15),col,"BR"); - clamp ? ImPlot::AnnotateClamped(0.75,0.75,ImVec2(15,-15),col,"TR") : ImPlot::Annotate(0.75,0.75,ImVec2(15,-15),col,"TR"); - clamp ? ImPlot::AnnotateClamped(0.25,0.75,ImVec2(-15,-15),col,"TL") : ImPlot::Annotate(0.25,0.75,ImVec2(-15,-15),col,"TL"); - clamp ? ImPlot::AnnotateClamped(0.5,0.5,ImVec2(0,0),col,"Center") : ImPlot::Annotate(0.5,0.5,ImVec2(0,0),col,"Center"); - - float bx[] = {1.2f,1.5f,1.8f}; - float by[] = {0.25f, 0.5f, 0.75f}; - ImPlot::PlotBars("##Bars",bx,by,3,0.2); - for (int i = 0; i < 3; ++i) - ImPlot::Annotate(bx[i],by[i],ImVec2(0,-5),"B[%d]=%.2f",i,by[i]); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Drag and Drop")) { - ImGui::BulletText("Drag/drop items from the left column."); - ImGui::BulletText("Drag/drop items between plots."); - ImGui::Indent(); - ImGui::BulletText("Plot 1 Targets: Plot, Y-Axes, Legend"); - ImGui::BulletText("Plot 1 Sources: Legend Items"); - ImGui::BulletText("Plot 2 Targets: Plot, X-Axis, Y-Axis"); - ImGui::BulletText("Plot 2 Sources: Plot, X-Axis, Y-Axis (hold Ctrl)"); - ImGui::Unindent(); - - // convenience struct to manage DND items; do this however you like - struct MyDndItem { - int Idx; - int Plt; - int Yax; - char Label[16]; - ImVector Data; - ImVec4 Color; - MyDndItem() { - static int i = 0; - Idx = i++; - Plt = 0; - Yax = ImPlotYAxis_1; - sprintf(Label, "%02d Hz", Idx+1); - Color = RandomColor(); - Data.reserve(1001); - for (int k = 0; k < 1001; ++k) { - float t = k * 1.0f / 999; - Data.push_back(ImVec2(t, 0.5f + 0.5f * sinf(2*3.14f*t*(Idx+1)))); - } - } - void Reset() { Plt = 0; Yax = ImPlotYAxis_1; } - }; - - const int k_dnd = 20; - static MyDndItem dnd[k_dnd]; - static MyDndItem* dndx = NULL; // for plot 2 - static MyDndItem* dndy = NULL; // for plot 2 - - // child window to serve as initial source for our DND items - ImGui::BeginChild("DND_LEFT",ImVec2(100,400)); - if (ImGui::Button("Reset Data", ImVec2(100, 0))) { - for (int k = 0; k < k_dnd; ++k) - dnd[k].Reset(); - dndx = dndy = NULL; - } - for (int k = 0; k < k_dnd; ++k) { - if (dnd[k].Plt > 0) - continue; - ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine(); - ImGui::Selectable(dnd[k].Label, false, 0, ImVec2(100, 0)); - if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) { - ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int)); - ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine(); - ImGui::TextUnformatted(dnd[k].Label); - ImGui::EndDragDropSource(); - } - } - ImGui::EndChild(); - if (ImGui::BeginDragDropTarget()) { - if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { - int i = *(int*)payload->Data; dnd[i].Reset(); - } - ImGui::EndDragDropTarget(); - } - - ImGui::SameLine(); - ImGui::BeginChild("DND_RIGHT",ImVec2(-1,400)); - // plot 1 (time series) - ImPlotAxisFlags flags = ImPlotAxisFlags_NoTickLabels | ImPlotAxisFlags_NoGridLines; - if (ImPlot::BeginPlot("##DND1", NULL, "[drop here]", ImVec2(-1,195), ImPlotFlags_YAxis2 | ImPlotFlags_YAxis3, flags | ImPlotAxisFlags_Lock, flags, flags, flags, "[drop here]", "[drop here]")) { - for (int k = 0; k < k_dnd; ++k) { - if (dnd[k].Plt == 1 && dnd[k].Data.size() > 0) { - ImPlot::SetPlotYAxis(dnd[k].Yax); - ImPlot::SetNextLineStyle(dnd[k].Color); - static char label[32]; - sprintf(label,"%s (Y%d)", dnd[k].Label, dnd[k].Yax+1); - ImPlot::PlotLine(label, &dnd[k].Data[0].x, &dnd[k].Data[0].y, dnd[k].Data.size(), 0, 2 * sizeof(float)); - // allow legend item labels to be DND sources - if (ImPlot::BeginDragDropSourceItem(label)) { - ImGui::SetDragDropPayload("MY_DND", &k, sizeof(int)); - ImPlot::ItemIcon(dnd[k].Color); ImGui::SameLine(); - ImGui::TextUnformatted(dnd[k].Label); - ImPlot::EndDragDropSource(); - } - } - } - // allow the main plot area to be a DND target - if (ImPlot::BeginDragDropTarget()) { - if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { - int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = 0; - } - ImPlot::EndDragDropTarget(); - } - // allow each y-axis to be a DND target - for (int y = 0; y < 3; ++y) { - if (ImPlot::BeginDragDropTargetY(y)) { - if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { - int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = y; - } - ImPlot::EndDragDropTarget(); - } - } - // allow the legend to be a DND target - if (ImPlot::BeginDragDropTargetLegend()) { - if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { - int i = *(int*)payload->Data; dnd[i].Plt = 1; dnd[i].Yax = 0; - } - ImPlot::EndDragDropTarget(); - } - ImPlot::EndPlot(); - } - // plot 2 (Lissajous) - ImPlot::PushStyleColor(ImPlotCol_XAxis, dndx == NULL ? ImPlot::GetStyle().Colors[ImPlotCol_XAxis] : dndx->Color); - ImPlot::PushStyleColor(ImPlotCol_YAxis, dndy == NULL ? ImPlot::GetStyle().Colors[ImPlotCol_YAxis] : dndy->Color); - if (ImPlot::BeginPlot("##DND2", dndx == NULL ? "[drop here]" : dndx->Label, dndy == NULL ? "[drop here]" : dndy->Label, ImVec2(-1,195), 0, flags, flags )) { - if (dndx != NULL && dndy != NULL) { - 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); - ImPlot::SetNextLineStyle(mixed); - ImPlot::PlotLine("##dndxy", &dndx->Data[0].y, &dndy->Data[0].y, dndx->Data.size(), 0, 2 * sizeof(float)); - } - // allow the x-axis to be a DND target - if (ImPlot::BeginDragDropTargetX()) { - if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { - int i = *(int*)payload->Data; dndx = &dnd[i]; - } - ImPlot::EndDragDropTarget(); - } - // allow the x-axis to be a DND source - if (dndx != NULL && ImPlot::BeginDragDropSourceX()) { - ImGui::SetDragDropPayload("MY_DND", &dndx->Idx, sizeof(int)); - ImPlot::ItemIcon(dndx->Color); ImGui::SameLine(); - ImGui::TextUnformatted(dndx->Label); - ImPlot::EndDragDropSource(); - } - // allow the y-axis to be a DND target - if (ImPlot::BeginDragDropTargetY()) { - if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { - int i = *(int*)payload->Data; dndy = &dnd[i]; - } - ImPlot::EndDragDropTarget(); - } - // allow the y-axis to be a DND source - if (dndy != NULL && ImPlot::BeginDragDropSourceY()) { - ImGui::SetDragDropPayload("MY_DND", &dndy->Idx, sizeof(int)); - ImPlot::ItemIcon(dndy->Color); ImGui::SameLine(); - ImGui::TextUnformatted(dndy->Label); - ImPlot::EndDragDropSource(); - } - // allow the plot area to be a DND target - if (ImPlot::BeginDragDropTarget()) { - if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("MY_DND")) { - int i = *(int*)payload->Data; dndx = dndy = &dnd[i]; - } - } - // allow the plot area to be a DND source - if (ImPlot::BeginDragDropSource()) { - ImGui::TextUnformatted("Yes, you can\ndrag this!"); - ImPlot::EndDragDropSource(); - } - ImPlot::EndPlot(); - } - ImPlot::PopStyleColor(2); - ImGui::EndChild(); - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Tables")) { -#ifdef IMGUI_HAS_TABLE - static ImGuiTableFlags flags = ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_RowBg; - static bool anim = true; - static int offset = 0; - ImGui::BulletText("Plots can be used inside of ImGui tables."); - ImGui::Checkbox("Animate",&anim); - if (anim) - offset = (offset + 1) % 100; - if (ImGui::BeginTable("##table", 3, flags, ImVec2(-1,0))) { - ImGui::TableSetupColumn("Electrode", ImGuiTableColumnFlags_WidthFixed, 75.0f); - ImGui::TableSetupColumn("Voltage", ImGuiTableColumnFlags_WidthFixed, 75.0f); - ImGui::TableSetupColumn("EMG Signal"); - ImGui::TableHeadersRow(); - ImPlot::PushColormap(ImPlotColormap_Cool); - for (int row = 0; row < 10; row++) { - ImGui::TableNextRow(); - static float data[100]; - srand(row); - for (int i = 0; i < 100; ++i) - data[i] = RandomRange(0.0f,10.0f); - ImGui::TableSetColumnIndex(0); - ImGui::Text("EMG %d", row); - ImGui::TableSetColumnIndex(1); - ImGui::Text("%.3f V", data[offset]); - ImGui::TableSetColumnIndex(2); - ImGui::PushID(row); - MyImPlot::Sparkline("##spark",data,100,0,11.0f,offset,ImPlot::GetColormapColor(row),ImVec2(-1, 35)); - ImGui::PopID(); - } - ImPlot::PopColormap(); - ImGui::EndTable(); - } -#else - ImGui::BulletText("You need to merge the ImGui 'tables' branch for this section."); -#endif - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Offset and Stride")) { - static const int k_circles = 11; - static const int k_points_per = 50; - static const int k_size = 2 * k_points_per * k_circles; - static double interleaved_data[k_size]; - for (int p = 0; p < k_points_per; ++p) { - for (int c = 0; c < k_circles; ++c) { - double r = (double)c / (k_circles - 1) * 0.2 + 0.2; - interleaved_data[p*2*k_circles + 2*c + 0] = 0.5 + r * cos((double)p/k_points_per * 6.28); - interleaved_data[p*2*k_circles + 2*c + 1] = 0.5 + r * sin((double)p/k_points_per * 6.28); - } - } - static int offset = 0; - ImGui::BulletText("Offsetting is useful for realtime plots (see above) and circular buffers."); - ImGui::BulletText("Striding is useful for interleaved data (e.g. audio) or plotting structs."); - ImGui::BulletText("Here, all circle data is stored in a single interleaved buffer:"); - ImGui::BulletText("[c0.x0 c0.y0 ... cn.x0 cn.y0 c0.x1 c0.y1 ... cn.x1 cn.y1 ... cn.xm cn.ym]"); - ImGui::BulletText("The offset value indicates which circle point index is considered the first."); - ImGui::BulletText("Offsets can be negative and/or larger than the actual data count."); - ImGui::SliderInt("Offset", &offset, -2*k_points_per, 2*k_points_per); - if (ImPlot::BeginPlot("##strideoffset",0,0,ImVec2(-1,0), ImPlotFlags_Equal)) { - ImPlot::PushColormap(ImPlotColormap_Jet); - char buff[16]; - for (int c = 0; c < k_circles; ++c) { - sprintf(buff, "Circle %d", c); - ImPlot::PlotLine(buff, &interleaved_data[c*2 + 0], &interleaved_data[c*2 + 1], k_points_per, offset, 2*k_circles*sizeof(double)); - } - ImPlot::EndPlot(); - ImPlot::PopColormap(); - } - // offset++; uncomment for animation! - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Custom Data and Getters")) { - ImGui::BulletText("You can plot custom structs using the stride feature."); - ImGui::BulletText("Most plotters can also be passed a function pointer for getting data."); - ImGui::Indent(); - ImGui::BulletText("You can optionally pass user data to be given to your getter function."); - ImGui::BulletText("C++ lambdas can be passed as function pointers as well!"); - ImGui::Unindent(); - - MyImPlot::Vector2f vec2_data[2] = { MyImPlot::Vector2f(0,0), MyImPlot::Vector2f(1,1) }; - - if (ImPlot::BeginPlot("##Custom Data")) { - - // custom structs using stride example: - ImPlot::PlotLine("Vector2f", &vec2_data[0].x, &vec2_data[0].y, 2, 0, sizeof(MyImPlot::Vector2f) /* or sizeof(float) * 2 */); - - // custom getter example 1: - ImPlot::PlotLineG("Spiral", MyImPlot::Spiral, NULL, 1000); - - // custom getter example 2: - static MyImPlot::WaveData data1(0.001, 0.2, 2, 0.75); - static MyImPlot::WaveData data2(0.001, 0.2, 4, 0.25); - ImPlot::PlotLineG("Waves", MyImPlot::SineWave, &data1, 1000); - ImPlot::PlotLineG("Waves", MyImPlot::SawWave, &data2, 1000); - ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f); - ImPlot::PlotShadedG("Waves", MyImPlot::SineWave, &data1, MyImPlot::SawWave, &data2, 1000); - ImPlot::PopStyleVar(); - - // you can also pass C++ lambdas: - // auto lamda = [](void* data, int idx) { ... return ImPlotPoint(x,y); }; - // ImPlot::PlotLine("My Lambda", lambda, data, 1000); - - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Custom Ticks##")) { - static bool custom_fmt = true; - static bool custom_ticks = false; - static bool custom_labels = true; - ImGui::Checkbox("Show Custom Format", &custom_fmt); - ImGui::SameLine(); - ImGui::Checkbox("Show Custom Ticks", &custom_ticks); - if (custom_ticks) { - ImGui::SameLine(); - ImGui::Checkbox("Show Custom Labels", &custom_labels); - } - double pi = 3.14; - const char* pi_str[] = {"PI"}; - static double yticks[] = {1,3,7,9}; - static const char* ylabels[] = {"One","Three","Seven","Nine"}; - static double yticks_aux[] = {0.2,0.4,0.6}; - static const char* ylabels_aux[] = {"A","B","C","D","E","F"}; - if (custom_fmt) { - ImPlot::SetNextPlotFormatX("%g ms"); - ImPlot::SetNextPlotFormatY("%g Hz", ImPlotYAxis_1); - ImPlot::SetNextPlotFormatY("%g dB", ImPlotYAxis_2); - ImPlot::SetNextPlotFormatY("%g km", ImPlotYAxis_3); - } - if (custom_ticks) { - ImPlot::SetNextPlotTicksX(&pi,1,custom_labels ? pi_str : NULL, true); - ImPlot::SetNextPlotTicksY(yticks, 4, custom_labels ? ylabels : NULL, ImPlotYAxis_1); - ImPlot::SetNextPlotTicksY(yticks_aux, 3, custom_labels ? ylabels_aux : NULL, false, ImPlotYAxis_2); - ImPlot::SetNextPlotTicksY(0, 1, 6, custom_labels ? ylabels_aux : NULL, false, ImPlotYAxis_3); - } - ImPlot::SetNextPlotLimits(2.5,5,0,10); - if (ImPlot::BeginPlot("Custom Ticks", NULL, NULL, ImVec2(-1,0), ImPlotFlags_YAxis2 | ImPlotFlags_YAxis3)) { - // nothing to see here, just the ticks - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Custom Styles")) { - ImPlot::PushColormap(ImPlotColormap_Deep); - // normally you wouldn't change the entire style each frame - ImPlotStyle backup = ImPlot::GetStyle(); - MyImPlot::StyleSeaborn(); - ImPlot::SetNextPlotLimits(-0.5f, 9.5f, 0, 10); - if (ImPlot::BeginPlot("seaborn style", "x-axis", "y-axis")) { - unsigned int lin[10] = {8,8,9,7,8,8,8,9,7,8}; - unsigned int bar[10] = {1,2,5,3,4,1,2,5,3,4}; - unsigned int dot[10] = {7,6,6,7,8,5,6,5,8,7}; - ImPlot::PlotBars("Bars", bar, 10, 0.5f); - ImPlot::PlotLine("Line", lin, 10); - ImPlot::NextColormapColor(); // skip green - ImPlot::PlotScatter("Scatter", dot, 10); - ImPlot::EndPlot(); - } - ImPlot::GetStyle() = backup; - ImPlot::PopColormap(); - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Custom Rendering")) { - if (ImPlot::BeginPlot("##CustomRend")) { - ImVec2 cntr = ImPlot::PlotToPixels(ImPlotPoint(0.5f, 0.5f)); - ImVec2 rmin = ImPlot::PlotToPixels(ImPlotPoint(0.25f, 0.75f)); - ImVec2 rmax = ImPlot::PlotToPixels(ImPlotPoint(0.75f, 0.25f)); - ImPlot::PushPlotClipRect(); - ImPlot::GetPlotDrawList()->AddCircleFilled(cntr,20,IM_COL32(255,255,0,255),20); - ImPlot::GetPlotDrawList()->AddRect(rmin, rmax, IM_COL32(128,0,255,255)); - ImPlot::PopPlotClipRect(); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Custom Context Menus")) { - ImGui::BulletText("You can implement legend context menus to inject per-item controls and widgets."); - ImGui::BulletText("Right click the legend label/icon to edit custom item attributes."); - - static float frequency = 0.1f; - static float amplitude = 0.5f; - static ImVec4 color = ImVec4(1,1,0,1); - static float alpha = 1.0f; - static bool line = false; - static float thickness = 1; - static bool markers = false; - static bool shaded = false; - - static float vals[101]; - for (int i = 0; i < 101; ++i) - vals[i] = amplitude * sinf(frequency * i); - - ImPlot::SetNextPlotLimits(0,100,-1,1); - if (ImPlot::BeginPlot("Right Click the Legend")) { - // rendering logic - ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha); - if (!line) { - ImPlot::SetNextFillStyle(color); - ImPlot::PlotBars("Right Click Me", vals, 101); - } - else { - if (markers) ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); - ImPlot::SetNextLineStyle(color, thickness); - ImPlot::PlotLine("Right Click Me", vals, 101); - if (shaded) ImPlot::PlotShaded("Right Click Me",vals,101); - } - ImPlot::PopStyleVar(); - // custom legend context menu - if (ImPlot::BeginLegendPopup("Right Click Me")) { - ImGui::SliderFloat("Frequency",&frequency,0,1,"%0.2f"); - ImGui::SliderFloat("Amplitude",&litude,0,1,"%0.2f"); - ImGui::Separator(); - ImGui::ColorEdit3("Color",&color.x); - ImGui::SliderFloat("Transparency",&alpha,0,1,"%.2f"); - ImGui::Checkbox("Line Plot", &line); - if (line) { - ImGui::SliderFloat("Thickness", &thickness, 0, 5); - ImGui::Checkbox("Markers", &markers); - ImGui::Checkbox("Shaded",&shaded); - } - ImPlot::EndLegendPopup(); - } - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- - if (ImGui::CollapsingHeader("Custom Plotters and Tooltips")) { - ImGui::BulletText("You can create custom plotters or extend ImPlot using implot_internal.h."); - 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}; - 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}; - 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}; - 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}; - 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}; - static bool tooltip = true; - ImGui::Checkbox("Show Tooltip", &tooltip); - ImGui::SameLine(); - static ImVec4 bullCol = ImVec4(0.000f, 1.000f, 0.441f, 1.000f); - static ImVec4 bearCol = ImVec4(0.853f, 0.050f, 0.310f, 1.000f); - ImGui::SameLine(); ImGui::ColorEdit4("##Bull", &bullCol.x, ImGuiColorEditFlags_NoInputs); - ImGui::SameLine(); ImGui::ColorEdit4("##Bear", &bearCol.x, ImGuiColorEditFlags_NoInputs); - ImPlot::GetStyle().UseLocalTime = false; - ImPlot::SetNextPlotFormatY("$%.0f"); - ImPlot::SetNextPlotLimits(1546300800, 1571961600, 1250, 1600); - if (ImPlot::BeginPlot("Candlestick Chart",NULL,NULL,ImVec2(-1,0),0,ImPlotAxisFlags_Time,ImPlotAxisFlags_AutoFit|ImPlotAxisFlags_RangeFit)) { - MyImPlot::PlotCandlestick("GOOGL",dates, opens, closes, lows, highs, 218, tooltip, 0.25f, bullCol, bearCol); - ImPlot::EndPlot(); - } - } - //------------------------------------------------------------------------- ImGui::End(); } diff --git a/implot_internal.h b/implot_internal.h index 994331c..2d0d427 100644 --- a/implot_internal.h +++ b/implot_internal.h @@ -139,6 +139,14 @@ static inline void ImMinMaxArray(const T* values, int count, T* min_out, T* max_ } *min_out = Min; *max_out = Max; } +// Finds the sim of an array +template +static inline T ImSum(const T* values, int count) { + T sum = 0; + for (int i = 0; i < count; ++i) + sum += values[i]; + return sum; +} // Finds the mean of an array template static inline double ImMean(const T* values, int count) { @@ -698,6 +706,28 @@ struct ImPlotAxis inline bool IsLog() const { return ImHasFlag(Flags, ImPlotAxisFlags_LogScale); } }; +// Align plots group data +struct ImPlotAlignmentData { + ImPlotOrientation Orientation; + float PadA; + float PadB; + float PadAMax; + float PadBMax; + ImPlotAlignmentData() { + Orientation = ImPlotOrientation_Vertical; + PadA = PadB = PadAMax = PadBMax = 0; + } + void Begin() { PadAMax = PadBMax = 0; } + void Update(float& pad_a, float& pad_b) { + if (PadAMax < pad_a) PadAMax = pad_a; + if (pad_a < PadA) pad_a = PadA; + if (PadBMax < pad_b) PadBMax = pad_b; + if (pad_b < PadB) pad_b = PadB; + } + void End() { PadA = PadAMax; PadB = PadBMax; } + void Reset() { PadA = PadB = PadAMax = PadBMax = 0; } +}; + // State information for Plot items struct ImPlotItem { @@ -719,50 +749,81 @@ struct ImPlotItem ~ImPlotItem() { ID = 0; } }; -// Holds Legend state labels and item references +// Holds Legend state struct ImPlotLegendData { - ImVector Indices; - ImGuiTextBuffer Labels; + ImVector Indices; + ImGuiTextBuffer Labels; + bool Hovered; + bool Outside; + bool CanGoInside; + bool FlipSideNextFrame; + ImPlotLocation Location; + ImPlotOrientation Orientation; + ImRect Rect; + + ImPlotLegendData() { + CanGoInside = true; + Hovered = Outside = FlipSideNextFrame = false; + Location = ImPlotLocation_North | ImPlotLocation_West; + Orientation = ImPlotOrientation_Vertical; + } + void Reset() { Indices.shrink(0); Labels.Buf.shrink(0); } }; +// Holds Items and Legend data +struct ImPlotItemGroup +{ + ImGuiID ID; + ImPlotLegendData Legend; + ImPool ItemPool; + int ColormapIdx; + + ImPlotItemGroup() { ColormapIdx = 0; } + + int GetItemCount() const { return ItemPool.GetBufSize(); } + ImGuiID GetItemID(const char* label_id) { return ImGui::GetID(label_id); /* GetIDWithSeed */ } + ImPlotItem* GetItem(ImGuiID id) { return ItemPool.GetByKey(id); } + ImPlotItem* GetItem(const char* label_id) { return GetItem(GetItemID(label_id)); } + ImPlotItem* GetOrAddItem(ImGuiID id) { return ItemPool.GetOrAddByKey(id); } + ImPlotItem* GetItemByIndex(int i) { return ItemPool.GetByIndex(i); } + int GetItemIndex(ImPlotItem* item) { return ItemPool.GetIndex(item); } + int GetLegendCount() const { return Legend.Indices.size(); } + ImPlotItem* GetLegendItem(int i) { return ItemPool.GetByIndex(Legend.Indices[i]); } + const char* GetLegendLabel(int i) { return Legend.Labels.Buf.Data + GetLegendItem(i)->NameOffset; } + void Reset() { ItemPool.Clear(); Legend.Reset(); ColormapIdx = 0; } +}; + // Holds Plot state information that must persist after EndPlot struct ImPlotPlot { - ImGuiID ID; - ImPlotFlags Flags; - ImPlotFlags PreviousFlags; - ImPlotAxis XAxis; - ImPlotAxis YAxis[IMPLOT_Y_AXES]; - ImPlotLegendData LegendData; - ImPool Items; - ImVec2 SelectStart; - ImRect SelectRect; - ImVec2 QueryStart; - ImRect QueryRect; - bool Initialized; - bool Selecting; - bool Selected; - bool ContextLocked; - bool Querying; - bool Queried; - bool DraggingQuery; - bool LegendHovered; - bool LegendOutside; - bool LegendFlipSideNextFrame; - bool FrameHovered; - bool PlotHovered; - int ColormapIdx; - int CurrentYAxis; - ImPlotLocation MousePosLocation; - ImPlotLocation LegendLocation; - ImPlotOrientation LegendOrientation; - ImRect FrameRect; - ImRect CanvasRect; - ImRect PlotRect; - ImRect AxesRect; - ImRect LegendRect; + ImGuiID ID; + ImPlotFlags Flags; + ImPlotFlags PreviousFlags; + ImPlotAxis XAxis; + ImPlotAxis YAxis[IMPLOT_Y_AXES]; + ImPlotItemGroup Items; + ImVec2 SelectStart; + ImRect SelectRect; + ImVec2 QueryStart; + ImRect QueryRect; + bool Initialized; + bool Selecting; + bool Selected; + bool ContextLocked; + bool Querying; + bool Queried; + bool DraggingQuery; + bool FrameHovered; + bool FrameHeld; + bool PlotHovered; + int CurrentYAxis; + ImPlotLocation MousePosLocation; + ImRect FrameRect; + ImRect CanvasRect; + ImRect PlotRect; + ImRect AxesRect; ImPlotPlot() { Flags = PreviousFlags = ImPlotFlags_None; @@ -770,22 +831,46 @@ struct ImPlotPlot for (int i = 0; i < IMPLOT_Y_AXES; ++i) YAxis[i].Orientation = ImPlotOrientation_Vertical; SelectStart = QueryStart = ImVec2(0,0); - Initialized = Selecting = Selected = ContextLocked = Querying = Queried = DraggingQuery = LegendHovered = LegendOutside = LegendFlipSideNextFrame = false; - ColormapIdx = CurrentYAxis = 0; - LegendLocation = ImPlotLocation_North | ImPlotLocation_West; - LegendOrientation = ImPlotOrientation_Vertical; + Initialized = Selecting = Selected = ContextLocked = Querying = Queried = DraggingQuery = false; + CurrentYAxis = 0; MousePosLocation = ImPlotLocation_South | ImPlotLocation_East; } - int GetLegendCount() const { return LegendData.Indices.size(); } - ImPlotItem* GetLegendItem(int i); - const char* GetLegendLabel(int i); - inline bool AnyYInputLocked() const { return YAxis[0].IsInputLocked() || (YAxis[1].Present ? YAxis[1].IsInputLocked() : false) || (YAxis[2].Present ? YAxis[2].IsInputLocked() : false); } inline bool AllYInputLocked() const { return YAxis[0].IsInputLocked() && (YAxis[1].Present ? YAxis[1].IsInputLocked() : true ) && (YAxis[2].Present ? YAxis[2].IsInputLocked() : true ); } inline bool IsInputLocked() const { return XAxis.IsInputLocked() && YAxis[0].IsInputLocked() && YAxis[1].IsInputLocked() && YAxis[2].IsInputLocked(); } }; +// Holds subplot data that must persist afer EndSubplot +struct ImPlotSubplot { + ImGuiID ID; + ImPlotSubplotFlags Flags; + ImPlotSubplotFlags PreviousFlags; + ImPlotItemGroup Items; + int Rows; + int Cols; + int CurrentIdx; + ImRect FrameRect; + ImRect GridRect; + ImVec2 CellSize; + ImVector RowAlignmentData; + ImVector ColAlignmentData; + ImVector RowRatios; + ImVector ColRatios; + ImVector RowLinkData; + ImVector ColLinkData; + float TempSizes[2]; + bool FrameHovered; + + ImPlotSubplot() { + Rows = Cols = CurrentIdx = 0; + FrameHovered = false; + Items.Legend.Location = ImPlotLocation_North; + Items.Legend.Orientation = ImPlotOrientation_Horizontal; + Items.Legend.CanGoInside = false; + } +}; + // Temporary data storage for upcoming plot struct ImPlotNextPlotData { @@ -859,10 +944,13 @@ struct ImPlotNextItemData { // Holds state information that must persist between calls to BeginPlot()/EndPlot() struct ImPlotContext { // Plot States - ImPool Plots; - ImPlotPlot* CurrentPlot; - ImPlotItem* CurrentItem; - ImPlotItem* PreviousItem; + ImPool Plots; + ImPool Subplots; + ImPlotPlot* CurrentPlot; + ImPlotSubplot* CurrentSubplot; + ImPlotItemGroup* CurrentItems; + ImPlotItem* CurrentItem; + ImPlotItem* PreviousItem; // Tick Marks and Labels ImPlotTickCollection CTicks; @@ -892,6 +980,7 @@ struct ImPlotContext { bool RenderX; bool RenderY[IMPLOT_Y_AXES]; + // Axis Locking Flags bool ChildWindowMade; @@ -909,13 +998,17 @@ struct ImPlotContext { ImVector Temp1, Temp2; // Misc - int VisibleItemCount; int DigitalPlotItemCnt; int DigitalPlotOffset; ImPlotNextPlotData NextPlotData; ImPlotNextItemData NextItemData; ImPlotInputMap InputMap; ImPlotPoint MousePos[IMPLOT_Y_AXES]; + + // Align plots + ImPool AlignmentData; + ImPlotAlignmentData* CurrentAlignmentH; + ImPlotAlignmentData* CurrentAlignmentV; }; //----------------------------------------------------------------------------- @@ -932,7 +1025,11 @@ namespace ImPlot { // Initializes an ImPlotContext IMPLOT_API void Initialize(ImPlotContext* ctx); // Resets an ImPlot context for the next call to BeginPlot -IMPLOT_API void Reset(ImPlotContext* ctx); +IMPLOT_API void ResetCtxForNextPlot(ImPlotContext* ctx); +// Restes an ImPlot context for the next call to BeginAlignedPlots +IMPLOT_API void ResetCtxForNextAlignedPlots(ImPlotContext* ctx); +// Resets an ImPlot context for the next call to BeginSubplot +IMPLOT_API void ResetCtxForNextSubplot(ImPlotContext* ctx); //----------------------------------------------------------------------------- // [SECTION] Input Utils @@ -955,6 +1052,16 @@ IMPLOT_API void BustPlotCache(); // Shows a plot's context menu. IMPLOT_API void ShowPlotContextMenu(ImPlotPlot& plot); +//----------------------------------------------------------------------------- +// [SECTION] Subplot Utils +//----------------------------------------------------------------------------- + +// Advances to next subplot +IMPLOT_API void SubplotNextCell(); + +// Shows a subplot's context menu. +IMPLOT_API void ShowSubplotsContextMenu(ImPlotSubplot& subplot); + //----------------------------------------------------------------------------- // [SECTION] Item Utils //----------------------------------------------------------------------------- @@ -1044,11 +1151,13 @@ static inline const char* GetFormatY(ImPlotYAxis y) { return GImPlot->NextPlotDa // Gets the position of an inner rect that is located inside of an outer rect according to an ImPlotLocation and padding amount. IMPLOT_API ImVec2 GetLocationPos(const ImRect& outer_rect, const ImVec2& inner_size, ImPlotLocation location, const ImVec2& pad = ImVec2(0,0)); // Calculates the bounding box size of a legend -IMPLOT_API ImVec2 CalcLegendSize(ImPlotPlot& plot, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orientation); +IMPLOT_API ImVec2 CalcLegendSize(ImPlotItemGroup& items, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orientation); // Renders legend entries into a bounding box -IMPLOT_API void ShowLegendEntries(ImPlotPlot& plot, const ImRect& legend_bb, bool interactable, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orientation, ImDrawList& DrawList); +IMPLOT_API bool ShowLegendEntries(ImPlotItemGroup& items, const ImRect& legend_bb, bool interactable, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orientation, ImDrawList& DrawList); // Shows an alternate legend for the plot identified by #title_id, outside of the plot frame (can be called before or after of Begin/EndPlot but must occur in the same ImGui window!). IMPLOT_API void ShowAltLegend(const char* title_id, ImPlotOrientation orientation = ImPlotOrientation_Vertical, const ImVec2 size = ImVec2(0,0), bool interactable = true); +// Shows an legends's context menu. +IMPLOT_API bool ShowLegendContextMenu(ImPlotLegendData& legend, bool visible); //----------------------------------------------------------------------------- // [SECTION] Tick Utils @@ -1089,6 +1198,8 @@ static inline ImU32 GetStyleColorU32(ImPlotCol idx) { return ImGui::ColorConve // Draws vertical text. The position is the bottom left of the text rect. IMPLOT_API void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char* text_begin, const char* text_end = NULL); +// Draws multiline horizontal text centered. +IMPLOT_API void AddTextCentered(ImDrawList* DrawList, ImVec2 top_center, ImU32 col, const char* text_begin, const char* text_end = NULL); // Calculates the size of vertical text static inline ImVec2 CalcTextSizeVertical(const char *text) { ImVec2 sz = ImGui::CalcTextSize(text); diff --git a/implot_items.cpp b/implot_items.cpp index 332cf12..d60e940 100644 --- a/implot_items.cpp +++ b/implot_items.cpp @@ -61,32 +61,30 @@ namespace ImPlot { ImPlotItem* RegisterOrGetItem(const char* label_id, bool* just_created) { ImPlotContext& gp = *GImPlot; - ImGuiID id = ImGui::GetID(label_id); + ImPlotItemGroup& Items = *gp.CurrentItems; + ImGuiID id = Items.GetItemID(label_id); if (just_created != NULL) - *just_created = gp.CurrentPlot->Items.GetByKey(id) == NULL; - ImPlotItem* item = gp.CurrentPlot->Items.GetOrAddByKey(id); + *just_created = Items.GetItem(id) == NULL; + ImPlotItem* item = Items.GetOrAddItem(id); if (item->SeenThisFrame) return item; item->SeenThisFrame = true; - int idx = gp.CurrentPlot->Items.GetIndex(item); + int idx = Items.GetItemIndex(item); item->ID = id; if (ImGui::FindRenderedTextEnd(label_id, NULL) != label_id) { - gp.CurrentPlot->LegendData.Indices.push_back(idx); - item->NameOffset = gp.CurrentPlot->LegendData.Labels.size(); - gp.CurrentPlot->LegendData.Labels.append(label_id, label_id + strlen(label_id) + 1); + Items.Legend.Indices.push_back(idx); + item->NameOffset = Items.Legend.Labels.size(); + Items.Legend.Labels.append(label_id, label_id + strlen(label_id) + 1); } else { item->Show = true; } - if (item->Show) - gp.VisibleItemCount++; return item; } ImPlotItem* GetItem(const char* label_id) { ImPlotContext& gp = *GImPlot; - ImGuiID id = ImGui::GetID(label_id); - return gp.CurrentPlot->Items.GetByKey(id); + return gp.CurrentItems->GetItem(label_id); } ImPlotItem* GetCurrentItem() { @@ -140,9 +138,11 @@ void BustItemCache() { ImPlotContext& gp = *GImPlot; for (int p = 0; p < gp.Plots.GetBufSize(); ++p) { ImPlotPlot& plot = *gp.Plots.GetByIndex(p); - plot.ColormapIdx = 0; - plot.Items.Clear(); - plot.LegendData.Reset(); + plot.Items.Reset(); + } + for (int p = 0; p < gp.Subplots.GetBufSize(); ++p) { + ImPlotSubplot& subplot = *gp.Subplots.GetByIndex(p); + subplot.Items.Reset(); } } @@ -152,12 +152,15 @@ void BustColorCache(const char* plot_title_id) { BustItemCache(); } else { - ImPlotPlot* plot = gp.Plots.GetByKey(ImGui::GetCurrentWindow()->GetID(plot_title_id)); - if (plot == NULL) - return; - plot->ColormapIdx = 0; - plot->Items.Clear(); - plot->LegendData.Reset(); + ImGuiID id = ImGui::GetCurrentWindow()->GetID(plot_title_id); + ImPlotPlot* plot = gp.Plots.GetByKey(id); + if (plot != NULL) + plot->Items.Reset(); + else { + ImPlotSubplot* subplot = gp.Subplots.GetByKey(id); + if (subplot != NULL) + subplot->Items.Reset(); + } } }