Javascript examples for Chart.js:Pie Chart
Display pie chart data values of each slice in chart.js
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript" src="http://code.createjs.com/createjs-2013.09.25.combined.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script> <script type="text/javascript"> window.onload=function(){/* w w w .j a v a 2 s . com*/ var data = [ { value: 300, color:"#F7464A", highlight: "#FF5A5E", label: "Red" }, { value: 50, color: "#46BFBD", highlight: "#5AD3D1", label: "Green" }, { value: 100, color: "#FDB45C", highlight: "#FFC870", label: "Yellow" } ]; var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var midX = canvas.width/2; var midY = canvas.height/2 var myPieChart = new Chart(ctx).Pie(data, { showTooltips: false, onAnimationProgress: drawSegmentValues }); var radius = myPieChart.outerRadius; function drawSegmentValues() { for(var i=0; i<myPieChart.segments.length; i++) { ctx.fillStyle="white"; var textSize = canvas.width/10; ctx.font= textSize+"px Verdana"; // Get needed variables var value = myPieChart.segments[i].value; var startAngle = myPieChart.segments[i].startAngle; var endAngle = myPieChart.segments[i].endAngle; var middleAngle = startAngle + ((endAngle - startAngle)/2); var posX = (radius/2) * Math.cos(middleAngle) + midX; var posY = (radius/2) * Math.sin(middleAngle) + midY; var w_offset = ctx.measureText(value).width/2; var h_offset = textSize/4; ctx.fillText(value, posX - w_offset, posY + h_offset); } } } </script> </head> <body> <canvas id="canvas" width="300" height="300"></canvas> </body> </html>