comparison gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java @ 1050:7f3154331bc1

Use the chart size/height to export it to pdf. The chart's aspect ratio keeps alive if the size exceeds the maximum page size (issue290). gnv-artifacts/trunk@1124 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 25 May 2010 15:41:31 +0000
parents 22c18083225e
children b30361464775
comparison
equal deleted inserted replaced
1049:778d86255d76 1050:7f3154331bc1
6 import com.lowagie.text.Rectangle; 6 import com.lowagie.text.Rectangle;
7 7
8 import com.lowagie.text.pdf.PdfContentByte; 8 import com.lowagie.text.pdf.PdfContentByte;
9 import com.lowagie.text.pdf.PdfTemplate; 9 import com.lowagie.text.pdf.PdfTemplate;
10 import com.lowagie.text.pdf.PdfWriter; 10 import com.lowagie.text.pdf.PdfWriter;
11
12 import de.intevation.artifacts.CallContext;
11 13
12 import de.intevation.artifactdatabase.XMLUtils; 14 import de.intevation.artifactdatabase.XMLUtils;
13 15
14 import de.intevation.gnv.chart.Chart; 16 import de.intevation.gnv.chart.Chart;
15 17
235 String pageFormat, 237 String pageFormat,
236 boolean landscape, 238 boolean landscape,
237 float marginLeft, 239 float marginLeft,
238 float marginRight, 240 float marginRight,
239 float marginTop, 241 float marginTop,
240 float marginBottom 242 float marginBottom,
243 CallContext context
241 ) { 244 ) {
242 log.info("export chart as pdf."); 245 log.info("export chart as pdf.");
243 246
244 if (pageFormat == null) 247 if (pageFormat == null)
245 pageFormat = DEFAULT_PAGE_SIZE; 248 pageFormat = DEFAULT_PAGE_SIZE;
246 249
250 // max size of the chart
247 Rectangle page = PageSize.getRectangle(pageFormat); 251 Rectangle page = PageSize.getRectangle(pageFormat);
248 int pageWidth = (int) (page.getRight(marginRight) - page.getLeft(marginLeft)); 252 int pageWidth = (int) (page.getRight(marginRight) - page.getLeft(marginLeft));
249 int pageHeight = (int) (page.getTop(marginTop) - page.getBottom(marginBottom)); 253 int pageHeight = (int) (page.getTop(marginTop) - page.getBottom(marginBottom));
254
255 // the chart width
256 int chartWidth = (Integer) context.getContextValue("chart.width");
257 int chartHeight = (Integer) context.getContextValue("chart.height");
258
259 int width = 0;
260 int height = 0;
261 if (landscape) {
262 width = pageHeight;
263 height = pageWidth;
264 }
265 else {
266 width = pageWidth;
267 height = pageHeight;
268 }
269
270 if (chartWidth > width) {
271 log.warn("Width of the chart is too big for pdf -> resize it now.");
272 double ratio = ((double)width) / chartWidth;
273 chartWidth *= ratio;
274 chartHeight *= ratio;
275 log.debug("Resized chart to " + chartWidth + "x" + chartHeight);
276 }
277
278 if (chartHeight > height) {
279 log.warn("Height of the chart is too big for pdf -> resize it now.");
280 double ratio = ((double)height) / chartHeight;
281 chartWidth *= ratio;
282 chartHeight *= ratio;
283 log.debug("Resized chart to " + chartWidth + "x" + chartHeight);
284 }
250 285
251 Document document = null; 286 Document document = null;
252 if (landscape) { 287 if (landscape) {
253 document = new Document(page.rotate()); 288 document = new Document(page.rotate());
254 log.debug("Create landscape pdf."); 289 log.debug("Create landscape pdf.");
258 293
259 try { 294 try {
260 PdfWriter writer = PdfWriter.getInstance(document, out); 295 PdfWriter writer = PdfWriter.getInstance(document, out);
261 296
262 document.addSubject(chart.getTitle().getText()); 297 document.addSubject(chart.getTitle().getText());
298 document.addCreationDate();
299 document.open();
300
301 PdfContentByte content = writer.getDirectContent();
302
303 PdfTemplate template = content.createTemplate(width, height);
304 Graphics2D graphics = template.createGraphics(width, height);
305 Rectangle2D area = new Rectangle2D.Double(
306 0.0D, 0.0D, chartWidth, chartHeight);
307
308 chart.draw(graphics, area);
309 graphics.dispose();
310 content.addTemplate(template, marginLeft, marginBottom);
311 }
312 catch (DocumentException de) {
313 log.error("Error while exporting chart to pdf.", de);
314 }
315 finally {
316 document.close();
317 }
318 }
319
320
321 /**
322 * A method to export <code>JFreeChart</code> histograms as PDF to an
323 * <code>OutputStream</code>. Each histogram contained in histograms is
324 * drawn to an own page in the resulting pdf.
325 *
326 * @param out OutputStream
327 * @param histograms JFreeChart histograms
328 * @param pageFormat String to specify a page format, {@link
329 * #DEFAULT_PAGE_SIZE} is used if no pageFormat is given
330 * @param landscape If this is true, the pdf is delivered in landscape
331 * format
332 * @param marginLeft Space to left border
333 * @param marginRight Space to right border
334 * @param marginTop Space to upper border
335 * @param marginBottom Space to lower border
336 */
337 public static void exportHistogramsAsPDF(
338 OutputStream out,
339 Chart[] histograms,
340 String pageFormat,
341 boolean landscape,
342 float marginLeft,
343 float marginRight,
344 float marginTop,
345 float marginBottom
346 ) {
347 log.info("export histogram as pdf.");
348
349 if (pageFormat == null)
350 pageFormat = DEFAULT_PAGE_SIZE;
351
352 Rectangle page = PageSize.getRectangle(pageFormat);
353 int pageWidth =
354 (int) (page.getRight(marginRight) - page.getLeft(marginLeft));
355 int pageHeight =
356 (int) (page.getTop(marginTop) - page.getBottom(marginBottom));
357
358 Document document = null;
359 if (landscape) {
360 document = new Document(page.rotate());
361 log.debug("Create landscape pdf.");
362 }
363 else
364 document = new Document(page);
365
366 try {
367 PdfWriter writer = PdfWriter.getInstance(document, out);
368
263 document.addCreationDate(); 369 document.addCreationDate();
264 document.open(); 370 document.open();
265 371
266 PdfContentByte content = writer.getDirectContent(); 372 PdfContentByte content = writer.getDirectContent();
267 373
274 else { 380 else {
275 width = pageWidth; 381 width = pageWidth;
276 height = pageHeight; 382 height = pageHeight;
277 } 383 }
278 384
279 PdfTemplate template = content.createTemplate(width, height);
280 Graphics2D graphics = template.createGraphics(width, height);
281 Rectangle2D area = new Rectangle2D.Double(0.0D, 0.0D,width,height);
282
283 chart.draw(graphics, area);
284 graphics.dispose();
285 content.addTemplate(template, marginLeft, marginBottom);
286 }
287 catch (DocumentException de) {
288 log.error("Error while exporting chart to pdf.", de);
289 }
290 finally {
291 document.close();
292 }
293 }
294
295
296 /**
297 * A method to export <code>JFreeChart</code> histograms as PDF to an
298 * <code>OutputStream</code>. Each histogram contained in histograms is
299 * drawn to an own page in the resulting pdf.
300 *
301 * @param out OutputStream
302 * @param histograms JFreeChart histograms
303 * @param pageFormat String to specify a page format, {@link
304 * #DEFAULT_PAGE_SIZE} is used if no pageFormat is given
305 * @param landscape If this is true, the pdf is delivered in landscape
306 * format
307 * @param marginLeft Space to left border
308 * @param marginRight Space to right border
309 * @param marginTop Space to upper border
310 * @param marginBottom Space to lower border
311 */
312 public static void exportHistogramsAsPDF(
313 OutputStream out,
314 Chart[] histograms,
315 String pageFormat,
316 boolean landscape,
317 float marginLeft,
318 float marginRight,
319 float marginTop,
320 float marginBottom
321 ) {
322 log.info("export histogram as pdf.");
323
324 if (pageFormat == null)
325 pageFormat = DEFAULT_PAGE_SIZE;
326
327 Rectangle page = PageSize.getRectangle(pageFormat);
328 int pageWidth =
329 (int) (page.getRight(marginRight) - page.getLeft(marginLeft));
330 int pageHeight =
331 (int) (page.getTop(marginTop) - page.getBottom(marginBottom));
332
333 Document document = null;
334 if (landscape) {
335 document = new Document(page.rotate());
336 log.debug("Create landscape pdf.");
337 }
338 else
339 document = new Document(page);
340
341 try {
342 PdfWriter writer = PdfWriter.getInstance(document, out);
343
344 document.addCreationDate();
345 document.open();
346
347 PdfContentByte content = writer.getDirectContent();
348
349 int width = 0;
350 int height = 0;
351 if (landscape) {
352 width = pageHeight;
353 height = pageWidth;
354 }
355 else {
356 width = pageWidth;
357 height = pageHeight;
358 }
359
360 int size = histograms.length; 385 int size = histograms.length;
361 for (int i = 0; i < size; i++) { 386 for (int i = 0; i < size; i++) {
362 if (i > 0) { 387 if (i > 0) {
363 document.newPage(); 388 document.newPage();
364 } 389 }

http://dive4elements.wald.intevation.org