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
14
15
16
17
18
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
34
35
36
37
38
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
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
68
69 public String getURL() {
70 return this.fileName;
71 }
72
73
74
75
76 public String getFullTitle() {
77 return this.title;
78 }
79
80
81
82
83 public int getWidth() {
84 return this.size.width;
85 }
86
87
88
89
90 public int getHeight() {
91 return this.size.height;
92 }
93 }