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: * SymbolicXYItemLabelGenerator.java
029: * ---------------------------------
030: * (C) Copyright 2001-2007, by Anthony Boulestreau and Contributors.
031: *
032: * Original Author: Anthony Boulestreau;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: SymbolicXYItemLabelGenerator.java,v 1.5.2.2 2007/02/02 15:52:42 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 29-Mar-2002 : Version 1, contributed by Anthony Boulestreau (DG);
040: * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
041: * 23-Mar-2003 : Implemented Serializable (DG);
042: * 13-Aug-2003 : Implemented Cloneable (DG);
043: * 17-Nov-2003 : Implemented PublicCloneable (DG);
044: * 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
045: * 19-Jan-2005 : Now accesses primitives only from dataset (DG);
046: * 20-Apr-2005 : Renamed XYLabelGenerator --> XYItemLabelGenerator (DG);
047: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
048: *
049: */
050:
051: package org.jfree.chart.labels;
052:
053: import java.io.Serializable;
054:
055: import org.jfree.data.time.RegularTimePeriod;
056: import org.jfree.data.time.TimeSeriesCollection;
057: import org.jfree.data.xy.XYDataset;
058: import org.jfree.data.xy.XisSymbolic;
059: import org.jfree.data.xy.YisSymbolic;
060: import org.jfree.util.PublicCloneable;
061:
062: /**
063: * A standard item label generator for plots that use data from an
064: * {@link XYDataset}.
065: */
066: public class SymbolicXYItemLabelGenerator implements
067: XYItemLabelGenerator, XYToolTipGenerator, Cloneable,
068: PublicCloneable, Serializable {
069:
070: /** For serialization. */
071: private static final long serialVersionUID = 3963400354475494395L;
072:
073: /**
074: * Generates a tool tip text item for a particular item within a series.
075: *
076: * @param data the dataset.
077: * @param series the series number (zero-based index).
078: * @param item the item number (zero-based index).
079: *
080: * @return The tool tip text (possibly <code>null</code>).
081: */
082: public String generateToolTip(XYDataset data, int series, int item) {
083:
084: String xStr, yStr;
085: if (data instanceof YisSymbolic) {
086: yStr = ((YisSymbolic) data).getYSymbolicValue(series, item);
087: } else {
088: double y = data.getYValue(series, item);
089: yStr = Double.toString(round(y, 2));
090: }
091: if (data instanceof XisSymbolic) {
092: xStr = ((XisSymbolic) data).getXSymbolicValue(series, item);
093: } else if (data instanceof TimeSeriesCollection) {
094: RegularTimePeriod p = ((TimeSeriesCollection) data)
095: .getSeries(series).getTimePeriod(item);
096: xStr = p.toString();
097: } else {
098: double x = data.getXValue(series, item);
099: xStr = Double.toString(round(x, 2));
100: }
101: return "X: " + xStr + ", Y: " + yStr;
102: }
103:
104: /**
105: * Generates a label for the specified item. The label is typically a
106: * formatted version of the data value, but any text can be used.
107: *
108: * @param dataset the dataset (<code>null</code> not permitted).
109: * @param series the series index (zero-based).
110: * @param category the category index (zero-based).
111: *
112: * @return The label (possibly <code>null</code>).
113: */
114: public String generateLabel(XYDataset dataset, int series,
115: int category) {
116: return null; //TODO: implement this method properly
117: }
118:
119: /**
120: * Round a double value.
121: *
122: * @param value the value.
123: * @param nb the exponent.
124: *
125: * @return The rounded value.
126: */
127: private static double round(double value, int nb) {
128: if (nb <= 0) {
129: return Math.floor(value + 0.5d);
130: }
131: double p = Math.pow(10, nb);
132: double tempval = Math.floor(value * p + 0.5d);
133: return tempval / p;
134: }
135:
136: /**
137: * Returns an independent copy of the generator.
138: *
139: * @return A clone.
140: *
141: * @throws CloneNotSupportedException if cloning is not supported.
142: */
143: public Object clone() throws CloneNotSupportedException {
144: return super .clone();
145: }
146:
147: /**
148: * Tests if this object is equal to another.
149: *
150: * @param obj the other object.
151: *
152: * @return A boolean.
153: */
154: public boolean equals(Object obj) {
155: if (obj == this ) {
156: return true;
157: }
158: if (obj instanceof SymbolicXYItemLabelGenerator) {
159: return true;
160: }
161: return false;
162: }
163:
164: }
|