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. * HighLowRenderer.java
  26. * --------------------
  27. * (C) Copyright 2001-2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): Richard Atkinson;
  31. * Christian W. Zuckschwerdt;
  32. *
  33. * $Id: HighLowRenderer.java,v 1.3 2004/09/30 16:44:13 mungady Exp $
  34. *
  35. * Changes
  36. * -------
  37. * 13-Dec-2001 : Version 1 (DG);
  38. * 23-Jan-2002 : Added DrawInfo parameter to drawItem(...) method (DG);
  39. * 28-Mar-2002 : Added a property change listener mechanism so that renderers no longer need to be
  40. * immutable (DG);
  41. * 09-Apr-2002 : Removed translatedRangeZero from the drawItem(...) method, and changed the return
  42. * type of the drawItem method to void, reflecting a change in the XYItemRenderer
  43. * interface. Added tooltip code to drawItem(...) method (DG);
  44. * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML image maps (RA);
  45. * 25-Mar-2003 : Implemented Serializable (DG);
  46. * 01-May-2003 : Modified drawItem(...) method signature (DG);
  47. * 30-Jul-2003 : Modified entity constructor (CZ);
  48. * 31-Jul-2003 : Deprecated constructor (DG);
  49. * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
  50. * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
  51. * 29-Jan-2004 : Fixed bug (882392) when rendering with PlotOrientation.HORIZONTAL (DG);
  52. * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed XYToolTipGenerator
  53. * --> XYItemLabelGenerator (DG);
  54. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
  55. *
  56. */
  57. package org.jfree.chart.renderer.xy;
  58. import java.awt.Graphics2D;
  59. import java.awt.Paint;
  60. import java.awt.Shape;
  61. import java.awt.Stroke;
  62. import java.awt.geom.Line2D;
  63. import java.awt.geom.Rectangle2D;
  64. import java.io.Serializable;
  65. import org.jfree.chart.axis.ValueAxis;
  66. import org.jfree.chart.entity.EntityCollection;
  67. import org.jfree.chart.entity.XYItemEntity;
  68. import org.jfree.chart.event.RendererChangeEvent;
  69. import org.jfree.chart.labels.XYToolTipGenerator;
  70. import org.jfree.chart.plot.CrosshairState;
  71. import org.jfree.chart.plot.PlotOrientation;
  72. import org.jfree.chart.plot.PlotRenderingInfo;
  73. import org.jfree.chart.plot.XYPlot;
  74. import org.jfree.data.xy.OHLCDataset;
  75. import org.jfree.data.xy.XYDataset;
  76. import org.jfree.ui.RectangleEdge;
  77. import org.jfree.util.PublicCloneable;
  78. /**
  79. * A renderer that draws high/low/open/close markers on an {@link XYPlot} (requires
  80. * a {@link OHLCDataset}).
  81. * <P>
  82. * This renderer does not include code to calculate the crosshair point for the plot.
  83. */
  84. public class HighLowRenderer extends AbstractXYItemRenderer
  85. implements XYItemRenderer,
  86. Cloneable,
  87. PublicCloneable,
  88. Serializable {
  89. /** A flag that controls whether the open ticks are drawn. */
  90. private boolean drawOpenTicks;
  91. /** A flag that controls whether the close ticks are drawn. */
  92. private boolean drawCloseTicks;
  93. /**
  94. * The default constructor.
  95. */
  96. public HighLowRenderer() {
  97. super();
  98. this.drawOpenTicks = true;
  99. this.drawCloseTicks = true;
  100. }
  101. /**
  102. * Returns the flag that controls whether open ticks are drawn.
  103. *
  104. * @return A boolean.
  105. */
  106. public boolean getDrawOpenTicks() {
  107. return this.drawOpenTicks;
  108. }
  109. /**
  110. * Sets the flag that controls whether open ticks are drawn, and sends a
  111. * {@link RendererChangeEvent} to all registered listeners.
  112. *
  113. * @param draw the flag.
  114. */
  115. public void setDrawOpenTicks(boolean draw) {
  116. this.drawOpenTicks = draw;
  117. notifyListeners(new RendererChangeEvent(this));
  118. }
  119. /**
  120. * Returns the flag that controls whether close ticks are drawn.
  121. *
  122. * @return A boolean.
  123. */
  124. public boolean getDrawCloseTicks() {
  125. return this.drawCloseTicks;
  126. }
  127. /**
  128. * Sets the flag that controls whether close ticks are drawn, and sends a
  129. * {@link RendererChangeEvent} to all registered listeners.
  130. *
  131. * @param draw the flag.
  132. */
  133. public void setDrawCloseTicks(boolean draw) {
  134. this.drawCloseTicks = draw;
  135. notifyListeners(new RendererChangeEvent(this));
  136. }
  137. /**
  138. * Draws the visual representation of a single data item.
  139. *
  140. * @param g2 the graphics device.
  141. * @param state the renderer state.
  142. * @param dataArea the area within which the plot is being drawn.
  143. * @param info collects information about the drawing.
  144. * @param plot the plot (can be used to obtain standard color information etc).
  145. * @param domainAxis the domain axis.
  146. * @param rangeAxis the range axis.
  147. * @param dataset the dataset.
  148. * @param series the series index (zero-based).
  149. * @param item the item index (zero-based).
  150. * @param crosshairState crosshair information for the plot (<code>null</code> permitted).
  151. * @param pass the pass index.
  152. */
  153. public void drawItem(Graphics2D g2,
  154. XYItemRendererState state,
  155. Rectangle2D dataArea,
  156. PlotRenderingInfo info,
  157. XYPlot plot,
  158. ValueAxis domainAxis,
  159. ValueAxis rangeAxis,
  160. XYDataset dataset,
  161. int series,
  162. int item,
  163. CrosshairState crosshairState,
  164. int pass) {
  165. // first make sure we have a valid x value...
  166. Number x = dataset.getX(series, item);
  167. if (x == null) {
  168. return; // if x is null, we can't do anything
  169. }
  170. double xdouble = x.doubleValue();
  171. if (!domainAxis.getRange().contains(xdouble)) {
  172. return; // the x value is not within the axis range
  173. }
  174. double xx = domainAxis.valueToJava2D(xdouble, dataArea, plot.getDomainAxisEdge());
  175. // setup for collecting optional entity info...
  176. Shape entityArea = null;
  177. EntityCollection entities = null;
  178. if (info != null) {
  179. entities = info.getOwner().getEntityCollection();
  180. }
  181. PlotOrientation orientation = plot.getOrientation();
  182. RectangleEdge location = plot.getRangeAxisEdge();
  183. Paint p = getItemPaint(series, item);
  184. Stroke s = getItemStroke(series, item);
  185. g2.setPaint(p);
  186. g2.setStroke(s);
  187. if (dataset instanceof OHLCDataset) {
  188. OHLCDataset hld = (OHLCDataset) dataset;
  189. double yHigh = hld.getHighValue(series, item);
  190. double yLow = hld.getLowValue(series, item);
  191. if (!Double.isNaN(yHigh) && !Double.isNaN(yLow)) {
  192. double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, location);
  193. double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, location);
  194. if (orientation == PlotOrientation.HORIZONTAL) {
  195. g2.draw(new Line2D.Double(yyLow, xx, yyHigh, xx));
  196. entityArea = new Rectangle2D.Double(
  197. Math.min(yyLow, yyHigh), xx - 1.0, Math.abs(yyHigh - yyLow), 2.0
  198. );
  199. }
  200. else if (orientation == PlotOrientation.VERTICAL) {
  201. g2.draw(new Line2D.Double(xx, yyLow, xx, yyHigh));
  202. entityArea = new Rectangle2D.Double(
  203. xx - 1.0, Math.min(yyLow, yyHigh), 2.0, Math.abs(yyHigh - yyLow)
  204. );
  205. }
  206. }
  207. double delta = 2.0;
  208. if (domainAxis.isInverted()) {
  209. delta = -delta;
  210. }
  211. if (getDrawOpenTicks()) {
  212. double yOpen = hld.getOpenValue(series, item);
  213. if (!Double.isNaN(yOpen)) {
  214. double yyOpen = rangeAxis.valueToJava2D(
  215. yOpen, dataArea, location
  216. );
  217. if (orientation == PlotOrientation.HORIZONTAL) {
  218. g2.draw(new Line2D.Double(yyOpen, xx + delta, yyOpen, xx));
  219. }
  220. else if (orientation == PlotOrientation.VERTICAL) {
  221. g2.draw(new Line2D.Double(xx - delta, yyOpen, xx, yyOpen));
  222. }
  223. }
  224. }
  225. if (getDrawCloseTicks()) {
  226. double yClose = hld.getCloseValue(series, item);
  227. if (!Double.isNaN(yClose)) {
  228. double yyClose = rangeAxis.valueToJava2D(yClose, dataArea, location);
  229. if (orientation == PlotOrientation.HORIZONTAL) {
  230. g2.draw(new Line2D.Double(yyClose, xx, yyClose, xx - delta));
  231. }
  232. else if (orientation == PlotOrientation.VERTICAL) {
  233. g2.draw(new Line2D.Double(xx, yyClose, xx + delta, yyClose));
  234. }
  235. }
  236. }
  237. }
  238. else {
  239. // not a HighLowDataset, so just draw a line connecting this point with the previous
  240. // point...
  241. if (item > 0) {
  242. Number x0 = dataset.getX(series, item - 1);
  243. Number y0 = dataset.getY(series, item - 1);
  244. Number y = dataset.getY(series, item);
  245. if (x0 == null || y0 == null || y == null) {
  246. return;
  247. }
  248. double xx0 = domainAxis.valueToJava2D(
  249. x0.doubleValue(), dataArea, plot.getDomainAxisEdge()
  250. );
  251. double yy0 = rangeAxis.valueToJava2D(y0.doubleValue(), dataArea, location);
  252. double yy = rangeAxis.valueToJava2D(y.doubleValue(), dataArea, location);
  253. if (orientation == PlotOrientation.HORIZONTAL) {
  254. g2.draw(new Line2D.Double(yy0, xx0, yy, xx));
  255. }
  256. else if (orientation == PlotOrientation.VERTICAL) {
  257. g2.draw(new Line2D.Double(xx0, yy0, xx, yy));
  258. }
  259. }
  260. }
  261. // add an entity for the item...
  262. if (entities != null) {
  263. String tip = null;
  264. XYToolTipGenerator generator = getToolTipGenerator(series, item);
  265. if (generator != null) {
  266. tip = generator.generateToolTip(dataset, series, item);
  267. }
  268. String url = null;
  269. if (getURLGenerator() != null) {
  270. url = getURLGenerator().generateURL(dataset, series, item);
  271. }
  272. XYItemEntity entity = new XYItemEntity(entityArea, dataset, series, item, tip, url);
  273. entities.add(entity);
  274. }
  275. }
  276. /**
  277. * Returns a clone of the renderer.
  278. *
  279. * @return A clone.
  280. *
  281. * @throws CloneNotSupportedException if the renderer cannot be cloned.
  282. */
  283. public Object clone() throws CloneNotSupportedException {
  284. return super.clone();
  285. }
  286. }