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