MATSIM
LineChart.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.plot.PlotOrientation;
26 import org.jfree.data.category.CategoryDataset;
27 import org.jfree.data.category.DefaultCategoryDataset;
28 
35 public class LineChart extends ChartUtil {
36 
37  private final String[] categories;
38  private final DefaultCategoryDataset dataset;
39 
48  public LineChart(final String title, final String xAxisLabel, final String yAxisLabel) {
49  this(title, xAxisLabel, yAxisLabel, new String[]{});
50  }
51 
60  public LineChart(final String title, final String xAxisLabel, final String yAxisLabel, final String[] categories) {
61  super(title, xAxisLabel, yAxisLabel);
62  this.dataset = new DefaultCategoryDataset();
63  this.chart = createChart(title, xAxisLabel, yAxisLabel, this.dataset);
64  this.categories = categories.clone();
66  }
67 
68  @Override
69  public JFreeChart getChart() {
70  return this.chart;
71  }
72 
73  private JFreeChart createChart(final String title, final String categoryAxisLabel,
74  final String valueAxisLabel, final CategoryDataset dataset) {
75  return ChartFactory.createLineChart(title, categoryAxisLabel, valueAxisLabel,
76  dataset, PlotOrientation.VERTICAL, true, // legend?
77  false, // tooltips?
78  false // URLs?
79  );
80  }
81 
88  public void addSeries(final String title, final double[] values) {
89  int cnt = 1;
90  for (double value : values) {
91  String category = (cnt > this.categories.length ? Integer.toString(cnt) : this.categories[cnt-1]);
92  this.dataset.addValue(value, title, category);
93  cnt++;
94  }
95  }
96 
97 }
final DefaultCategoryDataset dataset
Definition: LineChart.java:38
void addSeries(final String title, final double[] values)
Definition: LineChart.java:88
JFreeChart createChart(final String title, final String categoryAxisLabel, final String valueAxisLabel, final CategoryDataset dataset)
Definition: LineChart.java:73
LineChart(final String title, final String xAxisLabel, final String yAxisLabel, final String[] categories)
Definition: LineChart.java:60
LineChart(final String title, final String xAxisLabel, final String yAxisLabel)
Definition: LineChart.java:48