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: * XYBubbleRenderer.java
029: * ---------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): Christian W. Zuckschwerdt;
034: *
035: * $Id: XYBubbleRenderer.java,v 1.8.2.13 2007/06/13 09:57:14 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 28-Jan-2003 : Version 1 (DG);
040: * 25-Mar-2003 : Implemented Serializable (DG);
041: * 01-May-2003 : Modified drawItem() method signature (DG);
042: * 30-Jul-2003 : Modified entity constructor (CZ);
043: * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
044: * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
045: * 10-Feb-2004 : Small change to drawItem() method to make cut-and-paste
046: * overriding easier (DG);
047: * 15-Jul-2004 : Switched getZ() and getZValue() methods (DG);
048: * 19-Jan-2005 : Now accesses only primitives from dataset (DG);
049: * 28-Feb-2005 : Modify renderer to use circles in legend (DG);
050: * 17-Mar-2005 : Fixed bug in bubble bounds calculation (DG);
051: * 20-Apr-2005 : Use generators for legend tooltips and URLs (DG);
052: * ------------- JFREECHART 1.0.x ---------------------------------------------
053: * 13-Dec-2005 : Added support for item labels (bug 1373371) (DG);
054: * 20-Jan-2006 : Check flag for drawing item labels (DG);
055: * 21-Sep-2006 : Respect the outline paint and stroke settings (DG);
056: * 24-Jan-2007 : Added new equals() override (DG);
057: * 06-Feb-2007 : Fixed bug 1086307, crosshairs with multiple axes (DG);
058: * 20-Apr-2007 : Updated getLegendItem() for renderer change (DG);
059: * 17-May-2007 : Set datasetIndex and seriesIndex in getLegendItem() (DG);
060: * 18-May-2007 : Set dataset and seriesKey for LegendItem (DG);
061: * 13-Jun-2007 : Fixed seriesVisibility bug (DG);
062: *
063: */
064:
065: package org.jfree.chart.renderer.xy;
066:
067: import java.awt.Graphics2D;
068: import java.awt.Paint;
069: import java.awt.Shape;
070: import java.awt.Stroke;
071: import java.awt.geom.Ellipse2D;
072: import java.awt.geom.Rectangle2D;
073: import java.io.Serializable;
074:
075: import org.jfree.chart.LegendItem;
076: import org.jfree.chart.axis.ValueAxis;
077: import org.jfree.chart.entity.EntityCollection;
078: import org.jfree.chart.plot.CrosshairState;
079: import org.jfree.chart.plot.PlotOrientation;
080: import org.jfree.chart.plot.PlotRenderingInfo;
081: import org.jfree.chart.plot.XYPlot;
082: import org.jfree.data.xy.XYDataset;
083: import org.jfree.data.xy.XYZDataset;
084: import org.jfree.ui.RectangleEdge;
085: import org.jfree.util.PublicCloneable;
086:
087: /**
088: * A renderer that draws a circle at each data point with a diameter that is
089: * determined by the z-value in the dataset (the renderer requires the dataset
090: * to be an instance of {@link XYZDataset}.
091: */
092: public class XYBubbleRenderer extends AbstractXYItemRenderer implements
093: XYItemRenderer, Cloneable, PublicCloneable, Serializable {
094:
095: /** For serialization. */
096: public static final long serialVersionUID = -5221991598674249125L;
097:
098: /**
099: * A constant to specify that the bubbles drawn by this renderer should be
100: * scaled on both axes (see {@link #XYBubbleRenderer(int)}).
101: */
102: public static final int SCALE_ON_BOTH_AXES = 0;
103:
104: /**
105: * A constant to specify that the bubbles drawn by this renderer should be
106: * scaled on the domain axis (see {@link #XYBubbleRenderer(int)}).
107: */
108: public static final int SCALE_ON_DOMAIN_AXIS = 1;
109:
110: /**
111: * A constant to specify that the bubbles drawn by this renderer should be
112: * scaled on the range axis (see {@link #XYBubbleRenderer(int)}).
113: */
114: public static final int SCALE_ON_RANGE_AXIS = 2;
115:
116: /** Controls how the width and height of the bubble are scaled. */
117: private int scaleType;
118:
119: /**
120: * Constructs a new renderer.
121: */
122: public XYBubbleRenderer() {
123: this (SCALE_ON_BOTH_AXES);
124: }
125:
126: /**
127: * Constructs a new renderer with the specified type of scaling.
128: *
129: * @param scaleType the type of scaling (must be one of:
130: * {@link #SCALE_ON_BOTH_AXES}, {@link #SCALE_ON_DOMAIN_AXIS},
131: * {@link #SCALE_ON_RANGE_AXIS}).
132: */
133: public XYBubbleRenderer(int scaleType) {
134: super ();
135: if (scaleType < 0 || scaleType > 2) {
136: throw new IllegalArgumentException("Invalid 'scaleType'.");
137: }
138: this .scaleType = scaleType;
139: }
140:
141: /**
142: * Returns the scale type that was set when the renderer was constructed.
143: *
144: * @return The scale type (one of: {@link #SCALE_ON_BOTH_AXES},
145: * {@link #SCALE_ON_DOMAIN_AXIS}, {@link #SCALE_ON_RANGE_AXIS}).
146: */
147: public int getScaleType() {
148: return this .scaleType;
149: }
150:
151: /**
152: * Draws the visual representation of a single data item.
153: *
154: * @param g2 the graphics device.
155: * @param state the renderer state.
156: * @param dataArea the area within which the data is being drawn.
157: * @param info collects information about the drawing.
158: * @param plot the plot (can be used to obtain standard color
159: * information etc).
160: * @param domainAxis the domain (horizontal) axis.
161: * @param rangeAxis the range (vertical) axis.
162: * @param dataset the dataset (an {@link XYZDataset} is expected).
163: * @param series the series index (zero-based).
164: * @param item the item index (zero-based).
165: * @param crosshairState crosshair information for the plot
166: * (<code>null</code> permitted).
167: * @param pass the pass index.
168: */
169: public void drawItem(Graphics2D g2, XYItemRendererState state,
170: Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
171: ValueAxis domainAxis, ValueAxis rangeAxis,
172: XYDataset dataset, int series, int item,
173: CrosshairState crosshairState, int pass) {
174:
175: // return straight away if the item is not visible
176: if (!getItemVisible(series, item)) {
177: return;
178: }
179:
180: PlotOrientation orientation = plot.getOrientation();
181:
182: // get the data point...
183: double x = dataset.getXValue(series, item);
184: double y = dataset.getYValue(series, item);
185: double z = Double.NaN;
186: if (dataset instanceof XYZDataset) {
187: XYZDataset xyzData = (XYZDataset) dataset;
188: z = xyzData.getZValue(series, item);
189: }
190: if (!Double.isNaN(z)) {
191: RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
192: RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
193: double transX = domainAxis.valueToJava2D(x, dataArea,
194: domainAxisLocation);
195: double transY = rangeAxis.valueToJava2D(y, dataArea,
196: rangeAxisLocation);
197:
198: double transDomain = 0.0;
199: double transRange = 0.0;
200: double zero;
201:
202: switch (getScaleType()) {
203: case SCALE_ON_DOMAIN_AXIS:
204: zero = domainAxis.valueToJava2D(0.0, dataArea,
205: domainAxisLocation);
206: transDomain = domainAxis.valueToJava2D(z, dataArea,
207: domainAxisLocation)
208: - zero;
209: transRange = transDomain;
210: break;
211: case SCALE_ON_RANGE_AXIS:
212: zero = rangeAxis.valueToJava2D(0.0, dataArea,
213: rangeAxisLocation);
214: transRange = zero
215: - rangeAxis.valueToJava2D(z, dataArea,
216: rangeAxisLocation);
217: transDomain = transRange;
218: break;
219: default:
220: double zero1 = domainAxis.valueToJava2D(0.0, dataArea,
221: domainAxisLocation);
222: double zero2 = rangeAxis.valueToJava2D(0.0, dataArea,
223: rangeAxisLocation);
224: transDomain = domainAxis.valueToJava2D(z, dataArea,
225: domainAxisLocation)
226: - zero1;
227: transRange = zero2
228: - rangeAxis.valueToJava2D(z, dataArea,
229: rangeAxisLocation);
230: }
231: transDomain = Math.abs(transDomain);
232: transRange = Math.abs(transRange);
233: Ellipse2D circle = null;
234: if (orientation == PlotOrientation.VERTICAL) {
235: circle = new Ellipse2D.Double(transX - transDomain
236: / 2.0, transY - transRange / 2.0, transDomain,
237: transRange);
238: } else if (orientation == PlotOrientation.HORIZONTAL) {
239: circle = new Ellipse2D.Double(
240: transY - transRange / 2.0, transX - transDomain
241: / 2.0, transRange, transDomain);
242: }
243: g2.setPaint(getItemPaint(series, item));
244: g2.fill(circle);
245: g2.setStroke(getItemOutlineStroke(series, item));
246: g2.setPaint(getItemOutlinePaint(series, item));
247: g2.draw(circle);
248:
249: if (isItemLabelVisible(series, item)) {
250: if (orientation == PlotOrientation.VERTICAL) {
251: drawItemLabel(g2, orientation, dataset, series,
252: item, transX, transY, false);
253: } else if (orientation == PlotOrientation.HORIZONTAL) {
254: drawItemLabel(g2, orientation, dataset, series,
255: item, transY, transX, false);
256: }
257: }
258:
259: // add an entity if this info is being collected
260: EntityCollection entities = null;
261: if (info != null) {
262: entities = info.getOwner().getEntityCollection();
263: if (entities != null && circle.intersects(dataArea)) {
264: addEntity(entities, circle, dataset, series, item,
265: circle.getCenterX(), circle.getCenterY());
266: }
267: }
268:
269: int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
270: int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
271: updateCrosshairValues(crosshairState, x, y,
272: domainAxisIndex, rangeAxisIndex, transX, transY,
273: orientation);
274: }
275:
276: }
277:
278: /**
279: * Returns a legend item for the specified series. The default method
280: * is overridden so that the legend displays circles for all series.
281: *
282: * @param datasetIndex the dataset index (zero-based).
283: * @param series the series index (zero-based).
284: *
285: * @return A legend item for the series.
286: */
287: public LegendItem getLegendItem(int datasetIndex, int series) {
288: LegendItem result = null;
289: XYPlot plot = getPlot();
290: if (plot == null) {
291: return null;
292: }
293:
294: XYDataset dataset = plot.getDataset(datasetIndex);
295: if (dataset != null) {
296: if (getItemVisible(series, 0)) {
297: String label = getLegendItemLabelGenerator()
298: .generateLabel(dataset, series);
299: String description = label;
300: String toolTipText = null;
301: if (getLegendItemToolTipGenerator() != null) {
302: toolTipText = getLegendItemToolTipGenerator()
303: .generateLabel(dataset, series);
304: }
305: String urlText = null;
306: if (getLegendItemURLGenerator() != null) {
307: urlText = getLegendItemURLGenerator()
308: .generateLabel(dataset, series);
309: }
310: Shape shape = new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0);
311: Paint paint = lookupSeriesPaint(series);
312: Paint outlinePaint = lookupSeriesOutlinePaint(series);
313: Stroke outlineStroke = lookupSeriesOutlineStroke(series);
314: result = new LegendItem(label, description,
315: toolTipText, urlText, shape, paint,
316: outlineStroke, outlinePaint);
317: result.setDataset(dataset);
318: result.setDatasetIndex(datasetIndex);
319: result.setSeriesKey(dataset.getSeriesKey(series));
320: result.setSeriesIndex(series);
321: }
322: }
323: return result;
324: }
325:
326: /**
327: * Tests this renderer for equality with an arbitrary object.
328: *
329: * @param obj the object (<code>null</code> permitted).
330: *
331: * @return A boolean.
332: */
333: public boolean equals(Object obj) {
334: if (obj == this ) {
335: return true;
336: }
337: if (!(obj instanceof XYBubbleRenderer)) {
338: return false;
339: }
340: XYBubbleRenderer that = (XYBubbleRenderer) obj;
341: if (this .scaleType != that.scaleType) {
342: return false;
343: }
344: return super .equals(obj);
345: }
346:
347: /**
348: * Returns a clone of the renderer.
349: *
350: * @return A clone.
351: *
352: * @throws CloneNotSupportedException if the renderer cannot be cloned.
353: */
354: public Object clone() throws CloneNotSupportedException {
355: return super.clone();
356: }
357:
358: }
|