2013-10-31 44 views
1

我正在經歷一些條形圖組件的怪異行爲。 我想創建兩個ChartSeries並將它們添加到CartesianChartModel。圖表系列由月份和每月的數字組成。但由於某些原因,第二個循環的值被置於錯誤的位置。primefaces圖表插入數據到錯誤的地方

 CartesianChartModel categoryModel = new CartesianChartModel(); 
     ChartSeries problems = new ChartSeries(); 
     ChartSeries incidents = new ChartSeries(); 

     problems.setLabel("PM"); 
     incidents.setLabel("IM"); 
     List<MonthCountWrapper> monthCountList = //get data 
     List<MonthCountWrapper> monthCountListInc = //get data 

     for (MonthCountWrapper mcw : monthCountListInc) { 
       System.out.println("Month : " + mcw.getMonth()); 
       incidents.set(mcw.getMonth(), mcw.getCount()); 
     } 
       categoryModel.addSeries(incidents); 

     for (MonthCountWrapper mcw : monthCountList) { 
       System.out.println("Month : " + mcw.getMonth()); 
       problems.set(mcw.getMonth(), mcw.getCount()); 
     } 
       categoryModel.addSeries(problems); 

     view.setCategoryModel(categoryModel); 

XHTML:

<p:barChart id="basic" value="#{statisticLateView.categoryModel}" 
      legendPosition="ne" diameter="600" 
      rendered="#{statisticLateView.categoryModel.series.size() != 0 and statisticLateView.showBarChart}" 
      min="0" max="150" showDataTip="true" /> 

第一個系統輸出產生這樣的:

13:20:08,345 INFO [STDOUT] Month : Januar 
13:20:08,345 INFO [STDOUT] Month : Februar 
13:20:08,345 INFO [STDOUT] Month : Marts 
13:20:08,345 INFO [STDOUT] Month : April 
13:20:08,345 INFO [STDOUT] Month : Maj 
13:20:08,345 INFO [STDOUT] Month : Juni 
13:20:08,345 INFO [STDOUT] Month : Juli 
13:20:08,345 INFO [STDOUT] Month : August 
13:20:08,345 INFO [STDOUT] Month : September 
13:20:08,345 INFO [STDOUT] Month : Oktober 

而第二本

13:20:08,345 INFO [STDOUT] Month : September 
13:20:08,345 INFO [STDOUT] Month : Oktober 

,但我的圖表看起來像這樣: enter image description here

爲什麼十月份和九月份的數值都放在前兩列以及如何解決這個問題?

PS。如果我更改for循環的順序,那麼只有10月和Septemeber統計信息會出現在圖表上,並且會跳過其他幾個月。

回答

0

我已經編寫一個醜陋的,但工作的解決方法:

public class ChartSeriesTable { 

    private final Set<String> cols = new TreeSet<String>(); 

    private final Set<String> rows = new HashSet<String>(); 

    private final Map<String, Number> data = new HashMap<String, Number>(); 

    public void set(String col, String row, Number value) { 
    cols.add(col); 
    rows.add(row); 
    data.put(row + "." + col, value); 
    } 

    public void setup(CartesianChartModel model) { 
    for (String row : rows) { 
     ChartSeries serie = new ChartSeries(); 
     serie.setLabel(row); 
     for (String col : cols) { 
     Number d = data.get(row + "." + col); 
     if (d == null) { 
      serie.set(col, 0); 
     } 
     else { 
      serie.set(col, d); 
     } 
     } 
     model.addSeries(serie); 
    } 
    } 

} 

所以你使用它作爲:

ChartSeriesTable table = new ChartSeriesTable(); 
table.add("2001", "Girls", 30); 
table.add("2001", "Boys", 20); 
table.add("2002", "Boys", 15); 
table.add("2003", "Boys", 10); 
table.add("2004", "Girls", 40); 

CartesianChartModel model = new CartesianChartModel(); 
table.setup(model);