Source for org.jfree.chart.labels.BoxAndWhiskerXYToolTipGenerator

   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:  * BoxAndWhiskerXYToolTipGenerator.java
  29:  * ------------------------------------
  30:  * (C) Copyright 2003-2007, by David Browning and Contributors.
  31:  *
  32:  * Original Author:  David Browning;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: BoxAndWhiskerXYToolTipGenerator.java,v 1.4.2.2 2007/02/02 15:52:42 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 05-Aug-2003 : Version 1, contributed by David Browning (DG);
  40:  * 13-Aug-2003 : Implemented Cloneable (DG);
  41:  * 28-Aug-2003 : Updated for changes in dataset API (DG);
  42:  * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
  43:  * 27-Feb-2004 : Renamed BoxAndWhiskerItemLabelGenerator --> 
  44:  *               BoxAndWhiskerXYItemLabelGenerator, and modified to use 
  45:  *               MessageFormat (DG);
  46:  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 
  47:  *               getYValue() (DG);
  48:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  49:  *
  50:  */
  51: 
  52: package org.jfree.chart.labels;
  53: 
  54: import java.io.Serializable;
  55: import java.text.DateFormat;
  56: import java.text.MessageFormat;
  57: import java.text.NumberFormat;
  58: import java.util.Date;
  59: 
  60: import org.jfree.data.statistics.BoxAndWhiskerXYDataset;
  61: import org.jfree.data.xy.XYDataset;
  62: 
  63: /**
  64:  * An item label generator for plots that use data from a 
  65:  * {@link BoxAndWhiskerXYDataset}.
  66:  * <P>
  67:  * The tooltip text and item label text are composed using a 
  68:  * {@link java.text.MessageFormat} object, that can aggregate some or all of 
  69:  * the following string values into a message.
  70:  * <table>
  71:  * <tr><td>0</td><td>Series Name</td></tr>
  72:  * <tr><td>1</td><td>X (value or date)</td></tr>
  73:  * <tr><td>2</td><td>Mean</td></tr>
  74:  * <tr><td>3</td><td>Median</td></tr>
  75:  * <tr><td>4</td><td>Minimum</td></tr>
  76:  * <tr><td>5</td><td>Maximum</td></tr>
  77:  * <tr><td>6</td><td>Quartile 1</td></tr>
  78:  * <tr><td>7</td><td>Quartile 3</td></tr>
  79:  * </table>
  80:  */
  81: public class BoxAndWhiskerXYToolTipGenerator extends StandardXYToolTipGenerator
  82:                                              implements XYToolTipGenerator,
  83:                                                         Cloneable,
  84:                                                         Serializable {
  85: 
  86:     /** For serialization. */
  87:     private static final long serialVersionUID = -2648775791161459710L;
  88:     
  89:     /** The default tooltip format string. */
  90:     public static final String DEFAULT_TOOL_TIP_FORMAT 
  91:         = "X: {1} Mean: {2} Median: {3} Min: {4} Max: {5} Q1: {6} Q3: {7} ";
  92:     
  93:     /**
  94:      * Creates a default item label generator.
  95:      */
  96:     public BoxAndWhiskerXYToolTipGenerator() {
  97:         super(
  98:             DEFAULT_TOOL_TIP_FORMAT,
  99:             NumberFormat.getInstance(), NumberFormat.getInstance()
 100:         );
 101:     }
 102: 
 103:     /**
 104:      * Creates a new item label generator.  If the date formatter is not 
 105:      * <code>null</code>, the x-values will be formatted as dates.
 106:      * 
 107:      * @param toolTipFormat  the tool tip format string (<code>null</code> not 
 108:      *                       permitted).
 109:      * @param numberFormat  the number formatter (<code>null</code> not 
 110:      *                      permitted).
 111:      * @param dateFormat  the date formatter (<code>null</code> permitted).
 112:      */
 113:     public BoxAndWhiskerXYToolTipGenerator(String toolTipFormat, 
 114:                                            DateFormat dateFormat, 
 115:                                            NumberFormat numberFormat) {
 116:         
 117:         super(toolTipFormat, dateFormat, numberFormat);
 118:     
 119:     }
 120:     
 121:     /**
 122:      * Creates the array of items that can be passed to the 
 123:      * {@link MessageFormat} class for creating labels.
 124:      *
 125:      * @param dataset  the dataset (<code>null</code> not permitted).
 126:      * @param series  the series (zero-based index).
 127:      * @param item  the item (zero-based index).
 128:      *
 129:      * @return The items (never <code>null</code>).
 130:      */
 131:     protected Object[] createItemArray(XYDataset dataset, int series, 
 132:                                        int item) {
 133:         Object[] result = new Object[8];
 134:         result[0] = dataset.getSeriesKey(series).toString();
 135:         Number x = dataset.getX(series, item);
 136:         if (getXDateFormat() != null) {
 137:             result[1] = getXDateFormat().format(new Date(x.longValue()));   
 138:         }
 139:         else {
 140:             result[1] = getXFormat().format(x);
 141:         }
 142:         NumberFormat formatter = getYFormat();
 143:         
 144:         if (dataset instanceof BoxAndWhiskerXYDataset) {
 145:             BoxAndWhiskerXYDataset d = (BoxAndWhiskerXYDataset) dataset;
 146:             result[2] = formatter.format(d.getMeanValue(series, item));
 147:             result[3] = formatter.format(d.getMedianValue(series, item));
 148:             result[4] = formatter.format(d.getMinRegularValue(series, item));
 149:             result[5] = formatter.format(d.getMaxRegularValue(series, item));
 150:             result[6] = formatter.format(d.getQ1Value(series, item));
 151:             result[7] = formatter.format(d.getQ3Value(series, item));
 152:         }
 153:         return result;
 154:     }
 155: 
 156:     /**
 157:      * Tests if this object is equal to another.
 158:      *
 159:      * @param obj  the other object.
 160:      *
 161:      * @return A boolean.
 162:      */
 163:     public boolean equals(Object obj) {
 164:         if (obj == this) {
 165:             return true;
 166:         }
 167:         if (!(obj instanceof BoxAndWhiskerXYToolTipGenerator)) {
 168:             return false;
 169:         }
 170:         return super.equals(obj);
 171:     }
 172:     
 173: }