View Javadoc

1   package net.sf.statcvs.charts;
2   
3   import java.awt.Color;
4   import java.awt.Dimension;
5   import java.io.File;
6   import java.io.IOException;
7   import java.util.logging.Logger;
8   
9   import org.jfree.chart.ChartUtilities;
10  import org.jfree.chart.JFreeChart;
11  
12  /**
13   * An image file for a chart.
14   * 
15   * TODO: Better integrate all charts with ReportConfig 
16   * @author jentzsch
17   * @author Richard Cyganiak (richard@cyganiak.de)
18   * @version $Id: ChartImage.java,v 1.9 2009/03/14 11:08:35 benoitx Exp $
19   */
20  public class ChartImage {
21      private final static Logger logger = Logger.getLogger("sf.net.statcvs");
22  
23  //    public final static Color BACKGROUND_COLOR = new Color(204, 204, 187);
24  //    public final static Color PLOT_COLOR = new Color(100, 204, 187);
25      public final static Color BACKGROUND_COLOR = new Color(255, 255, 255);
26      public final static Color PLOT_COLOR = new Color(0xD0, 0xE0, 0xE0);
27      
28      private final String rootDirectory;
29      private JFreeChart chart;
30      private final String fileName;
31      private final Dimension size;
32      private final String title;
33      private boolean written = false;
34  
35      /**
36       * Creates a new ChartFile.
37       * @param rootDirectory The report root directory with trailing slash
38       * @param fileName The relative file name for the chart, with .png extension
39       * @param title The chart's title
40       * @param chart The JFreeChart object to save as a file
41       * @param size width and height of the chart in pixels
42       */
43      public ChartImage(final String rootDirectory, final String fileName, final String title, final JFreeChart chart, final Dimension size) {
44          this.rootDirectory = rootDirectory;
45          this.fileName = fileName;
46          this.title = title;
47          this.chart = chart;
48          this.size = size;
49          //        chart.setBackgroundPaint(BACKGROUND_COLOR);
50      }
51  
52      /**
53       * Writes the chart to disk as a PNG file.
54       */
55      public void write() {
56          if (this.written) {
57              return;
58          }
59          logger.info("writing chart '" + this.title + "' to " + this.fileName);
60          try {
61              ChartUtilities.saveChartAsPNG(new File(rootDirectory + fileName), chart, size.width, size.height);
62          } catch (final IOException e) {
63              logger.warning("could not write chart '" + fileName + "': " + e);
64          }
65          this.written = true;
66          this.chart = null; // Free memory? Not sure if this has any effect ... 
67      }
68  
69      /**
70       * Returns the chart's URL, relative to the report root.
71       */
72      public String getURL() {
73          return this.fileName;
74      }
75  
76      /**
77       * Returns the chart's title.
78       */
79      public String getFullTitle() {
80          return this.title;
81      }
82  
83      /**
84       * Returns the chart's width in pixels.
85       */
86      public int getWidth() {
87          return this.size.width;
88      }
89  
90      /**
91       * Returns the chart's height in pixels.
92       */
93      public int getHeight() {
94          return this.size.height;
95      }
96  }