0
votes

I'm putting together a pie chart using JQPlot being fed via JSON from a MySQL table and am trying to see if there are any gurus out there who might be able to answer a question for me. I have the pie chart working as it's supposed to in that it takes an array, divided it up by percentage of the total amount, and displays the chart. From here, is it possible to feed in another value to become the pie's total value, subtract the total value of the array from that, and show the array items' percentages in relation to the entire pie?

For example, if I give JQPlot this array, [[a,2],[b,4],[c,2]], it would output a chart that shows a=25%, b=50%, c=25% of the total pie. I want to give it another value of 10 to represent the entire pie thus forcing that array to output a=20%, b=40%, c=20% with an extra 20% left over.

The code I'm starting with is the basic pie chart example from the JQPlot site, and my desired total pie value is set up to be fed in from getGrossTotal.php. If it can't be done, it can't be done. I just want to check around for ideas as to how to handle it.

$(document).ready(function(){
$.getJSON('getGrossTop.php', function(grossTop){

var plot1 = jQuery.jqplot ('pieChart', [grossTop],{ 
    seriesDefaults: {
        renderer: jQuery.jqplot.PieRenderer, 
        rendererOptions: {
            showDataLabels: true
        }
    }, 
    legend: {
        show:true,
        location: 'e'
    },
});

});
});

Thanks!

2

2 Answers

0
votes

Nevermind. Instead of trying to handle the extra number in javascript, I went back to the PHP side, totaled the values of each minor array, subtracted that result from the sum of everything in the database, and pushed that result into the major array to encode. It basically made an additional minor array item with a label of nothing and value of the number I wanted. When I passed all that into JQPlot, it gave me what I was looking for. Thanks for all who took a look.

0
votes

If you want to set in at jqPlot level (but i think that this work is cleaner if you work with PHP) I think that it is better to work at the PHP level, however if you need to work on the input data array, you can do it in two ways;

1) Before the $.jqplot() call

you want to add a further element to the array containing the 10% of the sum of all values. So

$.getJSON('getGrossTop.php', function(grossTop){

grossTop.push(['d',8.8]); //here you can insert what and many elements you want

var plot1 = jQuery.jqplot ('pieChart', [grossTop],{ 

2) After the $.jqplot() call

You need to add the fourth element directly in the jqPlot object

plot1.data[0].push(['d',8.8]);

but remember that you need to replot again the chart!