plouc / nivo

nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
https://nivo.rocks
MIT License
13.07k stars 1.02k forks source link

Line Chart : crash when hovering an empty chart that is interactive #2597

Open akassaei opened 3 months ago

akassaei commented 3 months ago

Describe/explain the bug When hovering a Line Chart with empty data, the chart crashes.

To Reproduce Here is the link to a Codesandbox showing the issue : https://codesandbox.io/p/sandbox/charming-mendel-3wsx4v?file=%2Fsrc%2Fcomponents%2FLineChart.js%3A110%2C25

Steps to reproduce the behavior:

  1. Create a Line Chart with empty Data
  2. Hover the Chart with your mouse
  3. See the crash

Expected behavior No crash πŸ˜…

Additional context I spotted where the issue is coming from : basically the findNode method in Mesh hooks relies on d3-delaunay and specifically on delaunay.find method. With no data points given, the .find returns NaN and it isn't a case checked in the code so we try to access the node related to the index and it crashes because it's undefined.https://github.com/plouc/nivo/blob/da6d6b751cff5570fb7378d57b52e3e4bdbcf872/packages/voronoi/src/hooks.ts#L172-L179

I've already opened an issue on d3-delaunay side because I don't think NaN is really an expected behavior πŸ˜… You can find the issue opened here : https://github.com/d3/d3-delaunay/issues/144

On our side, I've patched Nivo to return null when nodes.length === 0. So I have a fix ready if you want me to handle it, but I'm not sure that's how you want to handle it πŸ˜„

gercog1 commented 1 month ago

hey @akassaei!

Did you find a quick workaround for this one?

I am also struggling ;)