-
Notifications
You must be signed in to change notification settings - Fork 648
Description
Bug: PlotBars baseline=0 breaks on log-scaled axes
When using ImPlot::PlotBars(...) with a log-scaled value axis (ImPlotScale_Log10), the bars are drawn with a baseline of 0. Since 0 is invalid on logarithmic axes, this can cause bar geometry to explode and appear to draw outside the plot boundaries / clip rect.
---
Minimal Repro
static double values[] = { 1, 10, 100, 1000 };
if (ImPlot::BeginPlot("Bars + Log Axis")) {
ImPlot::SetupAxisScale(ImAxis_Y1, ImPlotScale_Log10);
ImPlot::SetupAxisLimits(ImAxis_Y1, 1e-1, 1e9);
ImPlot::PlotBars("bars", values, 4, 0.8);
ImPlot::EndPlot();
}
Observed
Bars may extend far beyond the plot area (or appear to draw outside plot boundaries) when the value axis uses ImPlotScale_Log10.
Expected
Bars should start at a log-safe baseline (e.g. the current axis minimum), not at 0, when the value axis is logarithmic.
Proposed Fix (small, localized)
Clamp the baseline used by PlotBars from 0 to a log-safe value when the value axis is log-scaled:
- If the relevant axis scale is ImPlotScale_Log10, use axis.Range.Min if > 0, otherwise fall back to a small epsilon (e.g. 1e-12).
- Keep the baseline behavior unchanged for linear axes.
This preserves existing rendering/legend/toggle behavior and only replaces an invalid baseline on log scales.
Patch Summary (implot_items.cpp)
The issue comes from getter2 always using IndexerConst(0) as the baseline. The fix is to replace that 0 with a log-safe baseline when the value axis is log-scaled.
Example (conceptually):
// before (vertical bars)
GetterXY<IndexerLin,IndexerConst> getter2(IndexerLin(1.0,shift), IndexerConst(0), count);
// after (vertical bars)
const double base = GetLogSafeBaseline(ImAxis_Y1, 0.0);
GetterXY<IndexerLin,IndexerConst> getter2(IndexerLin(1.0,shift), IndexerConst(base), count);
Same idea applies to:
- horizontal bars (value axis is X1)
- PlotBars(xs, ys, ...)
- PlotBarsG(...)
I have a working patch applied locally and can provide the full diff if desired.