ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 {
15  protected $id; // [string]
16  protected $width; // [string]
17  protected $height; // [string]
18  protected $data; // [array]
19  protected $legend; // [ilChartLegend]
20  protected $shadow; // [int]
21  protected $colors; // [array]
22  protected $auto_resize; // [bool]
23  protected $stacked; // [bool]
24 
25  const TYPE_GRID = 1;
26  const TYPE_PIE = 2;
27  const TYPE_SPIDER = 3;
28 
34  protected function __construct($a_id)
35  {
36  $this->id = $a_id;
37  $this->data = array();
38 
39  $this->setShadow(2);
40  }
41 
49  public static function getInstanceByType($a_type, $a_id)
50  {
51  switch($a_type)
52  {
53  case self::TYPE_GRID:
54  include_once "Services/Chart/classes/class.ilChartGrid.php";
55  return new ilChartGrid($a_id);
56 
57  case self::TYPE_PIE:
58  include_once "Services/Chart/classes/class.ilChartPie.php";
59  return new ilChartPie($a_id);
60 
61  case self::TYPE_SPIDER:
62  include_once "Services/Chart/classes/class.ilChartSpider.php";
63  return new ilChartSpider($a_id);
64  }
65  }
66 
72  abstract public function getDataInstance($a_type = null);
73 
79  abstract protected function isValidDataType(ilChartData $a_series);
80 
86  protected function isValid()
87  {
88  if(sizeof($this->data))
89  {
90  return true;
91  }
92  return false;
93  }
94 
101  public function setSize($a_x, $a_y)
102  {
103  $this->width = $a_x;
104  $this->height = $a_y;
105  }
106 
114  public function addData(ilChartData $a_series, $a_idx = null)
115  {
116  if($this->isValidDataType($a_series))
117  {
118  if($a_idx === null)
119  {
120  $a_idx = sizeof($this->data);
121  }
122  $this->data[$a_idx] = $a_series;
123  return $a_idx;
124  }
125  }
126 
132  public function setLegend(ilChartLegend $a_legend)
133  {
134  $this->legend = $a_legend;
135  }
136 
142  public function setColors($a_values)
143  {
144  foreach($a_values as $color)
145  {
146  if(self::isValidColor($color))
147  {
148  $this->colors[] = $color;
149  }
150  }
151  }
152 
158  public function getColors()
159  {
160  return $this->colors;
161  }
162 
169  public static function isValidColor($a_value)
170  {
171  if(preg_match("/^#[0-9a-f]{3}$/i", $a_value, $match))
172  {
173  return true;
174  }
175  else if(preg_match("/^#[0-9a-f]{6}$/i", $a_value, $match))
176  {
177  return true;
178  }
179  }
180 
188  public static function renderColor($a_value, $a_opacity = 1)
189  {
190  if(self::isValidColor($a_value))
191  {
192  if(strlen($a_value) == 4)
193  {
194  return "rgba(".hexdec($a_value[1].$a_value[1]).", ".
195  hexdec($a_value[2].$a_value[2]).", ".
196  hexdec($a_value[3].$a_value[3]).", ".$a_opacity.")";
197  }
198  else
199  {
200  return "rgba(".hexdec($a_value[1].$a_value[2]).", ".
201  hexdec($a_value[3].$a_value[4]).", ".
202  hexdec($a_value[5].$a_value[6]).", ".$a_opacity.")";
203  }
204  }
205  }
206 
212  public function setShadow($a_value)
213  {
214  $this->shadow = (int)$a_value;
215  }
216 
222  public function getShadow()
223  {
224  return $this->shadow;
225  }
226 
232  public function setAutoResize($a_value)
233  {
234  $this->auto_resize = (bool)$a_value;
235  }
236 
242  public function setStacked($a_value)
243  {
244  $this->stacked = (bool)$a_value;
245  }
246 
250  protected function initJS()
251  {
252  global $tpl;
253 
254  include_once "Services/jQuery/classes/class.iljQueryUtil.php";
256 
257  $tpl->addJavascript("Services/Chart/js/flot/excanvas.min.js");
258  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.min.js");
259 
260  if((bool)$this->auto_resize)
261  {
262  // #13108
263  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.resize.min.js");
264  }
265 
266  if((bool)$this->stacked)
267  {
268  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.stack.min.js");
269  }
270 
271  $this->addCustomJS();
272  }
273 
277  protected function addCustomJS()
278  {
279 
280  }
281 
287  public function parseGlobalOptions(stdClass $a_options)
288  {
289 
290  }
291 
295  public function getHTML()
296  {
297  if(!$this->isValid())
298  {
299  return;
300  }
301 
302  $this->initJS();
303 
304  $chart = new ilTemplate("tpl.grid.html", true, true, "Services/Chart");
305  $chart->setVariable("ID", $this->id);
306 
307  if($this->width)
308  {
309  if(is_numeric($this->width))
310  {
311  $chart->setVariable("WIDTH", "width:".$this->width."px;");
312  }
313  else
314  {
315  $chart->setVariable("WIDTH", "width:".$this->width.";");
316  }
317  }
318  if($this->height)
319  {
320  if(is_numeric($this->height))
321  {
322  $chart->setVariable("HEIGHT", "height:".$this->height."px;");
323  }
324  else
325  {
326  $chart->setVariable("HEIGHT", "height:".$this->height.";");
327  }
328  }
329 
330 
331  // (series) data
332 
333  $json_series = array();
334  foreach($this->data as $series)
335  {
336  $series->parseData($json_series);
337  }
338  $chart->setVariable("SERIES", json_encode($json_series));
339 
340 
341  // global options
342 
343  $json_options = new stdClass();
344  $json_options->series = new stdClass();
345  $json_options->series->shadowSize = (int)$this->getShadow();
346  $json_options->series->lines = new stdClass();
347  $json_options->series->lines->show = false;
348  $json_options->series->stack = (bool)$this->stacked;
349 
350  foreach($this->data as $series)
351  {
352  $series->parseGlobalOptions($json_options, $this);
353  }
354 
355  $this->parseGlobalOptions($json_options);
356 
357  $colors = $this->getColors();
358  if($colors)
359  {
360  $json_options->colors = array();
361  foreach($colors as $color)
362  {
363  $json_options->colors[] = self::renderColor($color);
364  }
365  }
366 
367  // legend
368  $json_options->legend = new stdClass();
369  if(!$this->legend)
370  {
371  $json_options->legend->show = false;
372  }
373  else
374  {
375  $this->legend->parseOptions($json_options->legend);
376  }
377 
378  $chart->setVariable("OPTIONS", json_encode($json_options));
379 
380  $ret = $chart->get();
381  return $ret;
382  }
383 
384  /*
385  ilChart
386  ->setColors
387  ->setHover() [tooltip?]
388  [->setClick]
389  ->setZooming
390  ->setPanning
391  ->setTooltip
392  ->addData[Series]
393  - labels
394  - type
395  - pie: nur 1x
396  - bar, lines, points, steps, stacked?
397  - highlight?!
398  => min/max, transmission type (google api)
399 
400 
401  grid-based
402  ->setGrid
403  ->setTicks(color, size, decimals, length)
404  ->addAxisX (multiple, Zero) [id, mode, position, color, label]
405  ->addAxisY (multiple?) [id, mode, position, color, label]
406  ->setBackgroundFill(opacity, color/gradient)
407  ->setThreshold(int/float)
408  ->setToggles(bool)
409 
410  pie
411  ->setCollapse(int/float, color, label)
412  ->setRotation(int angle?)
413  ->setRadius(inner, outer)
414  */
415 }
416 
417 ?>
Add some data
const TYPE_SPIDER
setSize($a_x, $a_y)
Set chart size.
setStacked($a_value)
Toggle stacking.
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.
global $tpl
Definition: ilias.php:8
$a_type
Definition: workflow.php:93
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.
Create styles array
The data for the language used.
const TYPE_GRID
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)
Init jQuery.
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.