Handling series events

JetChart provides handling of events associated with mouse actions performed on series and their data points. These events are denominated series events, and are represented by the SerieEvent class.

The actions detected and dispatched as a SerieEvent object include clicking a series data point, dragging a series data point(only charts developed on top of the Graph class), clicking a series symbol in the legend box and moving mouse cursor over a series data point.

JetChart dispatches a SerieEvent object to registered listeners that implements the SerieListener interface. The application below plots a bar series and a line series, and implements the SerieListener interface to handle events dispatched if any data point or legend box symbol is clicked or moused over. Additionally, series data point dragging is enabled, to let user dynamically change series values.

If any data point or symbol in the legend box is clicked, information about the clicked series and respective data point is printed to the console.
To drag a series data point, move mouse cursor to the top of any bar or over a line series' data point, and when the cursor shape changes, click and drag. The data point value is automatically updated and printed to the console.
If mouse is moved over a series data point, respective value is displayed inside the bottom panel.

import javax.swing.*;
import java.awt.*;
import com.jinsight.jetchart.*;

public class Main extends JFrame implements SerieListener {

   JLabel label1,label2;

   public Main() { 

       Graph graph=new Graph();
       
       graph.addSerieListener(this);

       // Series data point dragging has to be enabled with the following method.
       graph.setSerieDraggingEnabled(true);
       
       graph.setTitle(new String[]{"The JetChart Library","Handling series events"});

       LineSerie ls=new LineSerie(new double[]{100,130,110,90,80},"Line series");
       ls.setColor(Color.red);

       BarSerie bs=new BarSerie(new double[]{70,80,60,50,70},"Bar series");
       bs.setColor(Color.green);
       
       graph.addSerie(ls);
       graph.addSerie(bs);

       graph.getGraphSet(0).getGrid().setEnabled(true);
       graph.getGraphSet(0).getGrid().setColor(Color.lightGray);
       
       Container ct=getContentPane();
       
       ct.add(graph);

       // Creates a bottom panel to display the series values captured by the 
       // mouseMoved handler.
       JPanel bottomPanel=new JPanel();
       label1=new JLabel(" ");
       label2=new JLabel(" ");

       // Adds both labels to the bottom panel.
       bottomPanel.add(label1);
       bottomPanel.add(label2);

       ct.add("South",bottomPanel);

       setSize(450,350);
       
       setVisible(true);
       
   }
      
   public void serieClicked(SerieEvent evt) {

       AbstractSerie as=evt.getSerie();

       // If any icon in the legend box was clicked...
       if (evt.isOverLegend()) {
          if (as instanceof BarSerie)
             System.out.println("The green icon in the legend box was clicked.");
          else
             System.out.println("The red icon in the legend box was clicked.");
       }
       else { // The click was performed on a series data point.
          int index=evt.getElementIndex();
          double[] values=as.getValues();
          if (as instanceof BarSerie)
            System.out.println("Bar series data point value: "+values[index]);
          else
            System.out.println("Line series data point value: "+values[index]);
       }

   }

   public void serieReleased(SerieEvent evt) {
   }

   public void serieDragged(SerieEvent evt) {
      AbstractSerie as=evt.getSerie();
      int index=evt.getElementIndex();
      double[] values=as.getValues();
      if (as instanceof BarSerie)
         System.out.println("New bar series data point value: "+values[index]);
      else
         System.out.println("New line series data point value: "+values[index]);
   }

   public void mouseMoved(SerieEvent evt) {

      // This handler receives a SerieEvent object for each series added to 
      // the chart context. An element index different from -1 means that the
      // cursor is within the area of a series data point.
      // The series data point value is displayed inside the bottom panel.

      AbstractSerie as=evt.getSerie();
      double[] values=as.getValues();

      if (as instanceof BarSerie) {
        if (evt.getElementIndex()!=-1) 
            label1.setText("Bar series value: "+values[evt.getElementIndex()]);
        else 
            label1.setText(" ");
                              
      }
      else {
        if (evt.getElementIndex()!=-1)
           label2.setText("Line series value: "+values[evt.getElementIndex()]);
        else
           label2.setText(" ");
      }


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