1👍
✅
After creating the chart, you can retrieve its metadata through .getDatasetMeta(index)
, change the hidden
attribute of the desired slice and update the chart as follows.
const chart = new Chart(document.getElementById('myChart'), {
type: 'pie',
data: {
labels: ['Red', 'Orange', 'Yellow', 'Green', 'Blue'],
datasets: [{
data: [100, 100, 200, 300, 140],
backgroundColor: ['rgb(255, 99, 132)', 'rgb(255, 159, 64)', 'rgb(255, 205, 86)', 'rgb(75, 192, 192)', 'rgb(54, 162, 235)']
}]
},
options: {
responsive: true,
maintainAspectRatio: true,
title: {
display: true,
text: 'Colors - Sample Pie Chart'
}
}
});
chart.getDatasetMeta(0).data[4].hidden = true;
chart.update();
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="150"> </canvas>
0👍
In V3 of chartjs you can use the API they provide to toggle the visibility of an item.
chart.toggleDataVisibility(2); // toggles the item in all datasets, at index 2
chart.update(); // chart now renders with item hidden
Note: Only doughnut / pie, polar area, and bar use this.
Same answer is posted in this thread: How to hide section in a Chart.js Pie Chart
Source:stackexchange.com