Customize the tooltip on a MUI X Charts graph
Asked Answered
H

1

6

I am building a distance versus elevation line chart from GeoJSON data, and I would like to edit the tooltip's style and content.

Specifically, I am wondering how to:

  • have the tooltip in relative position underneath the graph in a more discreet way, rather than ovrlapping like it is right now
  • round the distance value to 2 decimals
  • remove the ',' thousands separator from the distance value
  • add ' m' to the distance value

Graph

<LineChart
    margin={{ top: 5, bottom: 30, left: 55, right: 0 }}
    xAxis={[
        {
            min: Math.min(...xAxis),
            max: Math.max(...xAxis),
            data: xAxis,
            valueFormatter: (element) => `${element} km`,
        },
    ]}
    yAxis={[
        {
            tickNumber: 5,
            min: Math.min(...yAxis),
            max: Math.max(...yAxis),
            valueFormatter: (element) => `${element} m`,
        },
    ]}
    series={[
        {
            data: yAxis,
        },
    ]}
/>
Hoggish answered 2/12, 2023 at 12:7 Comment(0)
W
1
  1. have the tooltip in relative position underneath the graph in a more discreet way, rather than ovrlapping like it is right now

    That would be a great feature to do that with a single props. I created an issue to track progress on this idea

  2. round the distance value to 2 decimals

    For that, the series config have a valueFormatter

  3. remove the ',' thousands separator from the distance value add ' m' to the distance value

    That's solved with the valueFormatter too.


Here is a codesandbox with proposed solutions: https://codesandbox.io/p/sandbox/reverent-night-xfjytg?file=%2Fsrc%2FDemo.tsx%3A32%2C17

Webster answered 21/12, 2023 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.