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
JFreeChart.draw() with SVGGraphics2D?
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: JFreeChart.draw() with SVGGraphics2D?
SVGGraphics2D extends java.awt.Graphics2D, so you can provide the former as parameter to any method that requires the latter.
Re: JFreeChart.draw() with SVGGraphics2D?
This does not seem to work for me.
Can you provide an example?
Can you provide an example?
Re: JFreeChart.draw() with SVGGraphics2D?
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.
SVGGraphics2D is a sub class of Graphics so any method that accepts Graphics (or Graphics2D) will work with an instance of SVGGraphics2D.
Re: JFreeChart.draw() with SVGGraphics2D?
Firstly, heres a minimal Config for what you're trying to do:

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:
Finally, heres a little proggy, SVG.java, that does what you're trying to do (and a bit more):
Best regards,
DaveLaw

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
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)
}
}
DaveLaw