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