Skip to content

Custom Tooltip

Example of a line chart with a tooltip in HTML, made with the <Line> component. Chart.js draws its tooltip on the canvas, and the tooltip looks only as its options allow. With enabled: false in options.plugins.tooltip, Chart.js stops drawing it, but still calls the external function whenever the tooltip changes. The function gets the tooltip model: opacity is 0 when the tooltip hides, caretX and caretY are where the tooltip points to on the canvas, title and dataPoints hold its content, and labelColors the colors of the datasets.

The example keeps this data in React state and renders the tooltip as an element over the canvas: a container with position: relative holds the chart and the tooltip. The tooltip has pointerEvents: 'none': under the pointer, it would take the pointer off the canvas, and Chart.js would hide it. The interaction option with mode: 'index' shows the values of both datasets for the label nearest to the pointer, as in the grouped bar chart.

The options are created once, with useMemo. react-chartjs-2 updates the chart when it gets a new options object, and after an update Chart.js calls external again while the pointer is on the chart. Options created on every render would make a loop: external sets the state, the new render gives the chart new options, and the update calls external again. In development, React reports this loop with an error:

Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.

See external (custom) tooltips and the tooltip model in the Chart.js documentation.

App.tsx
import type { TooltipModel } from 'chart.js'
import {
useMemo,
useState
} from 'react'
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend
} from 'chart.js'
import { Line } from 'react-chartjs-2'
import { faker } from '@faker-js/faker'
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend
)
const labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July']
export const data = {
labels,
datasets: [
{
label: 'Dataset 1',
data: labels.map(() => faker.number.int({
min: -1000,
max: 1000
})),
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgba(255, 99, 132, 0.5)'
},
{
label: 'Dataset 2',
data: labels.map(() => faker.number.int({
min: -1000,
max: 1000
})),
borderColor: 'rgb(53, 162, 235)',
backgroundColor: 'rgba(53, 162, 235, 0.5)'
}
]
}
interface TooltipContext {
chart: ChartJS
tooltip: TooltipModel<'line'>
}
interface TooltipState {
x: number
y: number
// The tooltip goes on the side of the point with more room
side: 'left' | 'right'
title: string
items: {
label: string
value: string
color: string
}[]
}
export function App() {
const [tooltip, setTooltip] = useState<TooltipState | null>(null)
// New options update the chart, and Chart.js calls `external` again after an update
const options = useMemo(() => ({
responsive: true,
interaction: {
mode: 'index' as const,
intersect: false
},
plugins: {
legend: {
position: 'top' as const
},
title: {
display: true,
text: 'Chart.js Line Chart with a Custom Tooltip'
},
tooltip: {
// No tooltip on the canvas
enabled: false,
external({ chart, tooltip }: TooltipContext) {
if (tooltip.opacity === 0) {
setTooltip(null)
return
}
setTooltip({
x: tooltip.caretX,
y: tooltip.caretY,
side: tooltip.caretX > chart.width / 2 ? 'left' : 'right',
title: tooltip.title.join(' '),
items: tooltip.dataPoints.map((point, index) => ({
label: point.dataset.label ?? '',
value: point.formattedValue,
color: tooltip.labelColors[index]?.borderColor as string
}))
})
}
}
}
}), [])
return (
<div
style={{
position: 'relative',
width: '100%'
}}
>
<Line
options={options}
data={data}
/>
{tooltip && (
<div
style={{
position: 'absolute',
left: tooltip.x,
top: tooltip.y,
transform: tooltip.side === 'right'
? 'translate(12px, -50%)'
: 'translate(calc(-100% - 12px), -50%)',
// Under the pointer, the tooltip would take it off the canvas and hide itself
pointerEvents: 'none',
padding: '8px 12px',
borderRadius: 6,
background: 'white',
boxShadow: '0 2px 8px rgba(0, 0, 0, 0.25)',
fontFamily: 'sans-serif',
fontSize: 13,
whiteSpace: 'nowrap'
}}
>
<strong>
{tooltip.title}
</strong>
{tooltip.items.map(item => (
<div
key={item.label}
style={{
display: 'flex',
alignItems: 'center',
gap: 8,
marginTop: 4
}}
>
<span
style={{
width: 10,
height: 10,
borderRadius: '50%',
background: item.color
}}
/>
{item.label}
<strong
style={{
marginLeft: 'auto'
}}
>
{item.value}
</strong>
</div>
))}
</div>
)}
</div>
)
}