ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChart.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "Services/Chart/classes/class.ilChartData.php";
5 include_once "Services/Chart/classes/class.ilChartLegend.php";
6 
14 class ilChart
15 {
16  protected $id; // [string]
17  protected $renderer; // [string]
18  protected $width; // [string]
19  protected $height; // [string]
20  protected $data; // [array]
21  protected $legend; // [ilChartLegend]
22  protected $shadow; // [int]
23  protected $colors; // [array]
24  protected $ticks; // [array]
25  protected $integer_axis; // [array]
26 
35  public function __construct($a_id, $a_width = 500, $a_height = 500, $a_renderer = "flot")
36  {
37  $this->id = $a_id;
38  $this->data = array();
39  $this->setXAxisToInteger(false);
40  $this->setYAxisToInteger(false);
41  $this->setSize($a_width, $a_height);
42  $this->setRenderer($a_renderer);
43  $this->setShadow(2);
44  }
45 
51  public function setRenderer($a_value)
52  {
53  if(in_array((string)$a_value, $this->getAllRenderers()))
54  {
55  $this->renderer = (string)$a_value;
56  }
57  }
58 
64  public function getAllRenderers()
65  {
66  return array("flot");
67  }
68 
75  public function setSize($a_x, $a_y)
76  {
77  $this->width = (int)$a_x;
78  $this->height = (int)$a_y;
79  }
80 
88  public function addData(ilChartData $a_series, $a_idx = null)
89  {
90  if($a_idx === null)
91  {
92  $a_idx = sizeof($this->data);
93  }
94  $this->data[$a_idx] = $a_series;
95  return $a_idx;
96  }
97 
103  public function setLegend(ilChartLegend $a_legend)
104  {
105  $this->legend = $a_legend;
106  }
107 
114  public static function isValidColor($a_value)
115  {
116  if(preg_match("/^#[0-9a-f]{3}$/i", $a_value, $match))
117  {
118  return true;
119  }
120  else if(preg_match("/^#[0-9a-f]{6}$/i", $a_value, $match))
121  {
122  return true;
123  }
124  }
125 
133  protected static function renderColor($a_value, $a_opacity = 1)
134  {
135  if(self::isValidColor($a_value))
136  {
137  if(strlen($a_value) == 4)
138  {
139  return "\"rgba(".hexdec($a_value[1].$a_value[1]).", ".
140  hexdec($a_value[2].$a_value[2]).", ".
141  hexdec($a_value[3].$a_value[3]).", ".$a_opacity.")\"";
142  }
143  else
144  {
145  return "\"rgba(".hexdec($a_value[1].$a_value[2]).", ".
146  hexdec($a_value[3].$a_value[4]).", ".
147  hexdec($a_value[5].$a_value[6]).", ".$a_opacity.")\"";
148  }
149  }
150  }
151 
157  public function setShadow($a_value)
158  {
159  $this->shadow = (int)$a_value;
160  }
161 
167  public function getShadow()
168  {
169  return $this->shadow;
170  }
171 
177  public function setColors($a_values)
178  {
179  foreach($a_values as $color)
180  {
181  if(self::isValidColor($color))
182  {
183  $this->colors[] = $color;
184  }
185  }
186  }
187 
193  public function getColors()
194  {
195  return $this->colors;
196  }
197 
205  public function setTicks($a_x, $a_y, $a_labeled = false)
206  {
207  $this->ticks = array("x" => $a_x, "y" => $a_y, "labeled" => (bool)$a_labeled);
208  }
209 
215  public function getTicks()
216  {
217  return $this->ticks;
218  }
219 
223  public function getHTML()
224  {
225  global $tpl;
226 
227  include_once "Services/jQuery/classes/class.iljQueryUtil.php";
229 
230  $tpl->addJavascript("Services/Chart/js/flot/excanvas.min.js");
231  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.min.js");
232  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.pie.js");
233 
234  $chart = new ilTemplate("tpl.grid.html", true, true, "Services/Chart");
235  $chart->setVariable("ID", $this->id);
236  $chart->setVariable("WIDTH", $this->width);
237  $chart->setVariable("HEIGHT", $this->height);
238 
239  $last = array_keys($this->data);
240  $last = array_pop($last);
241  $has_pie = false;
242  foreach($this->data as $idx => $series)
243  {
244  $chart->setCurrentBlock("series");
245  $chart->setVariable("SERIES_LABEL", str_replace("\"", "\\\"", $series->getLabel()));
246  $chart->setVariable("SERIES_TYPE", $series->getType());
247 
248  $type = $series->getType();
249 
250  $points = array();
251  if($type != "pie")
252  {
253  foreach($series->getData() as $point)
254  {
255  $points[] = "[".$point[0].",".$point[1]."]";
256  }
257  $chart->setVariable("SERIES_DATA", "[ ".implode(",", $points)." ]");
258  }
259  else
260  {
261  $has_pie = true;
262  $chart->setVariable("SERIES_DATA", array_pop($series->getData()));
263  }
264  if($idx != $last)
265  {
266  $chart->setVariable("SERIES_END", ",");
267  }
268 
269  $options = array("show: ".($series->isHidden() ? "false" : "true"));
270  if($type != "points")
271  {
272  $width = $series->getLineWidth();
273  if($width !== null)
274  {
275  $options[] = "lineWidth:".$width;
276  }
277  if($type == "bars")
278  {
279  $bar_options = $series->getBarOptions();
280  if($bar_options["width"] !== null)
281  {
282  $options[] = "barWidth:".str_replace(",", ".", $bar_options["width"]);
283  $options[] = "align: \"".$bar_options["align"]."\"";
284  if($bar_options["horizontal"])
285  {
286  $options[] = "horizontal: true";
287  }
288  }
289  }
290  else if($type == "lines")
291  {
292  if($series->getLineSteps())
293  {
294  $options[] = "steps: true";
295  }
296  }
297  }
298  else
299  {
300  $radius = $series->getPointRadius();
301  if($radius !== null)
302  {
303  $options[] = "radius:".$radius;
304  }
305  }
306  $fill = $series->getFill();
307  if($fill["fill"])
308  {
309  $options[] = "fill: ".$fill["fill"];
310  if($fill["color"])
311  {
312  $options[] = "fillColor: ".self::renderColor($fill["color"], $fill["fill"]);
313  }
314  }
315  $chart->setVariable("SERIES_OPTIONS", implode(", ", $options));
316 
317  $chart->parseCurrentBlock();
318  }
319 
320 
321  // global options
322 
323  $chart->setVariable("SHADOW", (int)$this->getShadow());
324  $chart->setVariable("IS_PIE", ($has_pie ? "true" : "false"));
325 
326  $colors = $this->getColors();
327  if($colors)
328  {
329  $tmp = array();
330  foreach($colors as $color)
331  {
332  $tmp[] = self::renderColor($color);
333  }
334  }
335  if(sizeof($tmp))
336  {
337  $chart->setVariable("COLORS", implode(",", $tmp));
338  }
339 
340  // legend
341  if(!$this->legend)
342  {
343  $chart->setVariable("LEGEND", "show: false");
344  }
345  else
346  {
347  $margin = $this->legend->getMargin();
348  $legend = array();
349  $legend[] = "show: true";
350  $legend[] = "noColumns: ".$this->legend->getColumns();
351  $legend[] = "position: \"".$this->legend->getPosition()."\"";
352  $legend[] = "margin: [".$margin["x"].", ".$margin["y"]."]";
353  $legend[] = "backgroundColor: ".self::renderColor($this->legend->getBackground());
354  $legend[] = "backgroundOpacity: ".str_replace(",",".",$this->legend->getOpacity());
355  $legend[] = "labelBoxBorderColor: ".self::renderColor($this->legend->getLabelBorder());
356 
357  $chart->setVariable("LEGEND", implode(", ", $legend));
358  }
359 
360  // axis/ticks
361  $tmp = array();
362  $ticks = $this->getTicks();
363  if($ticks)
364  {
365  foreach($ticks as $axis => $def)
366  {
367  if(is_numeric($def))
368  {
369  $tmp[$axis] = $axis."axis: { ticks: ".$def." }";
370  }
371  else if(is_array($def))
372  {
373  $ttmp = array();
374  foreach($def as $idx => $value)
375  {
376  if($ticks["labeled"])
377  {
378  $ttmp[] = "[".$idx.", \"".$value."\"]";
379  }
380  else
381  {
382  $ttmp[] = $value;
383  }
384  }
385  $tmp[$axis] = $axis."axis: { ticks: [".implode(", ", $ttmp)."] }";
386  }
387  }
388  }
389 
390  // optional: remove decimals
391  if(!isset($tmp["x"]) && $this->integer_axis["x"])
392  {
393  $tmp["x"] = "xaxis: { tickDecimals: 0 }";
394  }
395  if(!isset($tmp["y"]) && $this->integer_axis["y"])
396  {
397  $tmp["y"] = "yaxis: { tickDecimals: 0 }";
398  }
399 
400  if(sizeof($tmp))
401  {
402  $chart->setVariable("AXIS", ",".implode(", ", $tmp));
403  }
404 
405  return $chart->get();
406  }
407 
408  function setYAxisToInteger($a_status)
409  {
410  $this->integer_axis["y"] = (bool)$a_status;
411  }
412 
413  function setXAxisToInteger($a_status)
414  {
415  $this->integer_axis["x"] = (bool)$a_status;
416  }
417 
418  /*
419  ilChart
420  ->setColors
421  ->setHover() [tooltip?]
422  [->setClick]
423  ->setZooming
424  ->setPanning
425  ->setTooltip
426  ->addData[Series]
427  - labels
428  - type
429  - pie: nur 1x
430  - bar, lines, points, steps, stacked?
431  - highlight?!
432  => min/max, transmission type (google api)
433 
434 
435  grid-based
436  ->setGrid
437  ->setTicks(color, size, decimals, length)
438  ->addAxisX (multiple, Zero) [id, mode, position, color, label]
439  ->addAxisY (multiple?) [id, mode, position, color, label]
440  ->setBackgroundFill(opacity, color/gradient)
441  ->setThreshold(int/float)
442  ->setToggles(bool)
443 
444  pie
445  ->setCollapse(int/float, color, label)
446  ->setRotation(int angle?)
447  ->setRadius(inner, outer)
448  */
449 }
450 
451 ?>