Annotations
Example of a line chart with a target line and a highlighted range, made with the <Line> component and chartjs-plugin-annotation. Install the plugin next to Chart.js:
pnpm add chartjs-plugin-annotationyarn add chartjs-plugin-annotationnpm install --save chartjs-plugin-annotationThe plugin does not register itself, so the example registers it with ChartJS.register. The annotations go into options.plugins.annotation.annotations, an object with an annotation under each key, and the type of an annotation picks its kind: line, box, label, point, ellipse or polygon. Coordinates are in the units of the scales: yMin: 800 and yMax: 800 put the target line at 800 on the y axis, and xMin: 'June' with xMax: 'August' place the box by the labels of the x axis. A coordinate left out stretches the annotation to the edge of the chart, so the line goes across the chart and the box is as high as it is. An annotation outside the data also extends the scales, unless its adjustScaleRange is false.
Each annotation can have a label, shown with display: true: the target line has one at its end, and the box at its top. See line and box annotations in the plugin documentation.
Importing the plugin adds its options to the types of Chart.js, and TypeScript tells the kinds of annotations apart by type. In options declared outside of a component, as in the example, 'line' as const keeps the type from widening to string. Without it, TypeScript rejects the options with an error that ends with Type 'string' is not assignable to type '"line"'.
Edit the example and see the result right away: StackBlitz runs it in your browser.
Open in new tabView on GitHubOpen in StackBlitz
import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend} from 'chart.js'import annotationPlugin from 'chartjs-plugin-annotation'import { Line } from 'react-chartjs-2'import { faker } from '@faker-js/faker'
ChartJS.register( CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, annotationPlugin)
export const options = { responsive: true, plugins: { legend: { position: 'top' as const }, title: { display: true, text: 'Chart.js Line Chart with Annotations' }, annotation: { annotations: { // Without x values, the line goes across the whole chart target: { type: 'line' as const, yMin: 800, yMax: 800, borderColor: 'rgb(75, 192, 192)', borderWidth: 2, borderDash: [6, 6], label: { display: true, content: 'Target', position: 'end' as const } }, // Without y values, the box is as high as the chart summer: { type: 'box' as const, xMin: 'June', xMax: 'August', backgroundColor: 'rgba(255, 206, 86, 0.2)', borderWidth: 0, label: { display: true, content: 'Summer sale', position: { x: 'center' as const, y: 'start' as const } } } } } }}
const labels = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
export const data = { labels, datasets: [ { label: 'Sales', data: labels.map(() => faker.number.int({ min: 0, max: 1000 })), borderColor: 'rgb(255, 99, 132)', backgroundColor: 'rgba(255, 99, 132, 0.5)' } ]}
export function App() { return ( <Line options={options} data={data} /> )}