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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * -------------------
  25. * XYStepRenderer.java
  26. * -------------------
  27. * (C) Copyright 2002-2005, by Roger Studner and Contributors.
  28. *
  29. * Original Author: Roger Studner;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. * Matthias Rose;
  32. *
  33. * $Id: XYStepRenderer.java,v 1.3 2005/01/19 12:41:39 mungady Exp $
  34. *
  35. * Changes
  36. * -------
  37. * 13-May-2002 : Version 1, contributed by Roger Studner (DG);
  38. * 25-Jun-2002 : Updated import statements (DG);
  39. * 22-Jul-2002 : Added check for null data items (DG);
  40. * 25-Mar-2003 : Implemented Serializable (DG);
  41. * 01-May-2003 : Modified drawItem(...) method signature (DG);
  42. * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
  43. * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
  44. * 28-Oct-2003 : Added tooltips, code contributed by Matthias Rose (RFE 824857) (DG);
  45. * 10-Feb-2004 : Removed working line (use line from state object instead) (DG);
  46. * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed XYToolTipGenerator
  47. * --> XYItemLabelGenerator (DG);
  48. * 19-Jan-2005 : Now accesses only primitives from dataset (DG);
  49. *
  50. */
  51. package org.jfree.chart.renderer.xy;
  52. import java.awt.Graphics2D;
  53. import java.awt.Paint;
  54. import java.awt.Shape;
  55. import java.awt.Stroke;
  56. import java.awt.geom.Line2D;
  57. import java.awt.geom.Rectangle2D;
  58. import java.io.Serializable;
  59. import org.jfree.chart.axis.ValueAxis;
  60. import org.jfree.chart.entity.EntityCollection;
  61. import org.jfree.chart.entity.XYItemEntity;
  62. import org.jfree.chart.labels.XYToolTipGenerator;
  63. import org.jfree.chart.plot.CrosshairState;
  64. import org.jfree.chart.plot.PlotOrientation;
  65. import org.jfree.chart.plot.PlotRenderingInfo;
  66. import org.jfree.chart.plot.XYPlot;
  67. import org.jfree.chart.urls.XYURLGenerator;
  68. import org.jfree.data.xy.XYDataset;
  69. import org.jfree.ui.RectangleEdge;
  70. import org.jfree.util.PublicCloneable;
  71. /**
  72. * Line/Step item renderer for an {@link XYPlot}. This class draws lines between data
  73. * points, only allowing horizontal or vertical lines (steps).
  74. *
  75. * @author Roger Studner
  76. */
  77. public class XYStepRenderer extends AbstractXYItemRenderer
  78. implements XYItemRenderer,
  79. Cloneable,
  80. PublicCloneable,
  81. Serializable {
  82. /**
  83. * Constructs a new renderer with no tooltip or URL generation.
  84. */
  85. public XYStepRenderer() {
  86. super();
  87. }
  88. /**
  89. * Constructs a new renderer.
  90. *
  91. * @param toolTipGenerator the item label generator.
  92. * @param urlGenerator the URL generator.
  93. */
  94. public XYStepRenderer(XYToolTipGenerator toolTipGenerator,
  95. XYURLGenerator urlGenerator) {
  96. super();
  97. setToolTipGenerator(toolTipGenerator);
  98. setURLGenerator(urlGenerator);
  99. }
  100. /**
  101. * Draws the visual representation of a single data item.
  102. *
  103. * @param g2 the graphics device.
  104. * @param state the renderer state.
  105. * @param dataArea the area within which the data is being drawn.
  106. * @param info collects information about the drawing.
  107. * @param plot the plot (can be used to obtain standard color information etc).
  108. * @param domainAxis the domain axis.
  109. * @param rangeAxis the vertical axis.
  110. * @param dataset the dataset.
  111. * @param series the series index (zero-based).
  112. * @param item the item index (zero-based).
  113. * @param crosshairState crosshair information for the plot (<code>null</code> permitted).
  114. * @param pass the pass index (ignored here).
  115. */
  116. public void drawItem(Graphics2D g2,
  117. XYItemRendererState state,
  118. Rectangle2D dataArea,
  119. PlotRenderingInfo info,
  120. XYPlot plot,
  121. ValueAxis domainAxis,
  122. ValueAxis rangeAxis,
  123. XYDataset dataset,
  124. int series,
  125. int item,
  126. CrosshairState crosshairState,
  127. int pass) {
  128. PlotOrientation orientation = plot.getOrientation();
  129. Paint seriesPaint = getItemPaint(series, item);
  130. Stroke seriesStroke = getItemStroke(series, item);
  131. g2.setPaint(seriesPaint);
  132. g2.setStroke(seriesStroke);
  133. // get the data point...
  134. double x1 = dataset.getXValue(series, item);
  135. double y1 = dataset.getYValue(series, item);
  136. if (Double.isNaN(y1)) {
  137. return;
  138. }
  139. RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
  140. RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
  141. double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
  142. double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
  143. if (item > 0) {
  144. // get the previous data point...
  145. double x0 = dataset.getXValue(series, item - 1);
  146. double y0 = dataset.getYValue(series, item - 1);
  147. if (Double.isNaN(y0)) {
  148. double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
  149. double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);
  150. Line2D line = state.workingLine;
  151. if (orientation == PlotOrientation.HORIZONTAL) {
  152. if (transY0 == transY1) { //this represents the situation for drawing a
  153. //horizontal bar.
  154. line.setLine(transY0, transX0, transY1, transX1);
  155. g2.draw(line);
  156. }
  157. else { //this handles the need to perform a 'step'.
  158. line.setLine(transY0, transX0, transY1, transX0);
  159. g2.draw(line);
  160. line.setLine(transY1, transX0, transY1, transX1);
  161. g2.draw(line);
  162. }
  163. }
  164. else if (orientation == PlotOrientation.VERTICAL) {
  165. if (transY0 == transY1) { //this represents the situation for drawing a
  166. //horizontal bar.
  167. line.setLine(transX0, transY0, transX1, transY1);
  168. g2.draw(line);
  169. }
  170. else { //this handles the need to perform a 'step'.
  171. line.setLine(transX0, transY0, transX1, transY0);
  172. g2.draw(line);
  173. line.setLine(transX1, transY0, transX1, transY1);
  174. g2.draw(line);
  175. }
  176. }
  177. }
  178. }
  179. updateCrosshairValues(crosshairState, x1, y1, transX1, transY1, orientation);
  180. // collect entity and tool tip information...
  181. if (state.getInfo() != null) {
  182. EntityCollection entities = state.getInfo().getOwner().getEntityCollection();
  183. if (entities != null) {
  184. Shape shape = orientation == PlotOrientation.VERTICAL
  185. ? new Rectangle2D.Double(transX1 - 2, transY1 - 2, 4.0, 4.0)
  186. : new Rectangle2D.Double(transY1 - 2, transX1 - 2, 4.0, 4.0);
  187. if (shape != null) {
  188. String tip = null;
  189. XYToolTipGenerator generator = getToolTipGenerator(series, item);
  190. if (generator != null) {
  191. tip = generator.generateToolTip(dataset, series, item);
  192. }
  193. String url = null;
  194. if (getURLGenerator() != null) {
  195. url = getURLGenerator().generateURL(dataset, series, item);
  196. }
  197. XYItemEntity entity = new XYItemEntity(shape, dataset, series, item, tip, url);
  198. entities.add(entity);
  199. }
  200. }
  201. }
  202. }
  203. /**
  204. * Returns a clone of the renderer.
  205. *
  206. * @return A clone.
  207. *
  208. * @throws CloneNotSupportedException if the renderer cannot be cloned.
  209. */
  210. public Object clone() throws CloneNotSupportedException {
  211. return super.clone();
  212. }
  213. }