1

I have nivo line chart with gaps like this: nivo line chart with data gaps Gaps are covered by passing y/value: null in november and december in data series

Tooltip displays only on data points and this is correct, but I want add tooltip at November and December with explanation why there is no data.

karczewski
  • 96
  • 1
  • 5

1 Answers1

3

The solution is to add custom layer 'mesh' which is responsible for displaying tooltips on line chart.

  1. You have to declare custom layers in <ResponsiveLine component:

      layers={[
          'grid',
          'markers',
          'axes',
          'areas',
          'crosshair',
          'lines',
          'slices',
          'points',
          CustomMesh,
          'legends',
        ]}
    
  2. Create CustomMesh component:

    const CustomMesh = (layerData: any) => {
    const { showTooltipAt, hideTooltip } = useTooltip();
    
    const handleMouseEnter = (point: any) => {
      showTooltipAt(
        layerData.tooltip({ point }),
        [point.x + layerData.margin.left, point.y + layerData.margin.top],
        'top'
      );
    };
    const handleMouseMove = (point: any) => {
      showTooltipAt(
        layerData.tooltip({ point }),
        [point.x + layerData.margin.left, point.y + layerData.margin.top],
        'top'
      );
    };
    const handleMouseLeave = (point: any) => {
      hideTooltip();
    };
    const nullValuePoints = layerData.series.reduce((acc: any[], cur: any) => {
        cur.data.forEach(({ data, position }: any) => {
      if (data.y === null) {
        const point = {
          x: position.x,
          y: 100, //whatever you want
          data: {
            x: data.x,
          },
        };
        acc.push(point);
      }
    });
        return acc;
      }, []);
    return (
      <Mesh
        nodes={[...layerData.points, ...nullValuePoints]}
        width={layerData.width}
        height={layerData.height}
        onMouseEnter={handleMouseEnter}
        onMouseMove={handleMouseMove}
        onMouseLeave={handleMouseLeave}
        debug={layerData.debugMesh}
      />
    );
    };
    

When nullValuePoints are my custom points with no data

  1. Import required packages:
import { Mesh } from '@nivo/voronoi';
import { useTooltip } from '@nivo/tooltip';

result: nivo line chart with custom mesh layer

karczewski
  • 96
  • 1
  • 5