The previous example on zooming charts was improved, implementing a scrolling bar at the bottom of the chart area
to let user scroll chart after zooming into a smaller area.
JetChart implements handling of two type of events: series events and zooming events. Series events are explained
at the next topic. This example implements handling of zoom events to set the properties of a scrolling bar with
respect to the values of the indexes that delimit an area to be zoomed. The values of these indexes range from 0
to the maximum number of data points, which can be either the length of the largest series or the maximum number
of labels displayed along the x axis, minus 1. The largest value is chosen.
Each time chart is zoomed-in or zoomed-out, a ZoomEvent object is dispatched to listeners that
implement the ZoomListener interface, which requires the implementation of the method
ZoomListener.chartZoomed(ZoomEvent evt). The ZoomEvent object carries information about the zoomed
area, like the values of the delimiting indexes and the maximum number of data points, which is calculated as
explained above. Then, the zoom event handler can use this information to set the properties of a scrolling bar.
In addition to dispatching zoom events, JetChart implements the method Graph.setDataSetRange(int dataSetStartIndex,
int dataSetEndIndex), which can be used to delimit a range of values to be displayed, out of the full set
of values passed to the series as an array of double precision numbers. The combination of this feature with the
zoom event handling brings flexibility to JetChart, since any type of scrolling component can be used for the purpose
discussed in this topic, provided that the component adheres to a programmable interface similar to that implemented
by the Scrollbar and JScrollBar classes.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import com.jinsight.jetchart.*; public class Main extends JFrame implements ZoomListener,AdjustmentListener { JScrollBar scrollBar; Graph graph; public Main() { graph=new Graph(); graph.addZoomListener(this); graph.setZoomEnabled(true); graph.setTitle(new String[]{"The JetChart Library","Zooming charts"}); double[] values={131.90,132.80,130.50,131.00,136.75,135.00,131.50,130.50, 132.40,133.30,130.90,123.40,122.25,118.00,120.00,116.10, 115.00,103.45,107.50,103.80,99.00,96.75,94.00,95.90,97.50, 98.40,100.20,102.00,110.40,104.50,108.70,107.75,107.45, 103.00,100.90,99.25,93.80,99.25,103.00,100.25,102.70, 107.15,106.00,107.15,109.00,104.00,96.50,97.95,99.70, 97.70,91.00,87.45,86.80,88.60,85.40,85.90,84.85, 76.55, 68.00,67.45,61.90,60.90,60.75,53.60,58.50, 57.20,51.25,48.50,45.75,46.90,52.55,51.40,54.00, 51.70,41.20,38.75,34.00,33.50,33.25,34.25,33.90, 30.00,33.50,34.95,31.45,26.95,28.40,30.90,28.90, 29.90,29.80,33.25,34.50,34.00,36.90,37.00,36.40, 34.70,37.50,38.30,39.40,43.50,45.80,48.70,46.45}; JPanel bottomPanel=new JPanel(new BorderLayout()); scrollBar=new JScrollBar(Scrollbar.HORIZONTAL,0,0,0,0); scrollBar.addAdjustmentListener(this); bottomPanel.add(scrollBar); AreaSerie as=new AreaSerie(values,"Area series"); as.setColor(Color.green); // Disables the area series vertical lines. as.setAreaLinesEnabled(false); graph.addSerie(as); GraphSet graphSet=graph.getGraphSet(0); // Disables labels marks on the x axis. graphSet.setLabelsMarksEnabled(false); // Sets the properties of the primary GraphSet scale and grid. Scale scale=graphSet.getScale(); Grid grid=graphSet.getGrid(); scale.setAutoScaleEnabled(false); scale.setMaxValue(140); scale.setIncrement(30); grid.setEnabled(true); grid.setStyle(Grid.DASHED); Container ct=getContentPane(); ct.add(graph); ct.add("South",bottomPanel); setSize(550,450); setVisible(true); } // Implements the ZoomListener interface. Each time chart is zoomed, // a ZoomEvent object is dispatched to this method. public void chartZoomed(ZoomEvent evt) { // A ZoomEvent object carries information concerning the maximum // number of data points, the selected number of data points and // the first value in the sequence of selected data points. These // three values are used to configure the properties of a scrolling // bar. if (evt.getZoomType()==ZoomEvent.ZOOM_IN) { // If chart is zoomed-in... scrollBar.setMaximum(evt.getMaximum()); scrollBar.setVisibleAmount(evt.getVisibleAmount()); scrollBar.setValue(evt.getValue()); } else if (evt.getZoomType()==ZoomEvent.ZOOM_OUT) { // If chart is zoomed-out... scrollBar.setMaximum(0); scrollBar.setVisibleAmount(0); scrollBar.setValue(0); } } public void adjustmentValueChanged(AdjustmentEvent evt) { int startIndex=evt.getValue(); int endIndex=evt.getValue()+scrollBar.getVisibleAmount()-1; // Adjusts data points delimiters to reflect the range // selected with the scrolling bar. graph.setDataSetRange(startIndex,endIndex); graph.repaint(); } public static void main(String[] args) { new Main(); } }