score:70
There is no built-in method to change background color, but you can use CSS. JSFiddle.
ctx.style.backgroundColor = 'rgba(255,0,0,255)';
EDIT
If you want to fill exact area of chart and no whole div, you can write your own chart.js plugin. Try it on JSFiddle.
Chart.pluginService.register({
beforeDraw: function (chart, easing) {
if (chart.config.options.chartArea && chart.config.options.chartArea.backgroundColor) {
var ctx = chart.chart.ctx;
var chartArea = chart.chartArea;
ctx.save();
ctx.fillStyle = chart.config.options.chartArea.backgroundColor;
ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
ctx.restore();
}
}
});
var config = {
type: 'bar',
data: {
labels:["Label1","Label2","Label3","Label4"],
borderColor : "#fffff",
datasets: [
{
data: ["2","3","1","4"],
borderColor : "#fff",
borderWidth : "3",
hoverBorderColor : "#000",
backgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5"
],
hoverBackgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5"
]
}]
},
options: {
scales: {
yAxes: [{
ticks:{
min : 0,
stepSize : 1,
fontColor : "#000",
fontSize : 14
},
gridLines:{
color: "#000",
lineWidth:2,
zeroLineColor :"#000",
zeroLineWidth : 2
},
stacked: true
}],
xAxes: [{
ticks:{
fontColor : "#000",
fontSize : 14
},
gridLines:{
color: "#fff",
lineWidth:2
}
}]
},
responsive:false,
chartArea: {
backgroundColor: 'rgba(251, 85, 85, 0.4)'
}
}
};
var ctx = document.getElementById("barChart").getContext("2d");
new Chart(ctx, config);
score:2
This works on latest chartjs
const custom_canvas_background_color = {
id: 'custom_canvas_background_color',
beforeDraw: (chart, args, options) => {
const {
ctx,
chartArea: { top, right, bottom, left, width, height },
scales: { x, y },
} = chart;
ctx.save();
ctx.globalCompositeOperation = 'destination-over';
ctx.fillStyle = '#E5E5E5';
ctx.fillRect(left, top, width, height);
ctx.restore();
},
};
Then add as plugin
plugins: [custom_canvas_background_color]
score:10
The canvas background is transparent by definition, like any element, so you just need to define the background-color in your canvas, for example, in your case you will need this CSS:
canvas#barChart {
background-color: #f00;
}
or HTML inline, to clarify the idea:
<canvas id="barChart" width="600" height="300" style="background-color: #f00;"></canvas>
Source: stackoverflow.com
Related Query
- Chart area background color chartjs
- Background color of the chart area in chartjs not working
- Remove background color in chartjs line chart
- Legend color not working with randomly generated background colors in chartjs pie chart
- Chart.js Polar Area Chart scale background color
- Chartjs Bar Chart add background color to value labels
- Trouble with setting background color for Line chart at Chartjs version 3.5.1
- Add an image as background in ChartJS chart area [Not to canvas]
- Bars of my chartjs chart has no background color
- Chart.js line chart set background color
- chartjs datalabels change font and color of text displaying inside pie chart
- set background color to save canvas chart
- chartjs - top and bottom padding of a chart area
- How to set a full length background color for each bar in chartjs bar
- ChartJS disable gridlines outside chart area
- Chartjs doughnut chart with gradient color
- ChartJS click on bar and change it's background color
- Angular 8 & ChartJs change color in pie chart
- Chart.js - Color specific parts of the background in a line chart
- unable to add background color to the canvas using jspdf and chartjs
- How to add background color between two lines in yAxis Chartjs
- Use transparent stroke color with chartjs pie chart
- How to add area with break on line chart with fill color
- Setting Common labels and background color common for all the charts in ChartJs
- How to add background color between two specific lines in Chartjs 3.1
- Chart js background color changes on click
- Change the background color of tooltip dynamically using chart color
- how to highlight a specific area on chartjs line chart
- ChartJS 2.6 - Change color of bar in Bar chart programmatically
- How to remove or color line/grid in polar chart area using Chart.js?
More Query from same tag
- Chart JS, backgroundColor not showing
- Pie chart legend styling using Chart js
- Chartjs Bar Chart Legend
- How to connect Firebase in Chart.js?
- I´m unable to map elements of an array
- Vue-Chartjs not rendering graph
- Data Labels on top of points in line charts
- Different styles in ticks on ChartJS
- How to set the xAxes min and max values of time cartesian chart in Chart.js
- Cannot read property 'map' of undefined when looking at an array with no index
- Secondary bar with chart.js
- How to add second Y-axis for Bar and Line chart in Chart.js?
- Ionic2 Angular2: I can't seem to get an ng2-chart to appear in my app
- Datalabels plugin throws error when using scriptable option
- How to import Chart.js with Webpack
- Add HTML to label of bar chart - chart js
- How can I add a unit to the end of my Y Axis values in ChartJS?
- ChartJS - Bottom labels displayed vertically
- Obtain max value of y axis of line chart rendered with Chart.js
- Force chart to redraw or update Chart.js
- Changing width and height in angular-chart.js module
- Remove data text from Chart.js on hover
- Getting data into script tag from route?
- how to use chart.js with angular-chart using requirejs
- Chartjs unselected lines on render
- Chart JS Logarithmic x-axis
- Gantt Chart Timeline Chart js 3
- How should i apply date picker in my chart
- The engine “node” is incompatible with this module when installing React-chartjs-2 Chart.js,
- Vertical Bar Chart not displaying Correctly