How to create an SVG with JFreeChart?
Asked Answered
A

3

7

On the JFreeChart web site it says that the library can output the chart in vector format.

From the JFreeChart site:

  • support for many output types, including Swing components, image files (including PNG and JPEG), and vector graphics file formats (including PDF, EPS and SVG);

But how can I actually output in SVG format?

There is a way using the Apache Batik library, but from the statement above I would think JFreeChart can do it without Batik.

I could figure out the output for PNG and JPG in the ChartUtilities class, but there seems to be no class for vector graphics output.

Apothegm answered 1/3, 2012 at 11:30 Comment(2)
There is a thread about this here: jfree.org/phpBB2/viewtopic.php?f=3&t=24365Jennie
@Jennie That link is dead by now, do you recall what the conclusion of the thread was?Solvolysis
S
15

No, JFreeChart supports SVG in the sense that it can be used in conjunction with Batik or JFreeSVG, which are required. Related resources include these:

  • The JFreeChart Developer Guide, discussed here.

  • Saving JFreeChart as SVG vector images using Batik.

  • JFreeSVG, which can "generate content in SVG format using the standard Java2D drawing API, Graphics2D." Demonstration programs may be found here, including this SVGBarChartDemo1 excerpt:

      JFreeChart chart = createChart(createDataset());
      SVGGraphics2D g2 = new SVGGraphics2D(600, 400);
      Rectangle r = new Rectangle(0, 0, 600, 400);
      chart.draw(g2, r);
      File f = new File("SVGBarChartDemo1.svg");
      SVGUtils.writeToSVG(f, g2.getSVGElement());
    

Disclaimer: Not affiliated with Object Refinery Limited; just a satisfied customer and very minor contributor.

Synergy answered 1/3, 2012 at 11:51 Comment(1)
JFreeSVG is another option that is available now.Erythrocyte
S
2

To just make it simple for other readers, the following code converts a jFreeChart to a SVG by using jFreeSVG:

import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.chart.JFreeChart;
import java.awt.geom.Rectangle2D;

public String getSvgXML(){
    final int widthOfSVG = 200;
    final int heightOfSVG = 200;
    final SVGGraphics2D svg2d = new SVGGraphics2D(widthOfSVG, heightOfSVG);

    final JFreeChart chart = createYourChart();
    chart.draw(svg2d,new Rectangle2D.Double(0, 0, widthOfSVG, heightOfSVG));

    final String svgElement = svg2d.getSVGElement();
    return svgElement;
}

To write the SVG elements to a PDF file, you can use the following code to generate a byte[] out of your SVG and then write it to a file. For this case I use apache batic :

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.fop.svg.PDFTranscoder;

public byte[] getSVGInPDF(){ 
     final Transcoder transcoder = new PDFTranscoder();
     final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
     final TranscoderInput transcoderInput = new TranscoderInput(
     new ByteArrayInputStream(getSvgXML().getBytes()));
     final TranscoderOutput transcoderOutput = new TranscoderOutput(outputStream);
     transcoder.transcode(transcoderInput, transcoderOutput);
     return outputStream.toByteArray();
}
Shani answered 18/12, 2018 at 10:31 Comment(0)
G
1

It seems that JFreeSVG may be more efficient than Batik: JFreeSVG vs Apache Batik

Gough answered 7/11, 2014 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.