A commenter on Reddit looked at one of my charts for about ten seconds and found a bug I had shipped to everyone.

The chart was a combo chart: revenue as bars on the left axis, profit margin as a line on the right axis. Pretty standard dashboard stuff. Their question was simple: "where is zero on the right axis?"

It wasn't where the bars started.

What went wrong

Two axes on one chart means two sets of tick labels sharing one set of grid lines. My first version got half of that right. The right axis picked the same number of ticks as the left, each one a round number, so every label sat neatly on a grid line.

What it never did was force the two zeros onto the same line.

Take revenue from $0 to $62K and a margin between −10% and 35%:

  • Left axis: 0, 20K, 40K, 60K, 80K (five lines)
  • Right axis: five lines, round step, must cover −10% to 35%, so: −20%, 0%, 20%, 40%, 60%

Look at the second line. The right axis says 0%, the left says $20K. The bars stand on the bottom line, but the margin's zero floats one line above it.

Now give one month a −5% margin. The dot lands above the bars' baseline. Anyone skimming the chart reads it as "slightly positive". It's a loss.

Every label was a round number, every tick sat on a grid line, and the chart was still wrong.

The same data before and after: on the left, the right axis puts 0% on the $20K line, so the negative months float above the bars' baseline. On the right, both zeros share one line and the negative months dip below it.

The fix

When the chart starts at zero, both axes now get the same number of steps below zero and the same number above it, the most either axis needs. Zero becomes one shared grid line, by construction.

The left axis keeps its own round step. The right axis tries a few candidates (1, 2 or 5 times a power of ten) and keeps the one with the best score:

// fewer lines is calmer, but an axis its values barely use squashes the line const filled = (hi - lo) / (step * (below + above)) const score = below + above + 4 * (1 - filled) 

The first term prefers fewer grid lines. The second punishes a step so large that the data only fills a sliver of the axis, which would flatten the line into a near-straight squiggle.

Same data, after the fix:

  • Left: −20K, 0, 20K, 40K, 60K, 80K
  • Right: −10%, 0%, 10%, 20%, 30%, 40%

Both zeros sit on one line, and the −5% month now dips below it, where a loss belongs.

Two smaller changes came with it:

  • The solid grid line moved from the bottom of the chart to zero, where the bars actually stand.
  • alignZero={false} turns it off, for the rare chart where two independent domains really are what you want.

What I took from it

Tests wouldn't have caught this. Every output was a valid, round, evenly spaced set of ticks. The bug only exists when you read the chart the way a person does, comparing one axis against the other.

So: show your charts to someone with negative numbers in their data. Mine all had happy, positive margins.

The component is free and MIT if you want to poke at it: https://dashral.com/components/combo-chart?utm_source=devto&utm_campaign=zero-baseline