xref: /plugin/statdisplay/vendor/szymach/c-pchart/resources/doc/stock.md (revision 03cdebb71906645bc452782ad23f052261c2cb18)
1# Drawing a stock chart
2
3[Reference](http://wiki.pchart.net/doc.stocks.drawstockchart.html)
4
5```php
6require '/path/to/your/vendor/autoload.php';
7
8use CpChart\Chart\Stock;
9use CpChart\Data;
10use CpChart\Image;
11
12/* Create and populate the Data object */
13$data = new Data();
14$data->addPoints([34, 55, 15, 62, 38, 42], "Open");
15$data->addPoints([42, 25, 40, 38, 49, 36], "Close");
16$data->addPoints([27, 14, 12, 25, 32, 32], "Min");
17$data->addPoints([45, 59, 47, 65, 64, 48], "Max");
18$data->setAxisDisplay(0, AXIS_FORMAT_CURRENCY, "$");
19$data->addPoints(["8h", "10h", "12h", "14h", "16h", "18h"], "Time");
20$data->setAbscissa("Time");
21
22/* Create the Image object */
23$image = new Image(700, 230, $data);
24
25/* Draw the background */
26$settings = ["R" => 170, "G" => 183, "B" => 87, "Dash" => 1, "DashR" => 190, "DashG" => 203, "DashB" => 107];
27$image->drawFilledRectangle(0, 0, 700, 230, $settings);
28
29/* Overlay with a gradient */
30$settings = ["StartR" => 219, "StartG" => 231, "StartB" => 139, "EndR" => 1, "EndG" => 138, "EndB" => 68, "Alpha" => 50];
31$image->drawGradientArea(0, 0, 700, 230, DIRECTION_VERTICAL, $settings);
32
33/* Draw the border */
34$image->drawRectangle(0, 0, 699, 229, ["R" => 0, "G" => 0, "B" => 0]);
35
36/* Write the title */
37$image->setFontProperties(["FontName" => "Forgotte.ttf", "FontSize" => 11]);
38$image->drawText(60, 45, "Stock price", ["FontSize" => 28, "Align" => TEXT_ALIGN_BOTTOMLEFT]);
39
40/* Draw the 1st scale */
41$image->setGraphArea(60, 60, 450, 190);
42$image->drawFilledRectangle(60, 60, 450, 190, [
43    "R" => 255,
44    "G" => 255,
45    "B" => 255,
46    "Surrounding" => -200,
47    "Alpha" => 10
48]);
49$image->drawScale(["DrawSubTicks" => true, "CycleBackground" => true]);
50
51/* Draw the 1st stock chart */
52$mystockChart = new Stock($image, $data);
53$image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 30]);
54$mystockChart->drawStockChart();
55
56/* Reset the display mode because of the graph small size */
57$data->setAxisDisplay(0, AXIS_FORMAT_DEFAULT);
58
59/* Draw the 2nd scale */
60$image->setShadow(false);
61$image->setGraphArea(500, 60, 670, 190);
62$image->drawFilledRectangle(500, 60, 670, 190, [
63    "R" => 255,
64    "G" => 255,
65    "B" => 255,
66    "Surrounding" => -200,
67    "Alpha" => 10
68]);
69$image->drawScale(["Pos" => SCALE_POS_TOPBOTTOM, "DrawSubTicks" => true]);
70
71/* Draw the 2nd stock chart */
72$mystockChart = new Stock($image, $data);
73$image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 30]);
74$mystockChart->drawStockChart();
75
76/* Render the picture (choose the best way) */
77$image->autoOutput("example.drawStockChart.png");
78```
79