Alexey Pavlyuk
Alexey Pavlyuk

Reputation: 35

Chart.js remove zero value sector in pie chart

Chart.js Please help: how remove zero value sector in pie chart. In data: [12,0,0]; version: 2.9.4 img

Upvotes: 2

Views: 1228

Answers (1)

LeeLenalee
LeeLenalee

Reputation: 31371

Yes this is possible by using the scriptable option for borderwidth like this:

var options = {
  type: 'pie',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange", "f", "d"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3, 0, 0],
      borderWidth: (a, b, c) => (a.dataset.data[a.dataIndex] === 0 ? 0 : 1),
      backgroundColor: ["red", "blue", "green", "yellow", "pink", "purple", "white", "black"],
      borderColor: ["red", "blue", "green", "yellow", "pink", "purple", "white", "black"]
    }]
  },
  options: {}
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <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>
</body>

if you change borderWidth: (a,b,c) => (a.dataset.data[a.dataIndex] === 0 ? 0 : 1), to borderWidth: (a,b,c) => (a.dataset.data[a.dataIndex] === 0 ? 10 : 1), you can see the old behaviour you have now.

Upvotes: 1

Related Questions