5 protected $width = null;
6 protected $height = null;
7 protected $data = array();
8 protected $title = null;
9 protected $labels = array();
10 protected $legend = true;
12 public function __construct($width = 600, $height = 400)
14 $this->width
= (int) $width;
15 $this->height
= (int) $height;
18 public function setTitle($title)
20 $this->title
= $title;
24 public function toggleLegend()
26 $this->legend
= !$this->legend
;
29 public function setLabels($labels)
31 $this->labels
= $labels;
35 public function add(SVGPlot_Data
$data)
37 $this->data
[] = $data;
41 public function display()
43 header('Content-Type: image/svg+xml');
47 public function output()
49 $out = '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . PHP_EOL
;
50 $out.= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd">' . PHP_EOL
;
51 $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
;
55 $out .= '<text x="'.round($this->width
/2).'" y="'.($this->height
* 0.07).'" font-size="'.($this->height
* 0.05).'" fill="white" '
56 . 'stroke="white" stroke-width="'.($this->height
* 0.01).'" stroke-linejoin="round" stroke-linecap="round" '
57 . 'text-anchor="middle" style="font-family: Verdana, Arial, sans-serif; font-weight: bold;">'.$this->title
.'</text>' . PHP_EOL
;
58 $out .= '<text x="'.round($this->width
/2).'" y="'.($this->height
* 0.07).'" font-size="'.($this->height
* 0.05).'" fill="black" '
59 . 'text-anchor="middle" style="font-family: Verdana, Arial, sans-serif; font-weight: bold;">'.$this->title
.'</text>' . PHP_EOL
;
62 $out .= $this->_renderLinegraph();
66 $x = $this->width
- ($this->width
* 0.06);
67 $y = $this->height
* 0.1;
69 foreach ($this->data
as $row)
71 $out .= '<rect x="'.$x.'" y="'.($y - $this->height
* 0.01).'" width="'.($this->width
* 0.04).'" height="'.($this->height
* 0.04).'" fill="'.$row->color
.'" stroke="black" stroke-width="1" rx="2" />' . PHP_EOL
;
75 $out .= '<text x="'.($x-($this->width
* 0.02)).'" y="'.($y+
($this->height
* 0.025)).'" '
76 . 'font-size="'.($this->height
* 0.05).'" fill="white" stroke="white" '
77 . 'stroke-width="'.($this->height
* 0.01).'" stroke-linejoin="round" '
78 . 'stroke-linecap="round" text-anchor="end" style="font-family: Verdana, Arial, '
79 . 'sans-serif;">'.$row->title
.'</text>' . PHP_EOL
;
80 $out .= '<text x="'.($x-($this->width
* 0.02)).'" y="'.($y+
($this->height
* 0.025)).'" '
81 . 'font-size="'.($this->height
* 0.05).'" fill="black" text-anchor="end" '
82 . 'style="font-family: Verdana, Arial, sans-serif;">'.$row->title
.'</text>' . PHP_EOL
;
85 $y +
= ($this->height
* 0.07);
94 protected function _renderLinegraph()
98 if (empty($this->data
))
103 // Figure out the maximum Y-axis value
107 foreach ($this->data
as $row)
111 $nb_elements = count($row->get());
114 $max = max($row->get());
116 if ($max > $max_value)
122 if ($nb_elements < 1)
127 $divide = round($max_value / ($this->height
* 0.8), 2) ?
: 1;
128 $y_axis_val = ceil(abs($max_value) / ($this->height
* 0.8)) * 50;
129 $space = round(($this->width
- ($this->width
* 0.1)) / $nb_elements, 2);
131 for ($i = 0; $i < 10; $i++
)
133 if (($y_axis_val * $i) <= $max_value)
135 $line_y = ($this->height
* 0.93) - (($y_axis_val / $divide) * $i);
136 $out .= '<line x1="'.($this->width
* 0.1).'" y1="'.($line_y).'" x2="'.$this->width
.'" y2="'.($line_y).'" stroke-width="1" stroke="#ccc" />' . PHP_EOL
;
137 $out .= '<g><text x="'.($this->width
* 0.08).'" y="'.($line_y).'" font-size="'.($this->height
* 0.04).'" fill="gray" text-anchor="end" style="font-family: Verdana, Arial, sans-serif;">'.($y_axis_val * $i).'</text></g>' . PHP_EOL
;
142 $y = $this->height
- ($this->height
* 0.07);
143 $x = $this->width
* 0.1;
146 foreach ($this->data
[0]->get() as $k=>$v)
148 if ($x >= $this->width
)
151 $out .= '<line x1="'.$x.'" y1="'.($y).'" x2="'.$x.'" y2="'.($this->height
* 0.1).'" stroke-width="1" stroke="#ccc" />' . PHP_EOL
;
152 $x +
= $space +
$this->data
[0]->width
;
156 $y = $this->height
- ($this->height
* 0.07);
158 $step = round($nb_elements / 5);
160 for ($i = 0; $i <= $nb_elements; $i +
= $step)
163 $x = ($i * ($space +
$this->data
[0]->width
)) +
($this->width
* 0.1);
165 if ($x >= $this->width
)
168 if (isset($this->labels
[$i]))
170 $out .= '<g><text x="'.$x.'" y="'.($y+
($this->height
* 0.06)).'" '
171 . 'font-size="'.($this->height
* 0.04).'" fill="gray" text-anchor="middle" '
172 . 'style="font-family: Verdana, Arial, sans-serif;">'
173 . ($this->labels
[$i]).'</text></g>' . PHP_EOL
;
177 $y = ($this->height
* 0.1);
178 $w = $this->width
- ($this->width
* 0.1);
179 $h = $this->height
- ($this->height
* 0.17);
181 foreach ($this->data
as $row)
183 $out .= '<polyline fill="none" stroke="'.$row->color
.'" stroke-width="'.$row->width
.'" '
184 .'stroke-linecap="round" points="';
186 $x = ($this->width
* 0.1);
188 foreach ($row->get() as $k=>$v)
190 $_y = $y +
($h - round($v / $divide, 2)) +
round($row->width
/ 2);
191 $out.= $x.','.$_y.' ';
192 $x +
= $space +
$row->width
;
195 $out .= '" />' . PHP_EOL
;
204 public $color = 'blue';
205 public $width = '10';
206 public $title = null;
207 protected $data = array();
209 public function __construct($data)
213 elseif (!is_object($data))
214 $this->append($data);
217 public function append($data)
219 $this->data
[] = $data;
223 public function get()