2 /* Libchart - PHP chart library
3 * Copyright (C) 2005-2008 Jean-Marc Tr�meaux (jm.tremeaux at gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @author Jean-Marc Tr�meaux (jm.tremeaux at gmail.com)
25 class PieChart extends Chart {
26 protected $pieCenterX;
27 protected $pieCenterY;
30 * Constructor of a pie chart.
32 * @param integer width of the image
33 * @param integer height of the image
35 public function PieChart($width = 600, $height = 250) {
36 parent::Chart($width, $height);
37 $this->plot->setGraphPadding(new Padding(15, 10, 30, 30));
41 * Computes the layout.
43 protected function computeLayout($hasCaption = true) {
44 $this->plot->setHasCaption($hasCaption);
45 $this->plot->computeLayout();
48 $graphArea = $this->plot->getGraphArea();
50 // Compute the coordinates of the pie
51 $this->pieCenterX = $graphArea->x1 + ($graphArea->x2 - $graphArea->x1) / 2;
52 $this->pieCenterY = $graphArea->y1 + ($graphArea->y2 - $graphArea->y1) / 2;
54 $this->pieWidth = round(($graphArea->x2 - $graphArea->x1) * 4 / 5);
55 $this->pieHeight = round(($graphArea->y2 - $graphArea->y1) * 3.7 / 5);
56 $this->pieDepth = round($this->pieWidth * 0.05);
60 * Compare two sampling point values, order from biggest to lowest value.
62 * @param double first value
63 * @param double second value
64 * @return integer result of the comparison
66 protected function sortPie($v1, $v2) {
67 return $v1[0] == $v2[0] ? 0 :
68 $v1[0] > $v2[0] ? -1 :
73 * Compute pie values in percentage and sort them.
75 protected function computePercent() {
77 $this->percent = array();
79 $pointList = $this->dataSet->getPointList();
80 foreach ($pointList as $point) {
81 $this->total += $point->getY();
84 foreach ($pointList as $point) {
85 $percent = $this->total == 0 ? 0 : 100 * $point->getY() / $this->total;
87 array_push($this->percent, array($percent, $point));
90 usort($this->percent, array("PieChart", "sortPie"));
94 * Creates the pie chart image.
96 protected function createImage() {
97 parent::createImage();
99 // Get graphical obects
100 $img = $this->plot->getImg();
101 $palette = $this->plot->getPalette();
102 $primitive = $this->plot->getPrimitive();
104 // Get the graph area
105 $graphArea = $this->plot->getGraphArea();
108 $primitive->outlinedBox($graphArea->x1, $graphArea->y1, $graphArea->x2, $graphArea->y2, $palette->axisColor[0], $palette->axisColor[1]);
110 // Aqua-like background
111 for ($i = $graphArea->y1 + 2; $i < $graphArea->y2 - 1; $i++) {
112 $color = $palette->aquaColor[($i + 3) % 4];
113 $primitive->line($graphArea->x1 + 2, $i, $graphArea->x2 - 2, $i, $color);
118 * Renders the caption.
120 protected function printCaption() {
121 // Create a list of labels
122 $labelList = array();
123 foreach($this->percent as $percent) {
124 list($percent, $point) = $percent;
125 $label = $point->getX();
127 array_push($labelList, $label);
130 // Create the caption
131 $caption = new Caption();
132 $caption->setPlot($this->plot);
133 $caption->setLabelList($labelList);
135 $palette = $this->plot->getPalette();
136 $pieColorSet = $palette->pieColorSet;
137 $caption->setColorSet($pieColorSet);
139 // Render the caption
146 * @param integer Center coordinate (y)
147 * @param array Colors for each portion
148 * @param bitfield Drawing mode
150 protected function drawDisc($cy, $colorArray, $mode) {
151 // Get graphical obects
152 $img = $this->plot->getImg();
158 foreach ($this->percent as $a) {
159 list ($percent, $point) = $a;
161 // If value is null, don't draw this arc
166 $color = $colorArray[$i % count($colorArray)];
167 $percentTotal += $percent;
168 $angle2 = (int)($percentTotal * 360 / 100);
170 if ($angle2 - $angle1 <= 0)
171 $angle2 = $angle1 + 1;
172 imagefilledarc($img, $this->pieCenterX, $cy, $this->pieWidth, $this->pieHeight, $angle1, $angle2, $color->getColor($img), $mode);
181 * Print the percentage text.
183 protected function drawPercent() {
184 // Get graphical obects
185 $img = $this->plot->getImg();
186 $palette = $this->plot->getPalette();
187 $text = $this->plot->getText();
188 $primitive = $this->plot->getPrimitive();
193 foreach ($this->percent as $a) {
194 list ($percent, $point) = $a;
196 // If value is null, don't print percentage
201 $percentTotal += $percent;
202 $angle2 = $percentTotal * 2 * M_PI / 100;
204 $angle = $angle1 + ($angle2 - $angle1) / 2;
205 $label = number_format($percent) . "%";
207 $x = cos($angle) * ($this->pieWidth + 35) / 2 + $this->pieCenterX;
208 $y = sin($angle) * ($this->pieHeight + 35) / 2 + $this->pieCenterY;
210 $text->printText($img, $x, $y, $this->plot->getTextColor(), $label, $text->fontCondensed, $text->HORIZONTAL_CENTER_ALIGN | $text->VERTICAL_CENTER_ALIGN);
217 * Print the pie chart.
219 protected function printPie() {
220 // Get graphical obects
221 $img = $this->plot->getImg();
222 $palette = $this->plot->getPalette();
223 $text = $this->plot->getText();
224 $primitive = $this->plot->getPrimitive();
226 // Get the pie color set
227 $pieColorSet = $palette->pieColorSet;
228 $pieColorSet->reset();
231 for ($cy = $this->pieCenterY + $this->pieDepth / 2; $cy >= $this->pieCenterY - $this->pieDepth / 2; $cy--) {
232 $this->drawDisc($cy, $palette->pieColorSet->shadowColorList, IMG_ARC_EDGED);
236 $this->drawDisc($this->pieCenterY - $this->pieDepth / 2, $palette->pieColorSet->colorList, IMG_ARC_PIE);
239 $this->drawPercent();
243 * Render the chart image.
245 * @param string name of the file to render the image to (optional)
247 public function render($fileName = null) {
248 $this->computePercent();
249 $this->computeLayout();
250 $this->createImage();
251 $this->plot->printLogo();
252 $this->plot->printTitle();
254 $this->printCaption();
256 $this->plot->render($fileName);