xref: /plugin/statdisplay/vendor/szymach/c-pchart/resources/doc/area.md (revision 03cdebb71906645bc452782ad23f052261c2cb18)
1# Drawing an area chart
2
3[Reference](http://wiki.pchart.net/doc.chart.drawareachart.html)
4
5```php
6require '/path/to/your/vendor/autoload.php';
7
8use CpChart\Data;
9use CpChart\Image;
10
11$data = new Data();
12for ($i = 0; $i <= 30; $i++) {
13    $data->addPoints(rand(1, 15), "Probe 1");
14}
15$data->setSerieTicks("Probe 2", 4);
16$data->setAxisName(0, "Temperatures");
17
18// Create the Image object
19$image = new Image(700, 230, $data);
20
21/* Turn off Antialiasing */
22$image->Antialias = false;
23
24/* Add a border to the picture */
25$image->drawRectangle(0, 0, 699, 229, ["R" => 0, "G" => 0, "B" => 0]);
26
27/* Write the chart title */
28$image->setFontProperties(["FontName" => "Forgotte.ttf", "FontSize" => 11]);
29$image->drawText(150, 35, "Average temperature", ["FontSize" => 20, "Align" => TEXT_ALIGN_BOTTOMMIDDLE]);
30
31/* Set the default font */
32$image->setFontProperties(["FontName" => "pf_arma_five.ttf", "FontSize" => 6]);
33
34/* Define the chart area */
35$image->setGraphArea(60, 40, 650, 200);
36
37/* Draw the scale */
38$scaleSettings = [
39    "XMargin" => 10,
40    "YMargin" => 10,
41    "Floating" => true,
42    "GridR" => 200,
43    "GridG" => 200,
44    "GridB" => 200,
45    "DrawSubTicks" => true,
46    "CycleBackground" => true
47];
48$image->drawScale($scaleSettings);
49
50/* Write the chart legend */
51$image->drawLegend(600, 20, ["Style" => LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL]);
52
53/* Turn on Antialiasing */
54$image->Antialias = true;
55
56/* Enable shadow computing */
57$image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10]);
58
59/* Draw the area chart */
60$Threshold = [];
61$Threshold[] = ["Min" => 0, "Max" => 5, "R" => 207, "G" => 240, "B" => 20, "Alpha" => 70];
62$Threshold[] = ["Min" => 5, "Max" => 10, "R" => 240, "G" => 232, "B" => 20, "Alpha" => 70];
63$Threshold[] = ["Min" => 10, "Max" => 20, "R" => 240, "G" => 191, "B" => 20, "Alpha" => 70];
64$image->drawAreaChart(["Threshold" => $Threshold]);
65
66/* Write the thresholds */
67$image->drawThreshold(5, ["WriteCaption" => true, "Caption" => "Warn Zone", "Alpha" => 70, "Ticks" => 2, "R" => 0, "G" => 0, "B" => 255]);
68$image->drawThreshold(10, ["WriteCaption" => true, "Caption" => "Error Zone", "Alpha" => 70, "Ticks" => 2, "R" => 0, "G" => 0, "B" => 255]);
69
70/* Render the picture (choose the best way) */
71$image->autoOutput("example.drawAreaChart.threshold.png");
72```
73