width = (int) $width; $this->height = (int) $height; } public function setTitle($title) { $this->title = $title; return true; } public function toggleLegend() { $this->legend = !$this->legend; } public function setLabels($labels) { $this->labels = $labels; return true; } public function add(SVGPlot_Data $data) { $this->data[] = $data; return true; } public function display() { header('Content-Type: image/svg+xml'); echo $this->output(); } public function output() { $out = '' . PHP_EOL; $out.= '' . PHP_EOL; $out.= '' . PHP_EOL; if ($this->title) { $out .= ''.$this->title.'' . PHP_EOL; $out .= ''.$this->title.'' . PHP_EOL; } $out .= $this->_renderLinegraph(); 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->title) { $out .= ''.$row->title.'' . PHP_EOL; $out .= ''.$row->title.'' . PHP_EOL; } $y += ($this->height * 0.07); } } $out .= ''; return $out; } protected function _renderLinegraph() { $out = ''; if (empty($this->data)) { return $out; } // Figure out the maximum Y-axis value $max_value = 0; $nb_elements = 0; foreach ($this->data as $row) { if ($max_value == 0) { $nb_elements = count($row->get()); } $max = max($row->get()); if ($max > $max_value) { $max_value = $max; } } if ($nb_elements < 1) { return $out; } $divide = round($max_value / ($this->height * 0.8), 2) ?: 1; $y_axis_val = ceil(abs($max_value) / ($this->height * 0.8)) * 50; $space = round(($this->width - ($this->width * 0.1)) / $nb_elements, 2); for ($i = 0; $i < 10; $i++) { if (($y_axis_val * $i) <= $max_value) { $line_y = ($this->height * 0.93) - (($y_axis_val / $divide) * $i); $out .= '' . PHP_EOL; $out .= ''.($y_axis_val * $i).'' . PHP_EOL; } } // X-axis lines $y = $this->height - ($this->height * 0.07); $x = $this->width * 0.1; $i = 0; foreach ($this->data[0]->get() as $k=>$v) { if ($x >= $this->width) break; $out .= '' . PHP_EOL; $x += $space + $this->data[0]->width; } // labels for x axis $y = $this->height - ($this->height * 0.07); $i = 0; $step = round($nb_elements / 5); for ($i = 0; $i <= $nb_elements; $i += $step) { //echo $x = ($i * ($space + $this->data[0]->width)) + ($this->width * 0.1); if ($x >= $this->width) break; if (isset($this->labels[$i])) { $out .= '' . ($this->labels[$i]).'' . PHP_EOL; } } $y = ($this->height * 0.1); $w = $this->width - ($this->width * 0.1); $h = $this->height - ($this->height * 0.17); foreach ($this->data as $row) { $out .= '' . PHP_EOL; } return $out; } } class SVGPlot_Data { public $color = 'blue'; public $width = '10'; public $title = null; protected $data = array(); public function __construct($data) { if (is_array($data)) $this->data = $data; elseif (!is_object($data)) $this->append($data); } public function append($data) { $this->data[] = $data; return true; } public function get() { return $this->data; } } ?>