Index page

Stock Example
Minimal Example
Line Element Example
Scrolling Example
Normalize Example
Compounding Example
Dynamic Data Example
     Example index : Scrolling Example

Scrolling Example
Uses scrollbars to pan and zoom in on the graph.

This example demonstrates the setView() method and shows one way of using scrollbars for "pan" and "zoom". In the example below, the top scrollbar zooms while the bottom scrollbar pans.

back to index

The source code:

/**
 * @version $Id: ScrollingExample.java,v 1.2 2000/11/13 18:47:17 Administrator Exp $
*/

import com.smartmoney.webgraph.*;
import com.smartmoney.webgraph.stats.*;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/**
 * ScrollingExample
 *
 * Demonstrates the use of the Graph setView() method.
 *
 * @author <a href="mailto:jdf@pobox.com">Jonathan Feinberg</a>
 */
public class ScrollingExample
	extends Applet
	implements AdjustmentListener
{
	static final int POINTS = 1200;
	static final int THUMB_PERCENT = 10;
	static final int INITIAL_PERCENT = 60;

	NumericGraph graph;
	double[] xvalues;
	Scrollbar panner, zoomer;

	int visiblePoints;

	// The scrollbars represent values from THUMB_PERCENT to 100.
	// Calculate the number of points currently visible based on the
	// current setting of the zoomer scrollbar (visiblePoints), and
	// set the graph view to look at data between the current setting
	// of the panner scrollbar (left) and left + visiblePoints.
	public void adjustmentValueChanged(AdjustmentEvent ae)
	{
		int left = panner.getValue();
		if (ae.getAdjustable() == zoomer) {
			int zoom = zoomer.getValue();
			visiblePoints = POINTS * (zoom + THUMB_PERCENT) / 100;
			if (left + visiblePoints >= POINTS)
				left -= left + visiblePoints - POINTS;
			panner.setValues( left, visiblePoints, 0, POINTS );
		}
		graph.setView(xvalues[left],
					  xvalues[left + visiblePoints - 1]);
	}

	public void init()
	{
		setBackground(Color.white);
		setLayout(new BorderLayout());

		graph = new NumericGraph();
		add(graph, BorderLayout.CENTER);

		xvalues = new double[POINTS];
		double[] sin = new double[xvalues.length];
		double[] cos = new double[xvalues.length];
		double[] f3 = new double[xvalues.length];
		for (int i = 0; i < xvalues.length; i++) {
			xvalues[i] = (Math.PI * 6 * i / xvalues.length) -.2;
			sin[i] = Math.sin(xvalues[i]);
			cos[i] = Math.cos(xvalues[i]);
			f3[i] = .15 + Math.sin(3*xvalues[i]) / 5*xvalues[i];
		}

		DataSource data = new SimpleDataSource(xvalues,
											   new double[][] { sin, cos, f3 });
		GraphModel model = new GraphModel(data);

		ParameterParser parser = new ParameterParser(this);

		GraphElement sineElement = new LineElement(model, 0);
		sineElement.setLabel("sine x =");
		sineElement.setColor(parser.getColor("line1color", Color.blue));

		GraphElement cosineElement = new LineElement(model, 1);
		cosineElement.setLabel("cosine x =");
		cosineElement.setColor(parser.getColor("line2color", Color.red));

		GraphElement modulatedElement = new LineElement(model, 2);
		modulatedElement.setLabel("f(x) =");
		modulatedElement.setColor(parser.getColor("line3color", Color.green));

		graph.addGraphElement(modulatedElement);
		graph.addGraphElement(sineElement);
		graph.addGraphElement(cosineElement);

		graph.setAutoScale(false);

		GraphParameterParser.parse( graph, parser );

		zoomer = new Scrollbar(Scrollbar.HORIZONTAL,
							   INITIAL_PERCENT, THUMB_PERCENT, 0, 100);
		zoomer.addAdjustmentListener(this);
		add(zoomer, BorderLayout.NORTH);

		visiblePoints = POINTS * (INITIAL_PERCENT + THUMB_PERCENT) / 100;
		panner = new Scrollbar(Scrollbar.HORIZONTAL,
							   0, visiblePoints, 0, POINTS);
		panner.addAdjustmentListener(this);
		graph.setView(xvalues[0],
					  xvalues[visiblePoints - 1]);
		add(panner, BorderLayout.SOUTH);
	}
}



Copyright (c) 2006 SmartMoney.com