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.24k stars 1.03k forks source link

Line Graph - Cannot read from undefined property (reading 'x') when useMesh is enabled. #2630

Open rob-cleeia opened 3 months ago

rob-cleeia commented 3 months ago

Describe/explain the bug We're using Nivo/Line (responsive) to render some line graphs. The data we use is nondeterministic, so we validate the data before we send it to the graph. Based on the documentation (here: https://nivo.rocks/line/), we use the following type for line graph data:

interface line {
    id:   string | number;
    data: {
        x: number | string | Date
        y: number | string | Date
    }[]
}[]

However, we've recently noticed that if we have the x axis as a number and the y axis as a string, with useMesh enabled, it throws a client side exception.

To Reproduce Render a Nivo/Line Graph with useMesh enabled, with data containing x axis as a number, and y axis as a string. EG:

import React from 'react';
import { ResponsiveLine } from '@nivo/line';

export const LineGraph: React.FC = () => (
  <div style={{ height: '20rem' }}>
    <ResponsiveLine
      data={[
        {
          id: '1',
          label: 'Joe Biden',
          data: [
            {
              x: 1973,
              y: 'Senator',
            },
            {
              x: 2009,
              y: 'Vice President',
            },
            {
              x: 2017,
              y: 'End of Vice Presidency',
            },
            {
              x: 2021,
              y: 'President',
            },
          ],
        },
      ]}
      useMesh
    />
  </div>
);
export default LineGraph;

Steps to reproduce the behaviour:

  1. Load above component.
  2. Hover mouse over Line Graph

Expected behaviour Should not cause a client side error.

Screenshots Chrome: Screenshot 2024-08-08 at 12 00 12 FireFox: Screenshot 2024-08-08 at 12 02 56

Desktop (please complete the following information - Not sure if this at all relevant, but okay):

Additional context

CTX-Rachel commented 1 month ago

I had the same problem, when temporarily removing LineChart everything was fine.

diego9497 commented 5 days ago

This issue is happening to me when the data is empty ([]) and I hover mouse over the graph, when there is data is working fine. I'm upgrading from 0.79.1 to 0.88.0, it was working fine in the old version.

Codesandbox

Edit: @plouc I added useMesh={data[0]?.data?.length > 1} to prevent the app break, but as I mentioned before this was working fine on the last version that I used 0.79.1