Source for org.jfree.chart.labels.BoxAndWhiskerToolTipGenerator

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2005, 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:  * BoxAndWhiskerToolTipGenerator.java
  29:  * ------------------------------------
  30:  * (C) Copyright 2004, 2005, by David Browning and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: BoxAndWhiskerToolTipGenerator.java,v 1.3.2.1 2005/10/25 20:49:02 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 02-Jun-2004 : Version 1 (DG);
  40:  * 23-Mar-2005 : Implemented PublicCloneable (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.chart.labels;
  45: 
  46: import java.io.Serializable;
  47: import java.text.MessageFormat;
  48: import java.text.NumberFormat;
  49: 
  50: import org.jfree.data.category.CategoryDataset;
  51: import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset;
  52: import org.jfree.util.PublicCloneable;
  53: 
  54: /**
  55:  * An item label generator for plots that use data from a 
  56:  * {@link BoxAndWhiskerCategoryDataset}.
  57:  * <P>
  58:  * The tooltip text and item label text are composed using a 
  59:  * {@link java.text.MessageFormat} object, that can aggregate some or all of 
  60:  * the following string values into a message.
  61:  * <table>
  62:  * <tr><td>0</td><td>Series Name</td></tr>
  63:  * <tr><td>1</td><td>X (value or date)</td></tr>
  64:  * <tr><td>2</td><td>Mean</td></tr>
  65:  * <tr><td>3</td><td>Median</td></tr>
  66:  * <tr><td>4</td><td>Minimum</td></tr>
  67:  * <tr><td>5</td><td>Maximum</td></tr>
  68:  * <tr><td>6</td><td>Quartile 1</td></tr>
  69:  * <tr><td>7</td><td>Quartile 3</td></tr>
  70:  * </table>
  71:  */
  72: public class BoxAndWhiskerToolTipGenerator 
  73:     extends StandardCategoryToolTipGenerator
  74:     implements CategoryToolTipGenerator, Cloneable, PublicCloneable,
  75:                Serializable {
  76: 
  77:     /** For serialization. */
  78:     private static final long serialVersionUID = -6076837753823076334L;
  79:     
  80:     /** The default tooltip format string. */
  81:     public static final String DEFAULT_TOOL_TIP_FORMAT 
  82:         = "X: {1} Mean: {2} Median: {3} Min: {4} Max: {5} Q1: {6} Q3: {7} ";
  83:     
  84:     /**
  85:      * Creates a default tool tip generator.
  86:      */
  87:     public BoxAndWhiskerToolTipGenerator() {
  88:         super(DEFAULT_TOOL_TIP_FORMAT, NumberFormat.getInstance());
  89:     }
  90:     
  91:     /**
  92:      * Creates a tool tip formatter.
  93:      * 
  94:      * @param format  the tool tip format string.
  95:      * @param formatter  the formatter.
  96:      */
  97:     public BoxAndWhiskerToolTipGenerator(String format, 
  98:                                          NumberFormat formatter) {
  99:         super(format, formatter);   
 100:     }
 101:     
 102:     /**
 103:      * Creates the array of items that can be passed to the 
 104:      * {@link MessageFormat} class for creating labels.
 105:      *
 106:      * @param dataset  the dataset (<code>null</code> not permitted).
 107:      * @param series  the series (zero-based index).
 108:      * @param item  the item (zero-based index).
 109:      *
 110:      * @return The items (never <code>null</code>).
 111:      */
 112:     protected Object[] createItemArray(CategoryDataset dataset, int series, 
 113:                                        int item) {
 114:         Object[] result = new Object[8];
 115:         result[0] = dataset.getRowKey(series);
 116:         Number y = dataset.getValue(series, item);
 117:         NumberFormat formatter = getNumberFormat();
 118:         result[1] = formatter.format(y);
 119:         if (dataset instanceof BoxAndWhiskerCategoryDataset) {
 120:             BoxAndWhiskerCategoryDataset d 
 121:                 = (BoxAndWhiskerCategoryDataset) dataset;
 122:             result[2] = formatter.format(d.getMeanValue(series, item));
 123:             result[3] = formatter.format(d.getMedianValue(series, item));
 124:             result[4] = formatter.format(d.getMinRegularValue(series, item));
 125:             result[5] = formatter.format(d.getMaxRegularValue(series, item));
 126:             result[6] = formatter.format(d.getQ1Value(series, item));
 127:             result[7] = formatter.format(d.getQ3Value(series, item));
 128:         }
 129:         return result;
 130:     }
 131: 
 132:     /**
 133:      * Tests if this object is equal to another.
 134:      *
 135:      * @param obj  the other object.
 136:      *
 137:      * @return A boolean.
 138:      */
 139:     public boolean equals(Object obj) {
 140:         if (obj == this) {
 141:             return true;
 142:         }
 143:         if (obj instanceof BoxAndWhiskerToolTipGenerator) {
 144:             return super.equals(obj);
 145:         }
 146:         return false;
 147:     }
 148:     
 149: }