Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 2234:46ec09c7f578
Refactoring: moved more base code from XYChartGenerator into its parent class ChartGenerator.
flys-artifacts/trunk@3878 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 02 Feb 2012 12:50:33 +0000 |
parents | 958a10e2e246 |
children | c2b15d9c0f43 |
comparison
equal
deleted
inserted
replaced
2233:958a10e2e246 | 2234:46ec09c7f578 |
---|---|
186 protected Map<Integer, Range> xRanges; | 186 protected Map<Integer, Range> xRanges; |
187 | 187 |
188 /** The max Y range to include all Y values of all series for each axis. */ | 188 /** The max Y range to include all Y values of all series for each axis. */ |
189 protected Map<Integer, Range> yRanges; | 189 protected Map<Integer, Range> yRanges; |
190 | 190 |
191 public static final Color DEFAULT_GRID_COLOR = Color.GRAY; | |
192 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; | |
193 public static final int DEFAULT_FONT_SIZE = 12; | |
194 public static final String DEFAULT_FONT_NAME = "Tahoma"; | |
195 | |
196 | 191 |
197 public XYChartGenerator() { | 192 public XYChartGenerator() { |
198 xRanges = new HashMap<Integer, Range>(); | 193 xRanges = new HashMap<Integer, Range>(); |
199 yRanges = new HashMap<Integer, Range>(); | 194 yRanges = new HashMap<Integer, Range>(); |
200 datasets = new TreeMap<Integer, AxisDataset>(); | 195 datasets = new TreeMap<Integer, AxisDataset>(); |
201 } | |
202 | |
203 | |
204 /** | |
205 * Returns the title of a chart. The return value depends on the existence | |
206 * of ChartSettings: if there are ChartSettings set, this method returns the | |
207 * chart title provided by those settings. Otherwise, this method returns | |
208 * getDefaultChartTitle(). | |
209 * | |
210 * @return the title of a chart. | |
211 */ | |
212 protected String getChartTitle() { | |
213 ChartSettings chartSettings = getChartSettings(); | |
214 | |
215 if (chartSettings != null) { | |
216 return getChartTitle(chartSettings); | |
217 } | |
218 | |
219 return getDefaultChartTitle(); | |
220 } | |
221 | |
222 | |
223 protected abstract String getDefaultChartTitle(); | |
224 | |
225 | |
226 /** | |
227 * Returns the subtitle of a chart. The return value depends on the | |
228 * existence of ChartSettings: if there are ChartSettings set, this method | |
229 * returns the chart title provided by those settings. Otherwise, this | |
230 * method returns getDefaultChartSubtitle(). | |
231 * | |
232 * @return the subtitle of a chart. | |
233 */ | |
234 protected String getChartSubtitle() { | |
235 ChartSettings chartSettings = getChartSettings(); | |
236 | |
237 if (chartSettings != null) { | |
238 return getChartSubtitle(chartSettings); | |
239 } | |
240 | |
241 return getDefaultChartSubtitle(); | |
242 } | |
243 | |
244 | |
245 /** | |
246 * This method always returns null. Override it in subclasses that require | |
247 * subtitles. | |
248 * | |
249 * @return null. | |
250 */ | |
251 protected String getDefaultChartSubtitle() { | |
252 // Override this method in subclasses | |
253 return null; | |
254 } | |
255 | |
256 | |
257 /** | |
258 * This method is used to determine, if the chart's legend is visible or | |
259 * not. If a <i>settings</i> instance is set, this instance determines the | |
260 * visibility otherwise, this method returns true as default if no | |
261 * <i>settings</i> is set. | |
262 * | |
263 * @return true, if the legend should be visible, otherwise false. | |
264 */ | |
265 protected boolean isLegendVisible() { | |
266 ChartSettings chartSettings = getChartSettings(); | |
267 if (chartSettings != null) { | |
268 return isLegendVisible(chartSettings); | |
269 } | |
270 | |
271 return true; | |
272 } | |
273 | |
274 | |
275 /** | |
276 * This method is used to determine the font size of the chart's legend. If | |
277 * a <i>settings</i> instance is set, this instance determines the font | |
278 * size, otherwise this method returns 12 as default if no <i>settings</i> | |
279 * is set or if it doesn't provide a legend font size. | |
280 * | |
281 * @return a legend font size. | |
282 */ | |
283 protected int getLegendFontSize() { | |
284 Integer fontSize = null; | |
285 | |
286 ChartSettings chartSettings = getChartSettings(); | |
287 if (chartSettings != null) { | |
288 fontSize = getLegendFontSize(chartSettings); | |
289 } | |
290 | |
291 return fontSize != null ? fontSize : DEFAULT_FONT_SIZE; | |
292 } | |
293 | |
294 | |
295 /** | |
296 * This method is used to determine if the resulting chart should display | |
297 * grid lines or not. <b>Note: this method always returns true!</b> | |
298 * | |
299 * @return true, if the chart should display grid lines, otherwise false. | |
300 */ | |
301 protected boolean isGridVisible() { | |
302 return true; | |
303 } | |
304 | |
305 | |
306 /** | |
307 * Returns the X-Axis label of a chart. | |
308 * | |
309 * @return the X-Axis label of a chart. | |
310 */ | |
311 protected String getXAxisLabel() { | |
312 ChartSettings chartSettings = getChartSettings(); | |
313 if (chartSettings == null) { | |
314 return getDefaultXAxisLabel(); | |
315 } | |
316 | |
317 AxisSection as = chartSettings.getAxisSection("X"); | |
318 if (as != null) { | |
319 String label = as.getLabel(); | |
320 | |
321 if (label != null) { | |
322 return label; | |
323 } | |
324 } | |
325 | |
326 return getDefaultXAxisLabel(); | |
327 } | |
328 | |
329 | |
330 /** | |
331 * Returns the default X-Axis label of a chart. | |
332 * | |
333 * @return the default X-Axis label of a chart. | |
334 */ | |
335 protected abstract String getDefaultXAxisLabel(); | |
336 | |
337 | |
338 /** | |
339 * This method is called to retrieve the default label for an Y axis at | |
340 * position <i>pos</i>. | |
341 * | |
342 * @param pos The position of an Y axis. | |
343 * | |
344 * @return the default Y axis label at position <i>pos</i>. | |
345 */ | |
346 protected abstract String getDefaultYAxisLabel(int pos); | |
347 | |
348 | |
349 /** | |
350 * This method returns the font size for the X axis. If the font size is | |
351 * specified in ChartSettings (if <i>chartSettings</i> is set), this size is | |
352 * returned. Otherwise the default font size 12 is returned. | |
353 * | |
354 * @return the font size for the x axis. | |
355 */ | |
356 protected int getXAxisLabelFontSize() { | |
357 ChartSettings chartSettings = getChartSettings(); | |
358 if (chartSettings == null) { | |
359 return DEFAULT_FONT_SIZE; | |
360 } | |
361 | |
362 AxisSection as = chartSettings.getAxisSection("X"); | |
363 Integer fontSize = as.getFontSize(); | |
364 | |
365 return fontSize != null ? fontSize : DEFAULT_FONT_SIZE; | |
366 } | |
367 | |
368 | |
369 /** | |
370 * This method returns the font size for an Y axis. If the font size is | |
371 * specified in ChartSettings (if <i>chartSettings</i> is set), this size is | |
372 * returned. Otherwise the default font size 12 is returned. | |
373 * | |
374 * @return the font size for the x axis. | |
375 */ | |
376 protected int getYAxisFontSize(int pos) { | |
377 ChartSettings chartSettings = getChartSettings(); | |
378 if (chartSettings == null) { | |
379 return DEFAULT_FONT_SIZE; | |
380 } | |
381 | |
382 YAxisWalker walker = getYAxisWalker(); | |
383 | |
384 AxisSection as = chartSettings.getAxisSection(walker.getId(pos)); | |
385 Integer fontSize = as.getFontSize(); | |
386 | |
387 return fontSize != null ? fontSize : DEFAULT_FONT_SIZE; | |
388 } | |
389 | |
390 | |
391 /** | |
392 * This method returns the export dimension specified in ChartSettings as | |
393 * int array [width,height]. | |
394 * | |
395 * @return an int array with [width,height]. | |
396 */ | |
397 protected int[] getExportDimension() { | |
398 ChartSettings chartSettings = getChartSettings(); | |
399 if (chartSettings == null) { | |
400 return new int[] { 600, 400 }; | |
401 } | |
402 | |
403 ExportSection export = chartSettings.getExportSection(); | |
404 Integer width = export.getWidth(); | |
405 Integer height = export.getHeight(); | |
406 | |
407 if (width != null && height != null) { | |
408 return new int[] { width, height }; | |
409 } | |
410 | |
411 return new int[] { 600, 400 }; | |
412 } | |
413 | |
414 | |
415 /** | |
416 * Generate chart. | |
417 */ | |
418 public void generate() | |
419 throws IOException | |
420 { | |
421 logger.debug("XYChartGenerator.generate"); | |
422 | |
423 JFreeChart chart = generateChart(); | |
424 | |
425 String format = getFormat(); | |
426 int[] size = getSize(); | |
427 | |
428 if (size == null) { | |
429 size = getExportDimension(); | |
430 } | |
431 | |
432 context.putContextValue("chart.width", size[0]); | |
433 context.putContextValue("chart.height", size[1]); | |
434 | |
435 if (format.equals(ChartExportHelper.FORMAT_PNG)) { | |
436 context.putContextValue("chart.image.format", "png"); | |
437 | |
438 ChartExportHelper.exportImage( | |
439 out, | |
440 chart, | |
441 context); | |
442 } | |
443 else if (format.equals(ChartExportHelper.FORMAT_PDF)) { | |
444 preparePDFContext(context); | |
445 | |
446 ChartExportHelper.exportPDF( | |
447 out, | |
448 chart, | |
449 context); | |
450 } | |
451 else if (format.equals(ChartExportHelper.FORMAT_SVG)) { | |
452 prepareSVGContext(context); | |
453 | |
454 ChartExportHelper.exportSVG( | |
455 out, | |
456 chart, | |
457 context); | |
458 } | |
459 else if (format.equals(ChartExportHelper.FORMAT_CSV)) { | |
460 context.putContextValue("chart.image.format", "csv"); | |
461 | |
462 ChartExportHelper.exportCSV( | |
463 out, | |
464 chart, | |
465 context); | |
466 } | |
467 } | 196 } |
468 | 197 |
469 | 198 |
470 /** | 199 /** |
471 * Generate the chart anew (including localized axis and all). | 200 * Generate the chart anew (including localized axis and all). |
506 | 235 |
507 // These have to go after the autozoom. | 236 // These have to go after the autozoom. |
508 addAnnotationsToRenderer(plot); | 237 addAnnotationsToRenderer(plot); |
509 | 238 |
510 return chart; | 239 return chart; |
511 } | |
512 | |
513 | |
514 protected void preparePDFContext(CallContext context) { | |
515 int[] dimension = getExportDimension(); | |
516 | |
517 context.putContextValue("chart.width", dimension[0]); | |
518 context.putContextValue("chart.height", dimension[1]); | |
519 context.putContextValue("chart.marginLeft", 5f); | |
520 context.putContextValue("chart.marginRight", 5f); | |
521 context.putContextValue("chart.marginTop", 5f); | |
522 context.putContextValue("chart.marginBottom", 5f); | |
523 context.putContextValue( | |
524 "chart.page.format", | |
525 ChartExportHelper.DEFAULT_PAGE_SIZE); | |
526 } | |
527 | |
528 | |
529 protected void prepareSVGContext(CallContext context) { | |
530 int[] dimension = getExportDimension(); | |
531 | |
532 context.putContextValue("chart.width", dimension[0]); | |
533 context.putContextValue("chart.height", dimension[1]); | |
534 context.putContextValue( | |
535 "chart.encoding", | |
536 ChartExportHelper.DEFAULT_ENCODING); | |
537 } | 240 } |
538 | 241 |
539 | 242 |
540 /** | 243 /** |
541 * Put debug output about datasets. | 244 * Put debug output about datasets. |