KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > HighLow


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ------------
23  * HighLow.java
24  * ------------
25  * (C) Copyright 2000-2003, by Andrzej Porebski and Contributors.
26  *
27  * Original Author: Andrzej Porebski;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: HighLow.java,v 1.3 2003/06/12 16:54:20 mungady Exp $
31  *
32  * Changes (from 18-Sep-2001)
33  * --------------------------
34  * 18-Sep-2001 : Added standard header and fixed DOS encoding problem (DG);
35  * 17-Nov-2001 : Renamed HiLow --> HighLow (DG);
36  * 06-Mar-2002 : Updated import statements (DG);
37  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
38  *
39  */

40
41 package org.jfree.chart.renderer;
42
43 import java.awt.BasicStroke JavaDoc;
44 import java.awt.Color JavaDoc;
45 import java.awt.Paint JavaDoc;
46 import java.awt.Stroke JavaDoc;
47 import java.awt.geom.Line2D JavaDoc;
48 import java.awt.geom.Rectangle2D JavaDoc;
49
50 /**
51  * Represents one point in the high/low/open/close plot.
52  * <P>
53  * All the coordinates in this class are in Java2D space.
54  *
55  * @author Andrzej Porebski
56  */

57 public class HighLow {
58
59     /** Useful constant for open/close value types. */
60     public static final int OPEN = 0;
61
62     /** Useful constant for open/close value types. */
63     public static final int CLOSE = 1;
64
65     /** The position of the line. */
66     private Line2D JavaDoc line;
67
68     /** The bounds. */
69     private Rectangle2D JavaDoc bounds;
70
71     /** The open value. */
72     private double open;
73
74     /** The close value. */
75     private double close;
76
77     /** The pen/brush used to draw the lines. */
78     private Stroke JavaDoc stroke;
79
80     /** The color used to draw the lines. */
81     private Paint JavaDoc paint;
82
83     /** The tick size. */
84     private double tickSize = 2;
85
86     /**
87      * Constructs a high-low item, with default values for the open/close and
88      * colors.
89      *
90      * @param x the x value.
91      * @param high the high value.
92      * @param low the low value.
93      */

94     public HighLow(double x, double high, double low) {
95         this(x, high, low, high, low, new BasicStroke JavaDoc(), Color.blue);
96     }
97
98     /**
99      * Constructs a high-low item, with default values for the colors.
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      */

107     public HighLow(double x, double high, double low, double open, double close) {
108         this(x, high, low, open, close, new BasicStroke JavaDoc(), Color.blue);
109     }
110
111     /**
112      * Constructs a high-low item.
113      *
114      * @param x the x value.
115      * @param high the high value.
116      * @param low the low value.
117      * @param open the open value.
118      * @param close the close value.
119      * @param stroke the stroke.
120      * @param paint the paint.
121      */

122     public HighLow(double x, double high, double low, double open, double close,
123                    Stroke JavaDoc stroke, Paint JavaDoc paint) {
124
125         this.line = new Line2D.Double JavaDoc(x, high, x, low);
126         this.bounds = new Rectangle2D.Double JavaDoc(x - this.tickSize, high,
127                                               2 * this.tickSize, low - high);
128         this.open = open;
129         this.close = close;
130         this.stroke = stroke;
131         this.paint = paint;
132
133     }
134
135     /**
136      * Sets the width of the open/close tick.
137      *
138      * @param newSize the new tick size.
139      */

140     public void setTickSize(double newSize) {
141         tickSize = newSize;
142     }
143
144     /**
145      * Returns the width of the open/close tick.
146      *
147      * @return the width of the open/close tick.
148      */

149     public double getTickSize() {
150         return tickSize;
151     }
152
153     /**
154      * Returns the line.
155      *
156      * @return the line.
157      */

158     public Line2D JavaDoc getLine() {
159         return line;
160     }
161
162     /**
163      * Returns the bounds.
164      *
165      * @return the bounds.
166      */

167     public Rectangle2D JavaDoc getBounds() {
168         return this.bounds;
169     }
170
171     /**
172      * Returns either OPEN or CLOSE value depending on the valueType.
173      *
174      * @param valueType which value <code>{OPEN|CLOSE}</code>.
175      *
176      * @return the open value for valueType <code>OPEN</code>, the close value
177      * otherwise.
178      */

179     public double getValue(int valueType) {
180         if (valueType == OPEN) {
181             return open;
182         }
183         else {
184             return close;
185         }
186     }
187
188     /**
189      * Sets either OPEN or Close value depending on the valueType.
190      *
191      * @param type the value type (OPEN or CLOSE).
192      * @param value the new value.
193      */

194     public void setValue(int type, double value) {
195         if (type == OPEN) {
196             open = value;
197         }
198         else {
199             close = value;
200         }
201     }
202
203     /**
204      * Returns the line for open tick.
205      *
206      * @return the line for open tick.
207      */

208     public Line2D JavaDoc getOpenTickLine() {
209         return getTickLine(getLine().getX1(), getValue(OPEN), (-1) * getTickSize());
210     }
211
212     /**
213      * Returns the line for close tick
214      *
215      * @return the line for close tick.
216      */

217     public Line2D JavaDoc getCloseTickLine() {
218         return getTickLine(getLine().getX1(), getValue(CLOSE), getTickSize());
219     }
220
221     /**
222      * Helper to get the tickLine for the OPEN/CLOSE value.
223      *
224      * @param x the X coordinate of the start point of the tick line.
225      * @param value the OPEN or the CLOSE value.
226      * @param width the width of the tickLine.
227      *
228      * @return a tickLine for the OPEN or the CLOSE value.
229      */

230     private Line2D JavaDoc getTickLine(double x, double value, double width) {
231         return new Line2D.Double JavaDoc(x, value, x + width, value);
232     }
233
234     /**
235      * Returns the Stroke object used to draw the line.
236      *
237      * @return the Stroke object used to draw the line.
238      */

239     public Stroke JavaDoc getStroke() {
240         return stroke;
241     }
242
243     /**
244      * Returns the Paint object used to color the line.
245      *
246      * @return the Paint object used to color the line.
247      */

248     public Paint JavaDoc getPaint() {
249         return paint;
250     }
251
252 }
253
Popular Tags