Compounding Example
Generates a new graph element by automatically compounding data from another graph element.
This example demonstrates the use of CompoundingGraphModel.
Here, the same DataSource is presented in two separate Graphs.
In the top Graph the DataSource is presented through
a regular GraphModel as plain data, a sequence of random numbers
between -0.15 and +0.17. In the bottom Graph
the DataSource is used to construct a CompoundingGraphModel,
which uses the numbers shown above as percent changes from a starting value
of 10,000.
back to index
The source code:
|
/**
* @version $Id: CompoundingExample.java,v 1.2 2000/11/13 18:47:16 Administrator Exp $
*/
import com.smartmoney.webgraph.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* CompoundingExample
*
* @author <a href="mailto:jdf@pobox.com">Jonathan Feinberg</a>
*/
public class CompoundingExample
extends Applet
implements AdjustmentListener
{
static final int POINTS = 200;
static final int THUMB_PERCENT = 10;
static final int INITIAL_PERCENT = 60;
NumericGraph graph1, graph2;
double[] xvalues;
Scrollbar panner, zoomer;
int visiblePoints = POINTS;
// 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);
}
graph1.setView(xvalues[left],
xvalues[left + visiblePoints - 1]);
graph2.setView(xvalues[left],
xvalues[left + visiblePoints - 1]);
}
public void init()
{
setBackground(Color.white);
setLayout(new BorderLayout());
graph1 = new NumericGraph();
graph2 = new NumericGraph();
graph1.addCalloutListener(graph2);
graph2.addCalloutListener(graph1);
graph1.setMessage("CompoundingGraphModel");
graph2.setMessage("GraphModel");
Panel p = new Panel();
p.setLayout(new GridLayout(2, 1));
p.add(graph2);
p.add(graph1);
add(p, BorderLayout.CENTER);
xvalues = new double[POINTS];
double[] performance = new double[xvalues.length];
for (int i = 0; i < xvalues.length; i++)
{
xvalues[i] = i;
performance[i] = (-.15 + Math.random() * .32);
}
DataSource ds = new SimpleDataSource(xvalues, performance);
CompoundingGraphModel cgm = new CompoundingGraphModel(ds, 10000.00);
GraphElement fundElement = new LineElement(cgm);
fundElement.setColor(Color.blue);
graph1.addGraphElement(fundElement);
graph1.setDrawXValue(false);
graph1.setXLabelFormatter(Formatters.PLAIN);
GraphModel vgm = new GraphModel(ds);
GraphElement performanceElement = new LineElement(vgm);
graph2.addGraphElement(performanceElement);
graph2.setDrawXLabels(false);
graph2.setDrawXValue(false);
graph2.setAutoScale(false);
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);
add(panner, BorderLayout.SOUTH);
}
}
Copyright (c) 2006 SmartMoney.com
|