1 /**
2 *
3 */
4 package net.sf.statcvs.charts;
5
6 import java.awt.BasicStroke;
7 import java.awt.Color;
8 import java.awt.Dimension;
9 import java.awt.Font;
10 import java.awt.Image;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13
14 import javax.swing.ImageIcon;
15
16 import net.sf.statcvs.output.ConfigurationOptions;
17 import net.sf.statcvs.util.StringUtils;
18
19 import org.jfree.chart.JFreeChart;
20 import org.jfree.chart.plot.Plot;
21 import org.jfree.chart.renderer.xy.XYItemRenderer;
22 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
23 import org.jfree.chart.title.TextTitle;
24 import org.jfree.data.xy.XYDataset;
25 import org.jfree.ui.Align;
26 import org.jfree.ui.HorizontalAlignment;
27 import org.jfree.ui.RectangleEdge;
28
29 /**
30 * @author benoitx
31 *
32 */
33 public final class ChartConfigUtil {
34 private ChartConfigUtil() {
35 }
36
37 /**
38 * returns the background color from the config file, tries properties:
39 * <pre>
40 * chart.<chartName>.backgroundColor
41 * chart.backgroundColor
42 * </pre>
43 * @param chartName
44 */
45 public static Color getBackgroundColor(final String chartName) {
46 return ConfigurationOptions.getConfigColorProperty("chart." + chartName + ".backgroundColor", "chart.backgroundColor", ChartImage.BACKGROUND_COLOR);
47 }
48
49 /**
50 * returns the PLOT color from the config file, tries properties:
51 * <pre>
52 * chart.<chartName>.plotColor
53 * chart.plotColor
54 * </pre>
55 * @param chartName
56 */
57 public static Color getPlotColor(final String chartName) {
58 return ConfigurationOptions.getConfigColorProperty("chart." + chartName + ".plotColor", "chart.plotColor", ChartImage.BACKGROUND_COLOR);
59 }
60
61 /**
62 * returns the PLOT color from the config file, tries properties:
63 * <pre>
64 * chart.<chartName>.width
65 * chart.width
66 * chart.<chartName>.height
67 * chart.height
68 * </pre>
69 * @param chartName
70 */
71 public static Dimension getDimension(final String chartName, final Dimension defaultDimension) {
72 final Integer width = ConfigurationOptions
73 .getConfigIntegerProperty("chart." + chartName + ".width", "chart.width", new Integer(defaultDimension.width));
74
75 final Integer height = ConfigurationOptions.getConfigIntegerProperty("chart." + chartName + ".height", "chart.height", new Integer(
76 defaultDimension.height));
77
78 return new Dimension(width.intValue(), height.intValue());
79 }
80
81 /**
82 * configure the lines for the chart, tries properties:
83 * <pre>
84 * chart.<chartName>.lineStroke
85 * chart.lineStroke
86 * </pre>
87 * @param chartName
88 */
89 public static void configureStroke(final String chartName, final XYItemRenderer renderer, final XYDataset data) {
90 final Float stroke = ConfigurationOptions.getConfigFloatProperty("chart." + chartName + ".lineStroke", "chart.lineStroke", null);
91 if (stroke != null) {
92 for (int i = 0; i < data.getSeriesCount(); i++) {
93 renderer.setSeriesStroke(i, new BasicStroke(stroke.floatValue()));
94 }
95 }
96 }
97
98 /**
99 * configure the shapes for the chart (if renderer is of type XYLineAndShapeRenderer) , tries properties:
100 * <pre>
101 * chart.<chartName>.showShapes
102 * chart.filledShapes
103 * </pre>
104 * @param chartName
105 */
106 public static void configureShapes(final String chartName, final XYItemRenderer renderer) {
107 if (renderer instanceof XYLineAndShapeRenderer) {
108 final Boolean showShapes = ConfigurationOptions.getConfigBooleanProperty("chart." + chartName + ".showShapes", "chart.showShapes", Boolean.FALSE);
109 final Boolean filledShapes = ConfigurationOptions.getConfigBooleanProperty("chart." + chartName + ".filledShapes", "chart.filledShapes",
110 Boolean.FALSE);
111
112 ((XYLineAndShapeRenderer) renderer).setBaseShapesVisible(showShapes.booleanValue());
113 ((XYLineAndShapeRenderer) renderer).setBaseShapesFilled(filledShapes.booleanValue());
114 }
115 }
116
117 /**
118 * Add a copyright notice on the bottom right part of the chart.
119 * <pre>
120 * chart.<chartName>.chartBackgroundImage.url
121 * chart.chartBackgroundImage.url
122 * chart.<chartName>.chartBackgroundImage.transparency
123 * chart.chartBackgroundImage.transparency (0-1, defaulted to 0.35)
124 * </pre>
125 * @param chartName
126 * @param chart
127 */
128 public static void configureChartBackgroungImage(final String chartName, final JFreeChart chart) {
129 final String imageLocation = ConfigurationOptions.getConfigStringProperty("chart." + chartName + ".chartBackgroundImage.url",
130 "chart.chartBackgroundImage.url", null);
131 final Float alpha = ConfigurationOptions.getConfigFloatProperty("chart." + chartName + ".chartBackgroundImage.transparency",
132 "chart.chartBackgroundImage.transparency", new Float(0.35));
133 if (StringUtils.isNotEmpty(imageLocation) && alpha != null) {
134 Image image = null;
135 try {
136 image = new ImageIcon(new URL(imageLocation)).getImage();
137 } catch (MalformedURLException e) {
138 e.printStackTrace();
139 }
140 if (image != null) {
141 chart.setBackgroundImageAlpha(alpha.floatValue());
142 chart.setBackgroundImageAlignment(Align.BOTTOM_LEFT);
143 chart.setBackgroundImage(image);
144 }
145 }
146 }
147
148 /**
149 * Add a copyright notice on the bottom right part of the chart.
150 * <pre>
151 * chart.<chartName>.plotImage.url eg file:///C:/project/statcvs/site/images/statcvslogo.gif
152 * chart.plotImage.url
153 * chart.<chartName>.plotImage.transparency
154 * chart.plotImage.transparency (0-1, default to 0.35)
155 * </pre>
156 * @param chartName
157 * @param chart
158 */
159 public static void configurePlotImage(final String chartName, final JFreeChart chart) {
160 final String imageLocation = ConfigurationOptions.getConfigStringProperty("chart." + chartName + ".plotImage.url", "chart.plotImage.url",
161 null);
162 final Float alpha = ConfigurationOptions.getConfigFloatProperty("chart." + chartName + ".plotImage.transparency", "chart.plotImage.transparency",
163 new Float(0.35));
164 if (StringUtils.isNotEmpty(imageLocation) && alpha != null) {
165 Image image = null;
166 try {
167 image = new ImageIcon(new URL(imageLocation)).getImage();
168 } catch (MalformedURLException e) {
169 e.printStackTrace();
170 }
171 if (image != null) {
172 final Plot plot = chart.getPlot();
173 plot.setBackgroundImageAlpha(alpha.floatValue());
174 plot.setBackgroundImage(image);
175 }
176 }
177 }
178
179 /**
180 * Add a copyright notice on the bottom right part of the chart.
181 * <pre>
182 * chart.<chartName>.copyright
183 * chart.copyright
184 * </pre>
185 * @param chartName
186 * @param chart
187 */
188 public static void configureCopyrightNotice(final String chartName, final JFreeChart chart) {
189 final String copyrightTxt = ConfigurationOptions.getConfigStringProperty("chart." + chartName + ".copyright", "chart.copyright", null);
190 final Integer copyrightTxtSize = ConfigurationOptions.getConfigIntegerProperty("chart." + chartName + ".copyrightTextSize", "chart.copyrightTextSize",
191 new Integer(9));
192 if (StringUtils.isNotEmpty(copyrightTxt)) {
193 final TextTitle copyright = new TextTitle(copyrightTxt, new Font("SansSerif", Font.PLAIN, copyrightTxtSize.intValue()));
194
195 copyright.setPosition(RectangleEdge.BOTTOM);
196 copyright.setHorizontalAlignment(HorizontalAlignment.RIGHT);
197 chart.addSubtitle(copyright);
198 }
199 }
200 }