MATSIM
XYLineChart.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 java.util.Map;
24 import java.util.Map.Entry;
25 
26 import org.jfree.chart.ChartFactory;
27 import org.jfree.chart.JFreeChart;
28 import org.jfree.chart.axis.LogarithmicAxis;
29 import org.jfree.chart.plot.PlotOrientation;
30 import org.jfree.chart.plot.XYPlot;
31 import org.jfree.data.xy.XYSeries;
32 import org.jfree.data.xy.XYSeriesCollection;
33 
39 public class XYLineChart extends ChartUtil {
40 
41  private final XYSeriesCollection dataset;
42  private final boolean isLogarithmicAxis;
43 
44  public XYLineChart(final String title, final String xAxisLabel, final String yAxisLabel) {
45  this(title, xAxisLabel, yAxisLabel, false);
46  }
47 
48  public XYLineChart(final String title, final String xAxisLabel,
49  final String yAxisLabel, boolean isLogarithmicAxis) {
50  super(title, xAxisLabel, yAxisLabel);
51  this.isLogarithmicAxis = isLogarithmicAxis;
52  this.dataset = new XYSeriesCollection();
53  this.chart = createChart(title, xAxisLabel, yAxisLabel, this.dataset);
55  }
56 
57  @Override
58  public JFreeChart getChart() {
59  return this.chart;
60  }
61 
62  private JFreeChart createChart(final String title, final String categoryAxisLabel,
63  final String valueAxisLabel, final XYSeriesCollection dataset) {
64  JFreeChart c = ChartFactory.createXYLineChart(title, categoryAxisLabel, valueAxisLabel,
65  dataset, PlotOrientation.VERTICAL, true, // legend?
66  false, // tooltips?
67  false // URLs?
68  );
69  if (this.isLogarithmicAxis){
70  XYPlot p = (XYPlot) c.getPlot();
71  LogarithmicAxis axis_x = new LogarithmicAxis(this.xAxisLabel);
72  LogarithmicAxis axis_y = new LogarithmicAxis(this.yAxisLabel);
73  axis_x.setAllowNegativesFlag(false);
74  axis_y.setAllowNegativesFlag(false);
75  p.setDomainAxis(axis_x);
76  p.setRangeAxis(axis_y);
77  }
78  return c;
79  }
80 
90  public final void addSeries(final String title, final double[] xs, final double[] ys) {
91  XYSeries series = new XYSeries(title, false, true);
92  for (int i = 0, n = Math.min(xs.length, ys.length); i < n; i++) {
93  series.add(xs[i], ys[i]);
94  }
95  this.dataset.addSeries(series);
96  }
97 
98  public final void addSeries(String title, Map<Integer, Double> map) {
99  XYSeries series = new XYSeries(title, false, true);
100  for ( Entry<Integer,Double> entry : map.entrySet() ) {
101  series.add(entry.getKey(), entry.getValue() );
102  }
103  this.dataset.addSeries(series);
104  }
105 
106 }
JFreeChart createChart(final String title, final String categoryAxisLabel, final String valueAxisLabel, final XYSeriesCollection dataset)
XYLineChart(final String title, final String xAxisLabel, final String yAxisLabel, boolean isLogarithmicAxis)
XYLineChart(final String title, final String xAxisLabel, final String yAxisLabel)
final void addSeries(final String title, final double[] xs, final double[] ys)
final void addSeries(String title, Map< Integer, Double > map)