Menu

annotated_candlesticks

PHPlot Sample - Annotated Candlesticks Plot

This sample shows a candlestick plot with an extra annotation label. The label is drawn on the right side Y axis and shows the closing price of the stock on the final day. A call-back is used to place the label.

The label position is calculated as follows. Take the final closing price, $y_final. This is the world coordinate Y value. Convert this to a Y device coordinate (pixel value) $yd. For X, use the plot_area array which is passed to the callback by PHPlot. This array contains the plot area coordinates as (x1, y1, x2, y2). The value we want is x2, the X device coordinate of the right side of the plot area. Using these values ($x2, $yd) as the base point, we can position the label just right of the right side Y axis, at the Y position corresponding to the closing price on the plot.

(2015-07-30)

Reference: Discussion forum thread

The Plot:

Plot Image


The Code:

~~~~
:::php
<?php

PHPlot test: Candlestick plot with annotation of closing price 7/30/2015

require_once 'phplot.php';

// Data array: Label (day of month), Open, High, Low, Close
// (Actual values from 13-24 July 2015 for a well known stock)
$data = array(
array('13', 532.88, 547.11, 532.40, 546.55),
array('14', 546.76, 565.85, 546.71, 561.10),
array('15', 560.13, 566.50, 556.79, 560.22),
array('16', 565.12, 580.68, 565.00, 579.85),
array('17', 649.00, 674.47, 645.00, 672.93),
array('20', 659.24, 668.88, 653.01, 663.02),
array('21', 655.21, 673.00, 654.30, 662.30),
array('22', 660.89, 678.64, 659.00, 662.10),
array('23', 661.27, 663.63, 641.00, 644.28),
array('24', 647.00, 648.17, 622.52, 623.56),
);

// Get the final closing price from a data array.
// $has_x_values is true for data type 'data-data', else false.
function get_final_close($data, $has_x_values)
{
$final_row_index = count($data) - 1;
$close_column_index = $has_x_values ? 5 : 4;
return $data[$final_row_index][$close_column_index];
}

// Post-drawing callback function draws the label.
// $passthru is an array with the PHPlot object and the data array.
function post_draw($img, $passthru, $plot_area)
{
list($plot, $data) = $passthru;

// Get the final close price. This is a Y value in World Coordinates.
$y_final= get_final_close($data, FALSE);

// Convert to device coordinates. Note X is unused.
list($unused_x, $yd) = $plot->GetDeviceXY(0, $y_final);

// Allocate colors for label text, box background and border:
$color_fg = imagecolorresolve($img, 255, 0, 0); // Red
$color_bg = imagecolorresolve($img, 0xff, 0xff, 0xcc); // Light yellow
$color_border = imagecolorresolve($img, 0, 0, 0); // Black

// Get the text size, and draw an outlined box behind the text:
list($text_width, $text_height) = $plot->SizeText('', 0, $y_final);
$x1 = $plot_area[2] + 2;
$y1 = $yd - $text_height / 2;
$x2 = $x1 + $text_width + 4;
$y2 = $yd + $text_height / 2 + 2;
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $color_bg);
imagerectangle($img, $x1, $y1, $x2, $y2, $color_border);

// Finally, draw the label:
$plot->DrawText('', 0, $x1 + 2, $yd, $color_fg, $y_final, 'left', 'center');

}

$plot = new PHPlot(600, 400);

$plot->SetTitle('Demo Candlestick Plot with Annotation');
$plot->SetDataType('text-data');
$plot->SetDataValues($data);
$plot->SetPlotType('candlesticks');

// Disable the Y axis 'zero magnet' : let it fit to the data.
$plot->TuneYAutoRange(0);
// Don't draw X ticks - with data labels they have no meaning.
$plot->SetXTickPos('none');

// Place the Y axis on the right:
$plot->SetYTickPos('plotright');
$plot->SetYTickLabelPos('plotright');
// And make the right margin wider, for the extra label
$plot->SetMarginsPixels(NULL, 50);

// Register a callback for drawing the extra label:
$plot->SetCallback('draw_all', 'post_draw', array($plot, $data));

// Draw the graph:
$plot->DrawGraph();

~~~~~


Related

Discussion: Possible to create a single y-axis data label?

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.