Why your Chart.js 4 chart is blank
Chart. js is one of the easiest chart libraries to start with, and one of the easiest to end up staring at an empty box with.
Chart.js is one of the easiest chart libraries to start with, and one of the easiest to end up staring at an empty box with. Almost every blank chart I’ve debugged came down to one of three things: a part of the chart that was never registered, a container with no height, or a canvas that still had an old chart attached.
1. Nothing was registered
Since version 3, Chart.js is tree-shakeable. The main entry point gives you the Chart class and the pieces, but doesn’t wire any of them up. If you import only Chart and create a line chart, it throws:
Error: "line" is not a registered controller.
Register the controller, and the next error tells you about the next missing piece:
Error: "category" is not a registered scale.
For a basic line chart with text labels on the x axis, you need all of these:
import {
Chart,
LineController,
LineElement,
PointElement,
CategoryScale,
LinearScale,
Tooltip,
Legend,
} from 'chart.js';
Chart.register(
LineController, LineElement, PointElement,
CategoryScale, LinearScale,
Tooltip, Legend,
);
The errors only cover controllers, elements and scales. Plugins are different: if you forget Tooltip or Legend, nothing throws. The chart simply has no tooltips or no legend, and your options.plugins.tooltip settings are ignored. That one can take a while to notice.
If bundle size doesn’t matter much, skip all of this:
import Chart from 'chart.js/auto'; // registers everything
A side note on version 4 itself: 4.0 shipped as an ES module only, which broke require(‘chart.js’) in older tooling. 4.1 added a CommonJS build back, so on 4.1 or later that particular error goes away.
2. The container has no height
Charts are responsive by default, and Chart.js sizes the canvas from its parent element, not from the canvas itself. The docs ask for a parent that’s relatively positioned and holds only the canvas:
<div class="chart-box">
<canvas id="sales"></canvas>
</div>
.chart-box {
position: relative;
height: 300px; /* or any size that doesn't depend on the canvas */
}
The classic blank chart is maintainAspectRatio: false inside a parent with no set height. Without the aspect ratio, the chart takes its height from the parent, and the parent takes its height from its content, which is the chart. Depending on the layout around it, that loop ends with a chart squashed to almost nothing, or one that grows a little taller every time the window resizes. Give the parent a height, or keep the aspect ratio on.
Setting width and height on the canvas in CSS doesn’t help either. It stretches the drawing instead of resizing it, which gives you blurry, distorted text.
3. The canvas already has a chart
In a React app in development, you may see this:
Canvas is already in use. Chart with ID ‘0’ must be destroyed before the canvas with ID ” can be reused.
React 18’s Strict Mode runs effects twice in development, to catch effects that don’t clean up. A chart created in an effect without a cleanup gets created twice on the same canvas, and Chart.js refuses. The fix is the cleanup it was asking for:
import { useEffect, useRef } from 'react';
import Chart from 'chart.js/auto';
export function LineChart({ labels, values }) {
const canvasRef = useRef(null);
const chartRef = useRef(null);
// create once, destroy on unmount
useEffect(() => {
chartRef.current = new Chart(canvasRef.current, {
type: 'line',
data: { labels: [], datasets: [{ label: 'Sales', data: [] }] },
});
return () => chartRef.current.destroy();
}, []);
// update the data in place
useEffect(() => {
const chart = chartRef.current;
chart.data.labels = labels;
chart.data.datasets[0].data = values;
chart.update();
}, [labels, values]);
return (
<div style={{ position: 'relative', height: 300 }}>
<canvas ref={canvasRef} />
</div>
);
}
Note the second effect. Updating chart.data and calling chart.update() animates smoothly from the old values to the new ones. Destroying and recreating the chart on every change works too, but it flashes and throws away the animation.
Bonus: the chart isn’t blank, it’s just slow
A line chart with 100,000 points renders, but every hover and resize redraws all of them. Chart.js has a decimation plugin that draws a representative subset instead:
import { Decimation } from 'chart.js';
Chart.register(Decimation);
new Chart(canvas, {
type: 'line',
data: {
datasets: [{ data: points, pointRadius: 0, borderWidth: 1 }], // points: [{ x, y }, ...]
},
options: {
parsing: false,
scales: { x: { type: 'linear' } },
plugins: {
decimation: { enabled: true, algorithm: 'lttb', samples: 500 },
},
},
});
It’s picky about when it applies, and when the conditions aren’t met it does nothing, silently. The data has to be { x, y } objects, parsing has to be false, and the x axis has to be linear or time. I tested 100,000 points both ways: with the setup above, the chart drew 500 points. With the same data as labels and a category axis, it still drew all 100,000.
A checklist for blank charts
- Open the console. A “not registered” error names exactly what to import.
- If there’s no error, check the parent’s height in devtools. Zero means the sizing loop.
- In React, make sure every new Chart has a matching destroy().
- Missing tooltips or legend, but no error? Register the plugin.