2012-07-24 60 views
0

我一直在谷歌搜索一段时间试图找到我的问题的解决方案,但我发现接近的一切都太复杂了!如何将简单的JSON数组传递给jQuery然后Flot?

我有一个工作Flot饼图。它接收以这种格式从Django的JSON数组:

[0, 3, 5, 6, 7]

阵列表示每个系列的实例的数目(即,系列1的0的情况下,系列2的3个实例等等)。

即阵列然后在使用该代码的简单FLOT饼图显示(theData高于阵列):

function loadTotalChart(theData) { 
    $.plot($("#total-pie-chart"), theData , 
    { 
    series: { 
     pie: { 
      show: true 
    } 
     }, 
label: { 
    show:true 
}, 
legend: { 
    show: false 
    }, 
grid: { 
    hoverable: true, 
    } 
    }); 
    } 

的问题是,所述标签是未定义的阵列中,所以我的饼图显示“未定义:30%”或每个系列相似。

我想要做的是为每个系列添加标签:标签对图表的每个实例都是相同的。

如何取JSON数组,添加标签然后将其传递给Flot?

非常感谢!

编辑:

感谢Liam McCann,我现在有这样的代码来绘制图表只有在有数据:

function checkJsons(otherJson,newJson) 
{ 
for (var key in otherJson) {if(otherJson[key] != newJson[key]) {return false;}} 
return true; 
} 

function loadWeekChart(theData) 
{ 
var blankData = [0, 0, 0, 0, 0]; 
if(checkJsons(blankData,theData)){$('#week-pie-chart').empty().append('Nothing to show yet');} 

else { $.plot($("#week-pie-chart"), theData , 
    { 
     series: { 
      pie: { 
       show: true 
      } 
     } 
    }); } 
} 

回答

2

原来我已经回答了我的问题。这是我现在正在使用采取一种未标记的JSON数组的代码(例如,[1, 2, 4, 1, 5]),确保数组包含数据(即不[0, 0, 0, 0, 0]),添加标签,然后显示在图表上:

function loadTotalChart(theData) { 
    var blankData = [0, 0, 0, 0, 0]; 
    if(checkJsons(blankData,theData)){ 
    $('#total-pie-chart').empty().append('Nothing to show yet'); 
} else { 
    var totalPieData = [ 
    {label: "1/5", data:theData[0]}, 
    {label: "2/5", data:theData[1]}, 
    {label: "3/5", data:theData[2]}, 
    {label: "4/5", data:theData[3]}, 
    {label: "5/5", data:theData[4]}, 
    ]; 
    $.plot($("#total-pie-chart"), totalPieData , 
    { 
     series: { 
      pie: { 
       show: true 
      } 
     }, 
     label: { 
       show:true 
     }, 
     legend: { 
       show: false 
     }, 
     grid: { 
       hoverable: true, 
     } 
    }); 
    } 
} 

如果有人可以看到代码的任何问题,或更好的方式,请让我知道!

编辑:回想起这个问题,我意识到我没有包含在这个函数中引用的checkJsons函数。这里是:

function checkJsons(otherJson,newJson){ 
    for (var key in otherJson) {if(otherJson[key] != newJson[key]) {return false;}} 
    return true; 
} 
相关问题