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

More Query from same tag