MATSIM
RouteTimeDiagram.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * RouteTimeDiagram.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 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.pt.analysis;
22 
23 import java.awt.Color;
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 
32 import org.jfree.chart.ChartFactory;
33 import org.jfree.chart.ChartUtils;
34 import org.jfree.chart.JFreeChart;
35 import org.jfree.chart.plot.PlotOrientation;
36 import org.jfree.chart.plot.XYPlot;
37 import org.jfree.chart.renderer.xy.XYItemRenderer;
38 import org.jfree.data.xy.XYSeries;
39 import org.jfree.data.xy.XYSeriesCollection;
40 import org.matsim.api.core.v01.Id;
49 
58 
62  private final Map<Id, List<Tuple<Id, Double>>> positions = new HashMap<Id, List<Tuple<Id, Double>>>();
63 
64  public void handleEvent(final VehicleArrivesAtFacilityEvent event) {
65  List<Tuple<Id, Double>> list = this.positions.get(event.getVehicleId());
66  if (list == null) {
67  list = new ArrayList<Tuple<Id, Double>>();
68  this.positions.put(event.getVehicleId(), list);
69  }
70  list.add(new Tuple<Id, Double>(event.getFacilityId(), Double.valueOf(event.getTime())));
71  }
72 
73  public void handleEvent(final VehicleDepartsAtFacilityEvent event) {
74  List<Tuple<Id, Double>> list = this.positions.get(event.getVehicleId());
75  if (list == null) {
76  list = new ArrayList<Tuple<Id, Double>>();
77  this.positions.put(event.getVehicleId(), list);
78  }
79  list.add(new Tuple<Id, Double>(event.getFacilityId(), Double.valueOf(event.getTime())));
80  }
81 
82  public void reset(final int iteration) {
83  this.positions.clear();
84  }
85 
86  public void writeData() {
87  for (List<Tuple<Id, Double>> list : this.positions.values()) {
88  for (Tuple<Id, Double> info : list) {
89  System.out.println(info.getFirst().toString() + "\t" + info.getSecond().toString());
90  }
91  System.out.println();
92  }
93  }
94 
95  public void createGraph(final String filename, final TransitRoute route) {
96 
97  HashMap<Id, Integer> stopIndex = new HashMap<Id, Integer>();
98  int idx = 0;
99  for (TransitRouteStop stop : route.getStops()) {
100  stopIndex.put(stop.getStopFacility().getId(), idx);
101  idx++;
102  }
103 
104  HashSet<Id> vehicles = new HashSet<Id>();
105  for (Departure dep : route.getDepartures().values()) {
106  vehicles.add(dep.getVehicleId());
107  }
108 
109  XYSeriesCollection dataset = new XYSeriesCollection();
110  int numSeries = 0;
111  double earliestTime = Double.POSITIVE_INFINITY;
112  double latestTime = Double.NEGATIVE_INFINITY;
113 
114  for (Map.Entry<Id, List<Tuple<Id, Double>>> entry : this.positions.entrySet()) {
115  if (vehicles.contains(entry.getKey())) {
116  XYSeries series = new XYSeries("t", false, true);
117  for (Tuple<Id, Double> pos : entry.getValue()) {
118  Integer stopIdx = stopIndex.get(pos.getFirst());
119  if (stopIdx != null) {
120  double time = pos.getSecond().doubleValue();
121  series.add(stopIdx.intValue(), time);
122  if (time < earliestTime) {
123  earliestTime = time;
124  }
125  if (time > latestTime) {
126  latestTime = time;
127  }
128  }
129  }
130  dataset.addSeries(series);
131  numSeries++;
132 
133  }
134  }
135 
136  JFreeChart c = ChartFactory.createXYLineChart("Route-Time Diagram, Route = " + route.getId(), "stops", "time",
137  dataset, PlotOrientation.VERTICAL,
138  false, // legend?
139  false, // tooltips?
140  false // URLs?
141  );
142  c.setBackgroundPaint(new Color(1.0f, 1.0f, 1.0f, 1.0f));
143 
144  XYPlot p = (XYPlot) c.getPlot();
145 
146  p.getRangeAxis().setInverted(true);
147  p.getRangeAxis().setRange(earliestTime, latestTime);
148  XYItemRenderer renderer = p.getRenderer();
149  for (int i = 0; i < numSeries; i++) {
150  renderer.setSeriesPaint(i, Color.black);
151  }
152 
153  try {
154  ChartUtils.saveChartAsPNG(new File(filename), c, 1024, 768, null, true, 9);
155  } catch (IOException e) {
156  e.printStackTrace();
157  }
158  }
159 
160 }
final Map< Id, List< Tuple< Id, Double > > > positions
Map< Id< Departure >, Departure > getDepartures()
void handleEvent(final VehicleArrivesAtFacilityEvent event)
void handleEvent(final VehicleDepartsAtFacilityEvent event)
void createGraph(final String filename, final TransitRoute route)