Frames | No Frames |
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: * Changes 36: * ------- 37: * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG); 38: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG); 39: * 40: */ 41: 42: package org.jfree.data.xy; 43: 44: 45: /** 46: * Represents a dense normalized matrix M[i,j] where each Mij item of the 47: * matrix has a value (default is 0). When a matrix item is observed using 48: * <code>getItem</code> method, it is normalized, that is, divided by the 49: * total sum of all items. It can be also be scaled by setting a scale factor. 50: */ 51: public class NormalizedMatrixSeries extends MatrixSeries { 52: 53: /** The default scale factor. */ 54: public static final double DEFAULT_SCALE_FACTOR = 1.0; 55: 56: /** 57: * A factor that multiplies each item in this series when observed using 58: * getItem method. 59: */ 60: private double m_scaleFactor = DEFAULT_SCALE_FACTOR; 61: 62: /** The sum of all items in this matrix */ 63: private double m_totalSum; 64: 65: /** 66: * Constructor for NormalizedMatrixSeries. 67: * 68: * @param name the series name. 69: * @param rows the number of rows. 70: * @param columns the number of columns. 71: */ 72: public NormalizedMatrixSeries(String name, int rows, int columns) { 73: super(name, rows, columns); 74: 75: /* 76: * we assum super is always initialized to all-zero matrix, so the 77: * total sum should be 0 upon initialization. However, we set it to 78: * Double.MIN_VALUE to get the same effect and yet avoid division by 0 79: * upon initialization. 80: */ 81: this.m_totalSum = Double.MIN_VALUE; 82: } 83: 84: /** 85: * Returns an item. 86: * 87: * @param itemIndex the index. 88: * 89: * @return The value. 90: * 91: * @see org.jfree.data.xy.MatrixSeries#getItem(int) 92: */ 93: public Number getItem(int itemIndex) { 94: int i = getItemRow(itemIndex); 95: int j = getItemColumn(itemIndex); 96: 97: double mij = get(i, j) * this.m_scaleFactor; 98: Number n = new Double(mij / this.m_totalSum); 99: 100: return n; 101: } 102: 103: /** 104: * Sets the factor that multiplies each item in this series when observed 105: * using getItem mehtod. 106: * 107: * @param factor new factor to set. 108: * 109: * @see #DEFAULT_SCALE_FACTOR 110: */ 111: public void setScaleFactor(double factor) { 112: this.m_scaleFactor = factor; 113: // FIXME: this should generate a series change event 114: } 115: 116: 117: /** 118: * Returns the factor that multiplies each item in this series when 119: * observed using getItem mehtod. 120: * 121: * @return The factor 122: */ 123: public double getScaleFactor() { 124: return this.m_scaleFactor; 125: } 126: 127: 128: /** 129: * Updates the value of the specified item in this matrix series. 130: * 131: * @param i the row of the item. 132: * @param j the column of the item. 133: * @param mij the new value for the item. 134: * 135: * @see #get(int, int) 136: */ 137: public void update(int i, int j, double mij) { 138: this.m_totalSum -= get(i, j); 139: this.m_totalSum += mij; 140: 141: super.update(i, j, mij); 142: } 143: 144: /** 145: * @see org.jfree.data.xy.MatrixSeries#zeroAll() 146: */ 147: public void zeroAll() { 148: this.m_totalSum = 0; 149: super.zeroAll(); 150: } 151: }