From a1ee8053f43e93260fc570a77180e8bddd74e3b9 Mon Sep 17 00:00:00 2001 From: Simon Wezstein Date: Thu, 15 May 2025 09:54:30 +0200 Subject: [PATCH] Adding extra flag to allow relative histograms --- implot.h | 5 +++-- implot_items.cpp | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/implot.h b/implot.h index ae1f6000..07636ea1 100644 --- a/implot.h +++ b/implot.h @@ -304,9 +304,10 @@ enum ImPlotHistogramFlags_ { ImPlotHistogramFlags_None = 0, // default ImPlotHistogramFlags_Horizontal = 1 << 10, // histogram bars will be rendered horizontally (not supported by PlotHistogram2D) ImPlotHistogramFlags_Cumulative = 1 << 11, // each bin will contain its count plus the counts of all previous bins (not supported by PlotHistogram2D) - ImPlotHistogramFlags_Density = 1 << 12, // counts will be normalized, i.e. the PDF will be visualized, or the CDF will be visualized if Cumulative is also set + ImPlotHistogramFlags_Density = 1 << 12, // counts will be normalized, i.e. the PDF will be visualized, or the CDF will be visualized if Cumulative is also set, or if ImPlotHistogramFlags_NormalizeIgnoreWidth is set the bin counts add up to 1 ImPlotHistogramFlags_NoOutliers = 1 << 13, // exclude values outside the specifed histogram range from the count toward normalizing and cumulative counts - ImPlotHistogramFlags_ColMajor = 1 << 14 // data will be read in column major order (not supported by PlotHistogram) + ImPlotHistogramFlags_ColMajor = 1 << 14, // data will be read in column major order (not supported by PlotHistogram) + ImPlotHistogramFlags_NormalizeIgnoreWidth = 1 << 15 // Ignore bin width for ImPlotHistogramFlags_Density, if both set, a relative histogram is plotted }; // Flags for PlotDigital (placeholder) diff --git a/implot_items.cpp b/implot_items.cpp index f7de3465..b5722142 100644 --- a/implot_items.cpp +++ b/implot_items.cpp @@ -2545,6 +2545,7 @@ double PlotHistogram(const char* label_id, const T* values, int count, int bins, const bool cumulative = ImHasFlag(flags, ImPlotHistogramFlags_Cumulative); const bool density = ImHasFlag(flags, ImPlotHistogramFlags_Density); const bool outliers = !ImHasFlag(flags, ImPlotHistogramFlags_NoOutliers); + const bool ignoreWidth = ImHasFlag(flags, ImPlotHistogramFlags_NormalizeIgnoreWidth); if (count <= 0 || bins == 0) return 0; @@ -2606,7 +2607,8 @@ double PlotHistogram(const char* label_id, const T* values, int count, int bins, max_count = bin_counts[bins-1]; } else if (density) { - double scale = 1.0 / ((outliers ? count : counted) * width); + double selectedWidth = ignoreWidth ? 1.0 : width; + double scale = 1.0 / ((outliers ? count : counted) * selectedWidth); for (int b = 0; b < bins; ++b) bin_counts[b] *= scale; max_count *= scale;