Reputation: 997
I have some datas for doughnut chart but at the beginning I want to show default doughnut chart and datas with 0 and equal size of parts.
I put beginAtZero
JS
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
}
}]
}
but value 0 of datas, chart is hidden I cant see any chart.But I need default equal size part of chart values with 0. is it possible with doughnut chart?
Example:(but I want to make value 1 to 0,with 5 equal part)
Thank you
Upvotes: 0
Views: 143
Reputation: 32879
Yes! It is possible.
To accomplish this, what you have to do is, generate the doughnut chart with...
[1, 1, 1, 1, 1]
as the data
array, at the beginning.
Then, use the following tooltips callback function, to show the value 0 on tooltip.
options: {
tooltips: {
callbacks: {
label: function(t, d) {
var isSame = d.datasets[t.datasetIndex].data.every(function(e) {
return e === 1;
});
if (isSame) return d.labels[t.index] + ': ' + 0;
else return d.labels[t.index] + ': ' + d.datasets[t.datasetIndex].data[t.index];
}
}
},
...
}
this will only show 0 on tooltip, if all the values of data
array is 1
ᴅᴇᴍᴏ ⧩
var chart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
data: [1, 1, 1, 1, 1],
backgroundColor: [
'rgba(0, 119, 204, 0.8)',
'rgba(0, 119, 204, 0.7)',
'rgba(0, 119, 204, 0.6)',
'rgba(0, 119, 204, 0.4)',
'rgba(0, 119, 204, 0.3)'
],
}]
},
options: {
responsive: false,
legend: false,
tooltips: {
callbacks: {
label: function(t, d) {
var isSame = d.datasets[t.datasetIndex].data.every(function(e) {
return e === 1;
});
if (isSame) return d.labels[t.index] + ': ' + 0;
else return d.labels[t.index] + ': ' + d.datasets[t.datasetIndex].data[t.index];
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx" height="200"></canvas>
ꜰʏɪ : you cannot create chart with all data as 0, to make 5 equal parts.
Upvotes: 1