view gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java @ 617:3d13fa281a7e

Added new ouput mode: histogram. gnv-artifacts/trunk@688 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 15 Feb 2010 11:44:01 +0000
parents a88e15972c72
children 65f09139e9b3
line wrap: on
line source
package de.intevation.gnv.exports;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

import java.awt.Transparency;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;

import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.apache.log4j.Logger;

import org.jfree.chart.JFreeChart;

import de.intevation.artifactdatabase.XMLUtils;
import de.intevation.gnv.chart.Chart;


/**
 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
 */
public class ChartExportHelper {

    private static final String  DEFAULT_PAGE_SIZE = "A4";
    private static final String  DEFAULT_ENCODING  = "UTF-8";

    private static Logger log = Logger.getLogger(ChartExportHelper.class);

    public static void exportImage(
        OutputStream out,
        JFreeChart chart,
        String format,
        int width,
        int height
    )
    throws IOException
    {
        log.info("export chart as png");

        ImageIO.write(
            chart.createBufferedImage(
                width, height, Transparency.BITMASK, null
            ),
            format,
            out
        );
    }


    public static void exportHistograms(
        OutputStream out,
        Chart[]      histograms,
        String       format,
        int          width,
        int          height)
    throws IOException
    {
        log.info("export histograms");

        int size            = histograms.length;
        BufferedImage image = new BufferedImage(
            width, height*size, BufferedImage.TYPE_INT_RGB);
        Graphics2D g        = image.createGraphics();

        for (int i = 0; i < size; i++) {
            JFreeChart chart = histograms[i].generateChart();
            chart.draw(g, new Rectangle2D.Double(0.0D, i*height, width, height));
        }
        g.finalize();

        ImageIO.write(image, format, out);
    }


    public static void exportSVG(
        OutputStream out,
        JFreeChart   chart,
        String       encoding,
        int          width,
        int          height
    ) {
        log.info("export chart as svg");

        if (encoding == null)
            encoding = DEFAULT_ENCODING;

        org.w3c.dom.Document document = XMLUtils.newDocument();
        SVGGraphics2D        graphics = new SVGGraphics2D(document);

        chart.draw(graphics, new Rectangle2D.Double(0.0D, 0.0D,width,height));

        try {
            graphics.stream(new OutputStreamWriter(out, encoding));
        }
        catch (SVGGraphics2DIOException svge) {
            log.error("Error while writing svg export to output stream.", svge);
        }
        catch (UnsupportedEncodingException uee) {
            log.error("Unsupported encoding: " + encoding, uee);
        }
    }


    public static void exportPDF(
        OutputStream out,
        JFreeChart   chart,
        String       pageFormat,
        boolean      landscape,
        float        marginLeft,
        float        marginRight,
        float        marginTop,
        float        marginBottom
    ) {
        log.info("export chart as pdf.");

        if (pageFormat == null)
            pageFormat = DEFAULT_PAGE_SIZE;

        Rectangle page = PageSize.getRectangle(pageFormat);
        int pageWidth  = (int) (page.getRight(marginRight) - page.getLeft(marginLeft));
        int pageHeight = (int) (page.getTop(marginTop) - page.getBottom(marginBottom));

        Document document = null;
        if (landscape) {
            document = new Document(page.rotate());
            log.debug("Create landscape pdf.");
        }
        else
            document = new Document(page);

        try {
            PdfWriter writer = PdfWriter.getInstance(document, out);

            document.addSubject(chart.getTitle().getText());
            document.addCreationDate();
            document.open();

            PdfContentByte content  = writer.getDirectContent();

            int width  = 0;
            int height = 0;
            if (landscape) {
                width  = pageHeight;
                height = pageWidth;
            }
            else {
                width  = pageWidth;
                height = pageHeight;
            }

            PdfTemplate template = content.createTemplate(width, height);
            Graphics2D  graphics = template.createGraphics(width, height);
            Rectangle2D area = new Rectangle2D.Double(0.0D, 0.0D,width,height);

            chart.draw(graphics, area);
            graphics.dispose();
            content.addTemplate(template, marginLeft, marginBottom);
        }
        catch (DocumentException de) {
            log.error("Error while exporting chart to pdf.", de);
        }
        finally {
            document.close();
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org