SVG drill-down and tooltips

A user can interact with an SVG image by means of hyperlinks automatically placed on a series data points, allowing for url drill-down. The AbstractSerie class implements the method setSVGURLs(String[] svgURLs), which takes a sequence of URLs to be associated with a series data points. AbstractSerie is the superclass of all series, hence this method can be used with any subclass of AbstractSerie, as LineSerie, BarSerie, PieSerie, etc.
Tooltips can also be displayed when mouse hovers over a series data point, in a similar way of that implemented by JetChart. SVG tooltips are automatically enabled if JetChart tooltips are enabled, using the method ToolTip.setEnabled(boolean isToolTipEnabled), as described in the topic Enabling tooltips.

The following example displays a bar series and assigns a URL to each bar displayed, also enabling tooltips. The top button generates SVG code containing hyperlinks associated with all bars. A script is also appended to the SVG code to allow for dispatch of mouse events and display of tooltips.


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

import com.jinsight.svg.*;

public class Main extends Frame implements ActionListener {

    Graph graph;

    Button svgButton;

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

	Panel p=new Panel();
	svgButton=new Button("Generate SVG file");
	svgButton.addActionListener(this);

	p.add(svgButton);

	add("North",p);

	graph=new Graph(new String[]{"l1","l2","l3","l4","l5","l6","l7"});
	
	graph.setTitle(new String[]{"The JetChart Library","SVG drill-down and Tooltips demo"});
	
	graph.set3DEnabled(true);
	
	graph.getGraphSet(0).getGrid().setEnabled(true);
	
	graph.setGradientColors(Color.blue,Color.yellow);
	
	graph.getToolTip().setEnabled(true);

	BarSerie bs=new BarSerie();
	bs.setValues(new double[]{100,80,60,40,90,40,140});
	bs.setColor(Color.cyan);
	bs.setTitle("Bar series");
	
	// Specifies the  URLs to be assigned to the bars. No
	// hyperlinks are provided to the bars whose URLs are
	// null.
	String[] urls={"http://www.jinsight.com/jetchart",
		       "http://www.javasoft.com",
		       null,
		       "http://www.yahoo.com",
		       "http://www.google.com",
		       null,
		       "http://www.msn.com"};

	bs.setSVGURLs(urls);
	
	graph.addSerie(bs);

	setSize(500,400);

	add("Center",graph);

	setVisible(true);

    }
    
    public void actionPerformed(ActionEvent evt) {
	
	svgButton.setEnabled(false);

	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 {

	    svgButton.setEnabled(true);

	    try {
		if (out!=null)
		    out.close();
	    }
	    catch (IOException e) {
		e.printStackTrace();
	    }

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


}