1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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. * SignalRenderer.java
  26. * -------------------
  27. * (C) Copyright 2001-2004, by Sylvain Viuejot and Contributors.
  28. *
  29. * Original Author: Sylvain Vieujot;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. * Richard Atkinson;
  32. * Christian W. Zuckschwerdt;
  33. *
  34. * $Id: SignalRenderer.java,v 1.2 2004/09/30 16:44:13 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 08-Jan-2002 : Version 1. Based on code in the SignalsPlot class, written by Sylvain
  39. * Vieujot (DG);
  40. * 23-Jan-2002 : Added DrawInfo parameter to drawItem(...) method (DG);
  41. * 14-Feb-2002 : Added small fix from Sylvain (DG);
  42. * 28-Mar-2002 : Added a property change listener mechanism so that renderers no longer need to be
  43. * immutable (DG);
  44. * 09-Apr-2002 : Removed translatedRangeZero from the drawItem(...) method, and changed the return
  45. * type of the drawItem method to void, reflecting a change in the XYItemRenderer
  46. * interface. Added tooltip code to drawItem(...) method (DG);
  47. * 25-Jun-2002 : Removed redundant code (DG);
  48. * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML image maps (RA);
  49. * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  50. * 25-Mar-2003 : Implemented Serializable (DG);
  51. * 01-May-2003 : Modified drawItem(...) method signature (DG);
  52. * 30-Jul-2003 : Modified entity constructor (CZ);
  53. * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
  54. * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
  55. * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState (DG);
  56. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
  57. *
  58. */
  59. package org.jfree.chart.renderer.xy;
  60. import java.awt.Color;
  61. import java.awt.Graphics2D;
  62. import java.awt.Paint;
  63. import java.awt.Stroke;
  64. import java.awt.geom.Ellipse2D;
  65. import java.awt.geom.GeneralPath;
  66. import java.awt.geom.Rectangle2D;
  67. import java.io.Serializable;
  68. import org.jfree.chart.axis.ValueAxis;
  69. import org.jfree.chart.entity.EntityCollection;
  70. import org.jfree.chart.entity.XYItemEntity;
  71. import org.jfree.chart.labels.XYToolTipGenerator;
  72. import org.jfree.chart.plot.CrosshairState;
  73. import org.jfree.chart.plot.PlotRenderingInfo;
  74. import org.jfree.chart.plot.XYPlot;
  75. import org.jfree.data.xy.SignalsDataset;
  76. import org.jfree.data.xy.XYDataset;
  77. import org.jfree.util.PublicCloneable;
  78. /**
  79. * A renderer that draws signals on an {@link XYPlot}.
  80. *
  81. * @author Sylvain Vieujot
  82. */
  83. public class SignalRenderer extends AbstractXYItemRenderer
  84. implements XYItemRenderer,
  85. Cloneable,
  86. PublicCloneable,
  87. Serializable {
  88. /** The mark offset. */
  89. private double markOffset = 5;
  90. /** The shape width. */
  91. private double shapeWidth = 15;
  92. /** The shape height. */
  93. private double shapeHeight = 25;
  94. /**
  95. * Creates a new renderer.
  96. */
  97. public SignalRenderer() {
  98. super();
  99. }
  100. /**
  101. * Returns the mark offset.
  102. *
  103. * @return the mark offset.
  104. */
  105. public double getMarkOffset() {
  106. return this.markOffset;
  107. }
  108. /**
  109. * Sets the mark offset.
  110. *
  111. * @param offset the mark offset.
  112. */
  113. public void setMarkOffset(double offset) {
  114. this.markOffset = offset;
  115. }
  116. /**
  117. * Returns the shape width.
  118. *
  119. * @return the shape width.
  120. */
  121. public double getShapeWidth() {
  122. return this.shapeWidth;
  123. }
  124. /**
  125. * Sets the shape width.
  126. *
  127. * @param width the shape width.
  128. */
  129. public void setShapeWidth(double width) {
  130. this.shapeWidth = width;
  131. }
  132. /**
  133. * Returns the shape height.
  134. *
  135. * @return the shape height.
  136. */
  137. public double getShapeHeight() {
  138. return this.shapeHeight;
  139. }
  140. /**
  141. * Sets the shape height.
  142. *
  143. * @param height the shape height.
  144. */
  145. public void setShapeHeight(double height) {
  146. this.shapeHeight = height;
  147. }
  148. /**
  149. * Draws the visual representation of a single data item.
  150. *
  151. * @param g2 the graphics device.
  152. * @param state the renderer state.
  153. * @param dataArea the area within which the plot is being drawn.
  154. * @param info collects information about the drawing.
  155. * @param plot the plot (can be used to obtain standard color information etc).
  156. * @param horizontalAxis the horizontal axis.
  157. * @param verticalAxis the vertical axis.
  158. * @param dataset the dataset.
  159. * @param series the series index (zero-based).
  160. * @param item the item index (zero-based).
  161. * @param crosshairState crosshair information for the plot (<code>null</code> permitted).
  162. * @param pass the pass index.
  163. */
  164. public void drawItem(Graphics2D g2,
  165. XYItemRendererState state,
  166. Rectangle2D dataArea,
  167. PlotRenderingInfo info,
  168. XYPlot plot,
  169. ValueAxis horizontalAxis,
  170. ValueAxis verticalAxis,
  171. XYDataset dataset,
  172. int series,
  173. int item,
  174. CrosshairState crosshairState,
  175. int pass) {
  176. // setup for collecting optional entity info...
  177. EntityCollection entities = null;
  178. if (info != null) {
  179. entities = info.getOwner().getEntityCollection();
  180. }
  181. SignalsDataset signalData = (SignalsDataset) dataset;
  182. Number x = signalData.getX(series, item);
  183. Number y = signalData.getY(series, item);
  184. int type = signalData.getType(series, item);
  185. //double level = signalData.getLevel(series, item);
  186. double xx = horizontalAxis.valueToJava2D(
  187. x.doubleValue(), dataArea, plot.getDomainAxisEdge()
  188. );
  189. double yy = verticalAxis.valueToJava2D(
  190. y.doubleValue(), dataArea, plot.getRangeAxisEdge()
  191. );
  192. Paint p = getItemPaint(series, item);
  193. Stroke s = getItemStroke(series, item);
  194. g2.setPaint(p);
  195. g2.setStroke(s);
  196. int direction = 1;
  197. if ((type == SignalsDataset.ENTER_LONG) || (type == SignalsDataset.EXIT_SHORT)) {
  198. yy = yy + this.markOffset;
  199. direction = -1;
  200. }
  201. else {
  202. yy = yy - this.markOffset;
  203. }
  204. GeneralPath path = new GeneralPath();
  205. if ((type == SignalsDataset.ENTER_LONG) || (type == SignalsDataset.ENTER_SHORT)) {
  206. path.moveTo((float) xx, (float) yy);
  207. path.lineTo(
  208. (float) (xx + this.shapeWidth / 2), (float) (yy - direction * this.shapeHeight / 3)
  209. );
  210. path.lineTo(
  211. (float) (xx + this.shapeWidth / 6), (float) (yy - direction * this.shapeHeight / 3)
  212. );
  213. path.lineTo(
  214. (float) (xx + this.shapeWidth / 6), (float) (yy - direction * this.shapeHeight)
  215. );
  216. path.lineTo(
  217. (float) (xx - this.shapeWidth / 6), (float) (yy - direction * this.shapeHeight)
  218. );
  219. path.lineTo(
  220. (float) (xx - this.shapeWidth / 6), (float) (yy - direction * this.shapeHeight / 3)
  221. );
  222. path.lineTo(
  223. (float) (xx - this.shapeWidth / 2), (float) (yy - direction * this.shapeHeight / 3)
  224. );
  225. path.lineTo((float) xx, (float) yy);
  226. }
  227. else {
  228. path.moveTo((float) xx, (float) yy);
  229. path.lineTo((float) xx, (float) (yy - direction * this.shapeHeight));
  230. Ellipse2D.Double ellipse = new Ellipse2D.Double(
  231. xx - this.shapeWidth / 2,
  232. yy + (direction == 1 ? -this.shapeHeight : this.shapeHeight - this.shapeWidth),
  233. this.shapeWidth,
  234. this.shapeWidth
  235. );
  236. path.append(ellipse, false);
  237. }
  238. g2.fill(path);
  239. g2.setPaint(Color.black);
  240. g2.draw(path);
  241. // add an entity for the item...
  242. if (entities != null) {
  243. String tip = null;
  244. XYToolTipGenerator generator = getToolTipGenerator(series, item);
  245. if (generator != null) {
  246. tip = generator.generateToolTip(dataset, series, item);
  247. }
  248. String url = null;
  249. if (getURLGenerator() != null) {
  250. url = getURLGenerator().generateURL(dataset, series, item);
  251. }
  252. XYItemEntity entity = new XYItemEntity(path, dataset, series, item, tip, url);
  253. entities.add(entity);
  254. }
  255. }
  256. /**
  257. * Returns a clone of the renderer.
  258. *
  259. * @return A clone.
  260. *
  261. * @throws CloneNotSupportedException if the renderer cannot be cloned.
  262. */
  263. public Object clone() throws CloneNotSupportedException {
  264. return super.clone();
  265. }
  266. }