3
* Pie graph implementation for the Horde_Graph package.
5
* $Horde: framework/Graph/Graph/Chart/pie.php,v 1.10.10.1 2005/01/03 12:19:00 jan Exp $
7
* Copyright 2002-2005 Chuck Hagenbuch <chuck@horde.org>
9
* See the enclosed file COPYING for license information (LGPL). If you
10
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
12
* @author Chuck Hagenbuch <chuck@horde.org>
13
* @version $Revision: 1.10.10.1 $
15
* @package Horde_Graph
17
class Horde_Graph_Chart_pie {
24
var $_colors = array('tan', 'palegoldenrod', 'olivedrab', 'blue', 'red', 'green', 'yellow',
25
'orange', 'gray', 'purple');
42
function Horde_Graph_Chart_pie(&$graph, $params)
44
$this->graph = &$graph;
46
foreach ($params as $param => $value) {
54
$data = $this->graph->_data['y'][$this->_dataset];
56
// Initialize some variables.
57
$diameter = min($this->graph->_graphWidth, $this->graph->_graphHeight) - ($this->_padding * 2);
58
$radius = $diameter / 2;
59
$count = count($data);
60
$xcenter = $this->graph->_graphLeft + $this->_padding + ($this->graph->_graphWidth / 2);
61
$ycenter = $this->graph->_graphTop + $this->_padding + ($this->graph->_graphHeight / 2);
63
// Calculate the sum of all slices.
65
foreach ($data as $x) {
69
// Convert each slice into the corresponding percentage of a
74
foreach ($data as $i => $y) {
75
if ((($y / $sum) * 360) > 0) {
76
$degrees[$degCount] = ($y / $sum) * 360;
77
$slices[$degCount] = $y;
78
$names[$degCount] = isset($this->graph->_data['x'][$i]) ? $this->graph->_data['x'][$i] : '';
86
$count = count($degrees);
87
for ($z = 0; $z < $count; $z++) {
88
// Calculate and draw arcs corresponding to each
90
$cz = $z % count($this->_colors);
91
$this->graph->img->arc($xcenter, $ycenter, $radius, $last_angle, ($last_angle + $degrees[$z]),
92
$this->_outline ? 'black' : $this->_colors[$cz], $this->_colors[$cz]);
93
$last_angle = $last_angle + $degrees[$z];
96
$this->graph->img->circle($xcenter, $ycenter, $radius, 'black', $this->_colors[0]);
99
// Create the color key and slice labels.
100
$yBase = $this->graph->_graphTop;
102
$max = strlen((string)max($data));
103
for ($z = 0; $z < $degCount; $z++) {
104
$cz = $z % count($this->_colors);
105
$percent = ($degrees[$z] / 360) * 100;
106
$percent = round($percent, 2);
109
$this->graph->img->rectangle($xBase, $yBase, 12, 12, 'black', $this->_colors[$cz]);
110
if ($slices[$z] >= 1000 && $slices[$z] < 1000000) {
111
$slices[$z] = $slices[$z] / 1000;
112
$slices[$z] = $slices[$z] . 'k';
114
$repeat = $max - strlen($slices[$z]);
118
$slices[$z] = str_repeat(' ', $repeat) . $slices[$z];
120
$this->graph->img->text($slices[$z], $xBase + 20, ($yBase + 1));
122
$label = $names[$z] . ' (' . $percent . '%)';
123
if (strlen($label) > 20) {
124
$labels = explode("\n", wordwrap($label, 20));
125
foreach ($labels as $i => $label) {
129
$this->graph->img->text($label, $xBase + 35 + ($max * 4), ($yBase + 1));
132
$this->graph->img->text($label, $xBase + 35 + ($max * 4), ($yBase + 1));