This topic details SVG encoding using an offscreen AWT application to paint a graph on a
java.awt.image.BufferedImage object and output data to an external file.
What is said in the previous topics about GIF/JPEG/PNG encoding from offscreen
applications also applies to SVG, especially the topic about usage of the BufferedImage
class. The methods below must always be invoked on the GenericGraph instance when generating
offscreen SVG images with a BufferedImage object:
The example below is the same found in the previous topics about SVG encoding, adapted
to run as an offscreen application.
import java.awt.*; import java.awt.event.*; import com.jinsight.jetchart.*; import java.io.*; import com.jinsight.svg.*; import java.util.Locale; import java.awt.image.BufferedImage; public class Main extends Frame { public static void main(String[] args) { Graph graph=new Graph(new String[]{"l1","l2","l3","l4","l5","l6","l7"}); graph.setTitle(new String[]{"The JetChart Library","Offscreen SVG Demo"}); graph.set3DEnabled(true); graph.getGraphSet(0).getGrid().setEnabled(true); graph.setGradientColors(Color.blue,Color.yellow); BarSerie bs=new BarSerie(); bs.setValues(new double[]{100,80,60,40,90,40,140}); bs.setColor(Color.cyan); bs.setTitle("Bar series"); graph.addSerie(bs); graph.setSize(500,400); graph.setOffScreenGraphEnabled(true); graph.setLocale(Locale.getDefault()); graph.setBufferedImageEnabled(true,BufferedImage.TYPE_INT_ARGB); generateSVG(graph); } public static void generateSVG(Graph graph) { ChartEncoder ce=new ChartEncoder(graph); OutputStream out=null; try { File f=new File("chart.svg"); out=new FileOutputStream(f); // Encodes chart and outputs SVG code to the chart.svg file. ce.svgEncode(out,false,SVGEncoder.HIGH_QUALITY); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out!=null) out.close(); } catch (IOException e) { e.printStackTrace(); } // A call to System.exit(0) has to be placed at this point to stop // application, because the AWT thread remains active. // There is no need for the line below if this application is run using // JDK 1.4 or newer versions. System.exit(0); } } }