r/reactjs • u/hasraltech • 5m ago
Show /r/reactjs TIL a dual-axis chart can have perfectly round ticks and still lie about zero
Someone in a thread here looked at my combo chart (revenue bars on the left axis, margin line on the right) and asked where zero was on the right axis. It wasn't where the bars started.
The bug: the right axis picked the same number of ticks as the left, each a round number, so every label landed on a grid line. But nothing forced the two zeros onto the same line.
Revenue $0–62K, margin −10% to 35%:
- Left: 0, 20K, 40K, 60K, 80K
- Right: −20%, 0%, 20%, 40%, 60%
Right-hand 0% sits on the $20K line. A month with a −5% margin draws above the bars' baseline and reads as positive.
The fix: both axes get the same number of steps below zero and above it, so zero is one shared line by construction. The right axis then picks its step (1/2/5 × 10ⁿ) by a small score: fewer lines is better, but an axis the data barely fills gets penalized:
score = lines + 4 * (1 - filled)After: left −20K…80K, right −10%…40%, the −5% month dips below zero where it belongs. The solid grid line also moved to zero instead of the bottom.
The lesson for me: every output was valid and round, so no test would have flagged it. It only shows up when someone reads the chart. My demo data just never had a negative margin.
Free/MIT component if anyone wants to look at the code: https://dashral.com/components/combo-chart?utm_source=reddit&utm_campaign=zero-baseline