1

我遇到了条形图组件的一些奇怪行为。我正在尝试创建两个 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

但我的图表看起来像这样: 在此处输入图像描述

为什么将 october 和 september beeing 的值放在前 2 列中以及如何解决此问题?

PS。如果我更改 for 循环的顺序,则只有 10 月和 9 月的统计数据会出现在图表上,它会跳过其他月份。

4

1 回答 1

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);
于 2014-01-15T13:28:30.517 回答