5 protected $width = null;
6 protected $height = null;
7 protected $data = array();
8 protected $title = null;
9 protected $legend = true;
11 public function __construct($width = 600, $height = 400)
13 $this->width
= (int) $width;
14 $this->height
= (int) $height;
17 public function add(SVGPie_Data
$data)
19 $this->data
[] = $data;
23 public function setTitle($title)
25 $this->title
= $title;
29 public function toggleLegend()
31 $this->legend
= !$this->legend
;
34 public function display()
36 header('Content-Type: image/svg+xml');
40 public function output()
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
;
46 $circle_size = min($this->width
, $this->height
);
47 $cx = $circle_size / 2;
48 $cy = $this->height
/ 2;
50 $radius = $circle_size / 2;
52 if (count($this->data
) == 1)
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\" />";
65 foreach ($this->data
as $row)
70 foreach ($this->data
as $row)
72 $row->angle
= ceil(360 * $row->data
/ $sum);
74 $start_angle = $end_angle;
75 $end_angle = $start_angle +
$row->angle
;
77 $x1 = $cx +
$radius * cos(deg2rad($start_angle));
78 $y1 = $cy +
$radius * sin(deg2rad($start_angle));
80 $x2 = $cx +
$radius * cos(deg2rad($end_angle));
81 $y2 = $cy +
$radius * sin(deg2rad($end_angle));
83 $arc = $row->angle
> 180 ?
1 : 0;
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\" />";
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
;
102 $x = $this->width
- ($this->width
* 0.06);
103 $y = $this->height
* 0.1;
105 foreach ($this->data
as $row)
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
;
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
;
121 $y +
= ($this->height
* 0.05);
132 public $fill = 'blue';
134 public $label = null;
136 public function __construct($data, $label = null, $fill = 'blue')
140 $this->label
= $label;