STORM
STORM

Reputation: 4341

Show values in Chart.js Pie chart parts

I want to use chart.js (version 2.7.0) pie chart for visualizing data on a web page. I need to use a pie chart which in general works. As seen on the sample page at http://www.chartjs.org/samples/latest/charts/pie.html there is no data visible on the pie chart parts, only on mouse over. I would like to have it displayed directly within the parts. I havent found anything on this in the documentation. Is this generally possible and if yes, how can i achieve this?

Upvotes: 1

Views: 9417

Answers (1)

ɢʀᴜɴᴛ
ɢʀᴜɴᴛ

Reputation: 32879

Yes, this is possible. You can achieve that using a Chart.js plugin called - Chart.PieceLabel.js.

Here is a basic example :

var chart = new Chart(ctx, {
   type: 'pie',
   data: {
      labels: ['Jan', 'Feb', 'Mar'],
      datasets: [{
         data: [30, 40, 25],
         backgroundColor: 'rgba(0,119, 220, 0.2)'
      }]
   },
   options: {
      pieceLabel: {
         render: 'value' //show values
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>
<script src="https://cdn.rawgit.com/emn178/Chart.PieceLabel.js/master/build/Chart.PieceLabel.min.js"></script>

<canvas id="ctx"></canvas>

To learn more about this plugin and it­'s use-cases, refer here.

Upvotes: 2

Related Questions