score:10

Accepted answer

You were close! In cases like this, I'd recommend doing something like the following:

.label(function(d) {
    console.log(JSON.stringify(d));
})

to get a feel for the structure of your data. If you do so, you'll see that key and value are under d.data, so you could have a label like

.label(function(d) {
    return d.data.key + ' ' + d.data.value + '%';
})

If you just want to calculate the fraction of the circle being displayed, you can use the startAngle and endAngle properties.

.label(function(d) {
    return d.data.key + ' ' + Math.round((d.endAngle - d.startAngle) / Math.PI * 50) + '%';
});

(d.endAngle - d.startAngle) will give you the number of radians the slice is displaying, so you can calculate a percentage from there by dividing by the number of radians in a circle.

score:0

Indeed, it does not have d.data. Instead I use the .renderlet() : :

.renderlet(function(chart){
        chart.selectAll('text.pie-slice').text( function(d) {
        return d.data.key + ' ' + dc.utils.printSingleValue((d.endAngle - d.startAngle) / (2*Math.PI) * 100) + '%';
        })
    })

score:1

.label is the right way to modify BUT pie chart has bug, Check by log on d

.label(d => {
    console.log(d);
    // {
    //  key: 'which you return in dimension',
    //  value: 'ex: 259.91'
    // }
})

Ex, have 6 objs in array, BUT log may just 3 or 4 of them @@ To work around this bug

.on('pretransition', function(chart){
    chart.selectAll('text.pie-slice').text(d => {
        console.log(d);
        // {
        //  data: {
        //      key: 'which you return in dimension',
        //      value: 'ex: 259.91'
        //  },
        //  endAngle: 'xxx',
        //  startAngle: 'yyy'
        // }
    })
})

d obj inside text()

By this way we can log enough 6 objs in array The label text is return val inside text(d => {})

chart.selectAll('text.pie-slice').text(function(d){
    let label = d.data.key;
    return label + ': ' + Number(d.data.value).toFixed(2);
})

//Or play with percentage
chart.selectAll('text.pie-slice').text(function(d){
    return d.data.key + ' ' + dc.utils.printSingleValue((d.endAngle - d.startAngle) / (2*Math.PI) * 100) + '%';
})

Related Query