Index page

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

Normalize Example
Normalizes data to compare the relative percent changes of the various sets of data.

This example demonstrates the functional use of setNormalize().

back to index

The source code:

/**
 * @version $Id: NormalizeExample.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.*;

/**
 * NormalizeExample
 *
 * Demonstrates hiding/showing of graph elements; demonstrates
 * normalizingraph.  See StockExample.java for a more detailed discussion
 * of normalizingraph.
 *
 * @author <a href="mailto:jdf@pobox.com">Jonathan Feinberg</a>
 */
public class NormalizeExample
        extends Applet
        implements ItemListener
{
    static final private int size = 60;
    static final private int seriesCount = 5;
    static final private Color[] colors = new Color[]{
        Color.red, Color.blue, Color.green, Color.gray, Color.orange
    };

    Checkbox cbNormalize;
    Checkbox[] cbSeries;
    GraphElement[] elements;
    Graph graph;

    public void init()
    {
        graph = new BusinessDateGraph();
        Panel cbp = new Panel();
        cbp.setBackground(Color.white);

        cbNormalize = new Checkbox("Normalize");
        addCheckboxTo(cbNormalize, cbp);
        graph.setNormalize(true);

        ParameterParser parser = new ParameterParser(this);

        elements = new GraphElement[seriesCount];
        cbSeries = new Checkbox[seriesCount];
        for (int i = 0; i < elements.length; i++)
        {
            FakeStock stock = new FakeStock(size);
            elements[i] = new LineElement
                    (new GraphModel(new SimpleDataSource(stock.dates, stock.close)));
            graph.addGraphElement(elements[i]);
            String param = "line" + (i + 1) + "color";
            elements[i].setColor(parser.getColor(param, colors[i % colors.length]));
            String label = (new Integer(i + 1)).toString();
            elements[i].setLabel("Series " + label + ":");
            cbSeries[i] = new Checkbox(label);
            addCheckboxTo(cbSeries[i], cbp);
        }

        setLayout(new BorderLayout());
        add(graph, BorderLayout.CENTER);
        add(cbp, BorderLayout.NORTH);

        GraphParameterParser.parse(graph, parser);
    }

    private void addCheckboxTo(Checkbox cb, Panel p)
    {
        cb.setState(true);
        cb.addItemListener(this);
        p.add(cb);
    }

    public void itemStateChanged(ItemEvent e)
    {
        for (int i = 0; i < seriesCount; i++)
            elements[i].setVisible(cbSeries[i].getState());
        graph.setNormalize(cbNormalize.getState());
    }
}



Copyright (c) 2006 SmartMoney.com