In React, with react-chartjs-2, i was able to set background color of chart like so:
const plugin = {
beforeDraw: (chartCtx) => {
const ctx = chartCtx.canvas.getContext('2d');
ctx.save();
ctx.globalCompositeOperation = 'destination-over';
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, chartCtx.width, chartCtx.height);
ctx.restore();
}
};
And then add the plugin to the chart:
<Line ref={chartRef} data={chartData} options={options} plugins={[plugin]} />
Reference to the Docs
To save the chart as an image:
I created a function that uses the toBase64Image function to extract the image. I attached this function to a button to help me download chart image on click of button.
function downloadImage(){
const link = document.createElement("a");
link.download = `${chart.name || 'chart'}.jpg`
link.href = chartRef.current.toBase64Image('image/jpeg', 1);
link.click();
}