Source for org.jfree.data.xy.NormalizedMatrixSeries

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ---------------------------
  28:  * NormalizedMatrixSeries.java
  29:  * ---------------------------
  30:  * (C) Copyright 2003-2007, by Barak Naveh and Contributors.
  31:  *
  32:  * Original Author:  Barak Naveh;;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: NormalizedMatrixSeries.java,v 1.3.2.3 2007/02/02 15:14:53 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
  40:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  41:  *
  42:  */
  43:  
  44: package org.jfree.data.xy;
  45: 
  46: 
  47: /**
  48:  * Represents a dense normalized matrix M[i,j] where each Mij item of the
  49:  * matrix has a value (default is 0). When a matrix item is observed using
  50:  * <code>getItem</code> method, it is normalized, that is, divided by the
  51:  * total sum of all items. It can be also be scaled by setting a scale factor.
  52:  */
  53: public class NormalizedMatrixSeries extends MatrixSeries {
  54:     
  55:     /** The default scale factor. */
  56:     public static final double DEFAULT_SCALE_FACTOR = 1.0;
  57: 
  58:     /**
  59:      * A factor that multiplies each item in this series when observed using 
  60:      * getItem method.
  61:      */
  62:     private double m_scaleFactor = DEFAULT_SCALE_FACTOR;
  63: 
  64:     /** The sum of all items in this matrix */
  65:     private double m_totalSum;
  66: 
  67:     /**
  68:      * Constructor for NormalizedMatrixSeries.
  69:      *
  70:      * @param name  the series name.
  71:      * @param rows  the number of rows.
  72:      * @param columns  the number of columns.
  73:      */
  74:     public NormalizedMatrixSeries(String name, int rows, int columns) {
  75:         super(name, rows, columns);
  76: 
  77:         /*
  78:          * we assum super is always initialized to all-zero matrix, so the
  79:          * total sum should be 0 upon initialization. However, we set it to
  80:          * Double.MIN_VALUE to get the same effect and yet avoid division by 0
  81:          * upon initialization.
  82:          */
  83:         this.m_totalSum = Double.MIN_VALUE;
  84:     }
  85: 
  86:     /**
  87:      * Returns an item.
  88:      * 
  89:      * @param itemIndex  the index.
  90:      * 
  91:      * @return The value.
  92:      * 
  93:      * @see org.jfree.data.xy.MatrixSeries#getItem(int)
  94:      */
  95:     public Number getItem(int itemIndex) {
  96:         int i = getItemRow(itemIndex);
  97:         int j = getItemColumn(itemIndex);
  98: 
  99:         double mij = get(i, j) * this.m_scaleFactor;
 100:         Number n = new Double(mij / this.m_totalSum);
 101: 
 102:         return n;
 103:     }
 104: 
 105:     /**
 106:      * Sets the factor that multiplies each item in this series when observed
 107:      * using getItem mehtod.
 108:      *
 109:      * @param factor new factor to set.
 110:      *
 111:      * @see #DEFAULT_SCALE_FACTOR
 112:      */
 113:     public void setScaleFactor(double factor) {
 114:         this.m_scaleFactor = factor;
 115:         // FIXME: this should generate a series change event
 116:     }
 117: 
 118: 
 119:     /**
 120:      * Returns the factor that multiplies each item in this series when
 121:      * observed using getItem mehtod.
 122:      *
 123:      * @return The factor
 124:      */
 125:     public double getScaleFactor() {
 126:         return this.m_scaleFactor;
 127:     }
 128: 
 129: 
 130:     /**
 131:      * @see org.jfree.data.xy.MatrixSeries#update(int, int, double)
 132:      */
 133:     public void update(int i, int j, double mij) {
 134:         this.m_totalSum -= get(i, j);
 135:         this.m_totalSum += mij;
 136: 
 137:         super.update(i, j, mij);
 138:     }
 139: 
 140:     /**
 141:      * @see org.jfree.data.xy.MatrixSeries#zeroAll()
 142:      */
 143:     public void zeroAll() {
 144:         this.m_totalSum = 0;
 145:         super.zeroAll();
 146:     }
 147: }