comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/XMLChartTheme.java @ 540:80630520e25a

merged gnv-artifacts/0.4
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:49 +0200
parents 22a6493e8460
children 3d13fa281a7e
comparison
equal deleted inserted replaced
415:9f4a0b990d27 540:80630520e25a
1 package de.intevation.gnv.chart;
2
3 import java.awt.Font;
4 import java.awt.Color;
5 import java.awt.geom.Ellipse2D;
6 import java.lang.NumberFormatException;
7
8 import org.apache.log4j.Logger;
9
10 import org.jfree.chart.StandardChartTheme;
11 import org.jfree.chart.plot.XYPlot;
12 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
13 import org.jfree.ui.RectangleInsets;
14 import org.jfree.util.UnitType;
15
16
17 import org.w3c.dom.Document;
18
19 import de.intevation.artifactdatabase.Config;
20
21 /**
22 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
23 */
24 public class XMLChartTheme
25 extends StandardChartTheme
26 {
27 private static final Color DEFAULT_COLOR = Color.BLACK;
28
29 private Logger log = Logger.getLogger(XMLChartTheme.class);
30
31 protected boolean domainCrosshairVisible;
32 protected boolean rangeCrosshairVisible;
33 protected boolean renderLines;
34 protected boolean renderShapes;
35
36 protected int pointWidth;
37 protected int pointHeight;
38
39
40 public XMLChartTheme(String name) {
41 super(name);
42 }
43
44
45 public void setDomainCrosshairVisible(boolean visible) {
46 this.domainCrosshairVisible = visible;
47 }
48
49
50 public boolean getDomainCrosshairVisible() {
51 return domainCrosshairVisible;
52 }
53
54
55 public boolean getRangeCrosshairVisible() {
56 return rangeCrosshairVisible;
57 }
58
59
60 public void setRangeCrosshairVisible(boolean visible) {
61 this.rangeCrosshairVisible = visible;
62 }
63
64
65 public void applyXMLConfiguration(Document document) {
66 log.debug("create XMLChartTheme");
67
68 init(document);
69 }
70
71
72 private void init(Document document) {
73 log.debug("init XMLChartTheme parameters");
74
75 initChartParameters(document);
76 initTitleParameters(document);
77 initSubtitleParameters(document);
78 initPlotParameters(document);
79 initAxisParameters(document);
80 initLegendParameters(document);
81 initRenderer(document);
82 }
83
84
85 private void initTitleParameters(Document document) {
86 log.debug("init title parameters.");
87
88 int size = getInt(document, "theme/title/font/size/@value");
89 String type = getString(document, "theme/title/font/type/@value");
90 boolean bold = getBool(document, "theme/title/font/bold/@value");
91
92 setExtraLargeFont(createFont(type, size, bold));
93
94 String color = getString(document, "theme/title/font/color/@value");
95 Color c = decodeColor(color);
96 if (c != null)
97 setTitlePaint(c);
98 }
99
100
101 private void initSubtitleParameters(Document document) {
102 log.debug("init title parameters.");
103
104 int size = getInt(document, "theme/subtitle/font/size/@value");
105 String type = getString(document, "theme/subtitle/font/type/@value");
106 boolean bold = getBool(document, "theme/subtitle/font/bold/@value");
107
108 setLargeFont(createFont(type, size, bold));
109
110 String col = getString(document, "theme/subtitle/font/color/@value");
111 setSubtitlePaint(Color.decode(col));
112 }
113
114
115 private void initChartParameters(Document document) {
116 log.debug("init chart parameters.");
117
118 String bg = getString(document, "theme/chart/background/color/@value");
119 Color c = decodeColor(bg);
120 if (c != null)
121 setChartBackgroundPaint(c);
122 }
123
124
125 private void initPlotParameters(Document document) {
126 log.debug("init plot parameters.");
127
128 String tmp = null;
129 tmp = getString(document, "theme/plot/background/color/@value");
130 Color c = decodeColor(tmp);
131 if (c != null)
132 setPlotBackgroundPaint(c);
133
134 tmp = getString(document, "theme/plot/outline/color/@value");
135 c = decodeColor(tmp);
136 if (c != null)
137 setPlotOutlinePaint(c);
138
139 tmp = getString(document, "theme/plot/domaingridline/color/@value");
140 c = decodeColor(tmp);
141 if (c != null)
142 setDomainGridlinePaint(c);
143
144 tmp = getString(document, "theme/plot/rangegridline/color/@value");
145 c = decodeColor(tmp);
146 if (c != null)
147 setRangeGridlinePaint(c);
148
149 boolean rangeCrosshairVisible = getBool(
150 document, "theme/plot/rangecrosshair/visible/@value");
151 setRangeCrosshairVisible(rangeCrosshairVisible);
152
153 boolean domainCrosshairVisible = getBool(
154 document, "theme/plot/domaincrosshair/visible/@value");
155 setDomainCrosshairVisible(domainCrosshairVisible);
156
157 int top = getInt(document, "theme/plot/offset/top/@value");
158 int bottom = getInt(document, "theme/plot/offset/bottom/@value");
159 int left = getInt(document, "theme/plot/offset/left/@value");
160 int right = getInt(document, "theme/plot/offset/right/@value");
161 setAxisOffset(new RectangleInsets(
162 UnitType.RELATIVE,
163 top, left, bottom, right)
164 );
165 }
166
167
168 private void initAxisParameters(Document document) {
169 log.debug("init axis parameters.");
170
171 String tmp = null;
172 tmp = getString(document, "theme/axis/label/color/@value");
173 Color c = decodeColor(tmp);
174 if (c != null)
175 setAxisLabelPaint(c);
176
177 tmp = getString(document, "theme/axis/ticklabel/color/@value");
178 c = decodeColor(tmp);
179 if (c != null)
180 setTickLabelPaint(c);
181 }
182
183
184 private void initLegendParameters(Document document) {
185 log.debug("init legend parameters.");
186
187 String tmp = null;
188 tmp = getString(document, "theme/legend/font/color/@value");
189 Color c = decodeColor(tmp);
190 if (c != null)
191 setLegendItemPaint(c);
192
193 tmp = getString(document, "theme/legend/background/color/@value");
194 c = decodeColor(tmp);
195 if (c != null)
196 setLegendBackgroundPaint(c);
197 }
198
199
200 private void initRenderer(Document document) {
201 log.debug("init renderer parameters.");
202
203 pointWidth = getInt(document, "theme/plot/itemrenderer/width/@value");
204 log.debug("Read point width of " + pointWidth);
205 pointHeight = getInt(document, "theme/plot/itemrenderer/height/@value");
206 log.debug("Read point height of " + pointHeight);
207 renderLines = getBool(
208 document, "theme/plot/itemrenderer/renderLines/@value"
209 );
210 renderShapes = getBool(
211 document, "theme/plot/itemrenderer/renderPoints/@value"
212 );
213 }
214
215
216 private static String getString(Document document, String xpath) {
217 return Config.getStringXPath(document, xpath);
218 }
219
220
221 private static int getInt(Document document, String xpath) {
222 String tmp = getString(document, xpath);
223
224 if (tmp != null)
225 return Integer.parseInt(tmp);
226 else
227 return 0;
228 }
229
230
231 private static boolean getBool(Document document, String xpath) {
232 String tmp = getString(document, xpath);
233
234 if (tmp != null)
235 return Boolean.parseBoolean(tmp);
236 else
237 return false;
238 }
239
240
241 protected Color decodeColor(String color) {
242 try {
243 if (color == null)
244 return null;
245
246 return Color.decode(color);
247 }
248 catch (NumberFormatException nfe) {
249 log.warn("Error while parsing color: " + color, nfe);
250 }
251
252 return null;
253 }
254
255
256 protected Font createFont(String type, int size, boolean bold) {
257 Font font = null;
258 if (bold)
259 font = new Font(type, Font.BOLD, size);
260 else
261 font = new Font(type, Font.PLAIN, size);
262
263 return font;
264 }
265
266
267 protected void applyToXYPlot(XYPlot plot) {
268 log.debug("apply theme parameter to XYPlot");
269
270 super.applyToXYPlot(plot);
271 plot.setDomainCrosshairVisible(this.domainCrosshairVisible);
272 plot.setRangeCrosshairVisible(this.rangeCrosshairVisible);
273
274 applyToXYLineAndShapeRenderer(plot);
275 }
276
277
278 protected void applyToXYLineAndShapeRenderer(XYPlot plot) {
279 if (plot == null)
280 return;
281
282 XYLineAndShapeRenderer renderer =
283 (XYLineAndShapeRenderer) plot.getRenderer();
284
285 Ellipse2D.Double point = new Ellipse2D.Double(
286 -(pointWidth/2), -(pointHeight/2), pointWidth, pointHeight
287 );
288
289 renderer.setSeriesShape(0, point);
290 renderer.setSeriesShapesVisible(0, renderShapes);
291 renderer.setSeriesLinesVisible(0, renderLines);
292
293 plot.setRenderer(renderer);
294 }
295 }

http://dive4elements.wald.intevation.org