// PropHistogram.java // // Author: Rahul Simha // Mar, 2008 // // A simple proportional histogram and display import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import java.text.*; public class PropHistogram extends JPanel { double[] counts; // The number of data values that fall in i-th bin. double[] heights; // The height, for a relative histogram. double delta; // Width of each bin. int numIntervals; // Number of bins. double left, right; // Left and right ends of interval. All other values are discarded. int numSamples; // # data points added. // To prettify. DecimalFormat df = new DecimalFormat(); // GUI stuff. int inset=60; // Inset of axes and bounding box. public PropHistogram (double left, double right, int numIntervals) { this.left = left; this.right = right; this.numIntervals = numIntervals; delta = (right-left) / numIntervals; counts = new double [numIntervals]; heights = new double [numIntervals]; numSamples = 0; df.setMaximumFractionDigits (4); } public void add (double x) { numSamples ++; int bin = (int) ((x-left) / delta); if ( (bin >= 0) && (bin < numIntervals) ) { counts[bin] ++; } else { // Discard. } } void computeHeights () { for (int i=0; i maxY) { maxY = heights[i]; } } // We'll make only 10 ticks. int numYTicks = 10; double yDelta = maxY / numYTicks; for (int i=0; i