ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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.ilChartLegend.php";
5 
13 abstract class ilChart
14 {
18  protected $tpl;
19 
20  protected $id; // [string]
21  protected $width; // [string]
22  protected $height; // [string]
23  protected $data; // [array]
24  protected $legend; // [ilChartLegend]
25  protected $shadow; // [int]
26  protected $colors; // [array]
27  protected $auto_resize; // [bool]
28  protected $stacked; // [bool]
29 
30  const TYPE_GRID = 1;
31  const TYPE_PIE = 2;
32  const TYPE_SPIDER = 3;
33 
39  protected function __construct($a_id)
40  {
41  global $DIC;
42 
43  $this->tpl = $DIC["tpl"];
44  $this->id = $a_id;
45  $this->data = array();
46 
47  $this->setShadow(2);
48  }
49 
57  public static function getInstanceByType($a_type, $a_id)
58  {
59  switch ($a_type) {
60  case self::TYPE_GRID:
61  include_once "Services/Chart/classes/class.ilChartGrid.php";
62  return new ilChartGrid($a_id);
63 
64  case self::TYPE_PIE:
65  include_once "Services/Chart/classes/class.ilChartPie.php";
66  return new ilChartPie($a_id);
67 
68  case self::TYPE_SPIDER:
69  include_once "Services/Chart/classes/class.ilChartSpider.php";
70  return new ilChartSpider($a_id);
71  }
72  }
73 
79  abstract public function getDataInstance($a_type = null);
80 
86  abstract protected function isValidDataType(ilChartData $a_series);
87 
93  protected function isValid()
94  {
95  if (sizeof($this->data)) {
96  return true;
97  }
98  return false;
99  }
100 
107  public function setSize($a_x, $a_y)
108  {
109  $this->width = $a_x;
110  $this->height = $a_y;
111  }
112 
120  public function addData(ilChartData $a_series, $a_idx = null)
121  {
122  if ($this->isValidDataType($a_series)) {
123  if ($a_idx === null) {
124  $a_idx = sizeof($this->data);
125  }
126  $this->data[$a_idx] = $a_series;
127  return $a_idx;
128  }
129  }
130 
136  public function setLegend(ilChartLegend $a_legend)
137  {
138  $this->legend = $a_legend;
139  }
140 
146  public function setColors($a_values)
147  {
148  foreach ($a_values as $color) {
149  if (self::isValidColor($color)) {
150  $this->colors[] = $color;
151  }
152  }
153  }
154 
160  public function getColors()
161  {
162  return $this->colors;
163  }
164 
171  public static function isValidColor($a_value)
172  {
173  if (preg_match("/^#[0-9a-f]{3}$/i", $a_value, $match)) {
174  return true;
175  } elseif (preg_match("/^#[0-9a-f]{6}$/i", $a_value, $match)) {
176  return true;
177  }
178  }
179 
187  public static function renderColor($a_value, $a_opacity = 1)
188  {
189  if (self::isValidColor($a_value)) {
190  if (strlen($a_value) == 4) {
191  return "rgba(" . hexdec($a_value[1] . $a_value[1]) . ", " .
192  hexdec($a_value[2] . $a_value[2]) . ", " .
193  hexdec($a_value[3] . $a_value[3]) . ", " . $a_opacity . ")";
194  } else {
195  return "rgba(" . hexdec($a_value[1] . $a_value[2]) . ", " .
196  hexdec($a_value[3] . $a_value[4]) . ", " .
197  hexdec($a_value[5] . $a_value[6]) . ", " . $a_opacity . ")";
198  }
199  }
200  }
201 
207  public function setShadow($a_value)
208  {
209  $this->shadow = (int) $a_value;
210  }
211 
217  public function getShadow()
218  {
219  return $this->shadow;
220  }
221 
227  public function setAutoResize($a_value)
228  {
229  $this->auto_resize = (bool) $a_value;
230  }
231 
237  public function setStacked($a_value)
238  {
239  $this->stacked = (bool) $a_value;
240  }
241 
245  protected function initJS()
246  {
247  $tpl = $this->tpl;
248 
249  include_once "Services/jQuery/classes/class.iljQueryUtil.php";
251 
252  $tpl->addJavascript("Services/Chart/js/flot/excanvas.min.js");
253  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.min.js");
254 
255  if ((bool) $this->auto_resize) {
256  // #13108
257  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.resize.min.js");
258  }
259 
260  if ((bool) $this->stacked) {
261  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.stack.min.js");
262  }
263 
264  $this->addCustomJS();
265  }
266 
270  protected function addCustomJS()
271  {
272  }
273 
279  public function parseGlobalOptions(stdClass $a_options)
280  {
281  }
282 
286  public function getHTML()
287  {
288  if (!$this->isValid()) {
289  return;
290  }
291 
292  $this->initJS();
293 
294  $chart = new ilTemplate("tpl.grid.html", true, true, "Services/Chart");
295  $chart->setVariable("ID", $this->id);
296 
297  if ($this->width) {
298  if (is_numeric($this->width)) {
299  $chart->setVariable("WIDTH", "width:" . $this->width . "px;");
300  } else {
301  $chart->setVariable("WIDTH", "width:" . $this->width . ";");
302  }
303  }
304  if ($this->height) {
305  if (is_numeric($this->height)) {
306  $chart->setVariable("HEIGHT", "height:" . $this->height . "px;");
307  } else {
308  $chart->setVariable("HEIGHT", "height:" . $this->height . ";");
309  }
310  }
311 
312 
313  // (series) data
314 
315  $json_series = array();
316  foreach ($this->data as $series) {
317  $series->parseData($json_series);
318  }
319  $chart->setVariable("SERIES", json_encode($json_series));
320 
321 
322  // global options
323 
324  $json_options = new stdClass();
325  $json_options->series = new stdClass();
326  $json_options->series->shadowSize = (int) $this->getShadow();
327  $json_options->series->lines = new stdClass();
328  $json_options->series->lines->show = false;
329  $json_options->series->stack = (bool) $this->stacked;
330 
331  foreach ($this->data as $series) {
332  $series->parseGlobalOptions($json_options, $this);
333  }
334 
335  $this->parseGlobalOptions($json_options);
336 
337  $colors = $this->getColors();
338  if ($colors) {
339  $json_options->colors = array();
340  foreach ($colors as $color) {
341  $json_options->colors[] = self::renderColor($color);
342  }
343  }
344 
345  // legend
346  $json_options->legend = new stdClass();
347  if (!$this->legend) {
348  $json_options->legend->show = false;
349  } else {
350  $this->legend->parseOptions($json_options->legend);
351  }
352 
353  $chart->setVariable("OPTIONS", json_encode($json_options));
354 
355  $ret = $chart->get();
356  return $ret;
357  }
358 
359  /*
360  ilChart
361  ->setColors
362  ->setHover() [tooltip?]
363  [->setClick]
364  ->setZooming
365  ->setPanning
366  ->setTooltip
367  ->addData[Series]
368  - labels
369  - type
370  - pie: nur 1x
371  - bar, lines, points, steps, stacked?
372  - highlight?!
373  => min/max, transmission type (google api)
374 
375 
376  grid-based
377  ->setGrid
378  ->setTicks(color, size, decimals, length)
379  ->addAxisX (multiple, Zero) [id, mode, position, color, label]
380  ->addAxisY (multiple?) [id, mode, position, color, label]
381  ->setBackgroundFill(opacity, color/gradient)
382  ->setThreshold(int/float)
383  ->setToggles(bool)
384 
385  pie
386  ->setCollapse(int/float, color, label)
387  ->setRotation(int angle?)
388  ->setRadius(inner, outer)
389  */
390 }
const TYPE_SPIDER
setSize($a_x, $a_y)
Set chart size.
setStacked($a_value)
Toggle stacking.
global $DIC
Definition: saml.php:7
Chart legend.
parseGlobalOptions(stdClass $a_options)
Convert (global) properties to flot config.
addCustomJS()
Add type-specific JS script.
Generator for spider charts.
getHTML()
Render.
__construct($a_id)
Constructor.
setShadow($a_value)
Set shadow.
isValidDataType(ilChartData $a_series)
Validate data series.
Abstract Chart generator base class.
const TYPE_PIE
setLegend(ilChartLegend $a_legend)
Set chart legend.
$a_type
Definition: workflow.php:92
initJS()
Init JS script files.
isValid()
Basic validation.
setAutoResize($a_value)
Toggle auto-resizing on window resize/redraw.
static isValidColor($a_value)
Validate html color code.
special template class to simplify handling of ITX/PEAR
getColors()
Get colors.
Generator for pie charts.
const TYPE_GRID
$this data['403_header']
static renderColor($a_value, $a_opacity=1)
Render html color code.
Generator for grid-based charts.
$ret
Definition: parser.php:6
setColors($a_values)
Set colors.
static initjQuery($a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
Abstract chart data series base class.
getDataInstance($a_type=null)
Get data series instance.
addData(ilChartData $a_series, $a_idx=null)
Add data series.
getShadow()
Get shadow.
static getInstanceByType($a_type, $a_id)
Get type instance.