MATSIM
XYScatterChart.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * XYScatterChart.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.LogarithmicAxis;
26 import org.jfree.chart.plot.PlotOrientation;
27 import org.jfree.chart.plot.XYPlot;
28 import org.jfree.data.xy.XYSeries;
29 import org.jfree.data.xy.XYSeriesCollection;
30 
36 public class XYScatterChart extends ChartUtil {
37 
38  private final XYSeriesCollection dataset;
39  private final boolean isLogarithmicAxis;
40 
41  public XYScatterChart(final String title, final String xAxisLabel,
42  final String yAxisLabel) {
43  this(title, xAxisLabel, yAxisLabel, false);
44  }
45 
46  public XYScatterChart(final String title, final String xAxisLabel,
47  final String yAxisLabel, boolean isLogarithmicAxis) {
48  super(title, xAxisLabel, yAxisLabel);
49  this.isLogarithmicAxis = isLogarithmicAxis;
50  this.dataset = new XYSeriesCollection();
51  this.chart = createChart(title, xAxisLabel, yAxisLabel, this.dataset);
53  }
54 
55  @Override
56  public JFreeChart getChart() {
57  return this.chart;
58  }
59 
60  private JFreeChart createChart(final String title,
61  final String categoryAxisLabel, final String valueAxisLabel,
62  final XYSeriesCollection dataset) {
63  JFreeChart c = ChartFactory.createScatterPlot(title, categoryAxisLabel,
64  valueAxisLabel, dataset, PlotOrientation.VERTICAL, true, // legend?
65  false, // tooltips?
66  false // URLs?
67  );
68  if (this.isLogarithmicAxis) {
69  XYPlot p = (XYPlot) c.getPlot();
70  LogarithmicAxis axis_x = new LogarithmicAxis(this.xAxisLabel);
71  LogarithmicAxis axis_y = new LogarithmicAxis(this.yAxisLabel);
72  axis_x.setAllowNegativesFlag(false);
73  axis_y.setAllowNegativesFlag(false);
74  p.setDomainAxis(axis_x);
75  p.setRangeAxis(axis_y);
76  }
77  return c;
78  }
79 
91  public void addSeries(final String title, final double[] xs,
92  final double[] ys) {
93  XYSeries series = new XYSeries(title, false, true);
94  for (int i = 0, n = Math.min(xs.length, ys.length); i < n; i++) {
95  series.add(xs[i], ys[i]);
96  }
97  this.dataset.addSeries(series);
98  }
99 
100 }
JFreeChart createChart(final String title, final String categoryAxisLabel, final String valueAxisLabel, final XYSeriesCollection dataset)
XYScatterChart(final String title, final String xAxisLabel, final String yAxisLabel)
XYScatterChart(final String title, final String xAxisLabel, final String yAxisLabel, boolean isLogarithmicAxis)
void addSeries(final String title, final double[] xs, final double[] ys)