MATSIM
ChartUtil.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ChartUtil.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.awt.Color;
24 import java.awt.Image;
25 import java.io.File;
26 import java.io.IOException;
27 
28 import org.jfree.chart.ChartUtils;
29 import org.jfree.chart.JFreeChart;
30 import org.jfree.chart.title.ImageTitle;
31 import org.jfree.chart.title.Title;
32 import org.jfree.chart.ui.HorizontalAlignment;
33 import org.jfree.chart.ui.RectangleEdge;
34 import org.jfree.chart.ui.VerticalAlignment;
36 
43 public abstract class ChartUtil {
44 
45  protected final String chartTitle;
46  protected final String xAxisLabel;
47  protected final String yAxisLabel;
48  protected JFreeChart chart = null;
49 
50  public ChartUtil(final String title, final String xAxisLabel, final String yAxisLabel) {
51  this.chartTitle = title;
52  this.xAxisLabel = xAxisLabel;
53  this.yAxisLabel = yAxisLabel;
54  }
55 
56  public abstract JFreeChart getChart();
57 
65  public void saveAsPng(final String filename, final int width, final int height) {
66  try {
67  ChartUtils.saveChartAsPNG(new File(filename), getChart(), width, height, null, true, 9);
68  } catch (IOException e) {
69  e.printStackTrace();
70  } catch ( Exception e ) {
71  e.printStackTrace() ;
72  // I just had an out of bounds error inside the method; don't know what that means but does not feel like a reason
73  // to not continue. kai, apr'30
74  }
75  }
76 
80  public void addMatsimLogo() {
81  try {
82  Image image = MatsimResource.getAsImage("matsim_logo_transparent_small.png");
83  Title subtitle = new ImageTitle(image, RectangleEdge.BOTTOM, HorizontalAlignment.RIGHT, VerticalAlignment.BOTTOM);
84  this.chart.addSubtitle(subtitle);
85  } catch ( Exception e ) {
86  e.printStackTrace() ;
87  // I just had a resource-not-found error inside the method; don't know what that means but does not feel like a reason
88  // to not continue. kai, apr'30
89  }
90  }
91 
96  protected void addDefaultFormatting() {
97  this.chart.setBackgroundPaint(new Color(1.0f, 1.0f, 1.0f, 1.0f));
98  this.chart.getLegend().setBorder(0.0, 0.0, 0.0, 0.0);
99  }
100 
101 }
ChartUtil(final String title, final String xAxisLabel, final String yAxisLabel)
Definition: ChartUtil.java:50
static final Image getAsImage(final String filename)
void saveAsPng(final String filename, final int width, final int height)
Definition: ChartUtil.java:65