I have problem with chart js, i want to coloring chart area like image above
I try to find configuration from charJs Docs , but nothing matched. its possible or not to change chart area background color? if possible anyone can help me?
Html
JavaScript
x
2
1
<canvas id="barChart" width="600" height="300"></canvas>
2
Javascript
JavaScript
1
58
58
1
var ctx = document.getElementById("barChart");
2
var barChart = new Chart(ctx,{
3
type: 'bar',
4
data: {
5
labels:["Label1","Label2","Label3","Label4"],
6
borderColor : "#fffff",
7
datasets: [
8
{
9
data: ["2","3","1","4"],
10
borderColor : "#fff",
11
borderWidth : "3",
12
hoverBorderColor : "#000",
13
backgroundColor: [
14
"#f38b4a",
15
"#56d798",
16
"#ff8397",
17
"#6970d5"
18
],
19
hoverBackgroundColor: [
20
"#f38b4a",
21
"#56d798",
22
"#ff8397",
23
"#6970d5"
24
]
25
}]
26
},
27
options: {
28
scales: {
29
yAxes: [{
30
ticks:{
31
min : 0,
32
stepSize : 1,
33
fontColor : "#000",
34
fontSize : 14
35
},
36
gridLines:{
37
color: "#000",
38
lineWidth:2,
39
zeroLineColor :"#000",
40
zeroLineWidth : 2
41
},
42
stacked: true
43
}],
44
xAxes: [{
45
ticks:{
46
fontColor : "#000",
47
fontSize : 14
48
},
49
gridLines:{
50
color: "#fff",
51
lineWidth:2
52
}
53
}]
54
},
55
responsive:false
56
}
57
});
58
Here’s my current code jsFiddle
so everyone can try for find solution. thanks for your help.
Advertisement
Answer
There is no built-in method to change background color, but you can use CSS. JSFiddle.
JavaScript
1
2
1
ctx.style.backgroundColor = 'rgba(255,0,0,255)';
2
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.
JavaScript
1
77
77
1
Chart.pluginService.register({
2
beforeDraw: function (chart, easing) {
3
if (chart.config.options.chartArea && chart.config.options.chartArea.backgroundColor) {
4
var ctx = chart.chart.ctx;
5
var chartArea = chart.chartArea;
6
7
ctx.save();
8
ctx.fillStyle = chart.config.options.chartArea.backgroundColor;
9
ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
10
ctx.restore();
11
}
12
}
13
});
14
15
var config = {
16
type: 'bar',
17
data: {
18
labels:["Label1","Label2","Label3","Label4"],
19
borderColor : "#fffff",
20
datasets: [
21
{
22
data: ["2","3","1","4"],
23
borderColor : "#fff",
24
borderWidth : "3",
25
hoverBorderColor : "#000",
26
backgroundColor: [
27
"#f38b4a",
28
"#56d798",
29
"#ff8397",
30
"#6970d5"
31
],
32
hoverBackgroundColor: [
33
"#f38b4a",
34
"#56d798",
35
"#ff8397",
36
"#6970d5"
37
]
38
}]
39
},
40
options: {
41
scales: {
42
yAxes: [{
43
ticks:{
44
min : 0,
45
stepSize : 1,
46
fontColor : "#000",
47
fontSize : 14
48
},
49
gridLines:{
50
color: "#000",
51
lineWidth:2,
52
zeroLineColor :"#000",
53
zeroLineWidth : 2
54
},
55
stacked: true
56
}],
57
xAxes: [{
58
ticks:{
59
fontColor : "#000",
60
fontSize : 14
61
},
62
gridLines:{
63
color: "#fff",
64
lineWidth:2
65
}
66
}]
67
},
68
responsive:false,
69
chartArea: {
70
backgroundColor: 'rgba(251, 85, 85, 0.4)'
71
}
72
}
73
};
74
75
var ctx = document.getElementById("barChart").getContext("2d");
76
new Chart(ctx, config);
77