001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------------------------------
028: * BoxAndWhiskerXYToolTipGenerator.java
029: * ------------------------------------
030: * (C) Copyright 2003-2007, by David Browning and Contributors.
031: *
032: * Original Author: David Browning;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: BoxAndWhiskerXYToolTipGenerator.java,v 1.4.2.2 2007/02/02 15:52:42 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 05-Aug-2003 : Version 1, contributed by David Browning (DG);
040: * 13-Aug-2003 : Implemented Cloneable (DG);
041: * 28-Aug-2003 : Updated for changes in dataset API (DG);
042: * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
043: * 27-Feb-2004 : Renamed BoxAndWhiskerItemLabelGenerator -->
044: * BoxAndWhiskerXYItemLabelGenerator, and modified to use
045: * MessageFormat (DG);
046: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
047: * getYValue() (DG);
048: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
049: *
050: */
051:
052: package org.jfree.chart.labels;
053:
054: import java.io.Serializable;
055: import java.text.DateFormat;
056: import java.text.MessageFormat;
057: import java.text.NumberFormat;
058: import java.util.Date;
059:
060: import org.jfree.data.statistics.BoxAndWhiskerXYDataset;
061: import org.jfree.data.xy.XYDataset;
062:
063: /**
064: * An item label generator for plots that use data from a
065: * {@link BoxAndWhiskerXYDataset}.
066: * <P>
067: * The tooltip text and item label text are composed using a
068: * {@link java.text.MessageFormat} object, that can aggregate some or all of
069: * the following string values into a message.
070: * <table>
071: * <tr><td>0</td><td>Series Name</td></tr>
072: * <tr><td>1</td><td>X (value or date)</td></tr>
073: * <tr><td>2</td><td>Mean</td></tr>
074: * <tr><td>3</td><td>Median</td></tr>
075: * <tr><td>4</td><td>Minimum</td></tr>
076: * <tr><td>5</td><td>Maximum</td></tr>
077: * <tr><td>6</td><td>Quartile 1</td></tr>
078: * <tr><td>7</td><td>Quartile 3</td></tr>
079: * </table>
080: */
081: public class BoxAndWhiskerXYToolTipGenerator extends
082: StandardXYToolTipGenerator implements XYToolTipGenerator,
083: Cloneable, Serializable {
084:
085: /** For serialization. */
086: private static final long serialVersionUID = -2648775791161459710L;
087:
088: /** The default tooltip format string. */
089: public static final String DEFAULT_TOOL_TIP_FORMAT = "X: {1} Mean: {2} Median: {3} Min: {4} Max: {5} Q1: {6} Q3: {7} ";
090:
091: /**
092: * Creates a default item label generator.
093: */
094: public BoxAndWhiskerXYToolTipGenerator() {
095: super (DEFAULT_TOOL_TIP_FORMAT, NumberFormat.getInstance(),
096: NumberFormat.getInstance());
097: }
098:
099: /**
100: * Creates a new item label generator. If the date formatter is not
101: * <code>null</code>, the x-values will be formatted as dates.
102: *
103: * @param toolTipFormat the tool tip format string (<code>null</code> not
104: * permitted).
105: * @param numberFormat the number formatter (<code>null</code> not
106: * permitted).
107: * @param dateFormat the date formatter (<code>null</code> permitted).
108: */
109: public BoxAndWhiskerXYToolTipGenerator(String toolTipFormat,
110: DateFormat dateFormat, NumberFormat numberFormat) {
111:
112: super (toolTipFormat, dateFormat, numberFormat);
113:
114: }
115:
116: /**
117: * Creates the array of items that can be passed to the
118: * {@link MessageFormat} class for creating labels.
119: *
120: * @param dataset the dataset (<code>null</code> not permitted).
121: * @param series the series (zero-based index).
122: * @param item the item (zero-based index).
123: *
124: * @return The items (never <code>null</code>).
125: */
126: protected Object[] createItemArray(XYDataset dataset, int series,
127: int item) {
128: Object[] result = new Object[8];
129: result[0] = dataset.getSeriesKey(series).toString();
130: Number x = dataset.getX(series, item);
131: if (getXDateFormat() != null) {
132: result[1] = getXDateFormat()
133: .format(new Date(x.longValue()));
134: } else {
135: result[1] = getXFormat().format(x);
136: }
137: NumberFormat formatter = getYFormat();
138:
139: if (dataset instanceof BoxAndWhiskerXYDataset) {
140: BoxAndWhiskerXYDataset d = (BoxAndWhiskerXYDataset) dataset;
141: result[2] = formatter.format(d.getMeanValue(series, item));
142: result[3] = formatter
143: .format(d.getMedianValue(series, item));
144: result[4] = formatter.format(d.getMinRegularValue(series,
145: item));
146: result[5] = formatter.format(d.getMaxRegularValue(series,
147: item));
148: result[6] = formatter.format(d.getQ1Value(series, item));
149: result[7] = formatter.format(d.getQ3Value(series, item));
150: }
151: return result;
152: }
153:
154: /**
155: * Tests if this object is equal to another.
156: *
157: * @param obj the other object.
158: *
159: * @return A boolean.
160: */
161: public boolean equals(Object obj) {
162: if (obj == this ) {
163: return true;
164: }
165: if (!(obj instanceof BoxAndWhiskerXYToolTipGenerator)) {
166: return false;
167: }
168: return super.equals(obj);
169: }
170:
171: }
|