1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ----------------------
  27. * YIntervalRenderer.java
  28. * ----------------------
  29. * (C) Copyright 2002-2004, by Object Refinery Limited.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): -;
  33. *
  34. * $Id: YIntervalRenderer.java,v 1.5 2005/03/10 11:21:03 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 05-Nov-2002 : Version 1 (DG);
  39. * 25-Mar-2003 : Implemented Serializable (DG);
  40. * 01-May-2003 : Modified drawItem(...) method signature (DG);
  41. * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
  42. * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
  43. * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
  44. * 27-Sep-2004 : Access double values from dataset (DG);
  45. * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
  46. *
  47. */
  48. package org.jfree.chart.renderer.xy;
  49. import java.awt.Graphics2D;
  50. import java.awt.Paint;
  51. import java.awt.Shape;
  52. import java.awt.Stroke;
  53. import java.awt.geom.Line2D;
  54. import java.awt.geom.Rectangle2D;
  55. import java.io.Serializable;
  56. import org.jfree.chart.axis.ValueAxis;
  57. import org.jfree.chart.entity.EntityCollection;
  58. import org.jfree.chart.entity.XYItemEntity;
  59. import org.jfree.chart.labels.XYToolTipGenerator;
  60. import org.jfree.chart.plot.CrosshairState;
  61. import org.jfree.chart.plot.PlotOrientation;
  62. import org.jfree.chart.plot.PlotRenderingInfo;
  63. import org.jfree.chart.plot.XYPlot;
  64. import org.jfree.data.xy.IntervalXYDataset;
  65. import org.jfree.data.xy.XYDataset;
  66. import org.jfree.ui.RectangleEdge;
  67. import org.jfree.util.PublicCloneable;
  68. import org.jfree.util.ShapeUtilities;
  69. /**
  70. * A renderer that draws a line connecting the start and end Y values for an
  71. * {@link XYPlot}.
  72. */
  73. public class YIntervalRenderer extends AbstractXYItemRenderer
  74. implements XYItemRenderer,
  75. Cloneable,
  76. PublicCloneable,
  77. Serializable {
  78. /**
  79. * The default constructor.
  80. */
  81. public YIntervalRenderer() {
  82. super();
  83. }
  84. /**
  85. * Draws the visual representation of a single data item.
  86. *
  87. * @param g2 the graphics device.
  88. * @param state the renderer state.
  89. * @param dataArea the area within which the plot is being drawn.
  90. * @param info collects information about the drawing.
  91. * @param plot the plot (can be used to obtain standard color
  92. * information etc).
  93. * @param domainAxis the domain axis.
  94. * @param rangeAxis the range axis.
  95. * @param dataset the dataset.
  96. * @param series the series index (zero-based).
  97. * @param item the item index (zero-based).
  98. * @param crosshairState crosshair information for the plot
  99. * (<code>null</code> permitted).
  100. * @param pass the pass index (ignored here).
  101. */
  102. public void drawItem(Graphics2D g2,
  103. XYItemRendererState state,
  104. Rectangle2D dataArea,
  105. PlotRenderingInfo info,
  106. XYPlot plot,
  107. ValueAxis domainAxis,
  108. ValueAxis rangeAxis,
  109. XYDataset dataset,
  110. int series,
  111. int item,
  112. CrosshairState crosshairState,
  113. int pass) {
  114. // setup for collecting optional entity info...
  115. Shape entityArea = null;
  116. EntityCollection entities = null;
  117. if (info != null) {
  118. entities = info.getOwner().getEntityCollection();
  119. }
  120. IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
  121. double x = intervalDataset.getXValue(series, item);
  122. double yLow = intervalDataset.getStartYValue(series, item);
  123. double yHigh = intervalDataset.getEndYValue(series, item);
  124. RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
  125. RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
  126. double xx = domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
  127. double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, yAxisLocation);
  128. double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, yAxisLocation);
  129. Paint p = getItemPaint(series, item);
  130. Stroke s = getItemStroke(series, item);
  131. Line2D line = null;
  132. Shape shape = getItemShape(series, item);
  133. Shape top = null;
  134. Shape bottom = null;
  135. PlotOrientation orientation = plot.getOrientation();
  136. if (orientation == PlotOrientation.HORIZONTAL) {
  137. line = new Line2D.Double(yyLow, xx, yyHigh, xx);
  138. top = ShapeUtilities.createTranslatedShape(shape, yyHigh, xx);
  139. bottom = ShapeUtilities.createTranslatedShape(shape, yyLow, xx);
  140. }
  141. else if (orientation == PlotOrientation.VERTICAL) {
  142. line = new Line2D.Double(xx, yyLow, xx, yyHigh);
  143. top = ShapeUtilities.createTranslatedShape(shape, xx, yyHigh);
  144. bottom = ShapeUtilities.createTranslatedShape(shape, xx, yyLow);
  145. }
  146. g2.setPaint(p);
  147. g2.setStroke(s);
  148. g2.draw(line);
  149. g2.fill(top);
  150. g2.fill(bottom);
  151. // add an entity for the item...
  152. if (entities != null) {
  153. if (entityArea == null) {
  154. entityArea = line.getBounds();
  155. }
  156. String tip = null;
  157. XYToolTipGenerator generator = getToolTipGenerator(series, item);
  158. if (generator != null) {
  159. tip = generator.generateToolTip(dataset, series, item);
  160. }
  161. String url = null;
  162. if (getURLGenerator() != null) {
  163. url = getURLGenerator().generateURL(dataset, series, item);
  164. }
  165. XYItemEntity entity = new XYItemEntity(
  166. entityArea, dataset, series, item, tip, url
  167. );
  168. entities.add(entity);
  169. }
  170. }
  171. /**
  172. * Returns a clone of the renderer.
  173. *
  174. * @return A clone.
  175. *
  176. * @throws CloneNotSupportedException if the renderer cannot be cloned.
  177. */
  178. public Object clone() throws CloneNotSupportedException {
  179. return super.clone();
  180. }
  181. }