comparison gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java @ 1087:92fce3b3d07f

Centered histograms in pdf exports. gnv-artifacts/trunk@1189 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 10 Jun 2010 09:23:33 +0000
parents f2127cd0fe31
children f953c9a559d8
comparison
equal deleted inserted replaced
1086:f2127cd0fe31 1087:92fce3b3d07f
357 357
358 if (pageFormat == null) 358 if (pageFormat == null)
359 pageFormat = DEFAULT_PAGE_SIZE; 359 pageFormat = DEFAULT_PAGE_SIZE;
360 360
361 Rectangle page = PageSize.getRectangle(pageFormat); 361 Rectangle page = PageSize.getRectangle(pageFormat);
362 int pageWidth = 362 float pageWidth = page.getWidth();
363 (int) (page.getRight(marginRight) - page.getLeft(marginLeft)); 363 float pageHeight = page.getHeight();
364 int pageHeight =
365 (int) (page.getTop(marginTop) - page.getBottom(marginBottom));
366 364
367 // the chart width 365 // the chart width
368 int chartWidth = (Integer) context.getContextValue("chart.width"); 366 int chartWidth = (Integer) context.getContextValue("chart.width");
369 int chartHeight = (Integer) context.getContextValue("chart.height"); 367 int chartHeight = (Integer) context.getContextValue("chart.height");
370 368
371 boolean landscape = chartWidth > chartHeight ? true : false; 369 boolean landscape = chartWidth > chartHeight ? true : false;
372 370
373 int width = 0; 371 float width = 0;
374 int height = 0; 372 float height = 0;
375 if (landscape) { 373 if (landscape) {
376 width = pageHeight; 374 width = pageHeight;
377 height = pageWidth; 375 height = pageWidth;
378 } 376 }
379 else { 377 else {
380 width = pageWidth; 378 width = pageWidth;
381 height = pageHeight; 379 height = pageHeight;
382 } 380 }
383 381
384 if (chartWidth > width) { 382 float spaceX = width - marginLeft - marginRight;
383 if (chartWidth > spaceX) {
385 log.warn("Histogram width is too big for pdf -> resize it now."); 384 log.warn("Histogram width is too big for pdf -> resize it now.");
386 double ratio = ((double)width) / chartWidth; 385 double ratio = ((double)spaceX) / chartWidth;
387 chartWidth *= ratio; 386 chartWidth *= ratio;
388 chartHeight *= ratio; 387 chartHeight *= ratio;
389 log.debug("Resized histogram to " + chartWidth + "x" + chartHeight); 388 log.debug("Resized chart to " + chartWidth + "x" + chartHeight);
390 } 389 }
391 390
392 if (chartHeight > height) { 391 float spaceY = height - marginTop - marginBottom;
392 if (chartHeight > spaceY) {
393 log.warn("Histogram height is too big for pdf -> resize it now."); 393 log.warn("Histogram height is too big for pdf -> resize it now.");
394 double ratio = ((double)height) / chartHeight; 394 double ratio = ((double)spaceY) / chartHeight;
395 chartWidth *= ratio; 395 chartWidth *= ratio;
396 chartHeight *= ratio; 396 chartHeight *= ratio;
397 log.debug("Resized histogram to " + chartWidth + "x" + chartHeight); 397 log.debug("Resized chart to " + chartWidth + "x" + chartHeight);
398 } 398 }
399 399
400 Document document = null; 400 Document document = null;
401 if (landscape) { 401 if (landscape) {
402 document = new Document(page.rotate()); 402 document = new Document(page.rotate());
419 document.newPage(); 419 document.newPage();
420 } 420 }
421 421
422 JFreeChart chart = histograms[i].generateChart(); 422 JFreeChart chart = histograms[i].generateChart();
423 PdfTemplate template = content.createTemplate(width, height); 423 PdfTemplate template = content.createTemplate(width, height);
424 Graphics2D graphics = template.createGraphics( 424 Graphics2D graphics = template.createGraphics(width, height);
425
426 double[] origin = getCenteredAnchor(
427 marginLeft, marginRight, marginBottom, marginTop,
428 width, height,
425 chartWidth, chartHeight); 429 chartWidth, chartHeight);
430
426 Rectangle2D area = new Rectangle2D.Double( 431 Rectangle2D area = new Rectangle2D.Double(
427 0.0D, 0.0D, chartWidth, chartHeight); 432 origin[0], origin[1], chartWidth, chartHeight);
428 433
429 chart.draw(graphics, area); 434 chart.draw(graphics, area);
430 graphics.dispose(); 435 graphics.dispose();
431 content.addTemplate(template, marginLeft, marginBottom); 436 content.addTemplate(template, 0f, 0f);
432 } 437 }
433 } 438 }
434 catch (DocumentException de) { 439 catch (DocumentException de) {
435 log.error("Error while exporting histogram to pdf.", de); 440 log.error("Error while exporting histogram to pdf.", de);
436 } 441 }

http://dive4elements.wald.intevation.org