xref: /plugin/statdisplay/vendor/szymach/c-pchart/resources/doc/2d_pie.md (revision 03cdebb71906645bc452782ad23f052261c2cb18)
1# Drawing a 2D pie chart
2
3[Reference](http://wiki.pchart.net/doc.pie.draw2dpie.html)
4
5```php
6require '/path/to/your/vendor/autoload.php';
7
8use CpChart\Chart\Pie;
9use CpChart\Data;
10use CpChart\Image;
11
12// Create and populate data
13$data = new Data();
14$data->addPoints([40, 60, 15, 10, 6, 4], "ScoreA");
15$data->setSerieDescription("ScoreA", "Application A");
16
17// Define the absissa serie
18$data->addPoints(["<10", "10<>20", "20<>40", "40<>60", "60<>80", ">80"], "Labels");
19$data->setAbscissa("Labels");
20
21// Create the image
22$image = new Image(700, 230, $data);
23
24// Draw a solid background
25$backgroundSettings = [
26    "R"     => 173,
27    "G"     => 152,
28    "B"     => 217,
29    "Dash"  => 1,
30    "DashR" => 193,
31    "DashG" => 172,
32    "DashB" => 237
33];
34$image->drawFilledRectangle(0, 0, 700, 230, $backgroundSettings);
35
36//Draw a gradient overlay
37$gradientSettings = [
38    "StartR" => 209,
39    "StartG" => 150,
40    "StartB" => 231,
41    "EndR"   => 111,
42    "EndG"   => 3,
43    "EndB"   => 138,
44    "Alpha"  => 50
45];
46$image->drawGradientArea(0, 0, 700, 230, DIRECTION_VERTICAL, $gradientSettings);
47$image->drawGradientArea(0, 0, 700, 20, DIRECTION_VERTICAL, [
48    "StartR" => 0,
49    "StartG" => 0,
50    "StartB" => 0,
51    "EndR"   => 50,
52    "EndG"   => 50,
53    "EndB"   => 50,
54    "Alpha"  => 100
55]);
56
57// Add a border to the picture
58$image->drawRectangle(0, 0, 699, 229, ["R" => 0, "G" => 0, "B" => 0]);
59
60// Write the picture title
61$image->setFontProperties(["FontName" => "Silkscreen.ttf", "FontSize" => 6]);
62$image->drawText(10, 13, "pPie - Draw 2D pie charts", ["R" => 255, "G" => 255, "B" => 255]);
63
64// Set the default font properties
65$image->setFontProperties(["FontName" => "Forgotte.ttf", "FontSize" => 10, "R" => 80, "G" => 80, "B" => 80]);
66
67// Enable shadow computing
68$image->setShadow(true, ["X" => 2, "Y" => 2, "R" => 150, "G" => 150, "B" => 150, "Alpha" => 100]);
69$image->drawText(140, 200, "Single AA pass", ["R" => 0, "G" => 0, "B" => 0, "Align" => TEXT_ALIGN_TOPMIDDLE]);
70
71// Create and draw the chart
72$pieChart = new Pie($image, $data);
73$pieChart->draw2DPie(140, 125, ["SecondPass" => false]);
74$pieChart->draw2DPie(340, 125, ["DrawLabels" => true, "Border" => true]);
75$pieChart->draw2DPie(540, 125, [
76    "DataGapAngle"  => 10,
77    "DataGapRadius" => 6,
78    "Border" => true,
79    "BorderR" => 255,
80    "BorderG" => 255,
81    "BorderB" => 255
82]);
83$image->drawText(540, 200, "Extended AA pass / Splitted", ["R" => 0, "G" => 0, "B" => 0, "Align" => TEXT_ALIGN_TOPMIDDLE]);
84
85$pieChart->pChartObject->autoOutput("example.draw2DPie.png");
86```
87
88