import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.ibm.eou.swingchart.*;

/**
  ChartReducerDemo

  This sample shows how to use the StackChart bean to graph
  sales data manipulated by the chart data reducer.

  Demonstrates the following techniques:

  Customizing the data reducing method
  Customizing the number of columns to appear in the chart
  Setting chart values from the output of the reducer

  (c) Copyright IBM Corp. 1998,1999
  */

public class ChartReducerDemo extends JApplet implements ItemListener
{

        // Constants

        private static double[][] INPUT_VALUES = {
                {1.23, 2.34, 3.45, 3.56, 1.23, 3.56, 4.34, 5.23, 3.23, 3.23, 4.45, 6.39},
                {3.56, 4.52, 1.23, 8.75, 5.35, 5.34, 4.56, 5.83, 6.12, 6.12, 4.12, 7.34},
                {2.45, 5.34, 3.65, 5.67, 5.24, 8.90, 7.23, 9.02, 9.13, 9.35, 9.40, 9.41},
                {1.23, 4.72, 7.45, 8.36, 9.71, 7.59, 8.50, 9.24, 9.56, 9.86, 9.91, 9.99},
                {1.45, 3.54, 3.67, 5.24, 7.25, 7.86, 8.01, 8.34, 8.76, 8.45, 6.34, 8.60},
                {1.45, 4.67, 4.44, 5.78, 2.33, 5.78, 6.03, 6.21, 6.42, 6.34, 6.54, 7.23}};

        private static String[] REDUCTION_NAMES = {"First",
                                                       "Middle",
                                                       "Last",
                                                       "Filter",
                                                       "Smooth"};

        private static int[] REDUCTION_TYPES = {ChartDataReducer.FIRST, ChartDataReducer.MIDDLE, ChartDataReducer.LAST, ChartDataReducer.FILTER, ChartDataReducer.SMOOTH};

        // Choices and labels

        private Choice iReductionChoice = new Choice();
        private Choice iNumberOutputChoice = new Choice();
        private JLabel iMainLabel = new JLabel("Sports Store: Sales for 1997 - 12 months of data for the 6 products");
        private JLabel iReductionLabel = new JLabel("Reduction type");
        private JLabel iColsLabel = new JLabel("No. of output cols");

        // Chart

        private StackChart iChart = new StackChart();

        // Reducer

        private ChartDataReducer iReducer = new ChartDataReducer();
/**
   Customizes the chart to the specified chart type, product group, and style
   */

private void customizeChart()
{
        String[] legends = {"Shorts", "Shirts", "Socks", "Boots", "Track Suits", "Footballs"};

        // Set the properties for the reducer

        iReducer.setValues(INPUT_VALUES);
        iReducer.setReducingRule(REDUCTION_TYPES[iReductionChoice.getSelectedIndex()]);
        iReducer.setMaxOutputColumns(iNumberOutputChoice.getSelectedIndex());

        // Get chart data from the reducer and set the legends

        iChart.setValues(iReducer.getOutputValues());
        iChart.setLegends(legends);
}

/**
   Called at applet startup to initialize the chart and other components.
   */

public void init()
{

        // Setup and size the applet

        getContentPane().setBackground(Color.white);
        getContentPane().setLayout(null);
        setSize(460, 250);

        // Set up choice controls and labels

        setupControls();

        // Position and add the chart component

        iChart.setBounds(150, 40, 300, 200);
        getContentPane().add(iChart);

        // Customize the chart

        customizeChart();
}
// Implementation of interface ItemListener

/**
   Responds to changed choice selections by updating the chart
   to reflect select product group. chart type, and style.
   */

public void itemStateChanged(ItemEvent e)
{
        customizeChart();
}
/**
   Sets up choices and labels
   */

private void setupControls()
{

        // Main label

        iMainLabel.setBounds(10, 5, 600, 20);
        getContentPane().add(iMainLabel);

        // Reduction method choice

        getContentPane().add(iReductionLabel);
        iReductionLabel.setBounds(10, 40, 120, 20);
        for (int i = 0; i < REDUCTION_NAMES.length; ++i)
        {
                iReductionChoice.add(REDUCTION_NAMES[i]);
        }
        getContentPane().add(iReductionChoice);
        iReductionChoice.select(0);
        iReductionChoice.setBounds(10, 60, 120, 20);
        iReductionChoice.addItemListener(this);

        // Data range choice

        getContentPane().add(iColsLabel);
        iColsLabel.setBounds(10, 90, 120, 20);
        int noCols = INPUT_VALUES[0].length;
        for (int i = 0; i <= noCols; i++)
        {
                iNumberOutputChoice.add("" + i);
        }
        getContentPane().add(iNumberOutputChoice);
        iNumberOutputChoice.select(noCols);
        iNumberOutputChoice.setBounds(10, 110, 120, 20);
        iNumberOutputChoice.addItemListener(this);
  }
}