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     * YIntervalRenderer.java
029     * ----------------------
030     * (C) Copyright 2002-2008, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * Changes
036     * -------
037     * 05-Nov-2002 : Version 1 (DG);
038     * 25-Mar-2003 : Implemented Serializable (DG);
039     * 01-May-2003 : Modified drawItem() method signature (DG);
040     * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
041     * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
042     * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
043     * 27-Sep-2004 : Access double values from dataset (DG);
044     * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
045     * 11-Apr-2008 : New override for findRangeBounds() (DG);
046     * 26-May-2008 : Added item label support (DG);
047     *
048     */
049    
050    package org.jfree.chart.renderer.xy;
051    
052    import java.awt.Font;
053    import java.awt.Graphics2D;
054    import java.awt.Paint;
055    import java.awt.Shape;
056    import java.awt.Stroke;
057    import java.awt.geom.Line2D;
058    import java.awt.geom.Point2D;
059    import java.awt.geom.Rectangle2D;
060    import java.io.Serializable;
061    
062    import org.jfree.chart.axis.ValueAxis;
063    import org.jfree.chart.entity.EntityCollection;
064    import org.jfree.chart.event.RendererChangeEvent;
065    import org.jfree.chart.labels.ItemLabelPosition;
066    import org.jfree.chart.labels.XYItemLabelGenerator;
067    import org.jfree.chart.plot.CrosshairState;
068    import org.jfree.chart.plot.PlotOrientation;
069    import org.jfree.chart.plot.PlotRenderingInfo;
070    import org.jfree.chart.plot.XYPlot;
071    import org.jfree.data.Range;
072    import org.jfree.data.general.DatasetUtilities;
073    import org.jfree.data.xy.IntervalXYDataset;
074    import org.jfree.data.xy.XYDataset;
075    import org.jfree.text.TextUtilities;
076    import org.jfree.ui.RectangleEdge;
077    import org.jfree.util.ObjectUtilities;
078    import org.jfree.util.PublicCloneable;
079    import org.jfree.util.ShapeUtilities;
080    
081    /**
082     * A renderer that draws a line connecting the start and end Y values for an
083     * {@link XYPlot}.  The example shown here is generated by the
084     * <code>YIntervalRendererDemo1.java</code> program included in the JFreeChart
085     * demo collection:
086     * <br><br>
087     * <img src="../../../../../images/YIntervalRendererSample.png"
088     * alt="YIntervalRendererSample.png" />
089     */
090    public class YIntervalRenderer extends AbstractXYItemRenderer
091            implements XYItemRenderer, Cloneable, PublicCloneable, Serializable {
092    
093        /** For serialization. */
094        private static final long serialVersionUID = -2951586537224143260L;
095    
096        /**
097         * An additional item label generator.  If this is non-null, the item
098         * label generated will be displayed near the lower y-value at the
099         * position given by getNegativeItemLabelPosition().
100         *
101         * @since 1.0.10
102         */
103        private XYItemLabelGenerator additionalItemLabelGenerator;
104    
105        /**
106         * The default constructor.
107         */
108        public YIntervalRenderer() {
109            super();
110            this.additionalItemLabelGenerator = null;
111        }
112    
113        /**
114         * Returns the generator for the item labels that appear near the lower
115         * y-value.
116         *
117         * @return The generator (possibly <code>null</code>).
118         *
119         * @see #setAdditionalItemLabelGenerator(XYItemLabelGenerator)
120         *
121         * @since 1.0.10
122         */
123        public XYItemLabelGenerator getAdditionalItemLabelGenerator() {
124            return this.additionalItemLabelGenerator;
125        }
126    
127        /**
128         * Sets the generator for the item labels that appear near the lower
129         * y-value and sends a {@link RendererChangeEvent} to all registered
130         * listeners.  If this is set to <code>null</code>, no item labels will be
131         * drawn.
132         *
133         * @param generator  the generator (<code>null</code> permitted).
134         *
135         * @see #getAdditionalItemLabelGenerator()
136         *
137         * @since 1.0.10
138         */
139        public void setAdditionalItemLabelGenerator(
140                XYItemLabelGenerator generator) {
141            this.additionalItemLabelGenerator = generator;
142            fireChangeEvent();
143        }
144    
145        /**
146         * Returns the range of values the renderer requires to display all the
147         * items from the specified dataset.
148         *
149         * @param dataset  the dataset (<code>null</code> permitted).
150         *
151         * @return The range (<code>null</code> if the dataset is <code>null</code>
152         *         or empty).
153         */
154        public Range findRangeBounds(XYDataset dataset) {
155            if (dataset != null) {
156                return DatasetUtilities.findRangeBounds(dataset, true);
157            }
158            else {
159                return null;
160            }
161        }
162    
163        /**
164         * Draws the visual representation of a single data item.
165         *
166         * @param g2  the graphics device.
167         * @param state  the renderer state.
168         * @param dataArea  the area within which the plot is being drawn.
169         * @param info  collects information about the drawing.
170         * @param plot  the plot (can be used to obtain standard color
171         *              information etc).
172         * @param domainAxis  the domain axis.
173         * @param rangeAxis  the range axis.
174         * @param dataset  the dataset.
175         * @param series  the series index (zero-based).
176         * @param item  the item index (zero-based).
177         * @param crosshairState  crosshair information for the plot
178         *                        (<code>null</code> permitted).
179         * @param pass  the pass index (ignored here).
180         */
181        public void drawItem(Graphics2D g2,
182                             XYItemRendererState state,
183                             Rectangle2D dataArea,
184                             PlotRenderingInfo info,
185                             XYPlot plot,
186                             ValueAxis domainAxis,
187                             ValueAxis rangeAxis,
188                             XYDataset dataset,
189                             int series,
190                             int item,
191                             CrosshairState crosshairState,
192                             int pass) {
193    
194            // setup for collecting optional entity info...
195            EntityCollection entities = null;
196            if (info != null) {
197                entities = info.getOwner().getEntityCollection();
198            }
199    
200            IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
201    
202            double x = intervalDataset.getXValue(series, item);
203            double yLow   = intervalDataset.getStartYValue(series, item);
204            double yHigh  = intervalDataset.getEndYValue(series, item);
205    
206            RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
207            RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
208    
209            double xx = domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
210            double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, yAxisLocation);
211            double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, yAxisLocation);
212    
213            Paint p = getItemPaint(series, item);
214            Stroke s = getItemStroke(series, item);
215    
216            Line2D line = null;
217            Shape shape = getItemShape(series, item);
218            Shape top = null;
219            Shape bottom = null;
220            PlotOrientation orientation = plot.getOrientation();
221            if (orientation == PlotOrientation.HORIZONTAL) {
222                line = new Line2D.Double(yyLow, xx, yyHigh, xx);
223                top = ShapeUtilities.createTranslatedShape(shape, yyHigh, xx);
224                bottom = ShapeUtilities.createTranslatedShape(shape, yyLow, xx);
225            }
226            else if (orientation == PlotOrientation.VERTICAL) {
227                line = new Line2D.Double(xx, yyLow, xx, yyHigh);
228                top = ShapeUtilities.createTranslatedShape(shape, xx, yyHigh);
229                bottom = ShapeUtilities.createTranslatedShape(shape, xx, yyLow);
230            }
231            g2.setPaint(p);
232            g2.setStroke(s);
233            g2.draw(line);
234    
235            g2.fill(top);
236            g2.fill(bottom);
237    
238            // for item labels, we have a special case because there is the
239            // possibility to draw (a) the regular item label near to just the
240            // upper y-value, or (b) the regular item label near the upper y-value
241            // PLUS an additional item label near the lower y-value.
242            if (isItemLabelVisible(series, item)) {
243                drawItemLabel(g2, orientation, dataset, series, item, xx, yyHigh,
244                        false);
245                drawAdditionalItemLabel(g2, orientation, dataset, series, item,
246                        xx, yyLow);
247            }
248    
249            // add an entity for the item...
250            if (entities != null) {
251                addEntity(entities, line.getBounds(), dataset, series, item, 0.0,
252                        0.0);
253            }
254    
255        }
256    
257        /**
258         * Draws an item label.
259         *
260         * @param g2  the graphics device.
261         * @param orientation  the orientation.
262         * @param dataset  the dataset.
263         * @param series  the series index (zero-based).
264         * @param item  the item index (zero-based).
265         * @param x  the x coordinate (in Java2D space).
266         * @param y  the y coordinate (in Java2D space).
267         * @param negative  indicates a negative value (which affects the item
268         *                  label position).
269         */
270        private void drawAdditionalItemLabel(Graphics2D g2,
271                PlotOrientation orientation, XYDataset dataset, int series,
272                int item, double x, double y) {
273    
274            if (this.additionalItemLabelGenerator == null) {
275                return;
276            }
277    
278            Font labelFont = getItemLabelFont(series, item);
279            Paint paint = getItemLabelPaint(series, item);
280            g2.setFont(labelFont);
281            g2.setPaint(paint);
282            String label = this.additionalItemLabelGenerator.generateLabel(dataset,
283                    series, item);
284    
285            ItemLabelPosition position = getNegativeItemLabelPosition(series, item);
286            Point2D anchorPoint = calculateLabelAnchorPoint(
287                    position.getItemLabelAnchor(), x, y, orientation);
288            TextUtilities.drawRotatedString(label, g2,
289                    (float) anchorPoint.getX(), (float) anchorPoint.getY(),
290                    position.getTextAnchor(), position.getAngle(),
291                    position.getRotationAnchor());
292        }
293    
294        /**
295         * Tests this renderer for equality with an arbitrary object.
296         *
297         * @param obj  the object (<code>null</code> permitted).
298         *
299         * @return A boolean.
300         */
301        public boolean equals(Object obj) {
302            if (obj == this) {
303                return true;
304            }
305            if (!(obj instanceof YIntervalRenderer)) {
306                return false;
307            }
308            YIntervalRenderer that = (YIntervalRenderer) obj;
309            if (!ObjectUtilities.equal(this.additionalItemLabelGenerator,
310                    that.additionalItemLabelGenerator)) {
311                return false;
312            }
313            return super.equals(obj);
314        }
315    
316        /**
317         * Returns a clone of the renderer.
318         *
319         * @return A clone.
320         *
321         * @throws CloneNotSupportedException  if the renderer cannot be cloned.
322         */
323        public Object clone() throws CloneNotSupportedException {
324            return super.clone();
325        }
326    
327    }