1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| var pieData = [ { value: 300, color: '#57c7d4', label: '1月' }, { value: 150, color: '#3aa99e', label: '2月' }, { value: 100, color: '#f2a654', label: '3月' }, { value: 140, color: '#f96868', label: '4月' }, { value: 120, color: '#926dde', label: '5月' } ]; var configs = { showTooltips:false, scaleFontColor: '#000', segmentShowStroke : true, segmentStrokeColor : "#fff", segmentStrokeWidth : 2, animateRotate : true, animateScale : false, legendTemplate : '<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li> <%if(segments[i].label){%><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%=segments[i].label%><%}%></li><%}%></ul>', onAnimationComplete: function () { var ctx = this.chart.ctx; var segs = this.segments; ctx.textAlign = 'start'; ctx.textBaseline = 'middle'; var total = 0; for (var i = 0; i < segs.length; i++) { total += parseFloat(segs[i].value); } ctx.fillText(total , ctx.width / 2 - 20, ctx.height / 2, 100); for(var i = 0; i < segs.length; i++){ var centreAngle = segs[i].startAngle + ((segs[i].endAngle - segs[i].startAngle) / 2), rangeFromCentre = (segs[i].outerRadius - segs[i].innerRadius) / 1.5 + segs[i].innerRadius; var x = segs[i].x + (Math.cos(centreAngle) * rangeFromCentre); var y = segs[i].y + (Math.sin(centreAngle) * rangeFromCentre); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillStyle = '#000'; ctx.font = 'normal 12px Helvetica'; ctx.fillText(segs[i].value, x, y); } } };
var ctx = document.getElementById('pie').getContext('2d'); var pie = new Chart(ctx).Pie(pieData, configs); var legend = document.getElementById('legend');
legend.innerHTML = pie.generateLegend();
|