Given my chartJS config below
JavaScript
x
46
46
1
var ctx = document.getElementById('myChart').getContext('2d');
2
Chart.defaults.global.defaultFontColor = 'rgba(255, 255, 255, 1)';
3
Chart.defaults.global.defaultFontFamily = 'Arial';
4
5
var myChart = new Chart(ctx, {
6
type: 'bar',
7
data: {
8
labels: ['Investment', 'Sustainable'],
9
datasets: [{
10
label: 'myLabel',
11
data: [11, 5],
12
backgroundColor: [
13
'rgba(234, 82, 4, 0.2)',
14
'rgba(0, 121, 109, 0.2)'
15
16
],
17
borderColor: [
18
'rgba(234, 82, 4, 1)',
19
'rgba(0, 121, 109, 1)'
20
21
],
22
borderWidth: 1
23
}]
24
},
25
options: {
26
legend: {
27
labels: {
28
display: true
29
}
30
},
31
scales: {
32
yAxes: [{
33
ticks: {
34
beginAtZero: true
35
},
36
gridLines: {
37
color: 'rgba(255, 255, 255, 0.1)'
38
},
39
scaleLabel: {
40
display: true,
41
},
42
}],
43
}
44
}
45
});
46
I need to get something as close as the following
Using Quickchart API, I am submitting the config through the URL, but I am having trouble setting the labels color? options:{legend:{labels:{fontColor: 'white'}},
JavaScript
1
2
1
https://quickchart.io/chart?c={type:%27bar%27,data:{labels:[%27Investment%27,%27Sustainable%20%27],datasets:[{label:%27myLabel%27,data:[11,5],backgroundColor:%20[%27rgba(234,%2082,%204,%200.2)%27,%27rgba(0,%20121,%20109,%200.2)%27],borderColor:%20[%27rgba(234,%2082,%204,%201)%27,%27rgba(0,%20121,%20109,%201)%27]}]}}
2
Update 2
I am trying to construct the URL but I am getting some issues;
JavaScript
1
64
64
1
<script type="text/javascript">// <![CDATA[
2
var carbon = {
3
type: 'bar',
4
data: {
5
labels: ['Average Investment', 'Sustainable Investment'],
6
datasets: [{
7
label: 'Tonnes of CO2 per year',
8
data: [11, 5],
9
borderWidth: 1,
10
backgroundColor: ['rgba(234, 82, 4, 0.2)', 'rgba(0, 121, 109, 0.2)'],
11
borderColor: ['rgba(234, 82, 4, 1)', 'rgba(0, 121, 109, 1)'],
12
}]
13
},
14
options: {
15
plugins: {
16
datalabels: {
17
anchor: 'end',
18
align: 'top',
19
color: '#fff',
20
backgroundColor: 'rgba(34, 139, 34, 0.6)',
21
borderColor: 'rgba(34, 139, 34, 1.0)',
22
borderWidth: 1,
23
borderRadius: 5,
24
formatter: (value) => {
25
return value + 'k';
26
},
27
},
28
},
29
legend: {
30
labels: {
31
fontColor: 'white'
32
}
33
},
34
title: {
35
display: true,
36
text: 'Tones of CO2 pear year'
37
},
38
scales: {
39
xAxes: [{
40
ticks: {
41
fontColor: 'white'
42
}
43
}],
44
yAxes: [{
45
ticks: {
46
beginAtZero: true,
47
fontColor: 'white'
48
},
49
gridLines: {
50
color: 'rgba(255, 255, 255, 0.1)'
51
},
52
}]
53
}
54
}
55
};
56
57
58
var link = JSON.stringify(carbon);
59
var link0 = JSON.parse(link);
60
var link2 = encodeURI(link0);
61
console.log(typeof link0+ " "+typeof link+" ------------------ "+typeof link2);
62
// ]]></script>
63
<div><img width="200" height="100" src="https://quickchart.io/chart?c="/></div>
64
Which should render the following
Advertisement
Answer
Which version of Chart.js are you using because it seems to be working fine with your config.
JavaScript
1
47
47
1
var options = {
2
type: 'bar',
3
data: {
4
labels: ['Investment', 'Sustainable'],
5
datasets: [{
6
label: 'Tonnes of CO2 per year',
7
data: [11, 5],
8
borderWidth: 1,
9
backgroundColor: [
10
'rgba(234, 82, 4, 0.2)',
11
'rgba(0, 121, 109, 0.2)'
12
13
],
14
borderColor: [
15
'rgba(234, 82, 4, 1)',
16
'rgba(0, 121, 109, 1)'
17
18
],
19
}]
20
},
21
options: {
22
legend: {
23
labels: {
24
fontColor: 'white'
25
}
26
},
27
scales: {
28
xAxes: [{
29
ticks: {
30
fontColor: 'white'
31
}
32
}],
33
yAxes: [{
34
ticks: {
35
beginAtZero: true,
36
fontColor: 'white'
37
},
38
gridLines: {
39
color: 'rgba(255, 255, 255, 0.1)'
40
},
41
}]
42
}
43
}
44
}
45
46
var ctx = document.getElementById('chartJSContainer').getContext('2d');
47
new Chart(ctx, options);
JavaScript
1
3
1
canvas {
2
background-color: #002A5E;
3
}
JavaScript
1
4
1
<body>
2
<canvas id="chartJSContainer" width="600" height="400"></canvas>
3
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js" integrity="sha512-hZf9Qhp3rlDJBvAKvmiG+goaaKRZA6LKUO35oK6EsM0/kjPK32Yw7URqrq3Q+Nvbbt8Usss+IekL7CRn83dYmw==" crossorigin="anonymous"></script>
4
</body>