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();
}