2012-02-16 105 views
1

我想創建Jqplot條形圖和在像創建多維數組遇到困難多維數組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> 
}); 

似乎格式是正確的,但我不明白的地方我錯了或這是正確的方式來創建數據。如果任何人都可以幫助我解決這個問題,那就太好了。

編輯: 我如何檢查我的數組是否以上述需要的格式創建?

回答

2

要查看你的數據是什麼樣的,我會使用Firebug(或者你最喜歡的瀏覽器的等價物)爲jqplot調用設置一個斷點。然後檢查你想檢查的變量。

另外它似乎jqplot應該使用twoDOuterArray但你通過它data這可能是一個問題。

還有一件事:如果您的原始數據對象包含多個元素,則外部<c:forEach>將導致jqplot覆蓋之前生成的新圖。

而不是混合JSTL與Javascript我還建議建立你的2D數組,無論你的其他變量設置(例如:控制器)。然後將該對象轉換爲帶有類似Gson的庫的JSON,並直接將其傳遞給jqplot。