我正在尝试创建 Jqplot 条形图,但在创建多维数组时遇到困难
var plot2 = $.jqplot('chart2', [
[[2,1], [4,2], [6,3], [3,4]],
[[5,1], [1,2], [3,3], [4,4]],
[[4,1], [7,2], [1,3], [2,4]]],
我的数据在 hashMap 里面的 HashMap 里面,就像是
{software={low=1,high=5, medium=4}, harware={low=3,high=3},network{low=3,high=3,medium=8}}
我如何解释上述多维数组中的数据。
我写了下面的代码来实现这个
$(document).ready(function(){
<c:forEach var="data" varStatus="main" items="${data}">
var cdata = new Array();
var twoDOuterArray = new Array(${data.size()});
<c:forEach var="mapEntry" varStatus="status" items="${data['cData']}">
twoDOuterArray[${status.index}] = new Array(3);
</c:forEach>
<c:forEach var="mapEntry" varStatus="status" items="${data['cData']}">
<c:forEach var="dataValue" varStatus="status2" items="${mapEntry.value}">
var valueArray = new Array();
alueArray.push(${dataValue.value});
valueArray.push("${mapEntry.key}");
twoDOuterArray[${status2.index}][${status.index}]=valueArray;
</c:forEach>
//alert(twoDOuterArray[${status.index}].length);
</c:forEach>
data.push(twoDOuterArray);
alert(twoDOuterArray);
plot2b = $.jqplot('chart2', data, {
seriesDefaults: {
renderer:$.jqplot.BarRenderer,
pointLabels: { show: true, location: 'e', edgeTolerance: -15 },
shadowAngle: 50,
rendererOptions: {
barDirection: 'horizontal'
}
},
title:{
text: "${title}",
show: true,
textColor:"#fff",
},
axes: {
yaxis: {
renderer: $.jqplot.CategoryAxisRenderer
}
},
legend: { show:true, location: 'e' }
});
);
</c:forEach>
});
看起来格式是正确的,但我不明白我哪里错了,或者这是创建数据的正确方法。如果有人可以帮助我解决这个问题,那就太好了。
编辑: 如何检查我的数组是否以上述要求的格式创建?
最佳答案
要查看您的数据是什么样子,我会使用 Firebug(或您最喜欢的浏览器的等效项)在对 jqplot 的调用上设置一个断点。然后检查要检查的变量。
而且 jqplot 似乎应该使用 twoDOuterArray
但你通过了它data
这可能是个问题。
还有一件事:如果您的原始数据对象包含多个元素,则外部 <c:forEach>
您将导致 jqplot 将新图覆盖在先前生成的图上。
与其将 JSTL 与 Javascript 混合,我还建议从设置其他变量的任何地方(例如: Controller )构建二维数组。然后使用类似 Gson 的库将对象转换为 JSON并将其直接传递给 jqplot。
关于javascript - jqplot 图表的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9319919/