Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java @ 1735:5966a20fc72c
Enabled support for PDF and SVG chart exports.
flys-artifacts/trunk@3023 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 19 Oct 2011 09:43:04 +0000 |
parents | e6aff80b59ff |
children | 8428de5846e8 |
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java Tue Oct 18 15:34:07 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartExportHelper.java Wed Oct 19 09:43:04 2011 +0000 @@ -51,15 +51,22 @@ */ public class ChartExportHelper { + public static final String FORMAT_PNG = "png"; + + public static final String FORMAT_PDF = "pdf"; + + public static final String FORMAT_SVG = "svg"; + + /** * Constant field to define A4 as default page size. */ - private static final String DEFAULT_PAGE_SIZE = "A4"; + public static final String DEFAULT_PAGE_SIZE = "A4"; /** * Constant field to define UTF-8 as default encoding. */ - private static final String DEFAULT_ENCODING = "UTF-8"; + public static final String DEFAULT_ENCODING = "UTF-8"; /** * Logger used for logging with log4j. @@ -81,10 +88,8 @@ */ public static void exportImage( OutputStream out, - JFreeChart chart, - String format, - int width, - int height + JFreeChart chart, + CallContext cc ) throws IOException { @@ -92,9 +97,13 @@ ChartRenderingInfo info = new ChartRenderingInfo(); + String format = (String) cc.getContextValue("chart.image.format"); + + int[] size = getSize(cc); + ImageIO.write( chart.createBufferedImage( - width, height, Transparency.BITMASK, info + size[0], size[1], Transparency.BITMASK, info ), format, out @@ -108,17 +117,16 @@ * * @param out OutputStream * @param chart JFreeChart to be exported - * @param encoding Encoding, defaults to {@link #DEFAULT_ENCODING} if null - * @param width Width the svg used to be - * @param height Height the svg used to be + * @param context The CallContext object that contains extra chart + * parameters. */ public static void exportSVG( OutputStream out, JFreeChart chart, - String encoding, - int width, - int height + CallContext context ) { + String encoding = (String) context.getContextValue("chart.encoding"); + log.info("export chart as svg"); if (encoding == null) @@ -127,7 +135,9 @@ org.w3c.dom.Document document = XMLUtils.newDocument(); SVGGraphics2D graphics = new SVGGraphics2D(document); - chart.draw(graphics, new Rectangle2D.Double(0.0D, 0.0D,width,height)); + int[] size = getSize(context); + + chart.draw(graphics, new Rectangle2D.Double(0.0D, 0.0D,size[0],size[1])); try { graphics.stream(new OutputStreamWriter(out, encoding)); @@ -159,15 +169,12 @@ public static void exportPDF( OutputStream out, JFreeChart chart, - String pageFormat, - float marginLeft, - float marginRight, - float marginTop, - float marginBottom, - CallContext context + CallContext cc ) { log.info("export chart as pdf."); + String pageFormat = (String) cc.getContextValue("chart.page.format"); + if (pageFormat == null) pageFormat = DEFAULT_PAGE_SIZE; @@ -177,10 +184,9 @@ float pageHeight = page.getHeight(); // the chart width - int chartWidth = (Integer) context.getContextValue("chart.width"); - int chartHeight = (Integer) context.getContextValue("chart.height"); + int[] size = getSize(cc); - boolean landscape = chartWidth > chartHeight; + boolean landscape = size[0] > size[1]; float width = 0; float height = 0; @@ -193,22 +199,34 @@ height = pageHeight; } + float marginLeft = (Float) cc.getContextValue( + "chart.marginLeft"); + + float marginRight = (Float) cc.getContextValue( + "chart.marginRight"); + + float marginTop = (Float) cc.getContextValue( + "chart.marginTop"); + + float marginBottom = (Float) cc.getContextValue( + "chart.marginBottom"); + float spaceX = width - marginLeft - marginRight; - if (chartWidth > spaceX) { + if (size[0] > spaceX) { log.warn("Width of the chart is too big for pdf -> resize it now."); - double ratio = ((double)spaceX) / chartWidth; - chartWidth *= ratio; - chartHeight *= ratio; - log.debug("Resized chart to " + chartWidth + "x" + chartHeight); + double ratio = ((double)spaceX) / size[0]; + size[0] *= ratio; + size[1] *= ratio; + log.debug("Resized chart to " + size[0] + "x" + size[1]); } float spaceY = height - marginTop - marginBottom; - if (chartHeight > spaceY) { + if (size[1] > spaceY) { log.warn("Height of the chart is too big for pdf -> resize it now."); - double ratio = ((double)spaceY) / chartHeight; - chartWidth *= ratio; - chartHeight *= ratio; - log.debug("Resized chart to " + chartWidth + "x" + chartHeight); + double ratio = ((double)spaceY) / size[1]; + size[0] *= ratio; + size[1] *= ratio; + log.debug("Resized chart to " + size[0] + "x" + size[1]); } Document document = null; @@ -234,10 +252,10 @@ double[] origin = getCenteredAnchor( marginLeft, marginRight, marginBottom, marginTop, width, height, - chartWidth, chartHeight); + size[0], size[1]); Rectangle2D area = new Rectangle2D.Double( - origin[0], origin[1], chartWidth, chartHeight); + origin[0], origin[1], size[0], size[1]); chart.draw(graphics, area); graphics.dispose(); @@ -252,6 +270,16 @@ } + public static int[] getSize(CallContext cc) { + int[] size = new int[2]; + + size[0] = (Integer) cc.getContextValue("chart.width"); + size[1] = (Integer) cc.getContextValue("chart.height"); + + return size; + } + + /** * This method returns the anchor of the chart so that the chart is centered * according to the given parameters.