/* * AnimatedGraphPaper * * Version 1.1 - Java 1.1 compatible * * Dec 6 2001 * * Copyright 2001 David Joiner and the Shodor Education Foundation */ import java.awt.*; import java.awt.event.*; import com.helplets.awt.*; /** * The AnimatedGraphPaper class displays a set of bar graphs * based on data over a range of time. A NumAnimatedSlider * lets the user view the data at a given time. */ public class AnimatedGraphPaper extends Panel implements ActionListener { /* * Members */ protected String title; protected String unit; protected AGPBarGraph theGraph; protected AGPBarData [] theData; protected int nBar; protected int maxBar; protected NumAnimatedSlider theSlider; /* * Constructor */ public AnimatedGraphPaper() { super(); theGraph = new AGPBarGraph(); setLayout (new PercentLayout()); theSlider = new NumAnimatedSlider(); add("left=0, right=100, top=0, bottom=90",theGraph); add("left=0, right=100, top=90, bottom=100",theSlider); theSlider.addActionListener(this); } /* * Drawing methods */ public void drawAll() { transferData(); theSlider.invalidate(); theGraph.drawBars(); } /* * Get and set methods */ public void reset() { nBar=0; } public void setUnit(String unit) { if (unit != null) this.unit = new String(unit); else this.unit = null; if (theGraph != null) theGraph.setUnit(unit); } public String getUnit() { return unit; } public void setTitle(String title) { if (title != null) this.title = new String(title); else this.title = null; if (theGraph != null) theGraph.setTitle(title); } public String getTitle() { return title; } public void setGraphMax(double graphMax) { if (theGraph != null) theGraph.setGraphMax(graphMax); } public double getGraphMax() { if (theGraph != null) return theGraph.getGraphMax(); else return 0.0; } public void setGraphMin (double graphMin) { if (theGraph != null) theGraph.setGraphMin(graphMin); } public double getGraphMin() { if (theGraph != null) return theGraph.getGraphMin(); else return 0.0; } public void setValues(int num, Color color, double [] varIndependent, double [] varDependent, String labelDependent) { checkBar(nBar+1); nBar++; theData[nBar-1] = new AGPBarData(num, color, varIndependent, varDependent, labelDependent); } public void checkBar(int n) { if (n>maxBar) { increaseBar(Math.max(2*maxBar,1)); } } public void increaseBar(int n) { AGPBarData [] tempData = new AGPBarData[n]; maxBar = n; for(int i=0;i0) { min=theData[0].indVariable[0]; max=theData[0].indVariable[theData[0].n - 1]; } for (int i=1;imax) max = theData[i].indVariable[theData[i].n-1]; } theSlider.setRealMin(min); theSlider.setRealMax(max); //convert value to the same scale as the independent // variables. double value = theSlider.getRealValue(); double [] height = new double[nBar]; Color [] color = new Color[nBar]; String [] label = new String[nBar]; for (int i=0;i= theData[i].indVariable[j] && value < theData[i].indVariable[j+1]) { done = true; lowerIndex=j; } } if (!done) lowerIndex = theData[i].n-2; height[i] = theData[i].depVariable[lowerIndex]+ (value-theData[i].indVariable[lowerIndex])/ (theData[i].indVariable[lowerIndex+1]-theData[i].indVariable[lowerIndex])* (theData[i].depVariable[lowerIndex+1]-theData[i].depVariable[lowerIndex]); color[i] = theData[i].color; label[i] = theData[i].label; } theGraph.setBars(nBar,height, color, label); theGraph.drawBars(); } }