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

/**
  ChartDemo1

  This sample shows how to use the UniversalChart bean to graph
  sales data derived from a data provider class.
  

Demonstrates the following techniques:

(c) Copyright IBM Corp. 1998,1999 */ public class ChartDemo1 extends JApplet implements ItemListener { // Constants private static String [] CHART_NAMES = {"Parallel bar chart", "Pie chart", "Scatter chart", "Stacked bar chart", "Surface chart"}; private static int [] CHART_TYPES = {Chart.PARALLEL, Chart.PIE, Chart.SCATTER, Chart.STACK, Chart.SURFACE}; // Choices and labels private Choice iChartChoice = new Choice(); private Choice iDataChoice = new Choice(); private Choice iStyleChoice = new Choice(); private JLabel iMainLabel = new JLabel("Sound and vision: sales history"); private JLabel iChartLabel = new JLabel("Chart type"); private JLabel iDataLabel = new JLabel("Product group"); private JLabel iStyleLabel = new JLabel("Style"); // Fonts private final Font iInformalFontBig = new Font("SansSerif", Font.ITALIC, 20); private final Font iFormalFontBig = new Font("Serif", Font.BOLD, 20); private final Font iNegativeFontBig = new Font("Courier", Font.PLAIN, 20); private final Font iInformalFont = new Font("SansSerif", Font.ITALIC, 12); private final Font iFormalFont = new Font("Serif", Font.BOLD, 12); private final Font iNegativeFont = new Font("Courier", Font.PLAIN, 12); private final Font [] iFonts = {iInformalFont, iFormalFont, iNegativeFont}; private final Font [] iBigFonts = {iInformalFontBig, iFormalFontBig, iNegativeFontBig}; // Colors private final Color [] iBackgroundColors = {new Color(255, 255, 200), new Color(204, 204, 204), Color.black}; private final Color [] iForegroundColors = {Color.black, new Color(0,0,128), Color.white}; // Chart private UniversalChart iChart = new UniversalChart(); // Data source private final SalesData iSalesData = new SalesData(); /** Called at applet startup to initialize the chart and other components. */ public void init() { // Setup and size the applet 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(); // Customize the panel customizePanel(); } /** Customizes the chart to the specified chart type, product group, and style */ private void customizeChart() { double values [][] = null; String [] legends = null; // Set the data and legends for the specified product group int dataSet = iDataChoice.getSelectedIndex(); switch (dataSet) { case 0: { values = new double [][] {iSalesData.getValues(SalesData.PERSONAL), iSalesData.getValues(SalesData.AUDIO)}; legends = new String [] {SalesData.PERSONAL, SalesData.AUDIO}; } break; case 1: { values = new double [][] {iSalesData.getValues(SalesData.TELEVISION), iSalesData.getValues(SalesData.VIDEO)}; legends = new String [] {SalesData.TELEVISION, SalesData.VIDEO}; } break; case 2: { values = new double [][] {iSalesData.getValues(SalesData.PERSONAL), iSalesData.getValues(SalesData.AUDIO), iSalesData.getValues(SalesData.TELEVISION), iSalesData.getValues(SalesData.VIDEO)}; legends = new String [] {SalesData.PERSONAL, SalesData.AUDIO,SalesData.TELEVISION, SalesData.VIDEO}; } break; } iChart.setValues(values); iChart.setLegends(legends); iChart.setLabels(iSalesData.getLabels()); // Set the chart type to the specified type iChart.setChartType(CHART_TYPES[iChartChoice.getSelectedIndex()]); // Set colors and font from the specified style int style = iStyleChoice.getSelectedIndex(); iChart.setFont(iFonts[style]); iChart.setForeground(iForegroundColors[style]); iChart.setBackground(iBackgroundColors[style]); repaint(); } /** Customizes colors and fonts for other components using the specified style */ private void customizePanel() { // Get the selected style int style = iStyleChoice.getSelectedIndex(); // Set the applet background getContentPane().setBackground(iBackgroundColors[style]); // Set the label colors Component[] components = getContentPane().getComponents(); for (int i =0 ; i < components.length; ++i) { if (components[i] instanceof JLabel) { components[i].setForeground(iForegroundColors[style]); components[i].setBackground(iBackgroundColors[style]); } } // Set the label fonts iMainLabel.setFont(iBigFonts[style]); iChartLabel.setFont(iFonts[style]); iDataLabel.setFont(iFonts[style]); iStyleLabel.setFont(iFonts[style]); } /** Sets up choices and labels */ private void setupControls() { // Main label iMainLabel.setBounds(10, 5, 600, 20); getContentPane().add(iMainLabel); // Chart type choice getContentPane().add(iChartLabel); iChartLabel.setBounds(10, 40, 120, 20); for (int i = 0; i < CHART_NAMES.length; ++i) { iChartChoice.add(CHART_NAMES[i]); } getContentPane().add(iChartChoice); iChartChoice.select(0); iChartChoice.setBounds(10, 60, 120, 20); iChartChoice.addItemListener(this); // Data range choice getContentPane().add(iDataLabel); iDataLabel.setBounds(10, 90, 120, 20); iDataChoice.add("Sound"); iDataChoice.add("Vision"); iDataChoice.add("All"); getContentPane().add(iDataChoice); iDataChoice.select(0); iDataChoice.setBounds(10, 110, 120, 20); iDataChoice.addItemListener(this); // Chart style choice getContentPane().add(iStyleLabel); iStyleLabel.setBounds(10, 140, 120, 20); iStyleChoice.add("Informal"); iStyleChoice.add("Formal"); iStyleChoice.add("Negative"); getContentPane().add(iStyleChoice); iStyleChoice.select(0); iStyleChoice.setBounds(10, 160, 120, 20); iStyleChoice.addItemListener(this); } // 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(); customizePanel(); } } /** Simple class used to simulate a real data provider. */ class SalesData { // Constants public final static String PERSONAL = "Personal"; public final static String AUDIO = "Audio"; public final static String TELEVISION = "Television"; public final static String VIDEO = "Video"; final static int YEARS = 6; final static int FIRST_YEAR = 1993; /** Returns a 1 dimensional array of values for a given product range. @param SalesData.PERSONAL, SalesData.AUDIO, SalesData.TELEVISION, SalesData.VIDEO */ double [] getValues(String productRange) { if (productRange.equals(PERSONAL)) { return new double [] {125640, 786453, 675430, 786543, 45321, 867543}; } else if (productRange.equals(AUDIO)) { return new double [] {257640, 645453, 775430, 886543, 55321, 767543}; } else if (productRange.equals(TELEVISION)) { return new double [] {147640, 245353, 265430, 656543, 35321, 578543}; } else if (productRange.equals(VIDEO)) { return new double [] {265640, 439353, 355430, 759641, 48561, 789013}; } else return null; } String [] getLabels() { String [] labels = new String [ YEARS ]; int firstYear = FIRST_YEAR; for (int i=0; i < YEARS; i++) { labels [i] = "" + (FIRST_YEAR+i); } return labels; } }