Class Histogram

All Implemented Interfaces:
Data, Drawable, LogMeasurable, Measurable

public class Histogram extends DataTable.DataModel implements Measurable, LogMeasurable, Data
Histogram maps bin number to occurrences. Histogram is Drawable and can be rendered on a DrawingPanel. Histogram also implements TableModel and can be displayed in a JTable. By default, bins consist of (notation: [ inclusive, ) exclusive): ..., [-1,0), [0,1), [1,2), ...
Version:
1.1
Author:
Joshua Gould, Wolfgang Christian
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static class 
    A class to save and load Dataset data in an XMLControl.
    class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    boolean
    Should the height be adjusted by bin width? Default is false.
    protected Color
    color of bins
    protected Color
    color of bins
    protected int
    style for drawing bins
    protected int
    an integer ID that identifies this object
    static final int
    draw bin from y min to top of bin
    static final int
    draw point at top of bin
    boolean
    Should histogram be drawn on a log scale? Default is false.
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Histogram constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    append(double value)
    Appends a value with 1 occurence.
    void
    append(double[] values)
    Appends an array of values with 1 occurence.
    void
    append(double value, double numberOfoccurrences)
    Append a value with number of occurrences to the Histogram.
    void
    append(String inputPathName)
    Appends values from an input file.
    void
    Clears all data from this histogram and resets min and max values.
    void
    draw(DrawingPanel drawingPanel, Graphics g)
    Draws this histogram in the drawing panel.
    protected void
    drawBin(DrawingPanel drawingPanel, Graphics g, int binNumber, double occurrences)
    Draws a filled bin.
    protected void
    drawPoint(DrawingPanel drawingPanel, Graphics g, int binNumber, double occurrences)
    Draws a point at the top of a bin.
    double
    Gets the offset of the bins.
    double
    Gets the width of a bin.
    int
    Gets the name of the colummn for rendering in a JTable
    getColumnName(int column)
    Gets the name of the column for rendering in a JTable
    The column names to be used in the data display tool
    double[][]
    Gets a 2D array of data.
    double[][][]
    Gets a 3D array of data.
    Some elements (a Group, for instance) do not contain data, but a list of subelements which do.
    Gets a list of OSP Datasets.
    Fill color to use for this data
    Fill colors for Data interface.
    int
    Returns a unique identifier for this Data.
    double
    getLeftMostBinPosition(int binNumber)
    Method getLeftMostBinPosition
    Line color to use for this data
    Line colors for Data interface.
    Returns the XML.ObjectLoader for this class.
    double[][]
    Gets a data array containing both the bin centers and the values within the bins.
    Gets the dataset name.
    double[][]
    Gets a data array containing both the bin centers and the values within the bins.
    double
    getRightMostBinPosition(int binNumber)
    Method getRightMostBinPosition
    int
    Gets the number of rows for rendering in a JTable.
    double
    getValueAt(int row, int column)
    Gets a bin number or occurrences for bin number for rendering in a JTable.
    double
    Gets the x world coordinate for the right hand side of this histogram.
    double
    Gets the maximum x needed to draw this object on a log scale.
    double
    Gets the x world coordinate for the left hand side of this histogram.
    double
    Gets the minimum x needed to draw this object on a log scale.
    double[]
    Gets an array containing the bin centers.
    double
    Gets the y world coordinate for the top of this histogram.
    double
    Gets the maximum y needed to draw this object on a log scale on a log scale.
    double
    Gets the y world coordinate for the bottom of this histogram.
    double
    Gets the minimum y needed to draw this object on a log scale.
    double[]
    Gets an array containing the values within the bins.
    int
    hashCode(double value)
    Computes the hash code (bin number) for the specified value
    boolean
    Gets the valid measure flag.
    void
    read(String inputPathName)
    Reads a file and appends the data contained in the file to this Histogram.
    void
    setBarOffset(double _barOffset)
    Set the offset of the bars as a fraction of a bin width.
    void
    setBinColor(Color binColor)
    Sets the bin color.
    void
    setBinColor(Color fillColor, Color edgeColor)
    Sets the bin's fill and edge colors.
    void
    setBinOffset(double _binOffset)
    Sets the offset of the bins.
    void
    setBinStyle(int style)
    Sets the style for drawing this histogram.
    void
    setBinWidth(double _binWidth)
    Sets the width of a bin.
    void
    setDiscrete(boolean _discrete)
    Sets the discrete flag.
    void
    setID(int id)
    Sets the ID number of this Data.
    void
    setMeasured(boolean measure)
    Forces the measured condition of the histogram
    void
    Sets a name that can be used to identify the dataset.
    void
    setNormalizedToOne(boolean b)
    Normalizes the occurrences in this histogram to one.
    void
    setVisible(boolean visibility)
    Sets the visibility of the histogram
    void
    setXYColumnNames(String _xColumnName, String _yColumnName)
    Sets the column names when rendering this histogram in a JTable.
    void
    setXYColumnNames(String _xColumnName, String _yColumnName, String _name)
    Sets the column names when rendering this histogram in a JTable.
    Creates a string representation of this Histogram.
    Creates a string representation of this Histogram.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.opensourcephysics.display.Drawable

    isInteractive
  • Field Details

    • model

      public final TableModel model
    • DRAW_POINT

      public static final int DRAW_POINT
      draw point at top of bin
      See Also:
    • DRAW_BIN

      public static final int DRAW_BIN
      draw bin from y min to top of bin
      See Also:
    • logScale

      public boolean logScale
      Should histogram be drawn on a log scale? Default is false.
    • adjustForWidth

      public boolean adjustForWidth
      Should the height be adjusted by bin width? Default is false.
    • binFillColor

      protected Color binFillColor
      color of bins
    • binEdgeColor

      protected Color binEdgeColor
      color of bins
    • binStyle

      protected int binStyle
      style for drawing bins
    • datasetID

      protected int datasetID
      an integer ID that identifies this object
  • Constructor Details

    • Histogram

      public Histogram()
      Histogram constructor.
  • Method Details

    • read

      public void read(String inputPathName) throws IOException
      Reads a file and appends the data contained in the file to this Histogram. The format of the file is bins \t occurrences. Lines beginning with # and empty lines are ignored.
      Parameters:
      inputPathName - A pathname string.
      Throws:
      IOException - Description of the Exception
    • toSortedString

      public String toSortedString()
      Creates a string representation of this Histogram. The bins are displayed in ascending order. The format of this string is bin number \t occurrences. Each bin starts on a new line.
      Returns:
      A String with the number of occurrences for each bin.
      See Also:
    • toString

      public String toString()
      Creates a string representation of this Histogram. The format is bin number\t occurrences. Each new bin starts on a new line.
      Overrides:
      toString in class Object
      Returns:
      A String with the number of occurrences for each bin.
    • hashCode

      public int hashCode(double value)
      Computes the hash code (bin number) for the specified value
      Parameters:
      value -
      Returns:
      the hash code
    • append

      public void append(double value, double numberOfoccurrences)
      Append a value with number of occurrences to the Histogram.
      Parameters:
      value -
      numberOfoccurrences -
    • append

      public void append(double value)
      Appends a value with 1 occurence.
      Parameters:
      value -
    • append

      public void append(String inputPathName) throws IOException
      Appends values from an input file. Each value is separated by a \n
      Parameters:
      inputPathName - A pathname string.
      Throws:
      IOException - Description of the Exception
    • append

      public void append(double[] values)
      Appends an array of values with 1 occurence.
      Parameters:
      values -
    • setVisible

      public void setVisible(boolean visibility)
      Sets the visibility of the histogram
      Parameters:
      visibility -
    • draw

      public void draw(DrawingPanel drawingPanel, Graphics g)
      Draws this histogram in the drawing panel.
      Specified by:
      draw in interface Drawable
      Parameters:
      drawingPanel -
      g -
    • clear

      public void clear()
      Clears all data from this histogram and resets min and max values.
    • setBinStyle

      public void setBinStyle(int style)
      Sets the style for drawing this histogram. Options are DRAW_POINT, which draws a point at the top of the bin, and DRAW_BIN which draws the entire bin down to the x axis. Default is DRAW_BIN.
      Parameters:
      style -
    • setDiscrete

      public void setDiscrete(boolean _discrete)
      Sets the discrete flag.
      Parameters:
      _discrete - true<\code> if bins are discrete, false<\code> if bins are continuous.
    • setBinOffset

      public void setBinOffset(double _binOffset)
      Sets the offset of the bins. Default is 0.
      Parameters:
      _binOffset -
    • setBarOffset

      public void setBarOffset(double _barOffset)
      Set the offset of the bars as a fraction of a bin width. The offset is the amount by which this histogram is shifted to the right, so that it peeks out from behind later histograms when displayed in a DrawingPanel.
      Parameters:
      _barOffset - The new barOffset value
    • getLineColor

      public Color getLineColor()
      Line color to use for this data
      Returns:
    • getLineColors

      public Color[] getLineColors()
      Line colors for Data interface.
      Specified by:
      getLineColors in interface Data
      Returns:
    • getFillColor

      public Color getFillColor()
      Fill color to use for this data
      Returns:
    • getFillColors

      public Color[] getFillColors()
      Fill colors for Data interface.
      Specified by:
      getFillColors in interface Data
      Returns:
    • setBinColor

      public void setBinColor(Color binColor)
      Sets the bin color.
      Parameters:
      binColor -
    • setBinColor

      public void setBinColor(Color fillColor, Color edgeColor)
      Sets the bin's fill and edge colors. If the fill color is null the bin is not filled.
      Parameters:
      fillColor -
      edgeColor -
    • setBinWidth

      public void setBinWidth(double _binWidth)
      Sets the width of a bin.
      Parameters:
      _binWidth -
    • setName

      public void setName(String name)
      Sets a name that can be used to identify the dataset.
      Parameters:
      name - String
    • getName

      public String getName()
      Gets the dataset name.
      Specified by:
      getName in interface Data
      Returns:
      String
    • getColumnNames

      public String[] getColumnNames()
      The column names to be used in the data display tool
      Specified by:
      getColumnNames in interface Data
      Returns:
    • getDataList

      public List<Data> getDataList()
      Some elements (a Group, for instance) do not contain data, but a list of subelements which do. This method is used by Data displaying tools to create as many pages as needed.
      Specified by:
      getDataList in interface Data
      Returns:
      A list of DataInformation elements, null if the element itself is a DataInformation
    • setXYColumnNames

      public void setXYColumnNames(String _xColumnName, String _yColumnName)
      Sets the column names when rendering this histogram in a JTable.
      Parameters:
      _binColumnName -
      _yColumnName -
    • setXYColumnNames

      public void setXYColumnNames(String _xColumnName, String _yColumnName, String _name)
      Sets the column names when rendering this histogram in a JTable.
      Parameters:
      _binColumnName -
      _yColumnName -
      _name - String the name of the histogram
    • setNormalizedToOne

      public void setNormalizedToOne(boolean b)
      Normalizes the occurrences in this histogram to one.
      Parameters:
      b -
    • getBinWidth

      public double getBinWidth()
      Gets the width of a bin.
      Returns:
      The bin width.
    • getBinOffset

      public double getBinOffset()
      Gets the offset of the bins.
      Returns:
      The bin offset.
    • getXMin

      public double getXMin()
      Gets the x world coordinate for the left hand side of this histogram.
      Specified by:
      getXMin in interface Measurable
      Returns:
      xmin
    • getXMax

      public double getXMax()
      Gets the x world coordinate for the right hand side of this histogram.
      Specified by:
      getXMax in interface Measurable
      Returns:
      xmax
    • getYMin

      public double getYMin()
      Gets the y world coordinate for the bottom of this histogram.
      Specified by:
      getYMin in interface Measurable
      Returns:
      minimum y value
    • getYMax

      public double getYMax()
      Gets the y world coordinate for the top of this histogram.
      Specified by:
      getYMax in interface Measurable
      Returns:
      xmax
    • getXMinLogscale

      public double getXMinLogscale()
      Gets the minimum x needed to draw this object on a log scale.
      Specified by:
      getXMinLogscale in interface LogMeasurable
      Returns:
      minimum
    • getXMaxLogscale

      public double getXMaxLogscale()
      Gets the maximum x needed to draw this object on a log scale.
      Specified by:
      getXMaxLogscale in interface LogMeasurable
      Returns:
      maximum
    • getYMinLogscale

      public double getYMinLogscale()
      Gets the minimum y needed to draw this object on a log scale.
      Specified by:
      getYMinLogscale in interface LogMeasurable
      Returns:
      minimum
    • getYMaxLogscale

      public double getYMaxLogscale()
      Gets the maximum y needed to draw this object on a log scale on a log scale.
      Specified by:
      getYMaxLogscale in interface LogMeasurable
      Returns:
      maximum
    • isMeasured

      public boolean isMeasured()
      Gets the valid measure flag. The measure is valid if this histogram is not empty.
      Specified by:
      isMeasured in interface Measurable
      Returns:
      true<\code> if measure is valid.
    • setMeasured

      public void setMeasured(boolean measure)
      Forces the measured condition of the histogram
      Parameters:
      visibility -
    • getColumnName

      public String getColumnName(int column)
      Gets the name of the column for rendering in a JTable
      Specified by:
      getColumnName in class DataTable.DataModel
      Parameters:
      column - the column whose value is to be queried
      Returns:
      the name
    • getRowCount

      public int getRowCount()
      Gets the number of rows for rendering in a JTable.
      Specified by:
      getRowCount in class DataTable.DataModel
      Returns:
      the count
    • getColumnCount

      public int getColumnCount()
      Gets the name of the colummn for rendering in a JTable
      Specified by:
      getColumnCount in class DataTable.DataModel
      Returns:
      the name
    • getValueAt

      public double getValueAt(int row, int column)
      Gets a bin number or occurrences for bin number for rendering in a JTable.
      Specified by:
      getValueAt in class DataTable.DataModel
      Parameters:
      row - the row whose value is to be queried
      column - the column whose value is to be queried
      Returns:
      the datum
    • setID

      public void setID(int id)
      Sets the ID number of this Data.
      Specified by:
      setID in interface Data
      Parameters:
      id - the ID number
    • getID

      public int getID()
      Returns a unique identifier for this Data.
      Specified by:
      getID in interface Data
      Returns:
      the ID number
    • drawPoint

      protected void drawPoint(DrawingPanel drawingPanel, Graphics g, int binNumber, double occurrences)
      Draws a point at the top of a bin.
      Parameters:
      drawingPanel -
      g -
      binNumber -
      occurrences -
    • drawBin

      protected void drawBin(DrawingPanel drawingPanel, Graphics g, int binNumber, double occurrences)
      Draws a filled bin.
      Parameters:
      drawingPanel -
      g -
      binNumber -
      occurrences -
    • getXPoints

      public double[] getXPoints()
      Gets an array containing the bin centers.
      Returns:
      the bins
    • getYPoints

      public double[] getYPoints()
      Gets an array containing the values within the bins.
      Returns:
      the values of the bins
    • getPoints

      public double[][] getPoints()
      Gets a data array containing both the bin centers and the values within the bins.
      Returns:
      a double[index][2] array of data
    • getLogPoints

      public double[][] getLogPoints()
      Gets a data array containing both the bin centers and the values within the bins.
      Returns:
      a double[index][2] array of data
    • getLeftMostBinPosition

      public double getLeftMostBinPosition(int binNumber)
      Method getLeftMostBinPosition
      Parameters:
      binNumber -
      Returns:
      position
    • getRightMostBinPosition

      public double getRightMostBinPosition(int binNumber)
      Method getRightMostBinPosition
      Parameters:
      binNumber -
      Returns:
      position
    • getData2D

      public double[][] getData2D()
      Description copied from interface: Data
      Gets a 2D array of data. The first column, double[0][] often contains x-values; Remaining columns often contain y values. May return null if data not yet generated or object does not support 2D data.
      Specified by:
      getData2D in interface Data
      Returns:
      double[][]
    • getData3D

      public double[][][] getData3D()
      Description copied from interface: Data
      Gets a 3D array of data. May return null if data not yet generated or object does not support 3D data.
      Specified by:
      getData3D in interface Data
      Returns:
      double[][][]
    • getDatasets

      public ArrayList<Dataset> getDatasets()
      Description copied from interface: Data
      Gets a list of OSP Datasets. May return null if data not yet generated or object does not support Datasets.
      Specified by:
      getDatasets in interface Data
      Returns:
      list of Datasets
    • getLoader

      public static XML.ObjectLoader getLoader()
      Returns the XML.ObjectLoader for this class.
      Returns:
      the object loader