我必须从以下 json 字符串中绘制图表:
{
"d":{
"results":[
{
"_metadata":{
"uri": "http://www.something.com/hi",
"type" : "something.preview.hi"
}, "Period", "1988", "Age": 20, "Size" : 80
},
"_metadata":{
"uri": "http://www.something.com/hi",
"type" : "something.preview.hi"
}, "Period", "1989", "Age": 25, "Size" : 90
}
] }}
我使用 jquery.flot.js 库来绘制图表。这是绘制图表的示例,它工作正常:
var d1 = [];
for (var i = 0; i < 14; i += 0.5)
d1.push([i, Math.sin(i)]);
var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];
myChart.setDataArray(d1);
myChart.setDataArray(d2);
根据示例,我已将 json 转换为对象数组:
var results = $.parseJSON(responseText)["d"]["results"];
var sampleData = [];
for (var i = 0, len = results.length; i < len; i++) {
var result = results[i];
sampleData.push({ Perioden: result.Perioden,
Size: result.Size,
Age: result.Age});
}
var myData = [];
for (var i in sampleData) {
myData.push([sampleData[i].Period, sampleData[i].Age]);
}
var myData1 = [];
for (var i in sampleData) {
myData1.push([sampleData[i].Period, sampleData[i].Size]);
}
myChart.setDataArray(myData);
myChart.setDataArray(myData1);
但我得到数据图表的数据格式错误的错误。任何人都可以看到我的代码和示例之间有什么不同吗?