#18

Cartesian charts can't render negative values (y-domain hardcoded to [0, max])

Openderekc00opened 4mo ago

Summary

The cartesian chart family (AreaChart / LineChart / BarChart) can't render negative values. The y-scale domain is hardcoded to start at 0, so any value below zero is pushed off the bottom of the plot instead of drawing below a zero baseline. This makes the library unusable for diverging data (P&L, deltas, temperature anomalies, net change, etc.).

Where

components/dither-kit/scales.ts:

export function buildYScale(max: number, plotHeight: number) {
  return scaleLinear().domain([0, max]).nice().range([plotHeight, 0])
}

computeBands only ever tracks a positive max and never a negative minimum:

bands[key] = data.map((row) => {
  const v = num(row[key])
  if (v > max) max = v      // no corresponding `min` tracking
  return [0, v]             // band floor is always 0
})

And chart-context.tsx builds the scale from that single max:

const y = useMemo(() => buildYScale(max, plotHeight), [max, plotHeight])

Both canvas painters then normalize against that 0-floored scale (e.g. bar-canvas.tsx and cartesian-canvas.tsx use y(b[0]) / h), so there's no path for a value to sit below the baseline.

Reproduction

const data = [
  { month: "Jan", net: 180 },
  { month: "Feb", net: -60 },   // loss
  { month: "Mar", net: 240 },
]
const config = { net: { label: "Net", color: "orange" } }

<BarChart data={data} config={config}>
  <YAxis />
  <XAxis dataKey="month" />
  <Bar dataKey="net" />
</BarChart>

Expected: the Feb bar drops below a zero baseline (like a diverging/waterfall bar). Actual: the y-domain is [0, 240], so net: -60 maps to a pixel below plotHeight and clips off-canvas; the axis has no zero line and the negative bar is effectively invisible.

The same applies to LineChart — a series that dips negative clips below the plot.

Suggested direction

  1. computeBands (default stack): track a negative min as well as max.
  2. buildYScale: use a diverging domain, e.g. scaleLinear().domain([Math.min(0, min), Math.max(0, max)]).nice().
  3. Canvas painters + <Bar> geometry: draw bars from the zero baseline (y(0)) rather than from plotHeight, so positive bars go up and negative bars go down.
  4. Optionally expose a zero-baseline / <ReferenceLine y={0}> primitive for the diverging case.

Context

Found while evaluating dither-kit (installed area-chart + bar-chart + core via the shadcn registry, shadcn@4.13.0, on 2026-07-13) as a replacement for recharts in a profit/loss tracker. The aesthetic is lovely, but losses live below zero for us, so we couldn't adopt it for those charts. Filing in case negative/diverging support is on the roadmap — happy to help test.

Activity

1
ripgrim4mo ago

Thank you for opening this! I'll fix and get it pushed up in the next patch version.

By the way, I recommend migrating to the cli, as that will provide you updates and access to the latest components :) npx dither-kit/cli