Skip to content

fix(LineChart): guard against fewer than two points#6

Open
chinesepowered wants to merge 1 commit into
stackblitz:mainfrom
chinesepowered:linechart-empty-guard
Open

fix(LineChart): guard against fewer than two points#6
chinesepowered wants to merge 1 commit into
stackblitz:mainfrom
chinesepowered:linechart-empty-guard

Conversation

@chinesepowered

Copy link
Copy Markdown

What

LineChart breaks when given fewer than two points:

  • Empty array (points={[]}) — coords ends up empty, so const [ex, ey] = coords[coords.length - 1] destructures undefined and throws, taking down the slide render.
  • Single point (points={[42]}) — i / (points.length - 1) is 0 / 0 = NaN, so the path/area d strings and the end-dot position are all NaN.

A line chart needs at least two points to be meaningful, so this guards for that case up front and renders an empty (but correctly sized, ref-attached) chart area instead of crashing or emitting NaN geometry.

Changes

  • src/components/Charts.tsx — early return in LineChart when points.length < 2, before any of the coordinate math.

Verification

  • npx tsc --noEmit
  • npm run build

With an empty points array, coords is empty and `const [ex, ey] = coords[coords.length - 1]` destructures undefined and throws. With a single point, `i / (points.length - 1)` is 0/0 = NaN, producing NaN path geometry. Return an empty (but correctly sized) chart area when given fewer than two points, since a line needs at least two.
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.

1 participant