2013-03-03 64 views
0

我下面的代碼來呈現一個jqplot餅圖。 由於某些原因,餅圖未正確顯示百分比。jQPlot餅圖來自json

$array= array(array("Males", $males),array("Female", $females)); 
json_encode($array); 

JSON文件的輸出是:

[["Books","8"],["Female","0"]] 

但對於餅圖顯示器正在示出10%,而不是100,這是我餅圖渲染

$.getJSON('jqPlot.php', function (data) { 

     var plot1 = jQuery.jqplot('chart1', [data], { 
      seriesDefaults: { 
       // Make this a pie chart. 
       renderer: jQuery.jqplot.PieRenderer, 
       rendererOptions: { 
        // Put data labels on the pie slices. 
        // By default, labels show the percentage of the slice. 
        showDataLabels: true 
       }, 
      }, 
      legend: { show: true, location: 'e' }}); 
    }); 
}); 

但是,如果我將json文件更改爲[["Books","8"]],餅圖工作正常。

回答

0

試試這個,工作正常。

$.getJSON(url, function(data) { 
    var items1 = new Array(); 
    var j=0; 
    for (var i in data) { 
     var items = new Array(); 
     items.push(i,Number(data[i])); 
     items1[j++] = items; 
    } 

    var plot1 = jQuery.jqplot('chart1', eval([items1]), { 

       seriesDefaults:{ 
        renderer:jQuery.jqplot.PieRenderer, 
        rendererOptions:{ 
         dataLabels:'value', 
         showDataLabels:true 
        } 
       }, 
       legend:{ show:true, location:'e' } 
      } 
    ); 

});