Skip to content

Commit 0d4d87c

Browse files
howpricebrenocq
andauthored
feat: add ImPlotLegendFlags_Reverse (#640)
* Add ImPlotLegendFlags_Reverse This is handy for making the order of legend items match the order of the data in stacked plots. See #292 --------- Co-authored-by: Breno Cunha Queiroz <brenocq.br@gmail.com>
1 parent 1eb22d6 commit 0d4d87c

File tree

3 files changed

+3
-1
lines changed

3 files changed

+3
-1
lines changed

implot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ bool ShowLegendEntries(ImPlotItemGroup& items, const ImRect& legend_bb, bool hov
652652
}
653653
// render
654654
for (int i = 0; i < num_items; ++i) {
655-
const int idx = indices[i];
655+
const int idx = ImHasFlag(items.Legend.Flags, ImPlotLegendFlags_Reverse) ? indices[num_items - 1 - i] : indices[i];
656656
ImPlotItem* item = items.GetLegendItem(idx);
657657
const char* label = items.GetLegendLabel(idx);
658658
const float label_width = ImGui::CalcTextSize(label, nullptr, true).x;

implot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ enum ImPlotLegendFlags_ {
195195
ImPlotLegendFlags_Outside = 1 << 4, // legend will be rendered outside of the plot area
196196
ImPlotLegendFlags_Horizontal = 1 << 5, // legend entries will be displayed horizontally
197197
ImPlotLegendFlags_Sort = 1 << 6, // legend entries will be displayed in alphabetical order
198+
ImPlotLegendFlags_Reverse = 1 << 7, // legend entries will be displayed in reverse order
198199
};
199200

200201
// Options for mouse hover text (see SetupMouseText)

implot_demo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ void Demo_ShadedPlots() {
379379
ImGui::DragFloat("Alpha",&alpha,0.01f,0,1);
380380

381381
if (ImPlot::BeginPlot("Shaded Plots")) {
382+
ImPlot::SetupLegend(ImPlotLocation_NorthWest, ImPlotLegendFlags_Reverse); // reverse legend to match vertical order on plot
382383
ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, alpha);
383384
ImPlot::PlotShaded("Uncertain Data",xs,ys1,ys2,1001);
384385
ImPlot::PlotLine("Uncertain Data", xs, ys, 1001);

0 commit comments

Comments
 (0)