score:32

Accepted answer

you'll have to add code in 2 places. as an example, take the doughnut. first add label info to the defaults (look at the original chart.js code and compare with this):

    chart.doughnut.defaults = {
        segmentshowstroke : true,
        segmentstrokecolor : "#fff",
        segmentstrokewidth : 2,
        percentageinnercutout : 50,
        animation : true,
        animationsteps : 100,
        animationeasing : "easeoutbounce",
        animaterotate : true,
        animatescale : false,
        onanimationcomplete : null,
        labelfontfamily : "arial",
        labelfontstyle : "normal",
        labelfontsize : 24,
        labelfontcolor : "#666"
    };

then go down to where the doughnut is drawn and add the four ctx lines.

    animationloop(config,null,drawpiesegments,ctx);

    function drawpiesegments (animationdecimal){
        ctx.font = config.labelfontstyle + " " + config.labelfontsize+"px " + config.labelfontfamily;
        ctx.fillstyle = 'black';
        ctx.textbaseline = 'middle';
        ctx.filltext(data[0].value + "%", width/2 - 20, width/2, 200);

the ctx.filltext call will put the text onto the canvas, so you can use that to write text with x,y coordinates. you ought to be able to use this way to do basic labels. here is the jsfiddle to tinker with:

http://jsfiddle.net/ncfgl/ (look at lines 281 and 772 in the javascript section of the jsfiddle for aforementioned code)

if you need something fancier, someone forked a version of charts.js and added tooltips. here is the discussion https://github.com/nnnick/chart.js/pull/35, and you'll be able to find the link to the forked version inside that discussion.

score:3

i have figured out a way so that we can display the values for each region out side the graph.

also i removed the rotation of the values and i referred to here

add the following lines of code inside the doughnut function. ( i have pasted the modified lines from the chart.js file).

    var doughnut = function(data,config,ctx){

    var segmenttotal = 0;

    //in case we have a canvas that is not a square. minus 10 pixels as padding round the edge.
    var doughnutradius = min([height/2,width/2]) - 15;
    var cutoutradius = doughnutradius * (config.percentageinnercutout/100);
    //modified for setting the label values out side the arc
    var outradius= doughnutradius + cutoutradius/3;
    var outradiustop= doughnutradius + cutoutradius/5;
    ......
    ......
    ......

    function drawpiesegments (animationdecimal){
    :
    :



       if (config.scaleshowvalues) {
                ctx.save();                
                ctx.translate(width / 2, height / 2);
                ctx.font = config.scalefontstyle + ' ' + config.scalefontsize + 'px ' + config.scalefontfamily;
                ctx.textbaselne = 'middle';
                var a = (cumulativeangle + cumulativeangle + segmentangle) / 2,
                    w = ctx.measuretext(data[i].value).width,
                    b = math.pi / 2 < a && a < math.pi * 3 / 2;
                var c  = 0 < a && a < math.pi;
                if(b){
                    ctx.textalign = 'right';
                }
                else{
                    ctx.textalign = 'left';
                }
                if(c){
                    ctx.translate(math.cos(a) * outradius +1 , math.sin(a) * outradius+1);

                }
                else{
                    ctx.translate(math.cos(a) * outradiustop, math.sin(a) * outradiustop);      
                }

                ctx.fillstyle = config.scalefontcolor;
                //if the segment angle less than 0.2, then the lables will overlap, so hiding it.
                if(segmentangle > 0.2){
                    ctx.filltext(data[i].value,0,0);

                }
                ctx.restore();
     }

     ......
     ...... 

now the values will be displayed out side each sections and it will not be rotated.

score:3

there is an forked version, chartnew, that provides this functionality out of the box.

if you need to use chartjs then you can use this revised version of @jack's solution:

chart.types.doughnut.extend({
    name: "doughnutalt",
    draw: function() {
        chart.types.doughnut.prototype.draw.apply(this, arguments);
        this.chart.ctx.fillstyle = 'black';
        this.chart.ctx.textbaseline = 'middle';
        this.chart.ctx.filltext(this.segments[0].value + "%", this.chart.width / 2 - 20, this.chart.width / 2, 200);
    }
});

score:7

this one literally took hours and hours and i found a working solution.

https://github.com/nnnick/chart.js/pull/116

this was my final code. i was trying to display percentages as labels on doughnut

chart.types.doughnut.extend({
name: "doughnutalt",
draw: function() {
    chart.types.doughnut.prototype.draw.apply(this, arguments);
    this.chart.ctx.fillstyle = 'black';
    this.chart.ctx.textbaseline = 'middle';
    this.chart.ctx.textalign = 'start';
    this.chart.ctx.font="18px verdana";

    var total = 0;
    for (var i = 0; i < this.segments.length; i++) { 
        total += this.segments[i].value;      
    }

    this.chart.ctx.filltext(total , this.chart.width / 2 - 20, this.chart.height / 2, 100);
    for(var i = 0; i < this.segments.length; i++){
        var percentage = ((this.segments[i].value / total) * 100).tofixed(1);
        if( percentage > 3 ){
            var centreangle = this.segments[i].startangle + ((this.segments[i].endangle - this.segments[i].startangle) / 2),
                rangefromcentre = (this.segments[i].outerradius - this.segments[i].innerradius) / 2 + this.segments[i].innerradius;
            var x = this.segments[i].x + (math.cos(centreangle) * rangefromcentre);
            var y = this.segments[i].y + (math.sin(centreangle) * rangefromcentre);
            this.chart.ctx.textalign = 'center';
            this.chart.ctx.textbaseline = 'middle';
            this.chart.ctx.fillstyle = '#fff';
            this.chart.ctx.font = 'normal 10px helvetica';
            this.chart.ctx.filltext(percentage , x, y);
        }
     }
}
});

Related Query

More Query from same tag