Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jqPlot show percent and value on pie chart

Tags:

jquery

jqplot

I'm using jqPlot with jqplot.PieRenderer to try and display a pie chart. On the label I'd like to show value (percent). The documentation says you can pass dataLabel an array of label types (source), however, putting %d%% (for percent) and %d (for value) in the dataLabelFormatString option end up showing nothing.

Any ideas here?

{ 
    seriesDefaults: {
        renderer: jQuery.jqplot.PieRenderer, 
        rendererOptions: {
            showDataLabels: true,
            dataLabels: ['value', 'percent'],
            dataLabelFormatString: "%d %d%%",
            sliceMargin: 4,
            fill: false
        }
    }, 
    legend: { show:true, location: 'e' }
}
like image 485
Snuffleupagus Avatar asked Jul 18 '12 16:07

Snuffleupagus


1 Answers

I read those docs a little differently. It's the options 'value', 'percent', 'label' OR an array of labels. Not an array of options. To do what you want you'll need to create your datalabels as a real array of labels.

For example:

data = [
    ['Heavy Industry', 12],['Retail', 9], ['Light Industry', 14], 
    ['Out of home', 16],['Commuting', 7], ['Orientation', 9]
];

var total = 0;
$(data).map(function(){total += this[1];})

myLabels = $.makeArray($(data).map(function(){return this[1] + " " + Math.round(this[1]/total * 100) + "%";}));

See example fiddle here.

like image 61
Mark Avatar answered Oct 14 '22 07:10

Mark