xref: /plugin/statdisplay/vendor/szymach/c-pchart/resources/doc/filled_spline.md (revision 03cdebb71906645bc452782ad23f052261c2cb18)
1# Drawing a filled spline chart
2
3[Reference](http://wiki.pchart.net/doc.chart.drawFilledSplineChart.html)
4
5```php
6require '/path/to/your/vendor/autoload.php';
7
8use CpChart\Data;
9use CpChart\Image;
10
11/* Create and populate the Data object */
12$data = new Data();
13$data->setAxisName(0, "Strength");
14for ($i = 0; $i <= 720; $i = $i + 20) {
15    $data->addPoints(cos(deg2rad($i)) * 100, "Probe 1");
16    $data->addPoints(cos(deg2rad($i + 90)) * 60, "Probe 2");
17}
18
19/* Create the Image object */
20$image = new Image(847, 304, $data);
21$image->drawGradientArea(0, 0, 847, 304, DIRECTION_VERTICAL, [
22    "StartR" => 47,
23    "StartG" => 47,
24    "StartB" => 47,
25    "EndR" => 17,
26    "EndG" => 17,
27    "EndB" => 17,
28    "Alpha" => 100
29]);
30$image->drawGradientArea(0, 250, 847, 304, DIRECTION_VERTICAL, [
31    "StartR" => 47,
32    "StartG" => 47,
33    "StartB" => 47,
34    "EndR" => 27,
35    "EndG" => 27,
36    "EndB" => 27,
37    "Alpha" => 100
38]);
39$image->drawLine(0, 249, 847, 249, ["R" => 0, "G" => 0, "B" => 0]);
40$image->drawLine(0, 250, 847, 250, ["R" => 70, "G" => 70, "B" => 70]);
41
42/* Add a border to the picture */
43$image->drawRectangle(0, 0, 846, 303, ["R" => 204, "G" => 204, "B" => 204]);
44
45/* Write the picture title */
46$image->setFontProperties(["FontName" => "pf_arma_five.ttf", "FontSize" => 6]);
47$image->drawText(423, 14, "Cyclic magnetic field strength", [
48    "R" => 255,
49    "G" => 255,
50    "B" => 255,
51    "Align" => TEXT_ALIGN_MIDDLEMIDDLE
52]);
53
54/* Define the chart area */
55$image->setGraphArea(58, 27, 816, 228);
56
57/* Draw a rectangle */
58$image->drawFilledRectangle(58, 27, 816, 228, [
59    "R" => 0,
60    "G" => 0,
61    "B" => 0,
62    "Dash" => true,
63    "DashR" => 0,
64    "DashG" => 51,
65    "DashB" => 51,
66    "BorderR" => 0,
67    "BorderG" => 0,
68    "BorderB" => 0
69]);
70
71/* Turn on shadow computing */
72$image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 20]);
73
74/* Draw the scale */
75$image->setFontProperties(["R" => 255, "G" => 255, "B" => 255]);
76$ScaleSettings = [
77    "XMargin" => 4,
78    "DrawSubTicks" => true,
79    "GridR" => 255,
80    "GridG" => 255,
81    "GridB" => 255,
82    "AxisR" => 255,
83    "AxisG" => 255,
84    "AxisB" => 255,
85    "GridAlpha" => 30,
86    "CycleBackground" => true
87];
88$image->drawScale($ScaleSettings);
89
90/* Draw the spline chart */
91$image->drawFilledSplineChart();
92
93/* Write the chart boundaries */
94$BoundsSettings = [
95    "MaxDisplayR" => 237,
96    "MaxDisplayG" => 23,
97    "MaxDisplayB" => 48,
98    "MinDisplayR" => 23,
99    "MinDisplayG" => 144,
100    "MinDisplayB" => 237
101];
102$image->writeBounds(BOUND_BOTH, $BoundsSettings);
103
104/* Write the 0 line */
105$image->drawThreshold(0, ["WriteCaption" => true]);
106
107/* Write the chart legend */
108$image->setFontProperties(["R" => 255, "G" => 255, "B" => 255]);
109$image->drawLegend(560, 266, ["Style" => LEGEND_NOBORDER]);
110
111/* Write the 1st data series statistics */
112$settings = ["R" => 188, "G" => 224, "B" => 46, "Align" => TEXT_ALIGN_BOTTOMLEFT];
113$image->drawText(620, 270, "Max : " . ceil($data->getMax("Probe 1")), $settings);
114$image->drawText(680, 270, "Min : " . ceil($data->getMin("Probe 1")), $settings);
115$image->drawText(740, 270, "Avg : " . ceil($data->getSerieAverage("Probe 1")), $settings);
116
117/* Write the 2nd data series statistics */
118$settings = ["R" => 224, "G" => 100, "B" => 46, "Align" => TEXT_ALIGN_BOTTOMLEFT];
119$image->drawText(620, 283, "Max : " . ceil($data->getMax("Probe 2")), $settings);
120$image->drawText(680, 283, "Min : " . ceil($data->getMin("Probe 2")), $settings);
121$image->drawText(740, 283, "Avg : " . ceil($data->getSerieAverage("Probe 2")), $settings);
122
123/* Render the picture (choose the best way) */
124$image->autoOutput("example.drawFilledSplineChart.png");
125```
126