Source for file Candle.php
Documentation is available at Candle.php
* Description: Candle Series
* $candleSeries = Candle($myChart->getChart());
* $candleSeries->fillSampleValues(30);
* @copyright (c) 1995-2008 by Steema Software SL. All Rights Reserved. <info@steema.com>
* @link http://www.steema.com
private static $DEFAULTCANDLEWIDTH = 4;
private $showOpenTick = true;
private $showCloseTick = true;
function __get( $property ) {
$method = "get{$property}";
function __set ( $property,$value ) {
$method = "set{$property}";
return $this->$method($value);
public function Candle($c= null) {
$this->candleWidth = self::$DEFAULTCANDLEWIDTH;
$this->style = CandleStyle::$CANDLESTICK;
* Determines how the Candle points will be drawn.
* When it is CandleStick, a vertical rectangle represents each candle
* When it is CandleBar, a vertical line is drawn, among Open and Close
* You can control both the candle colors and width.
* Default value: CandleStick
* Determines how the Candle points will be drawn.
* Default value: CandleStick
* $candleSeries = new Candle($myChart->getChart());
* $candleSeries->fillSampleValues(30);
* $candleSeries->setStyle(CandleStyles::$OpenClose);
* @param CandleStyles $value
if ($this->style != $value) {
parent::createSubGallery($addSubChart);
$this->getPen()->setVisible(true);
$this->showOpenTick = false;
$this->getPen()->setVisible(true);
$this->showCloseTick = false;
$this->getPen()->setVisible(false);
parent::setSubGallery($index);
* The horizontal Candle Size in pixels.
* It is based on pixels for Screen charts. When printing, this number is
* multiplied by the ratio between the Printer width and the Screen width.
return $this->candleWidth;
* Sets the horizontal Candle Size in pixels.<br>
* Candle color fill when Open value is greater than Close value.<br>
* By default, UpCloseColor is WHITE and DownCloseColor is RED. <br>
* Each Candle will be filled with a different color depending on its Open
* If Open value is greater than Close value, then the selected color will
* be the UpCloseColor color.<br>
* If Close value is greater or equal than Open value, then the selected
* color will be the DownCloseColor color. <br>
return $this->upCloseColor;
* Candle color fill when Open value is greater than Close value.<br>
* Candle color fill when Close value is greater than Open value.<br>
* By default, UpCloseColor is WHITE and DownCloseColor is RED. <br>
* Each Candle will be filled with a different color depending on its Open
* If Open value is greater than Close value, then the selected color will
* be the UpCloseColor color.<br>
* If Close value is greater or equal than Open value, then the selected
* color will be the DownCloseColor color. <br>
return $this->downCloseColor;
* Candle color fill when Close value is greater than Open value.<br>
* Determines whether Open prices will be displayed.<br>
* It only has effect when Candle series.CandleStyle is set to csCandleBar.
* CandleWidth determines the length in pixels of Open and Close ticks. <br>
return $this->showOpenTick;
* Determines whether Open prices will be displayed.<br>
* Determines whether Close prices will be displayed.<br>
* It only has effect when Candle series.CandleStyle is set to csCandleBar.
* CandleWidth determines the length in pixels of Open and Close ticks. <br>
return $this->showCloseTick;
* Determines whether Close prices will be displayed.<br>
private function calculateColor($valueIndex) {
$result = $this->downCloseColor;
$result = $this->upCloseColor;
// color algorithm when open is equal to close
$result = $this->upCloseColor; // <-- first point
$result = $this->downCloseColor;
$result = $this->upCloseColor;
* Called internally. Draws the "ValueIndex" point of the Series.
// TODO $this->onGetPointerStyle($valueIndex, $tmpStyle);
$g = $this->chart->getGraphics3D();
/* Pointer Pen && Brush styles */
$this->point->prepareCanvas($g, new Color(0,0,0));
/* The horizontal position */
// Vertical positions of Open, High, Low & Close values for this point
$tmpLeftWidth = $this->candleWidth / 2; /* Width */
$tmpRightWidth = $this->candleWidth - $tmpLeftWidth;
if ($this->chart->getAspect()->getView3D() && $this->point->getDraw3D()) {
$this->tmpBottom = $yClose;
$this->tmpBottom = $yOpen;
// Draw Candle Vertical Line from bottom to Low
$g->verticalLine($x, $this->tmpBottom, $yLow, $this->getMiddleZ());
$g->getBrush()->setColor( $this->calculateColor($valueIndex));
$g->getPen()->setColor( $this->calculateColor($valueIndex));
$g->cube($x - $tmpLeftWidth, $this->tmpTop, $x + $tmpRightWidth,
// Draw Candle Vertical Line from Top to High
$g->verticalLine($x, $this->tmpTop, $yHigh, $this->getMiddleZ());
// Draw Candle Vertical Line from High to Low
if ($this->chart->getAspect()->getView3D()) {
$g->verticalLine($x, $yLow, $yHigh, $this->getMiddleZ());
$g->verticalLine($x, $yLow, $yHigh);
// remember that Y coordinates are inverted
// prevent zero height rectangles 5.02
// in previous releases, an horizontal line was displayed instead
// of the small candle rectangle
$g->getBrush()->setColor( $this->calculateColor($valueIndex));
if ($this->chart->getAspect()->getView3D()) {
$g->rectangleWithZ(new Rectangle($x - $tmpLeftWidth,
$tmpLeftWidth + $tmpRightWidth,
if (!$this->point->getPen()->getVisible()) {
$g->rectangle(new Rectangle($x - $tmpLeftWidth, $yOpen,
$tmpLeftWidth + $tmpRightWidth + 1, $yClose - $yOpen));
$g->setPen($this->point->getPen());
$g->getPen()->setColor($this->calculateColor($valueIndex));
// Draw Candle Vertical Line from High to Low
if ($this->chart->getAspect()->getView3D()) {
$g->verticalLine($x, $yLow, $yHigh, $this->getMiddleZ());
if ($this->showOpenTick) {
$g->horizontalLine($x, $x - $tmpLeftWidth - 1, $yOpen,
if ($this->showCloseTick) {
$g->horizontalLine($x, $x + $tmpRightWidth + 1,
$g->verticalLine($x, $yLow, $yHigh);
if ($this->showOpenTick) {
$g->horizontalLine($x, $x - $tmpLeftWidth - 1, $yOpen);
if ($this->showCloseTick) {
$g->horizontalLine($x, $x + $tmpRightWidth + 1,
$p = new Point($x, $yClose);
if (($valueIndex != $tmpFirst) && (!$this->isNull($valueIndex))) {
$g->getPen()->setColor( $this->calculateColor($valueIndex));
if ($this->chart->getAspect()->getView3D()) {
$g->line($this->oldP, $p);
parent::prepareForGallery($isEnabled);
$this->point->getPen()->setWidth(2);
* Returns the ValueIndex of the x,y located point in the Series.
* Returns the ValueIndex of the "clicked" point in the Series.
public function clicked($p)
if (firstVisible>-1 && lastVisible>-1)
if (chart!=null) chart.getGraphics3D().calculate2DPosition(p.x, p.y, startZ);
//Point p = new Point(x,y);
for (int t=firstVisible; t<=lastVisible; t++)
* Returns true if point p is inside the bounds of the ValueIndex candle
public function clickedCandle($valueIndex, $p)
int tmpX =calcXPosValue(getDateValues().value[valueIndex]); /* The horizontal position
int yOpen =calcYPosValue(vOpenValues.value[valueIndex]);
int yHigh =calcYPosValue(vHighValues.value[valueIndex]);
int yLow =calcYPosValue(vLowValues.value[valueIndex]);
int yClose=calcYPosValue(getCloseValues().value[valueIndex]);
int tmpLeftWidth=candleWidth / 2; /* calc half Candle Width
int tmpRightWidth=candleWidth-tmpLeftWidth;
if (style==CandleStyle::$CANDLESTICK || style==CandleStyle::$OPENCLOSE)
if (chart.getAspect().getView3D() && getPointer().getDraw3D())
if (tmpTop>tmpBottom) Utils.swapInteger(tmpTop,tmpBottom);
if (style==CandleStyle::$CANDLESTICK &&
(Graphics3D.pointInLineTolerance(p,tmpX,tmpBottom,tmpX,yLow,3) ||
Graphics3D.pointInLineTolerance(p,tmpX,tmpTop,tmpX,yHigh,3)))
Rectangle tmpR=Rectangle::fromLTRB(tmpX-tmpLeftWidth,tmpTop,tmpX+tmpRightWidth,tmpBottom);
if (tmpR.contains(p.getX(), p.getY()))
if (style==CandleStyle::$CANDLESTICK &&
Graphics3D.pointInLineTolerance(p,tmpX,yLow,tmpX,yHigh,3))
if (yOpen==yClose) yClose--;
if (chart.getAspect().getView3D())
Rectangle tmpR=Rectangle::fromLTRB(tmpX-tmpLeftWidth,yOpen,tmpX+tmpRightWidth,yClose);
if (tmpR.contains(p.getX(), p.getY()))
if (!getPen().getVisible())
Rectangle tmpR=Rectangle::fromLTRB(tmpX-tmpLeftWidth,yOpen,tmpX+tmpRightWidth+1,yClose);
if (tmpR.contains(p.getX(), p.getY()))
else if (style==CandleStyle::$LINE)
if (valueIndex!=tmpFirst)
result = Graphics3D.pointInLineTolerance(p,oldP.x,oldP.y,tmpTo.x,tmpTo.y,3);
if (Graphics3D.pointInLineTolerance(p,tmpX,yLow,tmpX,yHigh,3) ||
(showOpenTick && Graphics3D.pointInLineTolerance(p,tmpX,yOpen,tmpX-tmpLeftWidth-1,yOpen,3)) ||
(showCloseTick && Graphics3D.pointInLineTolerance(p,tmpX,yClose,tmpX+tmpRightWidth+1,yClose,3)))
|