Generating SVG images

SVG stands for Scalable Vector Graphics, and it is an open source XML based language used to describe 2D vector graphics. The adoption of SVG has been increasing fast, replacing proprietary technologies that has been around for a long time.
For more detailed information about SVG, visit the W3C Consortium website, at http://www.w3c.org/TR/SVG.

JetChart implements an SVG encoder to generate SVG code from chart images. SVG images can be generated from servlets, in a similar way to that used to generate GIF, JPEG and PNG images, as detailed in previous topics of this tutorial.
The SVG encoder is contained in a separate package, named com.jinsight.svg, which has to be imported together with the main JetChart package. The encoding process is triggered by the ChartEncoder class, invoking either the method ChartEncoder.svgEncode(File f,boolean encodeAsImage, int quality) or ChartEncoder.svgEncode(OutputStream out,boolean encodeAsImage,int quality). The former saves the SVG code into a file, whereas the latter delivers the SVG code to an output stream object.

The following servlet example creates and SVG-encodes a bar chart. The resulting output is sent back to the client browser over a servlet output stream. Chart tooltips are enabled and a list of URLs are associated with the bar series' data points. Javascript code is automatically appended to the output to handle mouse events and display tooltips, so when mouse cursor hovers over a bar the corresponding tooltip is displayed, and if a bar is clicked the URL it is associated with is loaded.

This servlet will only work properly if the client browser is capable of displaying SVG images.

Get the example code here.

import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.image.BufferedImage;
import java.util.Locale;
import java.io.*;
import java.awt.*;

import com.jinsight.jetchart.*;

import com.jinsight.svg.*;

public class Example7 extends HttpServlet {

    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { 
        
        // Sets the content type adequately. 
	res.setContentType("image/svg+xml");
	
	// Gets a reference to the servlet output stream.
	ServletOutputStream out=res.getOutputStream();
	
	// Specifies chart labels. Labels might come from a database or be passed
        // as servlet parameters.
	String[] labels={"MSN","YAHOO","GOOGLE","AOL","ASKJEEVES","LOOKSMART","INFOSPACE","NETSCAPE","OVERTURE"};
	
	// Values for the bar series. Values might come from a database or be passed
        // as servlet parameters.
	double[] values={.370,.340,.289,.224,.157,.092,.089,.082,.069};
	
	// The value format is a percentage.
	String valueFormat="#0.00 %";

	// Creates a bar series object.
	BarSerie bs=new BarSerie();
	
	// Disables the bar series legend.
	bs.setLegendEnabled(false);
	  
	bs.setValues(values);
	bs.setColor(Color.green);
	bs.setValueFormat(valueFormat);
	
	// Specifies the URLs to be associated with the equivalent SVG code of each bar. This method has no effect
	// if the 'encodeAsImage' parameter of the method ChartEncoder.svgEncoder is set to true.
	bs.setSVGURLs(new String[]{"http://www.msn.com",
				   "http://www.yahoo.com",
				   "http://www.google.com",
				   "http://www.aol.com",
				   "http://www.askjeeves.com",
				   "http://www.looksmart.com",
				   "http://www.infospace.com",
				   "http://www.netscape.com",
				   "http://www.overture.com"});
	
	// Creates the chart context, passing labels.
	Graph graph=new Graph(labels);
	
	// Adds the bar series to the chart context.
	graph.addSerie(bs);
	
	// Enables grid.
	graph.getGraphSet(0).getGrid().setEnabled(true);
	
	// Sets gradient colors.
	graph.setGradientColors(Color.blue,Color.yellow);
	
	// Sets the format of the scale numbers.
	graph.getGraphSet(0).getScale().setValueFormat("#0.00 %");
	
	// Enables tooltip and change its font. Javascript code is automatically appended
	// to the SVG output to handle mouse events and display tooltips. Tooltips are not
	// displayed if the 'encodeAsImage' parameter of the method ChartEncoder.svgEncoder
	// is set to true.
	graph.getToolTip().setEnabled(true);
	graph.getToolTip().setFont(new Font("SansSerif",Font.PLAIN,14));
	
	// Sets chart title and title font.
	graph.setTitle(new String[]{"Internet Audience Reach","March 2002"});
	graph.setTitleFont(new Font("SansSerif",Font.BOLD,14));
	
	// Enables 3D mode.
	graph.set3DEnabled(true);
	
	// Chart is horizontallly oriented.
	graph.setHorizontalGraphEnabled(true);
	
	// Sets the default locale. If offscreen images are generated using java.awt.BufferedImage
        // the chart context(Graph) is not laid out on a  container, and consequently the default 
        // locale information is not passed to the Graph object. To prevent an exception from being
        // raised we manually set the default locale.
	graph.setLocale(Locale.getDefault());
	
        // Enables generation of an offscreen image using a BufferedImage object.
	graph.setBufferedImageEnabled(true,BufferedImage.TYPE_INT_RGB);
	
        // Enables offscreen generation using an Image object, in this case a BufferedImage object.
	graph.setOffScreenGraphEnabled(true);
	
	graph.setSize(550,350);
	
        // Creates the chart encoder.
	ChartEncoder ce=new ChartEncoder(graph);
	
	try {
	    
	    /**
	     * Starts encoding chart. This method takes the following parameters:
	     * 1) an OutputStream object
	     * 2) a boolean flag to indicate whether the output must be a sequence of
	     *    shape elements describing each of the chart components or a base64
	     *    encoded sequence of characters passed to the SVG  tag
	     * 3) the output quality of the SVG image.
	     */
	    ce.svgEncode(out,false,SVGEncoder.HIGH_QUALITY);
	    
	}
	catch (Exception e) {
	    e.printStackTrace();
	}
	finally {
	    
	    try {
		if (out!=null)
                    // Closes the output.
		    out.close();
	    }
	    catch (IOException e) {
		e.printStackTrace();
	    }
	    
	}
	
	
    }
    
    
}