Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an SVG with JFreeChart?

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.

like image 330
super_ernie77 Avatar asked Mar 01 '12 11:03

super_ernie77


People also ask

Are SVG vector files?

What is an SVG file? Scalable Vector Graphics (SVG) is a web-friendly vector file format. As opposed to pixel-based raster files like JPEGs, vector files store images via mathematical formulas based on points and lines on a grid.

What is SVG explain any two components of SVG with suitable example?

Scalable Vector Graphics commonly known as SVG is a XML based format to draw vector images. It is used to draw two dimentional vector images. This tutorial will teach you basics of SVG. Tutorial contains chapters discussing all the basic components of SVG with suitable examples.


2 Answers

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.

  • The JFreeChart forum.

  • 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.

like image 50
trashgod Avatar answered Sep 20 '22 01:09

trashgod


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();
}
like image 26
Farshad Bakhshandegan Moghadda Avatar answered Sep 22 '22 01:09

Farshad Bakhshandegan Moghadda