我正在尝试检索flot饼图的数据,并希望确保我正确地执行此操作。对于任何反馈,我们都表示感谢。我还将发布我的JSON数据,以验证它是否具有正确的语法。
<script src="js/flot/jquery-1.11.2.min.js"></script>
<script src="js/flot/jquery.flot.js"></script>
<script src="js/flot/jquery.flot.pie.js"></script>
<style type="text/css">
#placeholder { width: 450px; height: 450px; }
</style>
<script type="text/javascript">
$.ajax({
url:'../cgi-bin/bpo_piechart.cgi',
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: 'json',
success: function (data) {
//alert("should be shown");
$.plot($("#placeholder"), data, {
series: {
pie: {
show: true
}
},
legend: {
labelBoxBorderColor: "none"
}
});
},
failure: function (response) {
alert(response.d);
}
});
</script>
我的JSON数据被淘汰出现如下:有人能告诉我这是否有问题?我认为这是正确的,但我可能不得不在稍后解析jQuery javascript块中的引号。
[
{
"acctlocv":"CHE3",
"percval":"3774"
},
{
"acctlocv":"CMI- CRL2",
"percval":"5"
},
{
"acctlocv":"CMI1",
"percval":"4106"
},
{
"acctlocv":"CMI2",
"percval":"10259"
},
{
"acctlocv":"CUST",
"percval":"75"
},
{
"acctlocv":"HELOCR",
"percval":"6"
},
{
"acctlocv":"Network",
"percval":"8675"
}
]
答案 0 :(得分:1)
是的,这是你的JSON。你需要&#34;标签&#34;和&#34;数据&#34;而不是&#34; acctlocv&#34;和&#34; percval&#34;。
[
{
"label":"CHE3",
"data":3774
},
{
"label":"CMI- CRL2",
"data":5
},
{
"label":"CMI1",
"data":4106
},
{
"label":"CMI2",
"data":10259
},
{
"label":"CUST",
"data":75
},
{
"label":"HELOCR",
"data":6
},
{
"label":"Network",
"data":8675
}
]
http://jsfiddle.net/xyktLseo/1/
编辑 - Raidri的评论是正确的,&#39;数据&#39;可以是字符串和flot将转换。 http://jsfiddle.net/xyktLseo/2/