Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java @ 1086:f2127cd0fe31
Centered charts in a pdf exports.
gnv-artifacts/trunk@1188 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 10 Jun 2010 09:05:58 +0000 |
parents | 0318fda0001e |
children | 92fce3b3d07f |
comparison
equal
deleted
inserted
replaced
1085:ec512e7992c6 | 1086:f2127cd0fe31 |
---|---|
246 if (pageFormat == null) | 246 if (pageFormat == null) |
247 pageFormat = DEFAULT_PAGE_SIZE; | 247 pageFormat = DEFAULT_PAGE_SIZE; |
248 | 248 |
249 // max size of the chart | 249 // max size of the chart |
250 Rectangle page = PageSize.getRectangle(pageFormat); | 250 Rectangle page = PageSize.getRectangle(pageFormat); |
251 int pageWidth = (int) (page.getRight(marginRight) - page.getLeft(marginLeft)); | 251 float pageWidth = page.getWidth(); |
252 int pageHeight = (int) (page.getTop(marginTop) - page.getBottom(marginBottom)); | 252 float pageHeight = page.getHeight(); |
253 | 253 |
254 // the chart width | 254 // the chart width |
255 int chartWidth = (Integer) context.getContextValue("chart.width"); | 255 int chartWidth = (Integer) context.getContextValue("chart.width"); |
256 int chartHeight = (Integer) context.getContextValue("chart.height"); | 256 int chartHeight = (Integer) context.getContextValue("chart.height"); |
257 | 257 |
258 boolean landscape = chartWidth > chartHeight ? true : false; | 258 boolean landscape = chartWidth > chartHeight ? true : false; |
259 | 259 |
260 int width = 0; | 260 float width = 0; |
261 int height = 0; | 261 float height = 0; |
262 if (landscape) { | 262 if (landscape) { |
263 width = pageHeight; | 263 width = pageHeight; |
264 height = pageWidth; | 264 height = pageWidth; |
265 } | 265 } |
266 else { | 266 else { |
267 width = pageWidth; | 267 width = pageWidth; |
268 height = pageHeight; | 268 height = pageHeight; |
269 } | 269 } |
270 | 270 |
271 if (chartWidth > width) { | 271 float spaceX = width - marginLeft - marginRight; |
272 if (chartWidth > spaceX) { | |
272 log.warn("Width of the chart is too big for pdf -> resize it now."); | 273 log.warn("Width of the chart is too big for pdf -> resize it now."); |
273 double ratio = ((double)width) / chartWidth; | 274 double ratio = ((double)spaceX) / chartWidth; |
274 chartWidth *= ratio; | 275 chartWidth *= ratio; |
275 chartHeight *= ratio; | 276 chartHeight *= ratio; |
276 log.debug("Resized chart to " + chartWidth + "x" + chartHeight); | 277 log.debug("Resized chart to " + chartWidth + "x" + chartHeight); |
277 } | 278 } |
278 | 279 |
279 if (chartHeight > height) { | 280 float spaceY = height - marginTop - marginBottom; |
281 if (chartHeight > spaceY) { | |
280 log.warn("Height of the chart is too big for pdf -> resize it now."); | 282 log.warn("Height of the chart is too big for pdf -> resize it now."); |
281 double ratio = ((double)height) / chartHeight; | 283 double ratio = ((double)spaceY) / chartHeight; |
282 chartWidth *= ratio; | 284 chartWidth *= ratio; |
283 chartHeight *= ratio; | 285 chartHeight *= ratio; |
284 log.debug("Resized chart to " + chartWidth + "x" + chartHeight); | 286 log.debug("Resized chart to " + chartWidth + "x" + chartHeight); |
285 } | 287 } |
286 | 288 |
300 document.open(); | 302 document.open(); |
301 | 303 |
302 PdfContentByte content = writer.getDirectContent(); | 304 PdfContentByte content = writer.getDirectContent(); |
303 | 305 |
304 PdfTemplate template = content.createTemplate(width, height); | 306 PdfTemplate template = content.createTemplate(width, height); |
305 Graphics2D graphics = template.createGraphics( | 307 Graphics2D graphics = template.createGraphics(width, height); |
308 | |
309 double[] origin = getCenteredAnchor( | |
310 marginLeft, marginRight, marginBottom, marginTop, | |
311 width, height, | |
306 chartWidth, chartHeight); | 312 chartWidth, chartHeight); |
313 | |
307 Rectangle2D area = new Rectangle2D.Double( | 314 Rectangle2D area = new Rectangle2D.Double( |
308 0.0D, 0.0D, chartWidth, chartHeight); | 315 origin[0], origin[1], chartWidth, chartHeight); |
309 | 316 |
310 chart.draw(graphics, area); | 317 chart.draw(graphics, area); |
311 graphics.dispose(); | 318 graphics.dispose(); |
312 content.addTemplate(template, marginLeft, marginBottom); | 319 content.addTemplate(template, 0f, 0f); |
313 } | 320 } |
314 catch (DocumentException de) { | 321 catch (DocumentException de) { |
315 log.error("Error while exporting chart to pdf.", de); | 322 log.error("Error while exporting chart to pdf.", de); |
316 } | 323 } |
317 finally { | 324 finally { |
429 } | 436 } |
430 finally { | 437 finally { |
431 document.close(); | 438 document.close(); |
432 } | 439 } |
433 } | 440 } |
441 | |
442 | |
443 /** | |
444 * This method returns the anchor of the chart so that the chart is centered | |
445 * according to the given parameters. | |
446 * | |
447 * @param mLeft Left margin | |
448 * @param mRight Right margin | |
449 * @param mBottom Bottom margin | |
450 * @param mTop Top margin | |
451 * @param width The complete width of the drawing area. | |
452 * @param height The complete height of the drawing area. | |
453 * @param chartWidth The width of the chart. | |
454 * @param chartHeight The height of the chart. | |
455 * | |
456 * @return an array that contains the anchor for a chart with the given | |
457 * parameters. The first value is the x point, the second value is the y | |
458 * point. | |
459 */ | |
460 public static double[] getCenteredAnchor( | |
461 double mLeft, double mRight, double mBottom, double mTop, | |
462 double width, double height, | |
463 double chartWidth, double chartHeight | |
464 ) { | |
465 if (log.isDebugEnabled()) { | |
466 log.debug("Calculate centered origin..."); | |
467 log.debug("-> PDF width : " + width); | |
468 log.debug("-> PDF height : " + height); | |
469 log.debug("-> Chart width : " + chartWidth); | |
470 log.debug("-> Chart height : " + chartHeight); | |
471 log.debug("-> margin left : " + mLeft); | |
472 log.debug("-> margin right : " + mRight); | |
473 log.debug("-> margin bottom: " + mBottom); | |
474 log.debug("-> margin top : " + mTop); | |
475 } | |
476 | |
477 double[] origin = new double[2]; | |
478 | |
479 double centerX = width / 2; | |
480 double centerY = height / 2; | |
481 | |
482 origin[0] = centerX - chartWidth / 2; | |
483 origin[1] = centerY - chartHeight / 2; | |
484 | |
485 origin[0] = origin[0] >= mLeft ? origin[0] : mLeft; | |
486 origin[1] = origin[1] >= mTop ? origin[1] : mTop; | |
487 | |
488 if (log.isDebugEnabled()) { | |
489 log.debug("==> centered left origin: " + origin[0]); | |
490 log.debug("==> centered top origin: " + origin[1]); | |
491 } | |
492 | |
493 return origin; | |
494 } | |
434 } | 495 } |
435 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | 496 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |