comparison gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java @ 1053:6169ddc827ac

The format of a histogram pdf depends on the width and height specified by the user - or the default size (issue290). gnv-artifacts/trunk@1127 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 26 May 2010 07:00:55 +0000
parents b30361464775
children 0318fda0001e
comparison
equal deleted inserted replaced
1052:b30361464775 1053:6169ddc827ac
337 */ 337 */
338 public static void exportHistogramsAsPDF( 338 public static void exportHistogramsAsPDF(
339 OutputStream out, 339 OutputStream out,
340 Chart[] histograms, 340 Chart[] histograms,
341 String pageFormat, 341 String pageFormat,
342 boolean landscape,
343 float marginLeft, 342 float marginLeft,
344 float marginRight, 343 float marginRight,
345 float marginTop, 344 float marginTop,
346 float marginBottom 345 float marginBottom,
346 CallContext context
347 ) { 347 ) {
348 log.info("export histogram as pdf."); 348 log.info("export histogram as pdf.");
349 349
350 if (pageFormat == null) 350 if (pageFormat == null)
351 pageFormat = DEFAULT_PAGE_SIZE; 351 pageFormat = DEFAULT_PAGE_SIZE;
354 int pageWidth = 354 int pageWidth =
355 (int) (page.getRight(marginRight) - page.getLeft(marginLeft)); 355 (int) (page.getRight(marginRight) - page.getLeft(marginLeft));
356 int pageHeight = 356 int pageHeight =
357 (int) (page.getTop(marginTop) - page.getBottom(marginBottom)); 357 (int) (page.getTop(marginTop) - page.getBottom(marginBottom));
358 358
359 // the chart width
360 int chartWidth = (Integer) context.getContextValue("chart.width");
361 int chartHeight = (Integer) context.getContextValue("chart.height");
362
363 boolean landscape = chartWidth > chartHeight ? true : false;
364
365 int width = 0;
366 int height = 0;
367 if (landscape) {
368 width = pageHeight;
369 height = pageWidth;
370 }
371 else {
372 width = pageWidth;
373 height = pageHeight;
374 }
375
376 if (chartWidth > width) {
377 log.warn("Histogram width is too big for pdf -> resize it now.");
378 double ratio = ((double)width) / chartWidth;
379 chartWidth *= ratio;
380 chartHeight *= ratio;
381 log.debug("Resized histogram to " + chartWidth + "x" + chartHeight);
382 }
383
384 if (chartHeight > height) {
385 log.warn("Histogram height is too big for pdf -> resize it now.");
386 double ratio = ((double)height) / chartHeight;
387 chartWidth *= ratio;
388 chartHeight *= ratio;
389 log.debug("Resized histogram to " + chartWidth + "x" + chartHeight);
390 }
391
359 Document document = null; 392 Document document = null;
360 if (landscape) { 393 if (landscape) {
361 document = new Document(page.rotate()); 394 document = new Document(page.rotate());
362 log.debug("Create landscape pdf."); 395 log.debug("Create landscape pdf.");
363 } 396 }
369 402
370 document.addCreationDate(); 403 document.addCreationDate();
371 document.open(); 404 document.open();
372 405
373 PdfContentByte content = writer.getDirectContent(); 406 PdfContentByte content = writer.getDirectContent();
374
375 int width = 0;
376 int height = 0;
377 if (landscape) {
378 width = pageHeight;
379 height = pageWidth;
380 }
381 else {
382 width = pageWidth;
383 height = pageHeight;
384 }
385 407
386 int size = histograms.length; 408 int size = histograms.length;
387 for (int i = 0; i < size; i++) { 409 for (int i = 0; i < size; i++) {
388 if (i > 0) { 410 if (i > 0) {
389 document.newPage(); 411 document.newPage();
390 } 412 }
391 413
392 JFreeChart chart = histograms[i].generateChart(); 414 JFreeChart chart = histograms[i].generateChart();
393 PdfTemplate template = content.createTemplate(width, height); 415 PdfTemplate template = content.createTemplate(width, height);
394 Graphics2D graphics = template.createGraphics(width, height); 416 Graphics2D graphics = template.createGraphics(
417 chartWidth, chartHeight);
395 Rectangle2D area = new Rectangle2D.Double( 418 Rectangle2D area = new Rectangle2D.Double(
396 0.0D, 0.0D,width,height); 419 0.0D, 0.0D,width,height);
397 420
398 chart.draw(graphics, area); 421 chart.draw(graphics, area);
399 graphics.dispose(); 422 graphics.dispose();
400 content.addTemplate(template, marginLeft, marginBottom); 423 content.addTemplate(template, marginLeft, marginBottom);
401 } 424 }
402 } 425 }
403 catch (DocumentException de) { 426 catch (DocumentException de) {
404 log.error("Error while exporting chart to pdf.", de); 427 log.error("Error while exporting histogram to pdf.", de);
405 } 428 }
406 finally { 429 finally {
407 document.close(); 430 document.close();
408 } 431 }
409 } 432 }

http://dive4elements.wald.intevation.org