Chart.js: How can I change the background color to the chartArea?

Created on 17 Oct 2016  ·  5Comments  ·  Source: chartjs/Chart.js

Hi, I've been trying to change the background color without using CSS because it change the background color to the whole square (chart/labels), is there any way to change the background color to just the area I need? (see the image below).

image

Hopefully someone can help me with this.

support

Most helpful comment

@echavezNS you can write a plugin to do this. The plugin below should work

Chart.plugins.register({
  beforeDraw: function(chartInstance, easing) {
    var ctx = chartInstance.chart.ctx;
    ctx.fillStyle = 'red'; // your color here

    var chartArea = chartInstance.chartArea;
    ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
  }
});

All 5 comments

@echavezNS you can write a plugin to do this. The plugin below should work

Chart.plugins.register({
  beforeDraw: function(chartInstance, easing) {
    var ctx = chartInstance.chart.ctx;
    ctx.fillStyle = 'red'; // your color here

    var chartArea = chartInstance.chartArea;
    ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
  }
});

Closing as answered

@etimberg thanks, it worked

.chart-container {
background-color: #ece9e9;
}

@etimberg thanks, it worked

Was this page helpful?
0 / 5 - 0 ratings