11package de .tum .bgu .msm .io .output ;
22
3+ import com .google .common .math .DoubleMath ;
4+ import com .google .common .math .Stats ;
35import de .tum .bgu .msm .data .DataSet ;
46import de .tum .bgu .msm .data .MitoTrip ;
7+ import de .tum .bgu .msm .data .Mode ;
58import de .tum .bgu .msm .data .Purpose ;
69import de .tum .bgu .msm .resources .Resources ;
710import org .apache .log4j .Logger ;
1518import java .io .File ;
1619import java .io .IOException ;
1720import java .util .ArrayList ;
21+ import java .util .Collections ;
1822import java .util .List ;
1923import java .util .Map ;
2024import java .util .stream .Collectors ;
@@ -30,18 +34,19 @@ public static void writeDistanceDistributions(DataSet dataSet, String scenarioNa
3034 new File (directory ).mkdirs ();
3135 logger .info ("Writing trip length plots to " + directory );
3236
33- List <Chart > individualCharts = new ArrayList <>();
37+ List <Chart > individualChartsByPurpose = new ArrayList <>();
38+ List <Chart > individualChartsByMode = new ArrayList <>();
3439
3540 // Create Chart
36- CategoryChart stackedChart = new CategoryChartBuilder ().width (1600 ).height (900 ).title ("Trip Length Frequency Distributions" )
41+ CategoryChart stackedChartByPurpose = new CategoryChartBuilder ().width (1600 ).height (900 ).title ("Trip Length Frequency Distributions" )
3742 .xAxisTitle ("Trip Length" ).yAxisTitle ("Frequency" ).theme (Styler .ChartTheme .GGPlot2 ).build ();
3843
3944
4045 // Customize Chart
41- stackedChart .getStyler ().setLegendPosition (Styler .LegendPosition .OutsideE );
42- stackedChart .getStyler ().setAvailableSpaceFill (1 );
43- stackedChart .getStyler ().setStacked (true );
44- stackedChart .getStyler ().setXAxisLabelRotation (90 );
46+ stackedChartByPurpose .getStyler ().setLegendPosition (Styler .LegendPosition .OutsideE );
47+ stackedChartByPurpose .getStyler ().setAvailableSpaceFill (1 );
48+ stackedChartByPurpose .getStyler ().setStacked (true );
49+ stackedChartByPurpose .getStyler ().setXAxisLabelRotation (90 );
4550
4651 final Map <Purpose , List <MitoTrip >> tripsByPurpose = dataSet .getTrips ().values ().stream ()
4752 .filter (trip -> trip .getTripOrigin () != null && trip .getTripDestination () != null )
@@ -51,8 +56,7 @@ public static void writeDistanceDistributions(DataSet dataSet, String scenarioNa
5156 if (tripsByPurpose .containsKey (purpose )) {
5257
5358 // Create Chart
54- CategoryChart individualChart = new CategoryChartBuilder ().width (800 ).height (600 ).title ("Trip Length Frequency Distribution - " + purpose .name ())
55- .xAxisTitle ("Trip Length" ).yAxisTitle ("Frequency" ).theme (Styler .ChartTheme .GGPlot2 ).build ();
59+ CategoryChart individualChart = new CategoryChartBuilder ().width (800 ).height (600 ).xAxisTitle ("Trip Length" ).yAxisTitle ("Frequency" ).theme (Styler .ChartTheme .GGPlot2 ).build ();
5660
5761 // Customize Chart
5862 individualChart .getStyler ().setLegendPosition (Styler .LegendPosition .OutsideE );
@@ -65,16 +69,84 @@ public static void writeDistanceDistributions(DataSet dataSet, String scenarioNa
6569 .getTravelDistance (t .getTripOrigin ().getZoneId (), t .getTripDestination ().getZoneId ());
6670 distances .add (travelDistance );
6771 }
68- Histogram histogram = new Histogram (distances , 50 , 0 , 50 );
69- stackedChart .addSeries (purpose .name (), histogram .getxAxisData (), histogram .getyAxisData ());
72+ Histogram histogram = new Histogram (distances , 50 , 0 , 100 );
73+ stackedChartByPurpose .addSeries (purpose .name (), histogram .getxAxisData (), histogram .getyAxisData ());
7074 individualChart .addSeries (purpose .name (), histogram .getxAxisData (), histogram .getyAxisData ());
71- individualCharts .add (individualChart );
75+ double avg = Stats .meanOf (distances );
76+ individualChart .setTitle ("Trip Length Frequency Distribution - " + purpose .name () + " - Avg: " + avg );
77+ individualChartsByPurpose .add (individualChart );
78+ }
79+ }
80+
81+ // Create Chart
82+ CategoryChart stackedChartByMode = new CategoryChartBuilder ().width (1600 ).height (900 ).title ("Trip Length Frequency Distributions" )
83+ .xAxisTitle ("Trip Length" ).yAxisTitle ("Frequency" ).theme (Styler .ChartTheme .GGPlot2 ).build ();
84+
85+
86+ // Customize Chart
87+ stackedChartByMode .getStyler ().setLegendPosition (Styler .LegendPosition .OutsideE );
88+ stackedChartByMode .getStyler ().setAvailableSpaceFill (1 );
89+ stackedChartByMode .getStyler ().setStacked (true );
90+ stackedChartByMode .getStyler ().setXAxisLabelRotation (90 );
91+
92+ final Map <Mode , List <MitoTrip >> tripsByMode = dataSet .getTrips ().values ().stream ()
93+ .filter (trip -> trip .getTripOrigin () != null && trip .getTripDestination () != null )
94+ .collect (Collectors .groupingBy (MitoTrip ::getTripMode ));
95+
96+ for (Mode mode : Mode .values ()) {
97+ if (tripsByMode .containsKey (mode )) {
98+
99+ // Create Chart
100+ CategoryChart individualChart = new CategoryChartBuilder ().width (800 ).height (600 ).xAxisTitle ("Trip Length" ).yAxisTitle ("Frequency" ).theme (Styler .ChartTheme .GGPlot2 ).build ();
101+
102+ // Customize Chart
103+ individualChart .getStyler ().setLegendPosition (Styler .LegendPosition .OutsideE );
104+ individualChart .getStyler ().setAvailableSpaceFill (1 );
105+ individualChart .getStyler ().setXAxisLabelRotation (90 );
106+
107+ List <Double > distances = new ArrayList <>();
108+ for (MitoTrip t : tripsByMode .get (mode )) {
109+ double travelDistance = dataSet .getTravelDistancesAuto ()
110+ .getTravelDistance (t .getTripOrigin ().getZoneId (), t .getTripDestination ().getZoneId ());
111+ distances .add (travelDistance );
112+ }
113+ Histogram histogram = new Histogram (distances , 50 , 0 , 100 );
114+ stackedChartByMode .addSeries (mode .name (), histogram .getxAxisData (), histogram .getyAxisData ());
115+
116+ individualChart .addSeries (mode .name (), histogram .getxAxisData (), histogram .getyAxisData ());
117+ double avg = Stats .meanOf (distances );
118+ individualChart .setTitle ("Trip Length Frequency Distribution - " + mode .name () + " - Avg: " + avg );
119+ individualChartsByMode .add (individualChart );
72120 }
73121 }
74122
75123 try {
76- BitmapEncoder .saveBitmap (stackedChart , directory + "/tripLengthsStacked" , BitmapEncoder .BitmapFormat .PNG );
77- BitmapEncoder .saveBitmap (individualCharts ,(int ) (individualCharts .size ()/3 ),3 , directory + "/tripLengthsIndividual" , BitmapEncoder .BitmapFormat .PNG );
124+ BitmapEncoder .saveBitmap (stackedChartByPurpose , directory + "/tripLengthsStackedByPurpose" , BitmapEncoder .BitmapFormat .PNG );
125+ BitmapEncoder .saveBitmap (stackedChartByMode , directory + "/tripLengthsStackedByMode" , BitmapEncoder .BitmapFormat .PNG );
126+
127+ Histogram fakeHistogram = new Histogram (new ArrayList <>(Collections .nCopies (50 , 0 )), 50 , 0 , 100 );
128+ List <Double > fakeXData = fakeHistogram .getxAxisData ();
129+ List <Double > fakeYData = new ArrayList <>(Collections .nCopies (50 , 0. ));
130+
131+ int numberOfRowsPurposes = (int ) Math .ceil (individualChartsByPurpose .size () / 3. );
132+ int numberOfEmptyChartsPurposes = numberOfRowsPurposes *3 -individualChartsByPurpose .size ();
133+ for (int i = 0 ; i <numberOfEmptyChartsPurposes ;i ++){
134+ CategoryChart EmptyChart = new CategoryChartBuilder ().width (800 ).height (600 ).xAxisTitle ("Trip Length" ).yAxisTitle ("Frequency" ).theme (Styler .ChartTheme .GGPlot2 ).build ();
135+ EmptyChart .addSeries ("NA" ,fakeXData , fakeYData );
136+ EmptyChart .setTitle ("Empty Chart" );
137+ individualChartsByPurpose .add (EmptyChart );
138+ }
139+ BitmapEncoder .saveBitmap (individualChartsByPurpose , numberOfRowsPurposes , 3 , directory + "/tripLengthsIndividualByPurpose" , BitmapEncoder .BitmapFormat .PNG );
140+
141+ int numberOfRowsModes = (int ) Math .ceil (individualChartsByMode .size () / 3. );
142+ int numberOfEmptyChartsModes = numberOfRowsModes *3 -individualChartsByMode .size ();
143+ for (int i = 0 ; i <numberOfEmptyChartsModes ;i ++){
144+ CategoryChart EmptyChart = new CategoryChartBuilder ().width (800 ).height (600 ).xAxisTitle ("Trip Length" ).yAxisTitle ("Frequency" ).theme (Styler .ChartTheme .GGPlot2 ).build ();
145+ EmptyChart .addSeries ("NA" ,fakeXData , fakeYData );
146+ EmptyChart .setTitle ("Empty Chart" );
147+ individualChartsByMode .add (EmptyChart );
148+ }
149+ BitmapEncoder .saveBitmap (individualChartsByMode ,numberOfRowsModes ,3 , directory + "/tripLengthsIndividualByMode" , BitmapEncoder .BitmapFormat .PNG );
78150
79151 } catch (IOException e ) {
80152 e .printStackTrace ();
0 commit comments