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.6 2008/04/02 11:22:15 benoitx Exp $
19 */
20 public class ChartImage {
21 private final static Logger logger = Logger.getLogger("sf.net.statcvs");
22
23 private final static Color BACKGROUND_COLOR = new Color(204, 204, 187);
24
25 private final String rootDirectory;
26 private JFreeChart chart;
27 private final String fileName;
28 private final Dimension size;
29 private final String title;
30 private boolean written = false;
31
32 /**
33 * Creates a new ChartFile.
34 * @param rootDirectory The report root directory with trailing slash
35 * @param fileName The relative file name for the chart, with .png extension
36 * @param title The chart's title
37 * @param chart The JFreeChart object to save as a file
38 * @param size width and height of the chart in pixels
39 */
40 public ChartImage(final String rootDirectory, final String fileName, final String title, final JFreeChart chart, final Dimension size) {
41 this.rootDirectory = rootDirectory;
42 this.fileName = fileName;
43 this.title = title;
44 this.chart = chart;
45 this.size = size;
46 chart.setBackgroundPaint(BACKGROUND_COLOR);
47 }
48
49 /**
50 * Writes the chart to disk as a PNG file.
51 */
52 public void write() {
53 if (this.written) {
54 return;
55 }
56 logger.info("writing chart '" + this.title + "' to " + this.fileName);
57 try {
58 ChartUtilities.saveChartAsPNG(new File(rootDirectory + fileName), chart, size.width, size.height);
59 } catch (final IOException e) {
60 logger.warning("could not write chart '" + fileName + "': " + e);
61 }
62 this.written = true;
63 this.chart = null;
64 }
65
66 /**
67 * Returns the chart's URL, relative to the report root.
68 */
69 public String getURL() {
70 return this.fileName;
71 }
72
73 /**
74 * Returns the chart's title.
75 */
76 public String getFullTitle() {
77 return this.title;
78 }
79
80 /**
81 * Returns the chart's width in pixels.
82 */
83 public int getWidth() {
84 return this.size.width;
85 }
86
87 /**
88 * Returns the chart's height in pixels.
89 */
90 public int getHeight() {
91 return this.size.height;
92 }
93 }