Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js line chart set background color

I'm working with Chart.js and want to convert a line chart to a PNG. The problem is that the image always downloads with a transparent background, which is not what I need. I tried many options, nothing really worked.

And suggestions?

like image 460
HXH Avatar asked May 26 '15 17:05

HXH


1 Answers

Here's a solution that works perfectly, also when saving to an image file (like png). I'm blatantly copying this from @etimberg at the chartjs issue tracker.

Chart.plugins.register({
  beforeDraw: function(chartInstance) {
    var ctx = chartInstance.chart.ctx;
    ctx.fillStyle = "white";
    ctx.fillRect(0, 0, chartInstance.chart.width, chartInstance.chart.height);
  }
});

This should go before your chart in the source.

like image 187
Mark Avatar answered Sep 21 '22 06:09

Mark