xref: /plugin/statdisplay/vendor/szymach/c-pchart/resources/doc/bar.md (revision 03cdebb71906645bc452782ad23f052261c2cb18)
1# Drawing a bar chart
2
3[Reference](http://wiki.pchart.net/doc.chart.drawbarchart.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->addPoints([13251, 4118, 3087, 1460, 1248, 156, 26, 9, 8], "Hits");
14$data->setAxisName(0, "Hits");
15$data->addPoints(["Firefox", "Chrome", "Internet Explorer", "Opera", "Safari", "Mozilla", "SeaMonkey", "Camino", "Lunascape"], "Browsers");
16$data->setSerieDescription("Browsers", "Browsers");
17$data->setAbscissa("Browsers");
18
19/* Create the Image object */
20$image = new Image(500, 500, $data);
21$image->drawGradientArea(0, 0, 500, 500, DIRECTION_VERTICAL, [
22    "StartR" => 240,
23    "StartG" => 240,
24    "StartB" => 240,
25    "EndR" => 180,
26    "EndG" => 180,
27    "EndB" => 180,
28    "Alpha" => 100
29]);
30$image->drawGradientArea(0, 0, 500, 500, DIRECTION_HORIZONTAL, [
31    "StartR" => 240,
32    "StartG" => 240,
33    "StartB" => 240,
34    "EndR" => 180,
35    "EndG" => 180,
36    "EndB" => 180,
37    "Alpha" => 20
38]);
39$image->setFontProperties(["FontName" => "pf_arma_five.ttf", "FontSize" => 6]);
40
41/* Draw the chart scale */
42$image->setGraphArea(100, 30, 480, 480);
43$image->drawScale([
44    "CycleBackground" => true,
45    "DrawSubTicks" => true,
46    "GridR" => 0,
47    "GridG" => 0,
48    "GridB" => 0,
49    "GridAlpha" => 10,
50    "Pos" => SCALE_POS_TOPBOTTOM
51]);
52
53/* Turn on shadow computing */
54$image->setShadow(true, ["X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10]);
55
56/* Draw the chart */
57$image->drawBarChart(["DisplayPos" => LABEL_POS_INSIDE, "DisplayValues" => true, "Rounded" => true, "Surrounding" => 30]);
58
59/* Write the legend */
60$image->drawLegend(570, 215, ["Style" => LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL]);
61
62/* Render the picture (choose the best way) */
63$image->autoOutput("example.drawBarChart.vertical.png");
64```
65