2017-04-13 62 views
0

我一直在使用Primefaces圖表取得了一些成功,但我想了解更多關於jqPlot的知識,這樣我就可以做更靈活的定製。一個例子是我想創建一個組合圖(條形圖和線組合)。jqPlot如何從java arraylist和JSF beans製作圖表

我想從XHTML頁面燒成一個jQuery函數來創建的圖表:

<button type="button" class="button" onclick="comboChart()">Combo Chart</button> 

功能是:

  function comboChart() { 

      var line1 = [#{chartDataBean.comboChartMapList}]; 
      var line2 = [#{chartDataBean.linePartChartMap}]; 

      var plot2 = $.jqplot('testchart', [line1, line2], { 
         series: [{renderer: $.jqplot.BarRenderer}, {xaxis: 'x2axis', yaxis: 'y2axis'}], 
      axesDefaults: { 
      tickRenderer: $.jqplot.CanvasAxisTickRenderer, 
      tickOptions: { 
           angle: 30 
          } 
         }, 
         axes: { 
          xaxis: { 
           renderer: $.jqplot.CategoryAxisRenderer 
          }, 
          x2axis: { 
           renderer: $.jqplot.CategoryAxisRenderer 
          }, 
          yaxis: { 
           autoscale: true 
          }, 
          y2axis: { 
           autoscale: true 
          } 
         } 
        }); 
       }; 

的JSF bean包含地圖可以看到每個數組列表地圖將會是一個系列:

public List<HashMap<Object, Double>> getComboChartMapList() { 
    List<List<Endatapoint>> modbinholder = new ArrayList<List<Endatapoint>> 
(); 
    modbinholder = anBean.getModelBinList(); 
    comboChartMap = new HashMap<>(); 
    comboChartMapList = new ArrayList<>(); 

    for (List<Endatapoint> cList : modbinholder) { 
     List<Endatapoint> pholder = new ArrayList<>(); 
     pholder = cList; 
     for (Endatapoint p : pholder) { 

      Date lDate = p.getPointdate(); 
      String pnits = p.getRecords().getSnipunit(); 

      Double aNum = p.getActualnum(); 
      comboChartMap.put(pnits, aNum); 
      comboChartMapList.add(comboChartMap); 
     } 
    } 
    return comboChartMapList; 
} 

以上只是產生一個沒有數據的空白圖表。綜觀JS控制檯在Chrome和FF(螢火蟲)我注意到在列表值都是這樣產生的語法錯誤:

var line1 = [[{Million Dollars=54.7}, {Million Dollars=54.7}]]; 

但jqplot文件需要將這樣生成的值:

var line1 = [['Million Dollars', 54], ['Million Dollars', 54.7]]; 

我的問題是:

  1. 什麼我在上面的代碼做錯了什麼?
  2. 我可以使用上面的代碼傳遞正確的值,還是需要使用任何其他方法,如JSON?

我在這裏閱讀的大部分示例和問題似乎都涉及到將動態值傳遞給jplot的JSON。如果可能,我想堅持使用Java對象。

+1

Json的轉換,這就是幕後PrimeFaces爲你做 – Kukeltje

回答

0

JSON是必需的,PrimeFaces,或註釋的REST接口,這是否對你來說,只是創造一個getter,做需要