Shopify/polaris-viz

Charts error when DataSeries.data is an empty array

Closed this issue · 2 comments

envex commented

The Issue

We're not factoring for series that do not have any data in the charts. This causing errors all over the repo because the charts assume that data is available.

<BarChart data={[]} /> is handled correctly because there are no series to render.

<BarChart data={[{name: '', data: []}]} /> will throw an error when moving the mouse over the chart because the <TooltipWrapper /> can't find the data for an index that doesn't exist.

The band-aid fix is to not render <TooltipWrapper /> if DataSeries.data is an empty array, but that doesn't fix the overall problem.

Bad and Good Data

What if a chart is provided the data...

[
   {name: 'Monday', data: [{key: 'Breakfast', value: 10}]},
   {name: '', data: []}
]

...it starts to error all over the charts because we now have bad data that we're trying to render.

Solutions?

  • Can we just assume that series with no data can be ignored entirely?
  • Is it on the consumer to make sure the data they're providing is correct?

IMO, this error should be handled on the consumer side. {name: '', data: []} would also break the experience as it's forcing a nameless series to be rendered. Maybe they should check if the data is valid before trying to visualize it? 🤔

envex commented

Fair - we could also just drop the bad data at the root chart level, but that's more overhead because now we have to check the data every time it changes.