primefaces图表将数据插入错误的地方

时间:2013-10-31 12:36:04

标签: primefaces

我遇到了条形图组件的一些奇怪行为。 我正在尝试创建两个chartSeries并将它们添加到CartesianChartModel。图表系列包括月份和每月的数字。但由于某种原因,第二个循环的值被置于错误的位置。

        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" />

第一个sysout生成:

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

为什么10月和9月的值列在前2列以及如何解决这个问题?

PS。如果我改变for循环的顺序,只有10月和Septemeber统计数据会出现在图表上,并且它将跳过其他几个月。

1 个答案:

答案 0 :(得分: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);
相关问题