xref: /plugin/statdisplay/vendor/szymach/c-pchart/resources/doc/bubble.md (revision 03cdebb71906645bc452782ad23f052261c2cb18)
1# Drawing a linear bubble chart
2
3[Reference](http://wiki.pchart.net/doc.bubble.drawbubblechart.html)
4
5```php
6require '/path/to/your/vendor/autoload.php';
7
8use CpChart\Chart\Bubble;
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], "Probe1");
15$data->addPoints([5, 10, 8, 9, 15, 10], "Probe1Weight");
16$data->addPoints([5, 10, -5, -1, 0, -10], "Probe2");
17$data->addPoints([6, 10, 14, 10, 14, 6], "Probe2Weight");
18$data->setSerieDescription("Probe1", "This year");
19$data->setSerieDescription("Probe2", "Last year");
20$data->setAxisName(0, "Current stock");
21$data->addPoints(["Apple", "Banana", "Orange", "Lemon", "Peach", "Strawberry"], "Product");
22$data->setAbscissa("Product");
23
24/* Create the Image object */
25$image = new Image(700, 230, $data);
26
27/* Draw the background */
28$settings = ["R" => 170, "G" => 183, "B" => 87, "Dash" => 1, "DashR" => 190, "DashG" => 203, "DashB" => 107];
29$image->drawFilledRectangle(0, 0, 700, 230, $settings);
30
31/* Overlay with a gradient */
32$settings = [
33    "StartR" => 219,
34    "StartG" => 231,
35    "StartB" => 139,
36    "EndR" => 1,
37    "EndG" => 138,
38    "EndB" => 68,
39    "Alpha" => 50
40];
41$image->drawGradientArea(0, 0, 700, 230, DIRECTION_VERTICAL, $settings);
42$image->drawGradientArea(0, 0, 700, 20, DIRECTION_VERTICAL, [
43    "StartR" => 0,
44    "StartG" => 0,
45    "StartB" => 0,
46    "EndR" => 50,
47    "EndG" => 50,
48    "EndB" => 50,
49    "Alpha" => 80
50]);
51
52/* Add a border to the picture */
53$image->drawRectangle(0, 0, 699, 229, ["R" => 0, "G" => 0, "B" => 0]);
54
55/* Write the picture title */
56$image->setFontProperties(["FontName" => "Silkscreen.ttf", "FontSize" => 6]);
57$image->drawText(10, 13, "drawBubbleChart() - draw a linear bubble chart", [
58    "R" => 255,
59    "G" => 255,
60    "B" => 255
61]);
62
63/* Write the title */
64$image->setFontProperties(["FontName" => "Forgotte.ttf", "FontSize" => 11]);
65$image->drawText(40, 55, "Current Stock / Needs chart", ["FontSize" => 14, "Align" => TEXT_ALIGN_BOTTOMLEFT]);
66
67/* Change the default font */
68$image->setFontProperties(["FontName" => "pf_arma_five.ttf", "FontSize" => 6]);
69
70/* Create the Bubble chart object and scale up */
71$bubbleChart = new Bubble($image, $data);
72
73/* Scale up for the bubble chart */
74$bubbleDataSeries = ["Probe1", "Probe2"];
75$bubbleWeightSeries = ["Probe1Weight", "Probe2Weight"];
76$bubbleChart->bubbleScale($bubbleDataSeries, $bubbleWeightSeries);
77
78/* Draw the 1st chart */
79$image->setGraphArea(40, 60, 430, 190);
80$image->drawFilledRectangle(40, 60, 430, 190, ["R" => 255, "G" => 255, "B" => 255,
81    "Surrounding" => -200, "Alpha" => 10]);
82$image->drawScale(["DrawSubTicks" => true, "CycleBackground" => true]);
83$image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 30]);
84$bubbleChart->drawBubbleChart($bubbleDataSeries, $bubbleWeightSeries);
85
86/* Draw the 2nd scale */
87$image->setShadow(false);
88$image->setGraphArea(500, 60, 670, 190);
89$image->drawFilledRectangle(500, 60, 670, 190, [
90    "R" => 255,
91    "G" => 255,
92    "B" => 255,
93    "Surrounding" => -200,
94    "Alpha" => 10
95]);
96$image->drawScale(["Pos" => SCALE_POS_TOPBOTTOM, "DrawSubTicks" => true]);
97
98/* Draw the 2nd stock chart */
99$image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 30]);
100$bubbleChart->drawbubbleChart($bubbleDataSeries, $bubbleWeightSeries);
101
102/* Write the chart legend */
103$image->drawLegend(550, 215, ["Style" => LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL]);
104
105/* Render the picture (choose the best way) */
106$image->autoOutput("example.drawBubbleChart.png");
107```
108