MATSIM
LegHistogramChart.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * * project: org.matsim.*
4  * * LegHistogramChart.java
5  * * *
6  * * *********************************************************************** *
7  * * *
8  * * copyright : (C) 2014 by the members listed in the COPYING, *
9  * * LICENSE and WARRANTY file. *
10  * * email : info at matsim dot org *
11  * * *
12  * * *********************************************************************** *
13  * * *
14  * * This program is free software; you can redistribute it and/or modify *
15  * * it under the terms of the GNU General Public License as published by *
16  * * the Free Software Foundation; either version 2 of the License, or *
17  * * (at your option) any later version. *
18  * * See also COPYING, LICENSE and WARRANTY file *
19  * * *
20  * * ***********************************************************************
21  */
22 
23 package org.matsim.analysis;
24 
25 import org.jfree.chart.ChartFactory;
26 import org.jfree.chart.ChartUtils;
27 import org.jfree.chart.JFreeChart;
28 import org.jfree.chart.axis.CategoryAxis;
29 import org.jfree.chart.axis.NumberAxis;
30 import org.jfree.chart.plot.PlotOrientation;
31 import org.jfree.chart.plot.XYPlot;
32 import org.jfree.data.xy.XYSeries;
33 import org.jfree.data.xy.XYSeriesCollection;
34 
35 import java.awt.*;
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.UncheckedIOException;
39 
40 public class LegHistogramChart {
41  static JFreeChart getGraphic(final LegHistogram.DataFrame dataFrame, final String mode, int iteration) {
42  final XYSeriesCollection xyData = new XYSeriesCollection();
43  final XYSeries departuresSerie = new XYSeries("departures", false, true);
44  final XYSeries arrivalsSerie = new XYSeries("arrivals", false, true);
45  final XYSeries onRouteSerie = new XYSeries("en route", false, true);
46  int onRoute = 0;
47  for (int i = 0; i < dataFrame.countsDep.length; i++) {
48  onRoute = onRoute + dataFrame.countsDep[i] - dataFrame.countsArr[i] - dataFrame.countsStuck[i];
49  double hour = i*dataFrame.binSize / 60.0 / 60.0;
50  departuresSerie.add(hour, dataFrame.countsDep[i]);
51  arrivalsSerie.add(hour, dataFrame.countsArr[i]);
52  onRouteSerie.add(hour, onRoute);
53  }
54 
55  xyData.addSeries(departuresSerie);
56  xyData.addSeries(arrivalsSerie);
57  xyData.addSeries(onRouteSerie);
58 
59  final JFreeChart chart = ChartFactory.createXYStepChart(
60  "Leg Histogram, " + mode + ", it." + iteration,
61  "time", "# persons",
62  xyData,
63  PlotOrientation.VERTICAL,
64  true, // legend
65  false, // tooltips
66  false // urls
67  );
68 
69  XYPlot plot = chart.getXYPlot();
70 
71  final CategoryAxis axis1 = new CategoryAxis("hour");
72  axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
73  plot.setDomainAxis(new NumberAxis("time"));
74 
75  plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0f));
76  plot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0f));
77  plot.getRenderer().setSeriesStroke(2, new BasicStroke(2.0f));
78  plot.setBackgroundPaint(Color.white);
79  plot.setRangeGridlinePaint(Color.gray);
80  plot.setDomainGridlinePaint(Color.gray);
81 
82  return chart;
83  }
84 
93  public static void writeGraphic(LegHistogram legHistogram, final String filename) {
94  try {
95  ChartUtils.saveChartAsPNG(new File(filename), getGraphic(legHistogram.getAllModesData(), "all", legHistogram.getIteration()), 1024, 768);
96  } catch (IOException e) {
97  throw new UncheckedIOException(e);
98  }
99  }
100 
111  public static void writeGraphic(LegHistogram legHistogram, final String filename, final String legMode) {
112  try {
113  ChartUtils.saveChartAsPNG(new File(filename), getGraphic(legHistogram.getDataForMode(legMode), legMode, legHistogram.getIteration()), 1024, 768);
114  } catch (IOException e) {
115  throw new UncheckedIOException(e);
116  }
117  }
118 }
static void writeGraphic(LegHistogram legHistogram, final String filename, final String legMode)
static void writeGraphic(LegHistogram legHistogram, final String filename)