This example demonstrate creating a candlestick chart, completed with labels to indicate the highest and lowest values.
[File: phpdemo/candlestick.php".]
setPlotArea(50, 25, 500, 250);
$plotAreaObj->setGridColor(0xc0c0c0, 0xc0c0c0);
#Add a title to the chart
$c->addTitle("Universal Stock Index on Jan 2001");
#Add a custom text at (50, 25) (the upper left corner of the plotarea). Use 12
#pts Arial Bold/pale green (0x40c040) as the font.
$c->addText(50, 25, "(c) Global XYZ ABC Company", "arialbd.ttf", 12, 0x40c040);
#Add a title to the x axis
$c->xAxis->setTitle("Jan 2001");
#Set the labels on the x axis. Rotate the labels by 45 degrees.
$labelsObj = $c->xAxis->setLabels($labels);
$labelsObj->setFontAngle(45);
#Add a title to the y axis
$c->yAxis->setTitle("Universal Stock Index");
#Draw the y axis on the right hand side of the plot area
$c->setYAxisOnRight(true);
#Reserve 10% margin at the top and bottom of the plot area during auto-scaling.
#This is to leave space for the high and low data labels.
$c->yAxis->setAutoScale(0.1, 0.1);
#Add a CandleStick layer to the chart using green (0xff00) for up candles and
#red (0xff0000) for down candles
$layer = $c->addCandleStickLayer($highData, $lowData, $openData, $closeData,
0xff00, 0xff0000);
#Set the line width to 2 pixels
$layer->setLineWidth(2);
#
#Now we add the "High" and "Low" text labels. We first find out which are the
#highest and lowest positions.
#
$obj = new ArrayMath($highData);
$highPos = $obj->maxIndex();
$obj = new ArrayMath($lowData);
$lowPos = $obj->minIndex();
#By default, we put text at the center position. If the data point is too close
#to the right or left border of the plot area, we align the text to the right
#and left to avoid the text overflows outside the plot area
$align = BottomCenter;
if ($highPos > 18) {
$align = BottomRight;
} else if ($highPos < 5) {
$align = BottomLeft;
}
#Add the custom high label at the high position
$customDataLabelObj = $layer->addCustomDataLabel(0, $highPos,
"High {high}\n{xLabel} Jan, 2001", "arialbd.ttf");
$customDataLabelObj->setAlignment($align);
#Similarly, we compute the alignment for the low label based on its x position.
$align = TopCenter;
if ($lowPos > 18) {
$align = TopRight;
} else if ($lowPos < 5) {
$align = TopLeft;
}
#Add the custom low label at the low position
$customDataLabelObj = $layer->addCustomDataLabel(0, $lowPos,
"Low {low}\n{xLabel} Jan, 2001", "arialbd.ttf");
$customDataLabelObj->setAlignment($align);
#output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));
?> |
© 2004 Advanced Software Engineering Limited. All rights reserved.