Chart.js line chart set background color

后端 未结 4 1445
伪装坚强ぢ
伪装坚强ぢ 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 23:07

    You can solve this pretty simply, if you're willing to change the code in chart.js.

    There's a function in chart.js called clear. You can find the clear function by searching for clearRect. The clear function is called every time chart.js redraws the chart, so that's where we'll set the background. Right below the line with clearRect, add the following lines.

    ...
    chart.ctx.clearRect(0,0,chart.width,chart.height);
    
    // Add these 3 lines
    chart.ctx.rect(0, 0, chart.width, chart.height);
    chart.ctx.fillStyle="#fff"; // Put your desired background color here
    chart.ctx.fill();
    ...
    

提交回复
热议问题