Chart.js line chart set background color

后端 未结 4 1452
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 22:14

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.

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 22:56

    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.

提交回复
热议问题