001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, 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     * XYDotRenderer.java
029     * ------------------
030     * (C) Copyright 2002-2005, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   Christian W. Zuckschwerdt;
034    ;
035     *
036     * $Id: XYDotRenderer.java,v 1.5.2.1 2005/10/25 20:56:21 mungady Exp $
037     *
038     * Changes (from 29-Oct-2002)
039     * --------------------------
040     * 29-Oct-2002 : Added standard header (DG);
041     * 25-Mar-2003 : Implemented Serializable (DG);
042     * 01-May-2003 : Modified drawItem() method signature (DG);
043     * 30-Jul-2003 : Modified entity constructor (CZ);
044     * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
045     * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
046     * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
047     * 19-Jan-2005 : Now uses only primitives from dataset (DG);
048     * 
049     */
050    
051    package org.jfree.chart.renderer.xy;
052    
053    import java.awt.Graphics2D;
054    import java.awt.geom.Rectangle2D;
055    import java.io.Serializable;
056    
057    import org.jfree.chart.axis.ValueAxis;
058    import org.jfree.chart.plot.CrosshairState;
059    import org.jfree.chart.plot.PlotOrientation;
060    import org.jfree.chart.plot.PlotRenderingInfo;
061    import org.jfree.chart.plot.XYPlot;
062    import org.jfree.data.xy.XYDataset;
063    import org.jfree.ui.RectangleEdge;
064    import org.jfree.util.PublicCloneable;
065    
066    /**
067     * A renderer that draws a small dot at each data point for an {@link XYPlot}.
068     */
069    public class XYDotRenderer extends AbstractXYItemRenderer 
070                               implements XYItemRenderer, 
071                                          Cloneable,
072                                          PublicCloneable,
073                                          Serializable {
074    
075        /** For serialization. */
076        private static final long serialVersionUID = -2764344339073566425L;
077        
078        /**
079         * Constructs a new renderer.
080         */
081        public XYDotRenderer() {
082            super();
083        }
084    
085        /**
086         * Draws the visual representation of a single data item.
087         *
088         * @param g2  the graphics device.
089         * @param state  the renderer state.
090         * @param dataArea  the area within which the data is being drawn.
091         * @param info  collects information about the drawing.
092         * @param plot  the plot (can be used to obtain standard color 
093         *              information etc).
094         * @param domainAxis  the domain (horizontal) axis.
095         * @param rangeAxis  the range (vertical) axis.
096         * @param dataset  the dataset.
097         * @param series  the series index (zero-based).
098         * @param item  the item index (zero-based).
099         * @param crosshairState  crosshair information for the plot 
100         *                        (<code>null</code> permitted).
101         * @param pass  the pass index.
102         */
103        public void drawItem(Graphics2D g2,
104                             XYItemRendererState state,
105                             Rectangle2D dataArea,
106                             PlotRenderingInfo info,
107                             XYPlot plot,
108                             ValueAxis domainAxis,
109                             ValueAxis rangeAxis,
110                             XYDataset dataset,
111                             int series,
112                             int item,
113                             CrosshairState crosshairState,
114                             int pass) {
115    
116            // get the data point...
117            double x = dataset.getXValue(series, item);
118            double y = dataset.getYValue(series, item);
119            if (!Double.isNaN(y)) {
120                RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
121                RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
122                double transX = domainAxis.valueToJava2D(
123                    x, dataArea, xAxisLocation
124                );
125                double transY = rangeAxis.valueToJava2D(
126                    y, dataArea, yAxisLocation
127                );
128    
129                g2.setPaint(getItemPaint(series, item));
130                PlotOrientation orientation = plot.getOrientation();
131                if (orientation == PlotOrientation.HORIZONTAL) {
132                    g2.drawRect((int) transY, (int) transX, 1, 1);
133                }
134                else if (orientation == PlotOrientation.VERTICAL) {
135                    g2.drawRect((int) transX, (int) transY, 1, 1);
136                }
137    
138                updateCrosshairValues(
139                    crosshairState, x, y, transX, transY, orientation
140                );
141            }
142    
143        }
144    
145        /**
146         * Returns a clone of the renderer.
147         * 
148         * @return A clone.
149         * 
150         * @throws CloneNotSupportedException  if the renderer cannot be cloned.
151         */
152        public Object clone() throws CloneNotSupportedException {
153            return super.clone();
154        }
155    
156    }