TeeChartPHP
[ class tree: TeeChartPHP ] [ index: TeeChartPHP ] [ all elements ]

Source for file Candle.php

Documentation is available at Candle.php

  1. <?php
  2.  
  3. /**
  4.  * Candle class
  5.  *
  6.  * Description: Candle Series
  7.  *
  8.  * Example:
  9.  * $candleSeries = Candle($myChart->getChart());
  10.  * $candleSeries->fillSampleValues(30);
  11.  *
  12.  * @author
  13.  * @copyright (c) 1995-2008 by Steema Software SL. All Rights Reserved. <info@steema.com>
  14.  * @version 1.0
  15.  * @package TeeChartPHP
  16.  * @subpackage styles
  17.  * @link http://www.steema.com
  18.  */
  19.  
  20.  class Candle extends OHLC {
  21.  
  22.     private static $DEFAULTCANDLEWIDTH 4;
  23.     private $upCloseColor;
  24.     private $downCloseColor;
  25.     private $candleWidth;
  26.     private $style;
  27.     private $showOpenTick true;
  28.     private $showCloseTick true;
  29.     private $oldP;
  30.  
  31.     // Interceptors
  32.     function __get$property {
  33.       $method ="get{$property}";
  34.       if method_exists$this$method ) ) {
  35.         return $this->$method();
  36.       }
  37.     }
  38.  
  39.     function __set $property,$value {
  40.       $method ="set{$property}";
  41.       if method_exists$this$method ) ) {
  42.         return $this->$method($value);
  43.       }
  44.     }
  45.  
  46.     public function Candle($c=null{
  47.  
  48.         $this->upCloseColor Color::WHITE();
  49.         $this->downCloseColor Color::RED();
  50.         $this->candleWidth self::$DEFAULTCANDLEWIDTH;
  51.         $this->style CandleStyle::$CANDLESTICK;
  52.  
  53.         parent::OHLC($c);
  54.  
  55.         $this->getPointer()->setDraw3D(false);
  56.     }
  57.  
  58.     /**
  59.     * Determines how the Candle points will be drawn.
  60.     * When it is CandleStick, a vertical rectangle represents each candle
  61.     * point.
  62.     * When it is CandleBar, a vertical line is drawn, among Open and Close
  63.     * horizontal tick marks.
  64.     * You can control both the candle colors and width.
  65.     * Default value: CandleStick
  66.     *
  67.     * @return CandleStyle 
  68.     */
  69.     public function getStyle({
  70.         return $this->style;
  71.     }
  72.  
  73.     /**
  74.     * Determines how the Candle points will be drawn.
  75.     * Default value: CandleStick
  76.     *
  77.     * Example:
  78.     * $candleSeries = new Candle($myChart->getChart());
  79.     * $candleSeries->fillSampleValues(30);
  80.     *
  81.     * $candleSeries->setStyle(CandleStyles::$OpenClose);
  82.     *
  83.     * @param CandleStyles $value 
  84.     */
  85.     public function setStyle($value{
  86.         if ($this->style != $value{
  87.             $this->style $value;
  88.             $this->invalidate();
  89.         }
  90.     }
  91.  
  92.     public function createSubGallery($addSubChart{
  93.         parent::createSubGallery($addSubChart);
  94.         $addSubChart->createSubChart(Language::getString("CandleBar"));
  95.         $addSubChart->createSubChart(Language::getString("CandleNoOpen"));
  96.         $addSubChart->createSubChart(Language::getString("CandleNoClose"));
  97.         $addSubChart->createSubChart(Language::getString("NoBorder"));
  98.         $addSubChart->createSubChart(Language::getString("Line"));
  99.     }
  100.  
  101.     public function setSubGallery($index{
  102.         switch ($index{
  103.         case 1:
  104.             $this->setStyle(CandleStyle::$CANDLEBAR);
  105.             break;
  106.         case 2{
  107.             $this->getPen()->setVisible(true);
  108.             $this->setStyle(CandleStyle::$CANDLEBAR);
  109.             $this->showOpenTick false;
  110.         }
  111.         break;
  112.         case 3{
  113.             $this->getPen()->setVisible(true);
  114.             $this->setStyle(CandleStyle::$CANDLEBAR);
  115.             $this->showCloseTick false;
  116.         }
  117.         break;
  118.         case 4{
  119.             $this->setStyle(CandleStyle::$CANDLESTICK);
  120.             $this->getPen()->setVisible(false);
  121.         }
  122.         break;
  123.         case 5{
  124.             $this->setStyle(CandleStyle::$LINE);
  125.         }
  126.         break;
  127.         default:
  128.             parent::setSubGallery($index);
  129.         }
  130.     }
  131.  
  132.     /**
  133.     * The horizontal Candle Size in pixels.
  134.     * It is based on pixels for Screen charts. When printing, this number is
  135.     * multiplied by the ratio between the Printer width and the Screen width.
  136.     *
  137.     * Default value: 6
  138.     *
  139.     * @return int 
  140.     */
  141.     public function getCandleWidth({
  142.         return $this->candleWidth;
  143.     }
  144.  
  145.     /**
  146.     * Sets the horizontal Candle Size in pixels.<br>
  147.     * Default value: 6
  148.     *
  149.     * @param int $value 
  150.     */
  151.     public function setCandleWidth($value{
  152.         $this->candleWidth $this->setIntegerProperty($this->candleWidth$value);
  153.     }
  154.  
  155.     /**
  156.     * Candle color fill when Open value is greater than Close value.<br>
  157.     * By default, UpCloseColor is WHITE and DownCloseColor is RED. <br>
  158.     * Each Candle will be filled with a different color depending on its Open
  159.     * and Close values.<br>
  160.     * If Open value is greater than Close value, then the selected color will
  161.     * be the UpCloseColor color.<br>
  162.     * If Close value is greater or equal than Open value, then the selected
  163.     * color will be the DownCloseColor color. <br>
  164.     * Default value: white
  165.     *
  166.     * @return Color 
  167.     */
  168.     public function getUpCloseColor({
  169.         return $this->upCloseColor;
  170.     }
  171.  
  172.         /**
  173.           * Candle color fill when Open value is greater than Close value.<br>
  174.           * Default value: white
  175.           *
  176.           * @param value Color
  177.           */
  178.     public function setUpCloseColor($value{
  179.         $this->upCloseColor $this->setColorProperty($this->upCloseColor$value);
  180.     }
  181.  
  182.         /**
  183.           * Candle color fill when Close value is greater than Open value.<br>
  184.           * By default, UpCloseColor is WHITE and DownCloseColor is RED. <br>
  185.           * Each Candle will be filled with a different color depending on its Open
  186.           * and Close values.<br>
  187.           * If Open value is greater than Close value, then the selected color will
  188.           * be the UpCloseColor color.<br>
  189.           * If Close value is greater or equal than Open value, then the selected
  190.           * color will be the DownCloseColor color. <br>
  191.           * Default value: red
  192.           *
  193.           *
  194.           *
  195.           * @return Color 
  196.           */
  197.     public function getDownCloseColor({
  198.         return $this->downCloseColor;
  199.     }
  200.  
  201.         /**
  202.           * Candle color fill when Close value is greater than Open value.<br>
  203.           * Default value: red
  204.           *
  205.           * @param value Color
  206.           */
  207.     public function setDownCloseColor($value{
  208.         $this->downCloseColor $this->setColorProperty($this->downCloseColor$value);
  209.     }
  210.  
  211.         /**
  212.           * Determines whether Open prices will be displayed.<br>
  213.           * It only has effect when Candle series.CandleStyle is set to csCandleBar.
  214.           * CandleWidth determines the length in pixels of Open and Close ticks. <br>
  215.           * Default value: true
  216.           *
  217.           * @return boolean 
  218.           */
  219.     public function getShowOpen({
  220.         return $this->showOpenTick;
  221.     }
  222.  
  223.         /**
  224.           * Determines whether Open prices will be displayed.<br>
  225.           * Default value: true
  226.           *
  227.           * @param value boolean
  228.           */
  229.     public function setShowOpen($value{
  230.         $this->showOpenTick $this->setBooleanProperty($this->showOpenTick$value);
  231.     }
  232.  
  233.         /**
  234.           * Determines whether Close prices will be displayed.<br>
  235.           * It only has effect when Candle series.CandleStyle is set to csCandleBar.
  236.           * CandleWidth determines the length in pixels of Open and Close ticks. <br>
  237.           * Default value: true
  238.           *
  239.           * @return boolean 
  240.           */
  241.     public function getShowClose({
  242.         return $this->showCloseTick;
  243.     }
  244.  
  245.         /**
  246.           * Determines whether Close prices will be displayed.<br>
  247.           * Default value: true
  248.           *
  249.           * @param value boolean
  250.           */
  251.     public function setShowClose($value{
  252.         $this->showCloseTick $this->setBooleanProperty($this->showCloseTick$value);
  253.     }
  254.  
  255.     private function calculateColor($valueIndex{
  256.         $result $this->getValueColor($valueIndex);
  257.  
  258.         if ($result == $this->getColor()) {
  259.             if ($this->vOpenValues->value[$valueIndex$this->getCloseValues()->value[$valueIndex])
  260.             /* 5.01 */
  261.             {
  262.                 $result $this->downCloseColor;
  263.             else
  264.             if ($this->vOpenValues->value[$valueIndex$this->getCloseValues()->value[$valueIndex]{
  265.                 $result $this->upCloseColor;
  266.             else {
  267.                 // color algorithm when open is equal to close
  268.                 if ($valueIndex == 0{
  269.                     $result $this->upCloseColor// <-- first point
  270.                 else
  271.                 if ($this->getCloseValues()->value[$valueIndex 1>
  272.                     $this->getCloseValues()->value[$valueIndex]{
  273.                     $result $this->downCloseColor;
  274.                 else
  275.                 if ($this->getCloseValues()->value[$valueIndex 1<
  276.                     $this->getCloseValues()->value[$valueIndex]{
  277.                     $result $this->upCloseColor;
  278.                 else {
  279.                     $result $this->getValueColor($valueIndex 1);
  280.                 }
  281.             }
  282.         }
  283.  
  284.         return $result;
  285.     }
  286.  
  287.         /**
  288.           * Called internally. Draws the "ValueIndex" point of the Series.
  289.           *
  290.           * @param valueIndex int
  291.           */
  292.     public function drawValue($valueIndex{
  293.  
  294.         $tmpStyle PointerStyle::$RECTANGLE;
  295.  
  296.         // TODO $this->onGetPointerStyle($valueIndex, $tmpStyle);
  297.  
  298.         $g $this->chart->getGraphics3D();
  299.  
  300.         /* Pointer Pen && Brush styles */
  301.         $this->point->prepareCanvas($gnew Color(0,0,0));
  302.  
  303.         /* The horizontal position */
  304.         $x $this->calcXPosValue($this->getDateValues()->value[$valueIndex]);
  305.  
  306.         // Vertical positions of Open, High, Low & Close values for this point
  307.         $yOpen $this->calcYPosValue($this->vOpenValues->value[$valueIndex]);
  308.         $yHigh $this->calcYPosValue($this->vHighValues->value[$valueIndex]);
  309.         $yLow $this->calcYPosValue($this->vLowValues->value[$valueIndex]);
  310.         $yClose $this->calcYPosValue($this->getCloseValues()->value[$valueIndex]);
  311.  
  312.         $tmpLeftWidth $this->candleWidth 2/* Width */
  313.         $tmpRightWidth $this->candleWidth $tmpLeftWidth;
  314.  
  315.         if (($this->style == CandleStyle::$CANDLESTICK||
  316.             ($this->style == CandleStyle::$OPENCLOSE)) {
  317.  
  318.             // draw Candle Stick
  319.             if ($this->chart->getAspect()->getView3D(&& $this->point->getDraw3D()) {
  320.                 (int)$this->tmpTop=0;
  321.                 (int)$this->tmpBottom=0;
  322.  
  323.                 if ($yClose $yOpen{
  324.                     $this->tmpTop $yOpen;
  325.                     $this->tmpBottom $yClose;
  326.                 else {
  327.                     $this->tmpTop $yClose;
  328.                     $this->tmpBottom $yOpen;
  329.                 }
  330.  
  331.                 // Draw Candle Vertical Line from bottom to Low
  332.                 if ($this->style == CandleStyle::$CANDLESTICK{
  333.                     $g->verticalLine($x$this->tmpBottom$yLow$this->getMiddleZ());
  334.                 }
  335.  
  336.                 // Draw 3D Candle
  337.                 $g->getBrush()->setColor$this->calculateColor($valueIndex));
  338.                 if ($yOpen == $yClose{
  339.                     $g->getPen()->setColor$this->calculateColor($valueIndex));
  340.                 }
  341.  
  342.                 if ($this->getTransparency(0{
  343.                     $g->getBrush()->setTransparency$this->getTransparency());
  344.                 }
  345.  
  346.                 $g->cube($x $tmpLeftWidth$this->tmpTop$x $tmpRightWidth,
  347.                        $this->tmpBottom$this->getStartZ()$this->getEndZ()$this->point->getDark3D());
  348.  
  349.                 // Draw Candle Vertical Line from Top to High
  350.                 if ($this->style == CandleStyle::$CANDLESTICK{
  351.                     $g->verticalLine($x$this->tmpTop$yHigh$this->getMiddleZ());
  352.                 }
  353.             else {
  354.                 // Draw Candle Vertical Line from High to Low
  355.                 if ($this->style == CandleStyle::$CANDLESTICK{
  356.                     if ($this->chart->getAspect()->getView3D()) {
  357.                         $g->verticalLine($x$yLow$yHigh$this->getMiddleZ());
  358.                     else {
  359.                         $g->verticalLine($x$yLow$yHigh);
  360.                     }
  361.                 }
  362.  
  363.                 // remember that Y coordinates are inverted
  364.  
  365.                 // prevent zero height rectangles 5.02
  366.                 // in previous releases, an horizontal line was displayed instead
  367.                 // of the small candle rectangle
  368.                 if ($yOpen == $yClose{
  369.                     $yClose--;
  370.                 }
  371.  
  372.                 // draw the candle
  373.                 $g->getBrush()->setColor$this->calculateColor($valueIndex));
  374.                 if ($this->getTransparency(0{
  375.                     $g->getBrush()->setTransparency($this->getTransparency());
  376.                 }
  377.  
  378.                 if ($this->chart->getAspect()->getView3D()) {
  379.                     $g->rectangleWithZ(new Rectangle($x $tmpLeftWidth,
  380.                                               $yOpen,
  381.                                               $tmpLeftWidth $tmpRightWidth,
  382.                                               $yClose $yOpen),
  383.                                               $this->getMiddleZ());
  384.                 else {
  385.                     if (!$this->point->getPen()->getVisible()) {
  386.                         if ($yOpen $yClose{
  387.                             $yOpen--;
  388.                         else {
  389.                             $yClose--;
  390.                         }
  391.                     }
  392.                     $g->rectangle(new Rectangle($x $tmpLeftWidth$yOpen,
  393.                                 $tmpLeftWidth $tmpRightWidth 1$yClose $yOpen));
  394.                 }
  395.             }
  396.         else
  397.         if ($this->style == CandleStyle::$CANDLEBAR{
  398.             // draw Candle bar
  399.             $g->setPen($this->point->getPen());
  400.             $g->getPen()->setColor($this->calculateColor($valueIndex));
  401.  
  402.             // Draw Candle Vertical Line from High to Low
  403.             if ($this->chart->getAspect()->getView3D()) {
  404.                 $g->verticalLine($x$yLow$yHigh$this->getMiddleZ());
  405.                 if ($this->showOpenTick{
  406.                     $g->horizontalLine($x$x $tmpLeftWidth 1$yOpen,
  407.                                      $this->getMiddleZ());
  408.                 }
  409.                 if ($this->showCloseTick{
  410.                     $g->horizontalLine($x$x $tmpRightWidth 1,
  411.                                      $yClose$this->getMiddleZ());
  412.                 }
  413.             else {
  414.                 // 5.02
  415.                 $g->verticalLine($x$yLow$yHigh);
  416.                 if ($this->showOpenTick{
  417.                     $g->horizontalLine($x$x $tmpLeftWidth 1$yOpen);
  418.                 }
  419.                 if ($this->showCloseTick{
  420.                     $g->horizontalLine($x$x $tmpRightWidth 1,
  421.                                      $yClose);
  422.                 }
  423.             }
  424.         else {
  425.             // Line
  426.             $p new Point($x$yClose);
  427.  
  428.             $tmpFirst $this->drawValuesForward($this->firstVisible :
  429.                            $this->lastVisible;
  430.  
  431.             if (($valueIndex != $tmpFirst&& (!$this->isNull($valueIndex))) {
  432.                 $g->setPen($this->getPen());
  433.                 $g->getPen()->setColor$this->calculateColor($valueIndex));
  434.                 if ($this->chart->getAspect()->getView3D()) {
  435.                     $g->line($this->oldP$p$this->getMiddleZ());
  436.                 else {
  437.                     $g->line($this->oldP$p);
  438.                 }
  439.             }
  440.  
  441.             $this->oldP $p;
  442.         }
  443.     }
  444.  
  445.     public function getPen({
  446.         return $this->getPointer()->getPen();
  447.     }
  448.  
  449.     public function prepareForGallery($isEnabled{
  450.         parent::prepareForGallery($isEnabled);
  451.         $this->fillSampleValues(4);
  452.         $this->setColorEach(true);
  453.  
  454.         if ($isEnabled{
  455.             $this->upCloseColor Color::getBlue();
  456.         else {
  457.             $this->upCloseColor Color::getSilver();
  458.             $this->downCloseColor Color::getSilver();
  459.             $this->point->getPen()->setColor(Color::getGray());
  460.         }
  461.         $this->point->getPen()->setWidth(2);
  462.         $this->candleWidth 12;
  463.     }
  464.  
  465.     /**
  466.     * Gets descriptive text.
  467.     *
  468.     * @return String 
  469.     */
  470.     public function getDescription({
  471.         return Language::getString("GalleryCandle");
  472.     }
  473.  
  474.     /*
  475.     * Returns the ValueIndex of the x,y located point in the Series.
  476.     */
  477.     public function clicked($xint)
  478.     {
  479.         return clicked(new TeePoint(x,y));
  480.     }
  481.  
  482.     /*
  483.     * Returns the ValueIndex of the "clicked" point in the Series.
  484.     */
  485.     /* TODO
  486.     public function clicked($p)
  487.         {
  488.                         int result=-1;
  489.  
  490.                         if (firstVisible>-1 && lastVisible>-1)
  491.                         {
  492.  
  493.                                         if (chart!=null) chart.getGraphics3D().calculate2DPosition(p.x, p.y, startZ);
  494.  
  495.                                         //Point p = new Point(x,y);
  496.  
  497.                                         for (int t=firstVisible; t<=lastVisible; t++)
  498.                                                         if (clickedCandle(t,p))
  499.                                                         {
  500.                                                                         result=t;
  501.                                                                         break;
  502.                                                         }
  503.                         }
  504.                         return result;
  505.         }
  506.                */
  507.         /*
  508.         * Returns true if point p is inside the bounds of the ValueIndex candle
  509.         */
  510.         /* TODO
  511.     public function clickedCandle($valueIndex, $p)
  512.         {
  513.                         boolean result = false;
  514.  
  515.                         int tmpX =calcXPosValue(getDateValues().value[valueIndex]); /* The horizontal position
  516.                         int yOpen =calcYPosValue(vOpenValues.value[valueIndex]);
  517.                         int yHigh =calcYPosValue(vHighValues.value[valueIndex]);
  518.                         int yLow  =calcYPosValue(vLowValues.value[valueIndex]);
  519.                         int yClose=calcYPosValue(getCloseValues().value[valueIndex]);
  520.  
  521.                         int tmpLeftWidth=candleWidth / 2; /* calc half Candle Width
  522.                         int tmpRightWidth=candleWidth-tmpLeftWidth;
  523.  
  524.                         int tmpTop;
  525.                         int tmpBottom;
  526.                         int tmpFirst;
  527.                         Point tmpTo=new Point();
  528.  
  529.                         if (style==CandleStyle::$CANDLESTICK || style==CandleStyle::$OPENCLOSE)
  530.                         {
  531.                                         if (chart.getAspect().getView3D() && getPointer().getDraw3D())
  532.                                         {
  533.                                                         tmpTop = yClose;
  534.                                                         tmpBottom = yOpen;
  535.  
  536.                                                         if (tmpTop>tmpBottom) Utils.swapInteger(tmpTop,tmpBottom);
  537.  
  538.                                                         if (style==CandleStyle::$CANDLESTICK &&
  539.                                                                         (Graphics3D.pointInLineTolerance(p,tmpX,tmpBottom,tmpX,yLow,3) ||
  540.                                                                           Graphics3D.pointInLineTolerance(p,tmpX,tmpTop,tmpX,yHigh,3)))
  541.                                                                         return true;
  542.  
  543.                                                         Rectangle tmpR=Rectangle::fromLTRB(tmpX-tmpLeftWidth,tmpTop,tmpX+tmpRightWidth,tmpBottom);
  544.                                                         if (tmpR.contains(p.getX(), p.getY()))
  545.                                                                         return true;
  546.                                         }
  547.                                         else
  548.                                         {
  549.                                                         if (style==CandleStyle::$CANDLESTICK &&
  550.                                                                         Graphics3D.pointInLineTolerance(p,tmpX,yLow,tmpX,yHigh,3))
  551.                                                                         return true;
  552.  
  553.                                                         if (yOpen==yClose) yClose--;
  554.  
  555.                                                         if (chart.getAspect().getView3D())
  556.                                                         {
  557.                                                                 Rectangle tmpR=Rectangle::fromLTRB(tmpX-tmpLeftWidth,yOpen,tmpX+tmpRightWidth,yClose);
  558.                                                                 if (tmpR.contains(p.getX(), p.getY()))
  559.                                                                         return true;
  560.                                                         }
  561.                                                         else
  562.                                                         {
  563.                                                                         if (!getPen().getVisible())
  564.                                                                                         if (yOpen<yClose)
  565.                                                                                                         yOpen--;
  566.                                                                                         else
  567.                                                                                                         yClose--;
  568.  
  569.                                                                         Rectangle tmpR=Rectangle::fromLTRB(tmpX-tmpLeftWidth,yOpen,tmpX+tmpRightWidth+1,yClose);
  570.                                                                         if (tmpR.contains(p.getX(), p.getY()))
  571.                                                                                         return true;
  572.                                                         }
  573.                                         }
  574.                         }
  575.                         else if (style==CandleStyle::$LINE)
  576.                         {
  577.                                         tmpFirst = firstVisible;
  578.                                         tmpTo.x=tmpX;
  579.                                         tmpTo.y=yClose;
  580.                                         if (valueIndex!=tmpFirst)
  581.                                             result = Graphics3D.pointInLineTolerance(p,oldP.x,oldP.y,tmpTo.x,tmpTo.y,3);
  582.                                         oldP = tmpTo;
  583.                         }
  584.                         else
  585.                         {
  586.                                         if (Graphics3D.pointInLineTolerance(p,tmpX,yLow,tmpX,yHigh,3) ||
  587.                                                         (showOpenTick && Graphics3D.pointInLineTolerance(p,tmpX,yOpen,tmpX-tmpLeftWidth-1,yOpen,3)) ||
  588.                                                         (showCloseTick && Graphics3D.pointInLineTolerance(p,tmpX,yClose,tmpX+tmpRightWidth+1,yClose,3)))
  589.                                                         result=true;
  590.                         }
  591.                         return result;
  592.         }
  593.         */
  594. }
  595.  
  596. ?>

Documentation generated on Wed, 16 Jun 2010 12:03:42 +0200 by phpDocumentor 1.4.1