JFreeChart.draw() with SVGGraphics2D?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
JamesFord
Posts: 2
Joined: Fri Jun 15, 2012 12:03 pm
antibot: No, of course not.

JFreeChart.draw() with SVGGraphics2D?

Post by JamesFord » Fri Jun 15, 2012 12:06 pm

Hello,

I am trying to use batik together with JFreeChart in order to draw charts in vector format. I saw some examples on the internet where the draw() method accepted SVGGraphics2D as parameter? But in the versions that I have downloaded the method never accepts a parameter like that?

Can anyone help me?

Thanks,

James Ford

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: JFreeChart.draw() with SVGGraphics2D?

Post by paradoxoff » Sat Jun 16, 2012 8:21 am

SVGGraphics2D extends java.awt.Graphics2D, so you can provide the former as parameter to any method that requires the latter.

JamesFord
Posts: 2
Joined: Fri Jun 15, 2012 12:03 pm
antibot: No, of course not.

Re: JFreeChart.draw() with SVGGraphics2D?

Post by JamesFord » Tue Jun 26, 2012 10:34 am

This does not seem to work for me.

Can you provide an example?

remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Re: JFreeChart.draw() with SVGGraphics2D?

Post by remiohead » Tue Jun 26, 2012 1:58 pm

See http://xmlgraphics.apache.org/batik/jav ... ics2D.html

SVGGraphics2D is a sub class of Graphics so any method that accepts Graphics (or Graphics2D) will work with an instance of SVGGraphics2D.

DaveLaw
Posts: 8
Joined: Fri Feb 04, 2011 10:23 pm
antibot: No, of course not.

Re: JFreeChart.draw() with SVGGraphics2D?

Post by DaveLaw » Wed Jul 04, 2012 1:59 pm

Firstly, heres a minimal Config for what you're trying to do:
Image

Batik 1.7 comes with about half a million jars.
I have packaged those needed for JFreeChart to render SVG in one jar as JFree Batik v1.7. They are:

Code: Select all

06/01/2008  11:11           403,287 batik-awt-util.jar
06/01/2008  11:11           173,750 batik-dom.jar
06/01/2008  11:11            10,261 batik-ext.jar
06/01/2008  11:11           215,802 batik-svggen.jar
06/01/2008  11:11           128,334 batik-util.jar
06/01/2008  11:11            30,862 batik-xml.jar
Finally, heres a little proggy, SVG.java, that does what you're trying to do (and a bit more):

Code: Select all

package de.davelaw;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.zip.GZIPOutputStream;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

public class SVG {
    private static final String   OPTION_NO      =  "No";
    private static final Object[] OPTIONS_Yes_NO = {"Murder it!", OPTION_NO};

    private static final boolean  ZIP            = false;

    private static final String   EXTENSION      = ZIP ?                         "svgz"  :              "svg";
    private static final String   FILE_TYPE      = ZIP ? "Compressed SVG Image (*.svgz)" : "SVG Image (*.svg)";
    private static final String   DOT_EXTENSION  = "." + EXTENSION;

    public static void main(String[] args) throws IOException {

        JFileChooser jfc = new JFileChooser();
        /**/         jfc.setApproveButtonText("Save");
        /**/         jfc.setFileFilter(new FileNameExtensionFilter(FILE_TYPE, EXTENSION));

        if (jfc.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) {
            return;
        }
        File out = jfc.getSelectedFile();
        if (out.getName().endsWith(DOT_EXTENSION) == false) {
            out = new File(out.getCanonicalPath() +  DOT_EXTENSION);
        }

        if (out.exists()) {
            if (JOptionPane.OK_OPTION != JOptionPane.showOptionDialog(
                    null,
                    "Do you want to overwrite it?",  "Output File exists",
                    JOptionPane.YES_NO_OPTION,       JOptionPane.WARNING_MESSAGE,
                    null,
                    OPTIONS_Yes_NO,  OPTION_NO)) {
                return;
            }
        }

        OutputStream ost = new FileOutputStream(out);
        JFreeChart chart = ChartFactory.createPieChart("Turkish Cities", createDataset(), true, true, false);

        final ChartRenderingInfo info  = new ChartRenderingInfo(new StandardEntityCollection());
        // (Rendering Info can be used for creating ImageMaps. It may be removed)
        generateChartSVG(chart, info, 640, 480, ost);
    }

    private static final PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("Istanbul", new Double(45.0 * (Math.random()/4+0.875))); 
        dataset.setValue("Ankara",   new Double(15.0 * (Math.random()/4+0.875)));
        dataset.setValue("Izmir",    new Double(25.2 * (Math.random()/4+0.875)));
        dataset.setValue("Antalya",  new Double(14.8 * (Math.random()/4+0.875)));

        return dataset;
    }

    private static final void generateChartSVG(JFreeChart chart, ChartRenderingInfo info, int width, int height, OutputStream outputStream) throws IOException {

        DOMImplementation   dim = GenericDOMImplementation.getDOMImplementation();
        Document            doc = dim.createDocument(null, "svg", null);

        SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
        /**/                ctx.setEmbeddedFontsOn(true);
        /**/                ctx.setPrecision(12); // Best (default=4)
        /**/                ctx.setComment("Copyright DaveLaw");

        SVGGraphics2D       g2d = new SVGGraphics2D(doc);
        /**/                g2d = new SVGGraphics2D(ctx, false);
        /**/                g2d.setSVGCanvasSize(new Dimension(width, height));

        chart.draw         (g2d, new Rectangle(width, height), info);
        OutputStream        ost = ZIP ? new GZIPOutputStream(outputStream) : outputStream;

        g2d.stream(new OutputStreamWriter(ost, "UTF-8"), true /* use css */);
        ost.close(); // (GZIPOutputStream closes underlying outputStream too)
    }
}
Best regards,
DaveLaw

Locked