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