Index page

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

Line Element Example
Shows addition of multiple graph lines to the chart.

This example sheds light on the construction of LineElement objects (and, by extension, other SimpleElement objects.

back to index

The source code:

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

import com.smartmoney.webgraph.*;

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

/**
 * LineElementExample
 *
 * Demonstrates the use of LineElement (and, by extension, any
 * implementor of SimpleElement) in rendering a GraphModel.  In this
 * case we build a SimpleDataSource with several Y values per X value,
 * construct a GraphModel from the SimpleDataSource, and
 * instruct each of the LineElements to render a chosen Y value
 * in that model.
 *
 * @author <a href="mailto:jdf@pobox.com">Jonathan Feinberg</a>
 */
public class LineElementExample
        extends Applet
{
    static final private int size = 500;

    public void init()
    {
        NumericGraph graph = new NumericGraph();

        double[] xvalues = new double[size];
        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 * 4 * 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];
        }

        // For each X value, this model can provide sin(X), cos(X),
        // and f3(x).
        DataSource ds = new SimpleDataSource(xvalues,
                new double[][]{sin, cos, f3});
        GraphModel model = new GraphModel(ds);

        // Create an element that renders the first (0th) Y value
        // provided by the GraphModel "model".
        GraphElement sineElement = new LineElement(model, 0);
        sineElement.setLabel("sine x =");
        sineElement.setColor(Color.blue);

        // Create an element that renders the second (index 1) Y value
        // provided by the GraphModel "model".
        GraphElement cosineElement = new LineElement(model, 1);
        cosineElement.setLabel("cosine x =");
        cosineElement.setColor(Color.red);

        // Create an element that renders the third (index 2) Y value
        // provided by the GraphModel "model".
        GraphElement modulatedElement = new LineElement(model, 2);
        modulatedElement.setLabel("f(x) =");
        modulatedElement.setColor(Color.green);

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

        setLayout(new BorderLayout());
        add(graph, BorderLayout.CENTER);
    }
}



Copyright (c) 2006 SmartMoney.com