我正在使用 JSON 和 Flot 创建饼图。创建饼图的 JS 函数从 Django 接收以下格式的 JSON 数组:
[1, 3, 2, 5, 4]
如果没有数据,则 JSON 数组为:
[0, 0, 0, 0, 0]
我正在尝试调整该函数,以便如果没有数据,则不会绘制饼图,而是会显示一些文本(例如“尚未显示任何内容”)。到目前为止我已经尝试过:
function loadWeekChart(theData) {
var blankData = [0, 0, 0, 0, 0];
if ($.data(theData) == $.data(blankData)){
$('#week-pie-chart').empty().append('Nothing to show yet');
} else {
$.plot($("#week-pie-chart"), theData ,
{
series: {
pie: {
show: true
}
}
});
}
}
JS 没有失败,但它既不打印饼图(没有数据),也不给我文本替换。
请有人告诉我哪里错了!
就我个人而言,我会执行以下操作,这是伪代码......
set boolean to true
for each element in the JSON
compare it with blank data element
if they are not equal boolean false
else continue
return boolean
然后你就会知道是否与该函数返回 true 相同,如果它们不是 false,则返回 false。
如果您需要帮助编码,请告诉我。不应该那么难吗
这也可能有帮助:类似问题
function checkJsons(otherJson,newJson)
{
var sameJson = true;
for (var key in otherJson) {
if(otherJson[key] != newJson[key]) {sameJson=false;} return sameJson;
}
}
这应该有帮助,但不是测试
一个更好的方法来做到这一点,但更难阅读是
function checkJsons(otherJson,newJson)
{
for (var key in otherJson) {if(otherJson[key] != newJson[key]) {return false;}}
return true;
}
function pieChartData(theData)
{
var blankData = [0, 0, 0, 0, 0];
if(checkJsons(blankData,theData)){$('#week-pie-chart').empty().append('Nothing to show yet');} else { // do your code here // }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)