0

我正在使用 JQPlot 从 MySQL 表中通过 JSON 提供一个饼图,并试图查看是否有任何大师可以为我回答问题。我的饼图按预期工作,因为它需要一个数组,将其除以总量的百分比,然后显示图表。从这里,是否可以输入另一个值成为饼图的总值,从中减去数组的总值,并显示数组项相对于整个饼图的百分比?

例如,如果我给 JQPlot 这个数组 [[a,2],[b,4],[c,2]],它将输出一个显示 a=25%, b=50%, c=25 的图表占总馅饼的百分比。我想给它另一个值 10 来表示整个饼图,从而强制该数组输出 a=20%、b=40%、c=20% 并剩余 20%。

我开始使用的代码是来自 JQPlot 站点的基本饼图示例,我希望的总饼图值设置为从 getGrossTotal.php 输入。如果做不到,那就做不到。我只是想看看如何处理它的想法。

$(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'
    },
});

});
});

谢谢!

4

2 回答 2

0

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.

于 2012-11-13T14:42:24.583 回答
0

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!

于 2013-12-27T10:49:13.670 回答