MATSIM
StackedBarChart.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * BarChart.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.charts;
22 
23 import org.jfree.chart.ChartFactory;
24 import org.jfree.chart.JFreeChart;
25 import org.jfree.chart.axis.CategoryAxis;
26 import org.jfree.chart.plot.CategoryPlot;
27 import org.jfree.chart.plot.PlotOrientation;
28 import org.jfree.chart.renderer.category.BarRenderer;
29 import org.jfree.data.category.CategoryDataset;
30 import org.jfree.data.category.DefaultCategoryDataset;
31 
39 public class StackedBarChart extends ChartUtil {
40 
41  private final String[] categories;
42  private final DefaultCategoryDataset dataset;
43  private final CategoryPlot plot;
44 
53  public StackedBarChart(final String title, final String xAxisLabel, final String yAxisLabel) {
54  this(title, xAxisLabel, yAxisLabel, new String[]{});
55  }
56 
65  public StackedBarChart(final String title, final String xAxisLabel, final String yAxisLabel, final String[] categories) {
66  super(title, xAxisLabel, yAxisLabel);
67  this.dataset = new DefaultCategoryDataset();
68  this.chart = createChart(title, xAxisLabel, yAxisLabel, this.dataset);
69  this.plot = this.chart.getCategoryPlot();
70  this.categories = categories.clone();
71 
73 
74  // leave a gap of 20% between categories (groups of bars)
75  final CategoryAxis axis1 = this.plot.getDomainAxis();
76  axis1.setCategoryMargin(0.20);
77 
78  // leave a gap of 10% between individual bars within one category
79  BarRenderer renderer = (BarRenderer)this.plot.getRenderer();
80  renderer.setItemMargin(0.10);
81  }
82 
83  @Override
84  public JFreeChart getChart() {
85  return this.chart;
86  }
87 
88  private JFreeChart createChart(final String title, final String categoryAxisLabel,
89  final String valueAxisLabel, final CategoryDataset dataset) {
90  return ChartFactory.createStackedBarChart(title, categoryAxisLabel, valueAxisLabel,
91  dataset, PlotOrientation.VERTICAL, true, // legend?
92  false, // tooltips?
93  false // URLs?
94  );
95  }
96 
103  public void addSeries(final String title, final double[] values) {
104  int cnt = 1;
105  for (double value : values) {
106  String category = (cnt > this.categories.length ? Integer.toString(cnt) : this.categories[cnt-1]);
107  this.dataset.addValue(value, title, category);
108  cnt++;
109  }
110  }
111 
112 }
void addSeries(final String title, final double[] values)
StackedBarChart(final String title, final String xAxisLabel, final String yAxisLabel, final String[] categories)
JFreeChart createChart(final String title, final String categoryAxisLabel, final String valueAxisLabel, final CategoryDataset dataset)
StackedBarChart(final String title, final String xAxisLabel, final String yAxisLabel)