////******************************************************************** // Histogram.java // //******************************************************************** // A simple histogram class. The setData(float f) finds in which bin // the value falls for nBins between the given minimum and maximum // values. An integer array keeps track of the number of times the input // value fell into a particular bin. // The DataChart class is used to display the histogram. This class // in turn uses the BarChart tool which needs the data passed as a // string array. This is not optimal but OK for demonstration purposes. // //******************************************************************** import java.awt.*; import java.applet.Applet; //******************************************************************** public class Histogram { int [] bins = null; int nBins; float xLow,xHigh; float delBin; int overFlows=0,underFlows=0; DataChart chart; String dataString=null; //---------------------------------------------------------------- Histogram (int nBins, float xLow, float xHigh,int _width,int _height){ this.nBins = nBins; this.xLow = xLow; this.xHigh = xHigh; bins = new int[nBins]; delBin = (xHigh-xLow)/(float)nBins; reset(); chart = new DataChart(dataString, _width, _height); } //---------------------------------------------------------------- // Extra constructor to allow for double values Histogram (int nBins, double xLow, double xHigh,int _width,int _height){ this(nBins, (float) xLow, (float) xHigh, _width, _height); } //---------------------------------------------------------------- void setData(double data){ setData((float)data); } //---------------------------------------------------------------- void setData(float data){ if( data < xLow) underFlows++; else if ( data >= xHigh) overFlows++; else{ int bin = (int)((data-xLow)/delBin); if(bin >=0 && bin < nBins) bins[bin]++; } } //---------------------------------------------------------------- // To display the histogram in a chart, we need to pass the data // as a string. public void graphIt(){ dataString = ""; for (int i=0; i