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. * HighLow.java
  26. * ------------
  27. * (C) Copyright 2000-2004, by Andrzej Porebski and Contributors.
  28. *
  29. * Original Author: Andrzej Porebski;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. *
  32. * $Id: HighLow.java,v 1.1 2004/08/31 14:49:29 mungady Exp $
  33. *
  34. * Changes (from 18-Sep-2001)
  35. * --------------------------
  36. * 18-Sep-2001 : Added standard header and fixed DOS encoding problem (DG);
  37. * 17-Nov-2001 : Renamed HiLow --> HighLow (DG);
  38. * 06-Mar-2002 : Updated import statements (DG);
  39. * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  40. *
  41. */
  42. package org.jfree.chart.renderer.xy;
  43. import java.awt.BasicStroke;
  44. import java.awt.Color;
  45. import java.awt.Paint;
  46. import java.awt.Stroke;
  47. import java.awt.geom.Line2D;
  48. import java.awt.geom.Rectangle2D;
  49. /**
  50. * Represents one point in the high/low/open/close plot.
  51. * <P>
  52. * All the coordinates in this class are in Java2D space.
  53. *
  54. * @author Andrzej Porebski
  55. */
  56. public class HighLow {
  57. /** Useful constant for open/close value types. */
  58. public static final int OPEN = 0;
  59. /** Useful constant for open/close value types. */
  60. public static final int CLOSE = 1;
  61. /** The position of the line. */
  62. private Line2D line;
  63. /** The bounds. */
  64. private Rectangle2D bounds;
  65. /** The open value. */
  66. private double open;
  67. /** The close value. */
  68. private double close;
  69. /** The pen/brush used to draw the lines. */
  70. private Stroke stroke;
  71. /** The color used to draw the lines. */
  72. private Paint paint;
  73. /** The tick size. */
  74. private double tickSize = 2;
  75. /**
  76. * Constructs a high-low item, with default values for the open/close and
  77. * colors.
  78. *
  79. * @param x the x value.
  80. * @param high the high value.
  81. * @param low the low value.
  82. */
  83. public HighLow(double x, double high, double low) {
  84. this(x, high, low, high, low, new BasicStroke(), Color.blue);
  85. }
  86. /**
  87. * Constructs a high-low item, with default values for the colors.
  88. *
  89. * @param x the x value.
  90. * @param high the high value.
  91. * @param low the low value.
  92. * @param open the open value.
  93. * @param close the close value.
  94. */
  95. public HighLow(double x, double high, double low, double open, double close) {
  96. this(x, high, low, open, close, new BasicStroke(), Color.blue);
  97. }
  98. /**
  99. * Constructs a high-low item.
  100. *
  101. * @param x the x value.
  102. * @param high the high value.
  103. * @param low the low value.
  104. * @param open the open value.
  105. * @param close the close value.
  106. * @param stroke the stroke.
  107. * @param paint the paint.
  108. */
  109. public HighLow(double x, double high, double low, double open, double close,
  110. Stroke stroke, Paint paint) {
  111. this.line = new Line2D.Double(x, high, x, low);
  112. this.bounds = new Rectangle2D.Double(x - this.tickSize, high,
  113. 2 * this.tickSize, low - high);
  114. this.open = open;
  115. this.close = close;
  116. this.stroke = stroke;
  117. this.paint = paint;
  118. }
  119. /**
  120. * Sets the width of the open/close tick.
  121. *
  122. * @param newSize the new tick size.
  123. */
  124. public void setTickSize(double newSize) {
  125. this.tickSize = newSize;
  126. }
  127. /**
  128. * Returns the width of the open/close tick.
  129. *
  130. * @return the width of the open/close tick.
  131. */
  132. public double getTickSize() {
  133. return this.tickSize;
  134. }
  135. /**
  136. * Returns the line.
  137. *
  138. * @return the line.
  139. */
  140. public Line2D getLine() {
  141. return this.line;
  142. }
  143. /**
  144. * Returns the bounds.
  145. *
  146. * @return the bounds.
  147. */
  148. public Rectangle2D getBounds() {
  149. return this.bounds;
  150. }
  151. /**
  152. * Returns either OPEN or CLOSE value depending on the valueType.
  153. *
  154. * @param valueType which value <code>{OPEN|CLOSE}</code>.
  155. *
  156. * @return the open value for valueType <code>OPEN</code>, the close value
  157. * otherwise.
  158. */
  159. public double getValue(int valueType) {
  160. if (valueType == OPEN) {
  161. return this.open;
  162. }
  163. else {
  164. return this.close;
  165. }
  166. }
  167. /**
  168. * Sets either OPEN or Close value depending on the valueType.
  169. *
  170. * @param type the value type (OPEN or CLOSE).
  171. * @param value the new value.
  172. */
  173. public void setValue(int type, double value) {
  174. if (type == OPEN) {
  175. this.open = value;
  176. }
  177. else {
  178. this.close = value;
  179. }
  180. }
  181. /**
  182. * Returns the line for open tick.
  183. *
  184. * @return the line for open tick.
  185. */
  186. public Line2D getOpenTickLine() {
  187. return getTickLine(getLine().getX1(), getValue(OPEN), (-1) * getTickSize());
  188. }
  189. /**
  190. * Returns the line for close tick
  191. *
  192. * @return the line for close tick.
  193. */
  194. public Line2D getCloseTickLine() {
  195. return getTickLine(getLine().getX1(), getValue(CLOSE), getTickSize());
  196. }
  197. /**
  198. * Helper to get the tickLine for the OPEN/CLOSE value.
  199. *
  200. * @param x the X coordinate of the start point of the tick line.
  201. * @param value the OPEN or the CLOSE value.
  202. * @param width the width of the tickLine.
  203. *
  204. * @return a tickLine for the OPEN or the CLOSE value.
  205. */
  206. private Line2D getTickLine(double x, double value, double width) {
  207. return new Line2D.Double(x, value, x + width, value);
  208. }
  209. /**
  210. * Returns the Stroke object used to draw the line.
  211. *
  212. * @return the Stroke object used to draw the line.
  213. */
  214. public Stroke getStroke() {
  215. return this.stroke;
  216. }
  217. /**
  218. * Returns the Paint object used to color the line.
  219. *
  220. * @return the Paint object used to color the line.
  221. */
  222. public Paint getPaint() {
  223. return this.paint;
  224. }
  225. }