width = (int) $width; $this->height = (int) $height; } public function add(SVGPie_Data $data) { $this->data[] = $data; return true; } public function setTitle($title) { $this->title = $title; return true; } public function toggleLegend() { $this->legend = !$this->legend; } public function display() { header('Content-Type: image/svg+xml'); echo $this->output(); } public function output() { $out = '' . PHP_EOL; $out.= '' . PHP_EOL; $out.= '' . PHP_EOL; $circle_size = min($this->width, $this->height); $cx = $circle_size / 2; $cy = $this->height / 2; $circle_size *= 0.98; $radius = $circle_size / 2; if (count($this->data) == 1) { $row = current($this->data); $out .= "fill}\" " . "stroke=\"white\" stroke-width=\"".($circle_size * 0.005)."\" stroke-linecap=\"round\" " . "stroke-linejoin=\"round\" />"; } else { $sum = 0; $start_angle = 0; $end_angle = 0; foreach ($this->data as $row) { $sum += $row->data; } foreach ($this->data as $row) { $row->angle = ceil(360 * $row->data / $sum); $start_angle = $end_angle; $end_angle = $start_angle + $row->angle; $x1 = $cx + $radius * cos(deg2rad($start_angle)); $y1 = $cy + $radius * sin(deg2rad($start_angle)); $x2 = $cx + $radius * cos(deg2rad($end_angle)); $y2 = $cy + $radius * sin(deg2rad($end_angle)); $arc = $row->angle > 180 ? 1 : 0; $out .= "fill}\" stroke=\"white\" stroke-width=\"".($circle_size * 0.005)."\" stroke-linecap=\"round\" stroke-linejoin=\"round\" />"; } } if ($this->title) { $out .= ''.$this->title.'' . PHP_EOL; $out .= ''.$this->title.'' . PHP_EOL; } if ($this->legend) { $x = $this->width - ($this->width * 0.06); $y = $this->height * 0.1; foreach ($this->data as $row) { $out .= '' . PHP_EOL; if ($row->label) { $out .= ''.$row->label.'' . PHP_EOL; $out .= ''.$row->label.'' . PHP_EOL; } $y += ($this->height * 0.05); } } $out .= ''; return $out; } } class SVGPie_Data { public $fill = 'blue'; public $data = 0.0; public $label = null; public function __construct($data, $label = null, $fill = 'blue') { $this->data = $data; $this->fill = $fill; $this->label = $label; } } ?>