Source for org.jfree.chart.labels.SymbolicXYItemLabelGenerator

   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:  * SymbolicXYItemLabelGenerator.java
  29:  * ---------------------------------
  30:  * (C) Copyright 2001-2007, by Anthony Boulestreau and Contributors.
  31:  *
  32:  * Original Author:  Anthony Boulestreau;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: SymbolicXYItemLabelGenerator.java,v 1.5.2.2 2007/02/02 15:52:42 mungady Exp $
  36:  * 
  37:  * Changes
  38:  * -------
  39:  * 29-Mar-2002 : Version 1, contributed by Anthony Boulestreau (DG);
  40:  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  41:  * 23-Mar-2003 : Implemented Serializable (DG);
  42:  * 13-Aug-2003 : Implemented Cloneable (DG);
  43:  * 17-Nov-2003 : Implemented PublicCloneable (DG);
  44:  * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
  45:  * 19-Jan-2005 : Now accesses primitives only from dataset (DG);
  46:  * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator (DG);
  47:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  48:  *
  49:  */
  50: 
  51: package org.jfree.chart.labels;
  52: 
  53: import java.io.Serializable;
  54: 
  55: import org.jfree.data.time.RegularTimePeriod;
  56: import org.jfree.data.time.TimeSeriesCollection;
  57: import org.jfree.data.xy.XYDataset;
  58: import org.jfree.data.xy.XisSymbolic;
  59: import org.jfree.data.xy.YisSymbolic;
  60: import org.jfree.util.PublicCloneable;
  61: 
  62: /**
  63:  * A standard item label generator for plots that use data from an 
  64:  * {@link XYDataset}.
  65:  */
  66: public class SymbolicXYItemLabelGenerator implements XYItemLabelGenerator, 
  67:                                                      XYToolTipGenerator,
  68:                                                      Cloneable, 
  69:                                                      PublicCloneable,
  70:                                                      Serializable {
  71: 
  72:     /** For serialization. */
  73:     private static final long serialVersionUID = 3963400354475494395L;
  74:     
  75:     /**
  76:      * Generates a tool tip text item for a particular item within a series.
  77:      *
  78:      * @param data  the dataset.
  79:      * @param series  the series number (zero-based index).
  80:      * @param item  the item number (zero-based index).
  81:      *
  82:      * @return The tool tip text (possibly <code>null</code>).
  83:      */
  84:     public String generateToolTip(XYDataset data, int series, int item) {
  85: 
  86:         String xStr, yStr;
  87:         if (data instanceof YisSymbolic) {
  88:             yStr = ((YisSymbolic) data).getYSymbolicValue(series, item);
  89:         }
  90:         else {
  91:             double y = data.getYValue(series, item);
  92:             yStr = Double.toString(round(y, 2));
  93:         }
  94:         if (data instanceof XisSymbolic) {
  95:             xStr = ((XisSymbolic) data).getXSymbolicValue(series, item);
  96:         }
  97:         else if (data instanceof TimeSeriesCollection) {
  98:             RegularTimePeriod p
  99:                 = ((TimeSeriesCollection) data).getSeries(series)
 100:                     .getTimePeriod(item);
 101:             xStr = p.toString();
 102:         }
 103:         else {
 104:             double x = data.getXValue(series, item);
 105:             xStr = Double.toString(round(x, 2));
 106:         }
 107:         return "X: " + xStr + ", Y: " + yStr;
 108:     }
 109: 
 110:     /**
 111:      * Generates a label for the specified item. The label is typically a 
 112:      * formatted version of the data value, but any text can be used.
 113:      *
 114:      * @param dataset  the dataset (<code>null</code> not permitted).
 115:      * @param series  the series index (zero-based).
 116:      * @param category  the category index (zero-based).
 117:      *
 118:      * @return The label (possibly <code>null</code>).
 119:      */
 120:     public String generateLabel(XYDataset dataset, int series, int category) {
 121:         return null;  //TODO: implement this method properly
 122:     }
 123: 
 124:     /**
 125:     * Round a double value.
 126:     *
 127:     * @param value  the value.
 128:     * @param nb  the exponent.
 129:     *
 130:     * @return The rounded value.
 131:     */
 132:     private static double round(double value, int nb) {
 133:         if (nb <= 0) {
 134:             return Math.floor(value + 0.5d);
 135:         }
 136:         double p = Math.pow(10, nb);
 137:         double tempval = Math.floor(value * p + 0.5d);
 138:         return tempval / p;
 139:     }
 140:     
 141:     /**
 142:      * Returns an independent copy of the generator.
 143:      * 
 144:      * @return A clone.
 145:      * 
 146:      * @throws CloneNotSupportedException if cloning is not supported.
 147:      */
 148:     public Object clone() throws CloneNotSupportedException { 
 149:         return super.clone();
 150:     }    
 151: 
 152:     /**
 153:      * Tests if this object is equal to another.
 154:      *
 155:      * @param obj  the other object.
 156:      *
 157:      * @return A boolean.
 158:      */
 159:     public boolean equals(Object obj) {
 160:         if (obj == this) {
 161:             return true;
 162:         }
 163:         if (obj instanceof SymbolicXYItemLabelGenerator) {
 164:             return true;
 165:         }
 166:         return false;
 167:     }
 168:     
 169: }