Skip to content

Pad OHLC x-axis range so edge candles are not clipped - #1027

Open
timmolter wants to merge 1 commit into
developfrom
issue-992-ohlc-edge-candle-padding
Open

Pad OHLC x-axis range so edge candles are not clipped#1027
timmolter wants to merge 1 commit into
developfrom
issue-992-ohlc-edge-candle-padding

Conversation

@timmolter

Copy link
Copy Markdown
Member

Fixes #992.

Problem

The OHLC x-axis range spanned exactly [dataMin, dataMax], so the first and last candles were centered on the plot's left/right boundaries and roughly half of each edge candle body was clipped. The only slack was the plot-content margin (~4% of width per side), which the candle half-width (xTickSpace / candleCount / 2) greatly exceeds for small candle counts — with 4 candles, half of each edge body is cut off. With many narrow candles the overrun is a sliver, which is why this went unnoticed.

Fix

Pad the auto-computed x-axis range by half the candle spacing per side in AxisPair.overrideMinMaxForXAxis(), next to the existing horizontal-bar outside-labels padding. This spot fixes all consumers at once — ticks, grid lines, cursor, tooltips, and annotations share the [min,max] → tickSpace transform, so a painter-only remap would have desynced candles from their tick labels.

The padding also makes the geometry self-consistent: the range becomes candleCount · spacing, so one candle period maps to exactly the candle width PlotContent_OHLC already draws — no painter changes needed.

Details:

  • Spacing = the widest per-series median of consecutive x-deltas. Median so weekend/holiday gaps in date data don't inflate the estimate; widest because the series with the fewest candles draws the widest bodies.
  • Line-style OHLC series have no width and don't trigger padding.
  • Logarithmic x-axes are skipped (padding would have to be multiplicative in log space).
  • Manual styler.setXAxisMin()/Max() still win — they are applied afterwards.

Tests

  • OhlcEdgeCandlePaddingTest: headless pixel-scan test (no XChartPanel) asserting no candle pixels touch the plot's boundary columns. Fails without the fix ("no candle pixels may touch the plot's left/right boundary columns"), passes with it.
  • Verified visually with the 4-candle repro from the issue and with 30 daily candles over a date axis including weekend gaps.
  • Full xchart suite: 169 tests, 0 failures.

🤖 Generated with Claude Code

The x-axis range spanned exactly [dataMin, dataMax], centering the first
and last candles on the plot boundaries and clipping half of each edge
candle body. The only slack was the plot-content margin (~4% per side),
which the candle half-width (xTickSpace / candleCount / 2) greatly
exceeds for small candle counts.

Pad the auto-computed range by half the candle spacing per side in
AxisPair.overrideMinMaxForXAxis(), next to the existing horizontal-bar
outside-labels padding. One candle period then maps to exactly the
candle width PlotContent_OHLC draws, so edge candles fit fully. The
spacing is the widest per-series median of consecutive x-deltas: median
so weekend/holiday gaps in date data don't inflate the estimate, widest
because the series with the fewest candles draws the widest bodies.
Line-style OHLC series have no width and don't trigger padding, nor do
logarithmic x-axes (padding would have to be multiplicative). Manual
styler min/max overrides still win, as they are applied afterwards.

Fixes #992

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OHLC: first and last candles are clipped at the plot edges

1 participant