init
[garradin.git] / include / libs / svgplot / lib.svgpie.php
1 <?php
2
3 class SVGPie
4 {
5 protected $width = null;
6 protected $height = null;
7 protected $data = array();
8 protected $title = null;
9 protected $legend = true;
10
11 public function __construct($width = 600, $height = 400)
12 {
13 $this->width = (int) $width;
14 $this->height = (int) $height;
15 }
16
17 public function add(SVGPie_Data $data)
18 {
19 $this->data[] = $data;
20 return true;
21 }
22
23 public function setTitle($title)
24 {
25 $this->title = $title;
26 return true;
27 }
28
29 public function toggleLegend()
30 {
31 $this->legend = !$this->legend;
32 }
33
34 public function display()
35 {
36 header('Content-Type: image/svg+xml');
37 echo $this->output();
38 }
39
40 public function output()
41 {
42 $out = '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . PHP_EOL;
43 $out.= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd">' . PHP_EOL;
44 $out.= '<svg width="'.$this->width.'" height="'.$this->height.'" viewBox="0 0 '.$this->width.' '.$this->height.'" xmlns="http://www.w3.org/2000/svg" version="1.1">' . PHP_EOL;
45
46 $circle_size = min($this->width, $this->height);
47 $cx = $circle_size / 2;
48 $cy = $this->height / 2;
49 $circle_size *= 0.98;
50 $radius = $circle_size / 2;
51
52 if (count($this->data) == 1)
53 {
54 $row = current($this->data);
55 $out .= "<circle cx=\"{$cx}\" cy=\"{$cy}\" r=\"{$radius}\" fill=\"{$row->fill}\" "
56 . "stroke=\"white\" stroke-width=\"".($circle_size * 0.005)."\" stroke-linecap=\"round\" "
57 . "stroke-linejoin=\"round\" />";
58 }
59 else
60 {
61 $sum = 0;
62 $start_angle = 0;
63 $end_angle = 0;
64
65 foreach ($this->data as $row)
66 {
67 $sum += $row->data;
68 }
69
70 foreach ($this->data as $row)
71 {
72 $row->angle = ceil(360 * $row->data / $sum);
73
74 $start_angle = $end_angle;
75 $end_angle = $start_angle + $row->angle;
76
77 $x1 = $cx + $radius * cos(deg2rad($start_angle));
78 $y1 = $cy + $radius * sin(deg2rad($start_angle));
79
80 $x2 = $cx + $radius * cos(deg2rad($end_angle));
81 $y2 = $cy + $radius * sin(deg2rad($end_angle));
82
83 $arc = $row->angle > 180 ? 1 : 0;
84
85 $out .= "<path d=\"M{$cx},{$cy} L{$x1},{$y1} A{$radius},{$radius} 0 {$arc},1 {$x2},{$y2} Z\"
86 fill=\"{$row->fill}\" stroke=\"white\" stroke-width=\"".($circle_size * 0.005)."\" stroke-linecap=\"round\"
87 stroke-linejoin=\"round\" />";
88 }
89 }
90
91 if ($this->title)
92 {
93 $out .= '<text x="'.($this->width * 0.98).'" y="'.($this->height * 0.07).'" font-size="'.($this->height * 0.05).'" fill="white" '
94 . 'stroke="white" stroke-width="'.($this->height * 0.01).'" stroke-linejoin="round" stroke-linecap="round" '
95 . 'text-anchor="end" style="font-family: Verdana, Arial, sans-serif; font-weight: bold;">'.$this->title.'</text>' . PHP_EOL;
96 $out .= '<text x="'.($this->width * 0.98).'" y="'.($this->height * 0.07).'" font-size="'.($this->height * 0.05).'" fill="black" '
97 . 'text-anchor="end" style="font-family: Verdana, Arial, sans-serif; font-weight: bold;">'.$this->title.'</text>' . PHP_EOL;
98 }
99
100 if ($this->legend)
101 {
102 $x = $this->width - ($this->width * 0.06);
103 $y = $this->height * 0.1;
104
105 foreach ($this->data as $row)
106 {
107 $out .= '<rect x="'.$x.'" y="'.($y - $this->height * 0.01).'" width="'.($this->width * 0.04).'" height="'.($this->height * 0.04).'" fill="'.$row->fill.'" stroke="black" stroke-width="1" rx="2" />' . PHP_EOL;
108
109 if ($row->label)
110 {
111 $out .= '<text x="'.($x-($this->width * 0.02)).'" y="'.($y+($this->height * 0.025)).'" '
112 . 'font-size="'.($this->height * 0.05).'" fill="white" stroke="white" '
113 . 'stroke-width="'.($this->height * 0.01).'" stroke-linejoin="round" '
114 . 'stroke-linecap="round" text-anchor="end" style="font-family: Verdana, '
115 . 'Arial, sans-serif;">'.$row->label.'</text>' . PHP_EOL;
116 $out .= '<text x="'.($x-($this->width * 0.02)).'" y="'.($y+($this->height * 0.025)).'" '
117 . 'font-size="'.($this->height * 0.05).'" fill="black" text-anchor="end" '
118 . 'style="font-family: Verdana, Arial, sans-serif;">'.$row->label.'</text>' . PHP_EOL;
119 }
120
121 $y += ($this->height * 0.05);
122 }
123 }
124
125 $out .= '</svg>';
126 return $out;
127 }
128 }
129
130 class SVGPie_Data
131 {
132 public $fill = 'blue';
133 public $data = 0.0;
134 public $label = null;
135
136 public function __construct($data, $label = null, $fill = 'blue')
137 {
138 $this->data = $data;
139 $this->fill = $fill;
140 $this->label = $label;
141 }
142 }
143
144 ?>