A gradient fill in react-chartjs-2 throws or never paints
Fix it fast
Most common cause, 1 of 3data is still passed as a function of the canvas
Pass a plain object and move the gradient into the dataset's backgroundColor as a function.
// before (react-chartjs-2 2.x and 3.x)
// <Line data={(canvas) => ({ datasets: [{ backgroundColor: makeGradient(canvas.getContext('2d')) }] })} />
// after
const data = {
labels: ['Mon', 'Tue', 'Wed'],
datasets: [{
label: 'Visits',
data: [12, 19, 7],
fill: true,
backgroundColor: (context) => areaGradient(context.chart),
}],
};
<Line data={data} />Short answer
A gradient fill in react-chartjs-2 4 and 5 fails in three ways: a data prop written as a function, removed in v4, ends in Cannot read properties of undefined (reading 'map'); a gradient built before the chart is laid out has no chartArea; and fill does nothing without the Filler plugin. Use a scriptable backgroundColor and register Filler. Applies to react-chartjs-2 5.3, Chart.js 4.6.
Which cause do you have?
Look at what the data prop receives and whether Filler is registered. A function in data is cause 1. A gradient built from chartRef.current or chart.chartArea outside a scriptable option is cause 2. No Filler in the register call, while the chart shows no error, is cause 3.
grep -rn "data={" src/ | grep "=>"
grep -rn "createLinearGradient\|chartArea\|chartRef.current" src/
grep -rn "ChartJS.register\|Chart.register" src/Then find the line you see in this list and open its fix:
- 1data is still passed as a function of the canvas
Cannot read properties of undefined (reading 'map'), with setDatasets or cloneData from react-chartjs-2 in the stack, and data={(canvas) => ...} in the component - 2The gradient is created before the chart has a layout
Cannot read properties of null (reading 'ctx') from chartRef.current during render, or reading 'bottom' of an undefined chartArea on the first draw - 3The Filler plugin is not registered
No error at all: the line takes the gradient colour but the area under it stays blank even with fill: true
What does the error look like?
TypeError: Cannot read properties of undefined (reading 'map')TypeError: Cannot read properties of null (reading 'ctx')TypeError: Cannot read properties of undefined (reading 'bottom')The line draws but the area under it stays empty
Cause 1 of 3
data is still passed as a function of the canvas
You seeCannot read properties of undefined (reading 'map'), with setDatasets or cloneData from react-chartjs-2 in the stack, and data={(canvas) => ...} in the component
Check
Passing data as a function of the canvas throws inside react-chartjs-2 4 and 5. The v4 migration guide says that option was removed; the component now copies data.labels and data.datasets straight from the prop, and a function has no datasets array, so the map call on it fails before Chart.js starts.
grep -rn "getContext('2d')" src/Fix
Pass a plain object and move the gradient into the dataset's backgroundColor as a function. Chart.js calls that function with a context whose chart property holds ctx and chartArea.
// before (react-chartjs-2 2.x and 3.x)
// <Line data={(canvas) => ({ datasets: [{ backgroundColor: makeGradient(canvas.getContext('2d')) }] })} />
// after
const data = {
labels: ['Mon', 'Tue', 'Wed'],
datasets: [{
label: 'Visits',
data: [12, 19, 7],
fill: true,
backgroundColor: (context) => areaGradient(context.chart),
}],
};
<Line data={data} />Verify
The chart mounts without a TypeError, and the console shows no error from cloneData.
console.log(typeof data) // "object"Confirmed by react-chartjs-2.js.org
Cause 2 of 3
The gradient is created before the chart has a layout
You seeCannot read properties of null (reading 'ctx') from chartRef.current during render, or reading 'bottom' of an undefined chartArea on the first draw
Check
A canvas-context error while building the gradient means the code ran before Chart.js finished its layout. During the first render the ref is still null, and the Chart.js linear gradient sample notes that chartArea is missing on the initial chart load, so reading its edges fails.
grep -rn -B2 -A6 "createLinearGradient" src/Fix
Build the gradient inside the scriptable option, return nothing until chartArea exists, and rebuild it only when the chart's size changes, as the official sample does.
let cached, cachedWidth, cachedHeight;
function areaGradient(chart) {
const { ctx, chartArea } = chart;
if (!chartArea) return; // first pass, before layout
const width = chartArea.right - chartArea.left;
const height = chartArea.bottom - chartArea.top;
if (!cached || width !== cachedWidth || height !== cachedHeight) {
cachedWidth = width;
cachedHeight = height;
cached = ctx.createLinearGradient(0, chartArea.bottom, 0, chartArea.top);
cached.addColorStop(0, 'rgba(54, 162, 235, 0)');
cached.addColorStop(1, 'rgba(54, 162, 235, 0.6)');
}
return cached;
}Verify
Reload and resize the window: the fill fades from bottom to top, and no TypeError appears on the first paint.
// resize the browser window and watch the consoleConfirmed by chartjs.org
Cause 3 of 3
The Filler plugin is not registered
You seeNo error at all: the line takes the gradient colour but the area under it stays blank even with fill: true
Check
When the gradient never fills the area but nothing throws, Chart.js is missing the plugin that draws fills. react-chartjs-2 4 and 5 are tree-shakable like Chart.js 3 and 4, so every element, scale and plugin must be registered, and the react-chartjs-2 FAQ names Filler as the one that background filling needs.
grep -rn "Filler" src/Fix
Register Filler next to the elements and scales the chart uses. The typed Line component already registers its own controller.
import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Filler } from 'chart.js';
import { Line } from 'react-chartjs-2';
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Filler);Verify
The area under the line now shows the gradient from cause 2.
grep -rn "Filler" src/Confirmed by react-chartjs-2.js.org
How do I stop it coming back?
- Keep gradient code in one helper that takes the chart, so every chart guards chartArea the same way.
- Register Chart.js parts in a single module imported once, and add Filler there as soon as any chart uses fill.
- When copying an example, check its react-chartjs-2 major: data as a function belongs to 2.x and 3.x code.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.