~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/Graph/Chart/pie.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Pie graph implementation for the Horde_Graph package.
 
4
 *
 
5
 * $Horde: framework/Graph/Graph/Chart/pie.php,v 1.10.10.1 2005/01/03 12:19:00 jan Exp $
 
6
 *
 
7
 * Copyright 2002-2005 Chuck Hagenbuch <chuck@horde.org>
 
8
 *
 
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.
 
11
 *
 
12
 * @author  Chuck Hagenbuch <chuck@horde.org>
 
13
 * @version $Revision: 1.10.10.1 $
 
14
 * @since   Horde 3.0
 
15
 * @package Horde_Graph
 
16
 */
 
17
class Horde_Graph_Chart_pie {
 
18
 
 
19
    var $graph;
 
20
 
 
21
    var $_dataset;
 
22
    var $_padding = 20;
 
23
    var $_outline = true;
 
24
    var $_colors = array('tan', 'palegoldenrod', 'olivedrab', 'blue', 'red', 'green', 'yellow',
 
25
                         'orange', 'gray', 'purple');
 
26
    /**
 
27
      "255,153,0",
 
28
      "0,204,153",
 
29
      "204,255,102",
 
30
      "255,102,102",
 
31
      "102,204,255",
 
32
      "204,153,255",
 
33
      "255,0,0",
 
34
      "51,0,255",
 
35
      "255,51,153",
 
36
      "204,0,255",
 
37
      "255,255,51",
 
38
      "51,255,51",
 
39
      "255,102,0");
 
40
    */
 
41
 
 
42
    function Horde_Graph_Chart_pie(&$graph, $params)
 
43
    {
 
44
        $this->graph = &$graph;
 
45
 
 
46
        foreach ($params as $param => $value) {
 
47
            $key = '_' . $param;
 
48
            $this->$key = $value;
 
49
        }
 
50
    }
 
51
 
 
52
    function draw()
 
53
    {
 
54
        $data = $this->graph->_data['y'][$this->_dataset];
 
55
 
 
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);
 
62
 
 
63
        // Calculate the sum of all slices.
 
64
        $sum = 0;
 
65
        foreach ($data as $x) {
 
66
            $sum += $x;
 
67
        }
 
68
 
 
69
        // Convert each slice into the corresponding percentage of a
 
70
        // 360-degree circle.
 
71
        $degCount = 0;
 
72
        $slices = array();
 
73
        $degrees = array();
 
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] : '';
 
79
                $degCount++;
 
80
            }
 
81
        }
 
82
 
 
83
        // Draw the baseline.
 
84
        if ($count > 1) {
 
85
            $last_angle = 0;
 
86
            $count = count($degrees);
 
87
            for ($z = 0; $z < $count; $z++) {
 
88
                // Calculate and draw arcs corresponding to each
 
89
                // slice.
 
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];
 
94
            }
 
95
        } else {
 
96
            $this->graph->img->circle($xcenter, $ycenter, $radius, 'black', $this->_colors[0]);
 
97
        }
 
98
 
 
99
        // Create the color key and slice labels.
 
100
        $yBase = $this->graph->_graphTop;
 
101
        $xBase = 5;
 
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);
 
107
            $yBase += 15;
 
108
 
 
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';
 
113
            }
 
114
            $repeat = $max - strlen($slices[$z]);
 
115
            if ($repeat < 0) {
 
116
                $repeat = 0;
 
117
            }
 
118
            $slices[$z] = str_repeat(' ', $repeat) . $slices[$z];
 
119
 
 
120
            $this->graph->img->text($slices[$z], $xBase + 20, ($yBase + 1));
 
121
 
 
122
            $label = $names[$z] . ' (' . $percent . '%)';
 
123
            if (strlen($label) > 20) {
 
124
                $labels = explode("\n", wordwrap($label, 20));
 
125
                foreach ($labels as $i => $label) {
 
126
                    if ($i > 0) {
 
127
                        $yBase += 15;
 
128
                    }
 
129
                    $this->graph->img->text($label, $xBase + 35 + ($max * 4), ($yBase + 1));
 
130
                }
 
131
            } else {
 
132
                $this->graph->img->text($label, $xBase + 35 + ($max * 4), ($yBase + 1));
 
133
            }
 
134
        }
 
135
    }
 
136
 
 
137
}