Custom Legend
Example of a pie chart with a legend in HTML, made with the <Pie> component and the data of the pie chart example. The legend of Chart.js is a plugin that draws on the canvas. The example does not register it, so Chart.js draws no legend, and React renders a list next to the chart instead: a list can show anything, here the number of votes for each color. With the Legend plugin registered, plugins.legend.display: false turns its legend off.
A click on an item hides or shows its slice. The ref of the chart gets the Chart.js instance, as How to access the Chart.js instance? explains, and toggleDataVisibility changes the visibility of the slice, which Chart.js applies on chart.update(). The legend keeps the visibility in React state, read back from the chart with getDataVisibility, and crosses out the hidden items. A legend of datasets, for a line or a bar chart, does the same with setDatasetVisibility and isDatasetVisible.
The items are buttons with aria-pressed, so the legend also works from the keyboard and with screen readers, unlike a legend drawn on the canvas. See the HTML legend sample and the API of Chart.js.
Edit the example and see the result right away: StackBlitz runs it in your browser.
Open in new tabView on GitHubOpen in StackBlitz
import { useRef, useState} from 'react'import { Chart as ChartJS, ArcElement, Tooltip} from 'chart.js'import { Pie } from 'react-chartjs-2'
// Without the Legend plugin, Chart.js draws no legend on the canvasChartJS.register(ArcElement, Tooltip)
const labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange']const votes = [12, 19, 3, 5, 2, 3]const backgroundColor = [ 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)']const borderColor = [ 'rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)']
export const data = { labels, datasets: [ { label: '# of Votes', data: votes, backgroundColor, borderColor, borderWidth: 1 } ]}
export function App() { const chartRef = useRef<ChartJS<'pie'>>(null) const [hidden, setHidden] = useState<boolean[]>([]) const toggle = (index: number) => { const chart = chartRef.current
if (!chart) { return }
// Hides the slice: a legend of datasets would call `setDatasetVisibility` chart.toggleDataVisibility(index) chart.update() setHidden(labels.map((_, i) => !chart.getDataVisibility(i))) }
return ( <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 32, width: '100%' }} > <div style={{ flex: 1, maxWidth: 400 }} > <Pie ref={chartRef} data={data} /> </div> <ul style={{ margin: 0, padding: 0, listStyle: 'none', fontFamily: 'sans-serif', fontSize: 14 }} > {labels.map((label, index) => ( <li key={label}> <button type='button' aria-pressed={!hidden[index]} onClick={() => toggle(index)} style={{ display: 'flex', alignItems: 'center', gap: 8, width: '100%', padding: '4px 0', border: 'none', background: 'none', font: 'inherit', cursor: 'pointer', textDecoration: hidden[index] ? 'line-through' : 'none' }} > <span style={{ width: 14, height: 14, border: `1px solid ${borderColor[index]}`, background: backgroundColor[index] }} /> {label} <strong style={{ marginLeft: 'auto', paddingLeft: 16 }} > {votes[index]} </strong> </button> </li> ))} </ul> </div> )}