Save This Page
Home » jcommon-1.0.13 » org.jfree » chart » renderer » xy » [javadoc | source]
    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 
   10    * under the terms of the GNU Lesser General Public License as published by 
   11    * the Free Software Foundation; either version 2.1 of the License, or 
   12    * (at your option) any later version.
   13    *
   14    * This library is distributed in the hope that it will be useful, but 
   15    * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
   16    * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
   17    * License for more details.
   18    *
   19    * You should have received a copy of the GNU Lesser General Public License 
   20    * along with this library; if not, write to the Free Software Foundation, 
   21    * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
   22    *
   23    * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
   24    * in the United States and other countries.]
   25    *
   26    * ------------
   27    * HighLow.java
   28    * ------------
   29    * (C) Copyright 2000-2004, by Andrzej Porebski and Contributors.
   30    *
   31    * Original Author:  Andrzej Porebski;
   32    * Contributor(s):   David Gilbert (for Object Refinery Limited);
   33    *
   34    * $Id: HighLow.java,v 1.2 2005/03/28 07:14:33 mungady Exp $
   35    *
   36    * Changes (from 18-Sep-2001)
   37    * --------------------------
   38    * 18-Sep-2001 : Added standard header and fixed DOS encoding problem (DG);
   39    * 17-Nov-2001 : Renamed HiLow --> HighLow (DG);
   40    * 06-Mar-2002 : Updated import statements (DG);
   41    * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
   42    *
   43    */
   44   
   45   package org.jfree.chart.renderer.xy;
   46   
   47   import java.awt.BasicStroke;
   48   import java.awt.Color;
   49   import java.awt.Paint;
   50   import java.awt.Stroke;
   51   import java.awt.geom.Line2D;
   52   import java.awt.geom.Rectangle2D;
   53   
   54   /**
   55    * Represents one point in the high/low/open/close plot.
   56    * <P>
   57    * All the coordinates in this class are in Java2D space.
   58    *
   59    * @author Andrzej Porebski
   60    */
   61   public class HighLow {
   62   
   63       /** Useful constant for open/close value types. */
   64       public static final int OPEN = 0;
   65   
   66       /** Useful constant for open/close value types. */
   67       public static final int CLOSE = 1;
   68   
   69       /** The position of the line. */
   70       private Line2D line;
   71   
   72       /** The bounds. */
   73       private Rectangle2D bounds;
   74   
   75       /** The open value. */
   76       private double open;
   77   
   78       /** The close value. */
   79       private double close;
   80   
   81       /** The pen/brush used to draw the lines. */
   82       private Stroke stroke;
   83   
   84       /** The color used to draw the lines. */
   85       private Paint paint;
   86   
   87       /** The tick size. */
   88       private double tickSize = 2;
   89   
   90       /**
   91        * Constructs a high-low item, with default values for the open/close and
   92        * colors.
   93        *
   94        * @param x  the x value.
   95        * @param high  the high value.
   96        * @param low  the low value.
   97        */
   98       public HighLow(double x, double high, double low) {
   99           this(x, high, low, high, low, new BasicStroke(), Color.blue);
  100       }
  101   
  102       /**
  103        * Constructs a high-low item, with default values for the colors.
  104        *
  105        * @param x  the x value.
  106        * @param high  the high value.
  107        * @param low  the low value.
  108        * @param open  the open value.
  109        * @param close  the close value.
  110        */
  111       public HighLow(double x, double high, double low, double open, 
  112                      double close) {
  113           this(x, high, low, open, close, new BasicStroke(), Color.blue);
  114       }
  115   
  116       /**
  117        * Constructs a high-low item.
  118        *
  119        * @param x  the x value.
  120        * @param high  the high value.
  121        * @param low  the low value.
  122        * @param open  the open value.
  123        * @param close  the close value.
  124        * @param stroke  the stroke.
  125        * @param paint  the paint.
  126        */
  127       public HighLow(double x, double high, double low, double open, double close,
  128                      Stroke stroke, Paint paint) {
  129   
  130           this.line = new Line2D.Double(x, high, x, low);
  131           this.bounds =  new Rectangle2D.Double(x - this.tickSize, high,
  132                                                 2 * this.tickSize, low - high);
  133           this.open = open;
  134           this.close = close;
  135           this.stroke = stroke;
  136           this.paint = paint;
  137   
  138       }
  139   
  140       /**
  141        * Sets the width of the open/close tick.
  142        *
  143        * @param newSize  the new tick size.
  144        */
  145       public void setTickSize(double newSize) {
  146           this.tickSize = newSize;
  147       }
  148   
  149       /**
  150        * Returns the width of the open/close tick.
  151        *
  152        * @return The width of the open/close tick.
  153        */
  154       public double getTickSize() {
  155           return this.tickSize;
  156       }
  157   
  158       /**
  159        * Returns the line.
  160        *
  161        * @return The line.
  162        */
  163       public Line2D getLine() {
  164           return this.line;
  165       }
  166   
  167       /**
  168        * Returns the bounds.
  169        *
  170        * @return The bounds.
  171        */
  172       public Rectangle2D getBounds() {
  173           return this.bounds;
  174       }
  175   
  176       /**
  177        * Returns either OPEN or CLOSE value depending on the valueType.
  178        *
  179        * @param valueType  which value <code>{OPEN|CLOSE}</code>.
  180        *
  181        * @return The open value for valueType <code>OPEN</code>, the close value
  182        *      otherwise.
  183        */
  184       public double getValue(int valueType) {
  185           if (valueType == OPEN) {
  186               return this.open;
  187           }
  188           else {
  189               return this.close;
  190           }
  191       }
  192   
  193       /**
  194        * Sets either OPEN or Close value depending on the valueType.
  195        *
  196        * @param type  the value type (OPEN or CLOSE).
  197        * @param value  the new value.
  198        */
  199       public void setValue(int type, double value) {
  200           if (type == OPEN) {
  201               this.open = value;
  202           }
  203           else {
  204               this.close = value;
  205           }
  206       }
  207   
  208       /**
  209        * Returns the line for open tick.
  210        *
  211        * @return The line for open tick.
  212        */
  213       public Line2D getOpenTickLine() {
  214           return getTickLine(
  215               getLine().getX1(), getValue(OPEN), (-1) * getTickSize()
  216           );
  217       }
  218   
  219       /**
  220        * Returns the line for close tick
  221        *
  222        * @return The line for close tick.
  223        */
  224       public Line2D getCloseTickLine() {
  225           return getTickLine(getLine().getX1(), getValue(CLOSE), getTickSize());
  226       }
  227   
  228       /**
  229        * Helper to get the tickLine for the OPEN/CLOSE value.
  230        *
  231        * @param x  the X coordinate of the start point of the tick line.
  232        * @param value  the OPEN or the CLOSE value.
  233        * @param width  the width of the tickLine.
  234        *
  235        * @return A tickLine for the OPEN or the CLOSE value.
  236        */
  237       private Line2D getTickLine(double x, double value, double width) {
  238           return new Line2D.Double(x, value, x + width, value);
  239       }
  240   
  241       /**
  242        * Returns the Stroke object used to draw the line.
  243        *
  244        * @return The Stroke object used to draw the line.
  245        */
  246       public Stroke getStroke() {
  247           return this.stroke;
  248       }
  249   
  250       /**
  251        * Returns the Paint object used to color the line.
  252        *
  253        * @return The Paint object used to color the line.
  254        */
  255       public Paint getPaint() {
  256           return this.paint;
  257       }
  258   
  259   }

Save This Page
Home » jcommon-1.0.13 » org.jfree » chart » renderer » xy » [javadoc | source]