xref: /plugin/statdisplay/vendor/szymach/c-pchart/resources/doc/scatter_best_fit.md (revision 03cdebb71906645bc452782ad23f052261c2cb18)
1# Drawing a scatter best fit chart
2
3[Reference](http://wiki.pchart.net/doc.scatter.drawscatterbestfit.html)
4
5```php
6require '/path/to/your/vendor/autoload.php';
7
8use CpChart\Chart\Scatter;
9use CpChart\Data;
10use CpChart\Image;
11
12/* Create the Data object */
13$data = new Data();
14
15/* Create the X axis and the binded series */
16for ($i = 0; $i <= 360; $i = $i + 10) {
17    $data->addPoints(rand(1, 20) * 10 + rand(0, $i), "Probe 1");
18}
19for ($i = 0; $i <= 360; $i = $i + 10) {
20    $data->addPoints(rand(1, 2) * 10 + rand(0, $i), "Probe 2");
21}
22$data->setAxisName(0, "X-Index");
23$data->setAxisXY(0, AXIS_X);
24$data->setAxisPosition(0, AXIS_POSITION_TOP);
25
26/* Create the Y axis and the binded series */
27for ($i = 0; $i <= 360; $i = $i + 10) {
28    $data->addPoints($i, "Probe 3");
29}
30$data->setSerieOnAxis("Probe 3", 1);
31$data->setAxisName(1, "Y-Index");
32$data->setAxisXY(1, AXIS_Y);
33$data->setAxisPosition(1, AXIS_POSITION_LEFT);
34
35/* Create the 1st scatter chart binding */
36$data->setScatterSerie("Probe 1", "Probe 3", 0);
37$data->setScatterSerieDescription(0, "This year");
38$data->setScatterSerieColor(0, ["R" => 0, "G" => 0, "B" => 0]);
39
40/* Create the 2nd scatter chart binding */
41$data->setScatterSerie("Probe 2", "Probe 3", 1);
42$data->setScatterSerieDescription(1, "Last Year");
43
44/* Create the Image object */
45$image = new Image(400, 400, $data);
46
47/* Draw the background */
48$settings = ["R" => 170, "G" => 183, "B" => 87, "Dash" => 1, "DashR" => 190, "DashG" => 203, "DashB" => 107];
49$image->drawFilledRectangle(0, 0, 400, 400, $settings);
50
51/* Overlay with a gradient */
52$image->drawGradientArea(0, 0, 400, 400, DIRECTION_VERTICAL, [
53    "StartR" => 219,
54    "StartG" => 231,
55    "StartB" => 139,
56    "EndR" => 1,
57    "EndG" => 138,
58    "EndB" => 68,
59    "Alpha" => 50
60]);
61$image->drawGradientArea(0, 0, 400, 20, DIRECTION_VERTICAL, [
62    "StartR" => 0,
63    "StartG" => 0,
64    "StartB" => 0,
65    "EndR" => 50,
66    "EndG" => 50,
67    "EndB" => 50,
68    "Alpha" => 80
69]);
70
71/* Write the picture title */
72$image->setFontProperties(["FontName" => "Silkscreen.ttf", "FontSize" => 6]);
73$image->drawText(10, 13, "drawScatterBestFit() - Linear regression", [
74    "R" => 255,
75    "G" => 255,
76    "B" => 255
77]);
78
79/* Add a border to the picture */
80$image->drawRectangle(0, 0, 399, 399, ["R" => 0, "G" => 0, "B" => 0]);
81
82/* Set the default font */
83$image->setFontProperties(["FontName" => "pf_arma_five.ttf", "FontSize" => 6]);
84
85/* Set the graph area */
86$image->setGraphArea(50, 60, 350, 360);
87
88/* Create the Scatter chart object */
89$myScatter = new Scatter($image, $data);
90
91/* Draw the scale */
92$myScatter->drawScatterScale();
93
94/* Turn on shadow computing */
95$image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10]);
96
97/* Draw a scatter plot chart */
98$myScatter->drawScatterPlotChart();
99
100/* Draw the legend */
101$myScatter->drawScatterLegend(280, 380, ["Mode" => LEGEND_HORIZONTAL, "Style" => LEGEND_NOBORDER]);
102
103/* Draw the line of best fit */
104$myScatter->drawScatterBestFit();
105
106/* Render the picture (choose the best way) */
107$image->autoOutput("example.drawScatterBestFit.png");
108```
109