Package sim.util.gui

Class MiniHistogram

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable

public class MiniHistogram extends JComponent

A very simple histogram class. Displays values as different colors left to right. Tooltips pop up describing the actual buckets and their densities.

This class can be used to describe any one-dimensional array of doubles: just provide an array of doubles to setBuckets and it'll show them on-screen. If you change the doubles in that array, no need to call setBuckets again: as soon as a repaint occurs, MiniHistogram will update them. You can also provide a set of bucket labels, one for each bucket, which get popped up in the tooltip along with the current bucket value.

As the class was created to describe histograms in particular, it has two functions (makeBuckets and makeBucketLabels) which do just that. Sample usage. The following values are placed into a histogram of four buckets.

    MiniHistogram m = new MiniHistogram();
    double[] d = new double[] {1, 1, 1, 1, 2, 2, 5, 1, 2, 3, 1, 2, 1, 1, 5, 4};
    m.setBuckets(m.makeBuckets(d, 4, 1, 5, false));
    m.setBucketLabels(m.makeBucketLabels(4, 1, 5));
    
See Also:
  • Constructor Details

    • MiniHistogram

      public MiniHistogram()
    • MiniHistogram

      public MiniHistogram(double[] buckets, String[] labels)
  • Method Details

    • getPreferredSize

      public Dimension getPreferredSize()
      Overrides:
      getPreferredSize in class JComponent
    • getMinimumSize

      public Dimension getMinimumSize()
      Overrides:
      getMinimumSize in class JComponent
    • setBuckets

      public void setBuckets(double[] buckets)
      Sets the displayed bucket array. If you change values in the provided array, then MiniHistogram will update itself appropriately on next repaint.
    • setBucketLabels

      public void setBucketLabels(String[] labels)
      Sets labels for the buckets provided in setBuckets.
    • setBucketsAndLabels

      public void setBucketsAndLabels(double[] buckets, String[] labels)
    • paintComponent

      public void paintComponent(Graphics graphics)
      Overrides:
      paintComponent in class JComponent
    • makeBucketLabels

      public static String[] makeBucketLabels(int numBuckets, double min, double max, boolean logScale)
      Generates a set of numBuckets bucket labels appropriate for use in a histogram. The values numBuckets, min, and max should be the same values you had provided to makeBuckets(...). Pass the resulting array into the setBuckets(...) function.
    • minimum

      public static double minimum(double[] vals)
      Returns the minimum over the provided vals. You might use this to set the minimum in makeBuckets if you don't have a prescribed minimum
    • maximum

      public static double maximum(double[] vals)
      Returns the minimum over the provided vals. You might use this to set the minimum in makeBuckets if you don't have a prescribed minimum
    • makeBuckets

      public static double[] makeBuckets(double[] vals, int numBuckets, double min, double max, boolean logScale)
      Generates a set of numBuckets buckets describing a histogram over the provided values in vals. min and max describe the ends of the histogram, inclusive: values outside those ranges are discarded. You can tell the histogram to bucket based on a log scale if you like (min and max are computed prior to log)), and in this case all values less than 0 are discarded as well. Pass the resulting array into the setBuckets(...) function.