Class CrosstabStore

All Implemented Interfaces:
PopupActivationListener, Serializable, Comparable, EventListener, ListSelectionListener, TableModel

public class CrosstabStore extends AttributeStore
This class creates a pivot table-like summary of of data in a parent store. Summaries are obtained by requesting a snapshot using the getSnapshot(boolean) method. The summary is 'static' and will not change in response to changes in the underlying table. All of the predefined report templates are aware of CrosstabStore's, and they will request a snapshot when each report is generated, thus providing up to date values.

Crosstab's are useful to reorganize data so that it may be more easily presented in reports. One common example is to create a report of average values. Consider getting a report of the average harvest age. The block table contains the treatment age and area harvested for each polygon. In order to get the average harvest age we need to sum the harvest age of each polygon, and then divide by the total area harvested. This operation can be expressed in as follows:

  CrosstabStore cts = new CrosstabStore(blockData, 
      "CURRENTTREATMENT ne ''", Horizon.periodList);
  cts.addValue("AgeArea", "TREATMENTAGE*MANAGEDAREA");
  cts.addValue("Area", "MANAGEDAREA");

  Report r = new PeriodValueReport("avAge", "Average treatment age", 
                                   "Age", cts,
                                   new String[] {"AgeArea/Area"},
                                   new String[] {"AvAge"},
                                   "Area > 0", Horizon.periodList, 
                                   null, false);
  reportWriter.addReport(r);
 
You can view the contents of a snapshot by displaying the table:
  cts.getSnapshot(false).showCube(true);
 
See Also:
Serialized Form
  • Constructor Details

    • CrosstabStore

      public CrosstabStore(AttributeStore parent, String reselectExpression, int[] periods)
    • CrosstabStore

      public CrosstabStore(AttributeStore parent, String reselectExpression, int[] periods, boolean transpose)
  • Method Details

    • getParent

      public AttributeStore getParent()
      Return a reference to the parent table
    • getReselectExpression

      public String getReselectExpression()
      Return the reselect expression
    • getPeriods

      public int[] getPeriods()
      Return the periods that the table will be calculated for
    • getTranspose

      public boolean getTranspose()
      Return the indicator if the columns in the table will be transposed
    • getSnapshot

      public AttributeStore getSnapshot(boolean useSelected)
      Get a snapshot of this crosstab definition. The queries will be run to compute the categories and expressions for the current values in the underlying dataset. The results will be contained in the snapshot and will not change even if the underlying data set changes.
      Overrides:
      getSnapshot in class AttributeStore
      Parameters:
      useSelected - The calculations will only be computed on the selected records.
      Returns:
      This method will return a new shapshot that captures the state of the crosstab definition for the current data set.
    • getSnapshot

      public AttributeStore getSnapshot(boolean useSelected, Observer progress)
      Get a snapshot of this crosstab definition. The queries will be run to compute the categories and expressions for the current values in the underlying dataset. The results will be contained in the snapshot and will not change even if the underlying data set changes.
      Overrides:
      getSnapshot in class AttributeStore
      Parameters:
      useSelected - The calculations will only be computed on the selected records.
      progress - A reference to a progress widget that will be updated with the progress of the calculations.
      Returns:
      This method will return a new shapshot that captures the state of the crosstab definition for the current data set.
    • setCategoryName

      public CrosstabStore setCategoryName(String columnName)
      Set the name of the column that will be used to hold the categories when the transposition option is used. The default value is 'Categories'.
    • setValueName

      public CrosstabStore setValueName(String columnName)
      Set the name of the column that will be used to hold values when the transposition option is used. The default value is 'Values'.
    • addCategory

      public void addCategory(String label, String expression, Comparable[] originalCategories)
      Add a category breakdown to the table. This will result in a new column in the table that will contain rows for each of the possible discrete values that can be returned by the expression.
      Parameters:
      label - The name to be given to the column heading
      expression - The expression to evaluate to compute the category value
      originalCategories - A list of the potential category values that should be summarized into this report. Only values in this list will be included in the report.
    • addRange

      public void addRange(String label, String expression, RangeLabel[] ranges)
      Add a numeric range breakdown to the table. This will result in a new column in the table that will contains additional rows for each range of input values.
      Parameters:
      label - The name to be given to the column heading
      expression - The expression to evaluate to compute the numeric value
      ranges - A list of the potential ranges that should be summarized into this report. Only ranges in this list will be included in the report. The input value will be excluded if the expression returns a value that falls outside all of the ranges.
    • addProportion

      public void addProportion(String label, String[] columns, String[] labels, int offset)
      Add a proportional breakdown to the table. This will result in a new column in the table that will contains additional rows for each input column used in the proportion calculation.

      Proportions work by adding new rows to the table, and then distributing the contents of the values columns proportionally between these rows. This is done by reading a row from the input table, summing the values of the proportional columns, and then dividing each proportional column by the total. The results of the value columns (see addValue(java.lang.String, java.lang.String) are then multiplied by these proportions.

      The column that is created by this method will contain the labels of the columns that are used to calculate the proportions.

      Parameters:
      label - The name to be given to the column heading
      columns - A list of the columns that will be used to determine the proportional split.
      labels - A list of labels that will be used to represent each column value.
      offset - This value will be added to the year of interest when looking up values from the underlying table. This makes it possible to compare values from different years. For example, an offset of -1 will use the values from the previous planning period for the calculation. This could be used to compute change in volume from one period to the next, or perhaps change in area as a result of succession.
    • addProportion

      public void addProportion(String label, String[] columns)
      Add a proportional breakdown to the table. This will result in a new column in the table that will contains additional rows for each input column used in the proportion calculation.

      Proportions work by adding new rows to the table, and then distributing the contents of the values columns proportionally between these rows. This is done by reading a row from the input table, summing the values of the proportional columns, and then dividing each proportional column by the total. The results of the value columns (see addValue(java.lang.String, java.lang.String) are then multiplied by these proportions.

      This method is identical to using

         addProportion(label, columns, columns, 0);
       

      The column that is created by this method will contain the names of the columns that are used to calculate the proportions.

      Parameters:
      label - The name to be given to the column heading
      columns - A list of the columns that will be used to determine the proportional split.
    • addProportion

      public void addProportion(String label, String pattern, int offset)
      Add a proportional breakdown to the table. This will result in a new column in the table that will contains additional rows for each input column used in the proportion calculation.

      Proportions work by adding new rows to the table, and then distributing the contents of the values columns proportionally between these rows. This is done by reading a row from the input table, summing the values of the proportional columns, and then dividing each proportional column by the total. The results of the value columns (see addValue(java.lang.String, java.lang.String) are then multiplied by these proportions.

      The column that is created by this method will contain the names of the columns that are used to calculate the proportions.

      Parameters:
      label - The name to be given to the column heading
      pattern - A pattern that identifies the columns that will be used to determine the proportional split.
      offset - This value will be added to the year of interest when looking up values from the underlying table. This makes it possible to compare values from different years. For example, an offset of -1 will use the values from the previous planning period for the calculation. This could be used to compute change in volume from one period to the next, or perhaps change in area as a result of succession.
    • addProportion

      public void addProportion(String label, String pattern)
    • addValue

      public void addValue(String label, String expression)
      Add a value to the table. The result of the expression will be added into the table that matches the row with the right combination of categories, ranges and proportions for the input record.
      Parameters:
      label - The name give to the column
      expression - The value to be computed and added to the column
    • getRowCount

      public int getRowCount()
      Description copied from class: AttributeStore
      Get the number of rows in this store
      Specified by:
      getRowCount in interface TableModel
      Specified by:
      getRowCount in class AttributeStore
      Returns:
      the number of rows in this store
    • getPeriodCount

      public int getPeriodCount()
      Description copied from class: AttributeStore
      Get the total number of periods that this store supports
      Specified by:
      getPeriodCount in class AttributeStore
      Returns:
      The number of periods in this store
    • getYearForPeriod

      public int getYearForPeriod(int period)
      Description copied from class: AttributeStore
      Get the ending year for a given period.
      Specified by:
      getYearForPeriod in class AttributeStore
      Parameters:
      period - The period of interest
      Returns:
      the year for a given period
    • getColumnCount

      public int getColumnCount()
      Description copied from class: AttributeStore
      Get the number of columns in this store
      Specified by:
      getColumnCount in interface TableModel
      Overrides:
      getColumnCount in class AttributeStore
      Returns:
      the number of columns in this store
    • getValueAt

      public Object getValueAt(int row, int column, int period)
      Description copied from class: AttributeStore
      Get a cell value
      Specified by:
      getValueAt in class AttributeStore
      Parameters:
      row - The row containing the cell
      column - The column containing the cell
      period - The period of interest
      Returns:
      An object containing the cell value.
    • findColumn

      public int findColumn(String field)
      Description copied from class: AttributeStore
      Get the column number of the given field in the table. The first matching column is returned. If the column is not found then return -1.
      Overrides:
      findColumn in class AttributeStore
      Parameters:
      field - The name of the field to look up
    • getAttributeFormat

      public AttributeFormat getAttributeFormat(int field)
      Description copied from class: AttributeStore
      Get the format descriptor for a given column.
      Overrides:
      getAttributeFormat in class AttributeStore
      Parameters:
      field - The column of interest
      Returns:
      The AttributeFormat for this field