Generating SVG images with AWT

SVG stands for Scalable Vector Graphics. It is a language for describing two-dimensional vector graphics using XML.
The use of SVG is rapidly increasing, especially because of the quality of vectorial graphics, superseding rasterized image formats such as GIF, JPEG and PNG.

JetChart implements an SVG encoder to output chart images as SVG text. SVG encoding consists basically of passing a reference of a GenericGraph object to a ChartEncoder instance and triggering the encoding process by invoking the method ChartEncoder.svgEncode(OutputStream out,boolean encodeAsImage,int quality). The first argument can be any OutputStream object which is used to output the SVG text. The second argument specifies whether the chart image shall be encoded into a PNG image and outputted as a base64 sequence of characters or shall be encoded into a sequence of SVG instructions corresponding to each one of the drawing methods invoked.
The third argument is an indicator to the SVG viewer as to the quality of the image to be displayed.

The SVG encoding process is carried out by classes found in the package com.jinsight.svg. A subclass of java.awt.Graphics, SVGGraphics, is passed to the paint(Graphics g) method of a GenericGraph object, like Graph and ScatterGraph. The SVGGraphics object implements the majority of the abstract methods found in the Graphics class, generating corresponding XML instructions using the SVG language.

The example found in this topic is built with AWT classes. The next topic brings a Swing example, to illustrate a minor difference between heavyweight and lightweight applications, with respect to SVG encoding. There are several SVG viewers available, which can be used to load and display the SVG file generated by the example below. The Adobe SVG Viewer is probably the most used and it can be found at the following address: http://www.adobe.com/svg/viewer/install/main.html There is also an SVG viewer called Batik, which is written in Java and can be found at http://xml.apache.org/batik.

import java.awt.*;
import java.awt.event.*;
import com.jinsight.jetchart.*;

import com.jinsight.svg.*;

import java.io.*;

public class Main extends Frame implements ActionListener {

    Graph graph;

    public Main() {

	addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent evt) {
		    System.exit(0);
		}
	    });

	Panel topPanel=new Panel();
	Button b=new Button("Generate SVG");
	b.addActionListener(this);

	topPanel.add(b);

	add("North",topPanel);

	graph=new Graph(new String[]{"l1","l2","l3","l4","l5","l6","l7"});

	graph.setTitle(new String[]{"The JetChart Library","SVG Encoding 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);

	add("Center",graph);

	setSize(500,400);

	setVisible(true);

    }
    
    public void actionPerformed(ActionEvent evt) {

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

    }
    
    public static void main(String[] args) {
	new Main();
    }
}