Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Let's not ignore NaN

Open SAKURA-CAT opened this issue 3 months ago • 3 comments

Feature Proposal

I expect to draw an image similar to the one below, where the dataset contains some NaN points that can help deep learning practitioners identify whether situations like gradient explosion have occurred:

Image

However, I did not find any relevant API in the Chart.js documentation that prevents NaN from being ignored. Is it possible to add a similar feature? Many thanks!

Possible Implementation

No response

SAKURA-CAT avatar Sep 25 '25 07:09 SAKURA-CAT

You can set the spanGaps property to true in the dataset. This will give you the result you want.

https://www.chartjs.org/docs/4.5.0/charts/line.html#line-styling

LeeLenaleee avatar Sep 28 '25 18:09 LeeLenaleee

You can set the spanGaps property to true in the dataset.

Thank you for your reply! When I set it to True,the chart looks like:

Image

The point with index 5 is NaN, but I can't get the element through this:

  const position = getRelativePosition(e, chart)
  // NaN points do not appear in the items.
  const items = Interaction.modes.nearest(chart, e, { intersect: false, axis: 'x' }, useFinalPosition)

So this interaction is like:

https://github.com/user-attachments/assets/415313f7-3f3a-4f8f-b83e-9642e42631d3

Is there any API that allows me to locate the points with NaN? After all, this is a canvas, and I can draw it myself.

SAKURA-CAT avatar Sep 29 '25 04:09 SAKURA-CAT

Sorry for the late reply but as far as I know there is not such an API. But with some custom code you can get every value on the line including your nan points. You can customize this so that it will suit your requirements. https://codepen.io/kurkle/pen/WNpdwgE

LeeLenaleee avatar Nov 17 '25 00:11 LeeLenaleee