Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/XMLChartTheme.java @ 767:79401c871da4
Added and repaired javadoc in de.intevation.gnv.chart package.
gnv-artifacts/trunk@823 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 24 Mar 2010 14:48:55 +0000 |
parents | 3d13fa281a7e |
children | 9a828e5a2390 |
comparison
equal
deleted
inserted
replaced
766:a23ce49423d5 | 767:79401c871da4 |
---|---|
20 import org.w3c.dom.Document; | 20 import org.w3c.dom.Document; |
21 | 21 |
22 import de.intevation.artifactdatabase.Config; | 22 import de.intevation.artifactdatabase.Config; |
23 | 23 |
24 /** | 24 /** |
25 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> | 25 * Implementation of JFreeChart's default implementation |
26 * <code>StandardChartTheme</code>. This class takes an xml document with a | |
27 * bunch of parameters and turns it into a <code>ChartTheme</code> to change | |
28 * the appearance of charts. | |
29 * | |
30 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
26 */ | 31 */ |
27 public class XMLChartTheme | 32 public class XMLChartTheme |
28 extends StandardChartTheme | 33 extends StandardChartTheme |
29 { | 34 { |
35 /** | |
36 * Default color. | |
37 */ | |
30 private static final Color DEFAULT_COLOR = Color.BLACK; | 38 private static final Color DEFAULT_COLOR = Color.BLACK; |
31 | 39 |
40 /** | |
41 * Logger used for logging with log4j. | |
42 */ | |
32 private Logger log = Logger.getLogger(XMLChartTheme.class); | 43 private Logger log = Logger.getLogger(XMLChartTheme.class); |
33 | 44 |
45 /** | |
46 * Field storing the visibility of the domain crosshair | |
47 */ | |
34 protected boolean domainCrosshairVisible; | 48 protected boolean domainCrosshairVisible; |
49 | |
50 /** | |
51 * Field storing the visibility of the range crosshair | |
52 */ | |
35 protected boolean rangeCrosshairVisible; | 53 protected boolean rangeCrosshairVisible; |
54 | |
55 /** | |
56 * Field storing the visiblity of lines. | |
57 */ | |
36 protected boolean renderLines; | 58 protected boolean renderLines; |
59 | |
60 /** | |
61 * Field storing the visibility of data points | |
62 */ | |
37 protected boolean renderShapes; | 63 protected boolean renderShapes; |
38 | 64 |
65 /** | |
66 * Field storing the width of a data point | |
67 */ | |
39 protected int pointWidth; | 68 protected int pointWidth; |
69 | |
70 /** | |
71 * Field storing the height of a data point. | |
72 */ | |
40 protected int pointHeight; | 73 protected int pointHeight; |
41 | 74 |
75 /** | |
76 * Field storing the base color of a bin in a histogram chart | |
77 */ | |
42 protected Paint histogramBasePaint; | 78 protected Paint histogramBasePaint; |
43 | 79 |
44 | 80 |
81 /** | |
82 * Constructor | |
83 * | |
84 * @param name Name for this theme. | |
85 */ | |
45 public XMLChartTheme(String name) { | 86 public XMLChartTheme(String name) { |
46 super(name); | 87 super(name); |
47 } | 88 } |
48 | 89 |
49 | 90 |
91 /** | |
92 * Setter method for the visibility of the domain crosshair. | |
93 * | |
94 * @param visible True, if domain crosshair should be visible | |
95 */ | |
50 public void setDomainCrosshairVisible(boolean visible) { | 96 public void setDomainCrosshairVisible(boolean visible) { |
51 this.domainCrosshairVisible = visible; | 97 this.domainCrosshairVisible = visible; |
52 } | 98 } |
53 | 99 |
54 | 100 |
101 /** | |
102 * Getter method for retrieving the visibility of the domain crosshair. | |
103 * | |
104 * @return Visibility of the domain crosshair. | |
105 */ | |
55 public boolean getDomainCrosshairVisible() { | 106 public boolean getDomainCrosshairVisible() { |
56 return domainCrosshairVisible; | 107 return domainCrosshairVisible; |
57 } | 108 } |
58 | 109 |
59 | 110 |
111 /** | |
112 * Getter method for retrieving the visibility of the range crosshair. | |
113 * | |
114 * @return Visibility of the range crosshair. | |
115 */ | |
60 public boolean getRangeCrosshairVisible() { | 116 public boolean getRangeCrosshairVisible() { |
61 return rangeCrosshairVisible; | 117 return rangeCrosshairVisible; |
62 } | 118 } |
63 | 119 |
64 | 120 |
121 /** | |
122 * Setter method for the visibility of the range crosshair. | |
123 * | |
124 * @param visible True, if range crosshair should be visible | |
125 */ | |
65 public void setRangeCrosshairVisible(boolean visible) { | 126 public void setRangeCrosshairVisible(boolean visible) { |
66 this.rangeCrosshairVisible = visible; | 127 this.rangeCrosshairVisible = visible; |
67 } | 128 } |
68 | 129 |
69 | 130 |
131 /** | |
132 * Method to set the bin color of histograms. | |
133 * | |
134 * @param c Bin color | |
135 */ | |
70 public void setHistogramBasePaint(Color c) { | 136 public void setHistogramBasePaint(Color c) { |
71 this.histogramBasePaint = c; | 137 this.histogramBasePaint = c; |
72 } | 138 } |
73 | 139 |
74 | 140 |
141 /** | |
142 * Getter method for retrieving the bin color. | |
143 * | |
144 * @return Bin color | |
145 */ | |
75 public Paint getHistogramBasePaint() { | 146 public Paint getHistogramBasePaint() { |
76 return histogramBasePaint; | 147 return histogramBasePaint; |
77 } | 148 } |
78 | 149 |
79 | 150 |
151 /** | |
152 * Take a given xml document and read the configuration of a chart | |
153 * appearance. | |
154 * | |
155 * @param document XML document | |
156 */ | |
80 public void applyXMLConfiguration(Document document) { | 157 public void applyXMLConfiguration(Document document) { |
81 log.debug("create XMLChartTheme"); | 158 log.debug("create XMLChartTheme"); |
82 | 159 |
83 init(document); | 160 init(document); |
84 } | 161 } |
85 | 162 |
86 | 163 |
164 /** | |
165 * Start parsing the different settings from <code>document</code>. | |
166 * | |
167 * @param document XML document | |
168 */ | |
87 private void init(Document document) { | 169 private void init(Document document) { |
88 log.debug("init XMLChartTheme parameters"); | 170 log.debug("init XMLChartTheme parameters"); |
89 | 171 |
90 initChartParameters(document); | 172 initChartParameters(document); |
91 initTitleParameters(document); | 173 initTitleParameters(document); |
96 initRenderer(document); | 178 initRenderer(document); |
97 initHistogramColor(document); | 179 initHistogramColor(document); |
98 } | 180 } |
99 | 181 |
100 | 182 |
183 /** | |
184 * Read parameters configuring the title of a chart. | |
185 * | |
186 * @param document XML document | |
187 */ | |
101 private void initTitleParameters(Document document) { | 188 private void initTitleParameters(Document document) { |
102 log.debug("init title parameters."); | 189 log.debug("init title parameters."); |
103 | 190 |
104 int size = getInt(document, "theme/title/font/size/@value"); | 191 int size = getInt(document, "theme/title/font/size/@value"); |
105 String type = getString(document, "theme/title/font/type/@value"); | 192 String type = getString(document, "theme/title/font/type/@value"); |
112 if (c != null) | 199 if (c != null) |
113 setTitlePaint(c); | 200 setTitlePaint(c); |
114 } | 201 } |
115 | 202 |
116 | 203 |
204 /** | |
205 * Read parameters configuring the subtitle of a chart. | |
206 * | |
207 * @param document XML document | |
208 */ | |
117 private void initSubtitleParameters(Document document) { | 209 private void initSubtitleParameters(Document document) { |
118 log.debug("init title parameters."); | 210 log.debug("init title parameters."); |
119 | 211 |
120 int size = getInt(document, "theme/subtitle/font/size/@value"); | 212 int size = getInt(document, "theme/subtitle/font/size/@value"); |
121 String type = getString(document, "theme/subtitle/font/type/@value"); | 213 String type = getString(document, "theme/subtitle/font/type/@value"); |
126 String col = getString(document, "theme/subtitle/font/color/@value"); | 218 String col = getString(document, "theme/subtitle/font/color/@value"); |
127 setSubtitlePaint(Color.decode(col)); | 219 setSubtitlePaint(Color.decode(col)); |
128 } | 220 } |
129 | 221 |
130 | 222 |
223 /** | |
224 * Read parameters configuring the background color of a chart. | |
225 * | |
226 * @param document XML document | |
227 */ | |
131 private void initChartParameters(Document document) { | 228 private void initChartParameters(Document document) { |
132 log.debug("init chart parameters."); | 229 log.debug("init chart parameters."); |
133 | 230 |
134 String bg = getString(document, "theme/chart/background/color/@value"); | 231 String bg = getString(document, "theme/chart/background/color/@value"); |
135 Color c = decodeColor(bg); | 232 Color c = decodeColor(bg); |
136 if (c != null) | 233 if (c != null) |
137 setChartBackgroundPaint(c); | 234 setChartBackgroundPaint(c); |
138 } | 235 } |
139 | 236 |
140 | 237 |
238 /** | |
239 * Read parameters configuring the plot of a chart. | |
240 * | |
241 * @param document XML document | |
242 */ | |
141 private void initPlotParameters(Document document) { | 243 private void initPlotParameters(Document document) { |
142 log.debug("init plot parameters."); | 244 log.debug("init plot parameters."); |
143 | 245 |
144 String tmp = null; | 246 String tmp = null; |
145 tmp = getString(document, "theme/plot/background/color/@value"); | 247 tmp = getString(document, "theme/plot/background/color/@value"); |
179 top, left, bottom, right) | 281 top, left, bottom, right) |
180 ); | 282 ); |
181 } | 283 } |
182 | 284 |
183 | 285 |
286 /** | |
287 * Read parameters configuring the axes of a plot. | |
288 * | |
289 * @param document XML document | |
290 */ | |
184 private void initAxisParameters(Document document) { | 291 private void initAxisParameters(Document document) { |
185 log.debug("init axis parameters."); | 292 log.debug("init axis parameters."); |
186 | 293 |
187 String tmp = null; | 294 String tmp = null; |
188 tmp = getString(document, "theme/axis/label/color/@value"); | 295 tmp = getString(document, "theme/axis/label/color/@value"); |
195 if (c != null) | 302 if (c != null) |
196 setTickLabelPaint(c); | 303 setTickLabelPaint(c); |
197 } | 304 } |
198 | 305 |
199 | 306 |
307 /** | |
308 * Read parameters configuring the legend of a chart. | |
309 * | |
310 * @param document XML document | |
311 */ | |
200 private void initLegendParameters(Document document) { | 312 private void initLegendParameters(Document document) { |
201 log.debug("init legend parameters."); | 313 log.debug("init legend parameters."); |
202 | 314 |
203 String tmp = null; | 315 String tmp = null; |
204 tmp = getString(document, "theme/legend/font/color/@value"); | 316 tmp = getString(document, "theme/legend/font/color/@value"); |
211 if (c != null) | 323 if (c != null) |
212 setLegendBackgroundPaint(c); | 324 setLegendBackgroundPaint(c); |
213 } | 325 } |
214 | 326 |
215 | 327 |
328 /** | |
329 * Read parameters configuring the renderer of a plot. | |
330 * | |
331 * @param document XML document | |
332 */ | |
216 private void initRenderer(Document document) { | 333 private void initRenderer(Document document) { |
217 log.debug("init renderer parameters."); | 334 log.debug("init renderer parameters."); |
218 | 335 |
219 pointWidth = getInt(document, "theme/plot/itemrenderer/width/@value"); | 336 pointWidth = getInt(document, "theme/plot/itemrenderer/width/@value"); |
220 log.debug("Read point width of " + pointWidth); | 337 log.debug("Read point width of " + pointWidth); |
227 document, "theme/plot/itemrenderer/renderPoints/@value" | 344 document, "theme/plot/itemrenderer/renderPoints/@value" |
228 ); | 345 ); |
229 } | 346 } |
230 | 347 |
231 | 348 |
349 /** | |
350 * Read base color of bins in histogram charts. | |
351 * | |
352 * @param document XML document | |
353 */ | |
232 private void initHistogramColor(Document document) { | 354 private void initHistogramColor(Document document) { |
233 log.debug("init histogram color"); | 355 log.debug("init histogram color"); |
234 String tmp = getString(document, "theme/histogram/bar/color/@value"); | 356 String tmp = getString(document, "theme/histogram/bar/color/@value"); |
235 Color c = decodeColor(tmp); | 357 Color c = decodeColor(tmp); |
236 | 358 |
237 if (c != null) | 359 if (c != null) |
238 setHistogramBasePaint(c); | 360 setHistogramBasePaint(c); |
239 } | 361 } |
240 | 362 |
241 | 363 |
364 /** | |
365 * Read a xpath expression and return the matched string. | |
366 * | |
367 * @param document Document | |
368 * @param xpath XPath expression | |
369 * | |
370 * @return Matched string | |
371 */ | |
242 private static String getString(Document document, String xpath) { | 372 private static String getString(Document document, String xpath) { |
243 return Config.getStringXPath(document, xpath); | 373 return Config.getStringXPath(document, xpath); |
244 } | 374 } |
245 | 375 |
246 | 376 |
377 /** | |
378 * Read a xpath and turn it into an integer. | |
379 * | |
380 * @param document Document | |
381 * @param xpath XPath expression | |
382 * | |
383 * @return Matched string as integer representation. Return 0 if no integer | |
384 * have been found at <code>xpath</code>. | |
385 */ | |
247 private static int getInt(Document document, String xpath) { | 386 private static int getInt(Document document, String xpath) { |
248 String tmp = getString(document, xpath); | 387 String tmp = getString(document, xpath); |
249 | 388 |
250 if (tmp != null) | 389 if (tmp != null) |
251 return Integer.parseInt(tmp); | 390 return Integer.parseInt(tmp); |
252 else | 391 else |
253 return 0; | 392 return 0; |
254 } | 393 } |
255 | 394 |
256 | 395 |
396 /** | |
397 * Read a xpath and turn it into a boolean. | |
398 * | |
399 * @param document Document | |
400 * @param xpath XPath expression | |
401 * | |
402 * @return Matched string as boolean representation. Return false if no | |
403 * boolean have been found at <code>xpath</code>. | |
404 */ | |
257 private static boolean getBool(Document document, String xpath) { | 405 private static boolean getBool(Document document, String xpath) { |
258 String tmp = getString(document, xpath); | 406 String tmp = getString(document, xpath); |
259 | 407 |
260 if (tmp != null) | 408 if (tmp != null) |
261 return Boolean.parseBoolean(tmp); | 409 return Boolean.parseBoolean(tmp); |
262 else | 410 else |
263 return false; | 411 return false; |
264 } | 412 } |
265 | 413 |
266 | 414 |
415 /** | |
416 * Turns a string into a color using {@link java.awt.Color}. | |
417 * | |
418 * @param Color as string | |
419 * | |
420 * @return Color | |
421 */ | |
267 protected Color decodeColor(String color) { | 422 protected Color decodeColor(String color) { |
268 try { | 423 try { |
269 if (color == null) | 424 if (color == null) |
270 return null; | 425 return null; |
271 | 426 |
277 | 432 |
278 return null; | 433 return null; |
279 } | 434 } |
280 | 435 |
281 | 436 |
437 /** | |
438 * Create a font with the given parameters. | |
439 * | |
440 * @param type Font type | |
441 * @param size Font size | |
442 * @param bold Font weight | |
443 * | |
444 * @return Font | |
445 */ | |
282 protected Font createFont(String type, int size, boolean bold) { | 446 protected Font createFont(String type, int size, boolean bold) { |
283 Font font = null; | 447 Font font = null; |
284 if (bold) | 448 if (bold) |
285 font = new Font(type, Font.BOLD, size); | 449 font = new Font(type, Font.BOLD, size); |
286 else | 450 else |
288 | 452 |
289 return font; | 453 return font; |
290 } | 454 } |
291 | 455 |
292 | 456 |
457 /** | |
458 * Apply settings of this <code>ChartTheme</code> to the given | |
459 * <code>XYPlot</code>. | |
460 * | |
461 * @param plot XYPlot | |
462 */ | |
293 protected void applyToXYPlot(XYPlot plot) { | 463 protected void applyToXYPlot(XYPlot plot) { |
294 log.debug("apply theme parameter to XYPlot"); | 464 log.debug("apply theme parameter to XYPlot"); |
295 | 465 |
296 super.applyToXYPlot(plot); | 466 super.applyToXYPlot(plot); |
297 plot.setDomainCrosshairVisible(this.domainCrosshairVisible); | 467 plot.setDomainCrosshairVisible(this.domainCrosshairVisible); |
306 if (renderer instanceof XYBarRenderer) | 476 if (renderer instanceof XYBarRenderer) |
307 applyToXYBarRenderer(plot); | 477 applyToXYBarRenderer(plot); |
308 } | 478 } |
309 | 479 |
310 | 480 |
481 /** | |
482 * Apply settings of this <code>ChartTheme</code> to the | |
483 * <code>XYLineAndShapeRenderer</code> of the given <code>XYPlot</code>. | |
484 * | |
485 * @param plot XYPlot | |
486 */ | |
311 protected void applyToXYLineAndShapeRenderer(XYPlot plot) { | 487 protected void applyToXYLineAndShapeRenderer(XYPlot plot) { |
312 if (plot == null) | 488 if (plot == null) |
313 return; | 489 return; |
314 | 490 |
315 XYLineAndShapeRenderer renderer = | 491 XYLineAndShapeRenderer renderer = |
325 | 501 |
326 plot.setRenderer(renderer); | 502 plot.setRenderer(renderer); |
327 } | 503 } |
328 | 504 |
329 | 505 |
506 /** | |
507 * Apply settings of this <code>ChartTheme</code> to the | |
508 * <code>XYBarRenderer</code> of the given <code>XYPlot</code>. | |
509 * | |
510 * @param plot XYPlot | |
511 */ | |
330 protected void applyToXYBarRenderer(XYPlot plot) { | 512 protected void applyToXYBarRenderer(XYPlot plot) { |
331 if (plot == null) | 513 if (plot == null) |
332 return; | 514 return; |
333 | 515 |
334 XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer(); | 516 XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer(); |