ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
ilChart Class Reference

Chart generator. More...

+ Collaboration diagram for ilChart:

Public Member Functions

 __construct ($a_id, $a_width=500, $a_height=500, $a_renderer="flot")
 Constructor. More...
 
 setRenderer ($a_value)
 Set renderer. More...
 
 getAllRenderers ()
 Get all available renderers. More...
 
 setSize ($a_x, $a_y)
 Set chart size. More...
 
 addData (ilChartData $a_series, $a_idx=null)
 Add data series. More...
 
 setLegend (ilChartLegend $a_legend)
 Set chart legend. More...
 
 setShadow ($a_value)
 Set shadow. More...
 
 getShadow ()
 Get shadow. More...
 
 setColors ($a_values)
 Set colors. More...
 
 getColors ()
 Get colors. More...
 
 setTicks ($a_x, $a_y, $a_labeled=false)
 Set ticks. More...
 
 getTicks ()
 Get ticks. More...
 
 setLegLabels ($a_val)
 Set leg labels. More...
 
 getLegLabels ()
 Get leg labels. More...
 
 getHTML ()
 Render (flot only currently) More...
 
 setYAxisToInteger ($a_status)
 
 setXAxisToInteger ($a_status)
 
 setYAxisMax ($a_val)
 Set y axis max value. More...
 
 getYAxisMax ()
 Get y axis max value. More...
 

Static Public Member Functions

static isValidColor ($a_value)
 Validate html color code. More...
 

Static Protected Member Functions

static renderColor ($a_value, $a_opacity=1)
 Render html color code. More...
 

Protected Attributes

 $id
 
 $renderer
 
 $width
 
 $height
 
 $data
 
 $legend
 
 $shadow
 
 $colors
 
 $ticks
 
 $integer_axis
 
 $leg_labels = array()
 
 $y_max = 0
 

Detailed Description

Chart generator.

Author
Jörg Lützenkirchen luetz.nosp@m.enki.nosp@m.rchen.nosp@m.@lei.nosp@m.fos.c.nosp@m.om
Version
$Id$

Definition at line 14 of file class.ilChart.php.

Constructor & Destructor Documentation

◆ __construct()

ilChart::__construct (   $a_id,
  $a_width = 500,
  $a_height = 500,
  $a_renderer = "flot" 
)

Constructor.

Parameters
string$a_id
int$a_width
int$a_height
string$a_renderer

Definition at line 37 of file class.ilChart.php.

References setRenderer(), setShadow(), setSize(), setXAxisToInteger(), and setYAxisToInteger().

38  {
39  $this->id = $a_id;
40  $this->data = array();
41  $this->setXAxisToInteger(false);
42  $this->setYAxisToInteger(false);
43  $this->setSize($a_width, $a_height);
44  $this->setRenderer($a_renderer);
45  $this->setShadow(2);
46  }
setSize($a_x, $a_y)
Set chart size.
setRenderer($a_value)
Set renderer.
setShadow($a_value)
Set shadow.
setXAxisToInteger($a_status)
setYAxisToInteger($a_status)
+ Here is the call graph for this function:

Member Function Documentation

◆ addData()

ilChart::addData ( ilChartData  $a_series,
  $a_idx = null 
)

Add data series.

Parameters
ilChartData$a_series
mixed$a_id
Returns
mixed index

Definition at line 90 of file class.ilChart.php.

References $data.

91  {
92  if($a_idx === null)
93  {
94  $a_idx = sizeof($this->data);
95  }
96  $this->data[$a_idx] = $a_series;
97  return $a_idx;
98  }

◆ getAllRenderers()

ilChart::getAllRenderers ( )

Get all available renderers.

Returns
array

Definition at line 66 of file class.ilChart.php.

Referenced by setRenderer().

67  {
68  return array("flot");
69  }
+ Here is the caller graph for this function:

◆ getColors()

ilChart::getColors ( )

Get colors.

Returns
array

Definition at line 195 of file class.ilChart.php.

References $colors.

Referenced by getHTML().

196  {
197  return $this->colors;
198  }
+ Here is the caller graph for this function:

◆ getHTML()

ilChart::getHTML ( )

Render (flot only currently)

Definition at line 245 of file class.ilChart.php.

References $colors, $legend, $options, $ret, $ticks, $tpl, $width, getColors(), getLegLabels(), getShadow(), getTicks(), getYAxisMax(), iljQueryUtil\initjQuery(), and ilUtil\shortenText().

246  {
247  global $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  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.pie.js");
255  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.highlighter.js");
256  $tpl->addJavascript("Services/Chart/js/flot/jquery.flot.spider.js");
257 
258  $chart = new ilTemplate("tpl.grid.html", true, true, "Services/Chart");
259  $chart->setVariable("ID", $this->id);
260  $chart->setVariable("WIDTH", $this->width);
261  $chart->setVariable("HEIGHT", $this->height);
262 
263  $last = array_keys($this->data);
264  $last = array_pop($last);
265  $has_pie = false;
266  $has_spider = false;
267  foreach($this->data as $idx => $series)
268  {
269  $fill = $series->getFill();
270 
271  if ($series->getType() == "spider")
272  {
273  $has_spider = true;
274 
275  if ($fill["color"] != "")
276  {
277  $chart->setCurrentBlock("series_property");
278  $chart->setVariable("SPROP", "color");
279  $chart->setVariable("SPROP_VAL", self::renderColor($fill["color"] , "0.5"));
280  $chart->parseCurrentBlock();
281  }
282  }
283 
284  $chart->setCurrentBlock("series");
285  $chart->setVariable("SERIES_LABEL", str_replace("\"", "\\\"", $series->getLabel()));
286  $chart->setVariable("SERIES_TYPE", $series->getType());
287 
288  $type = $series->getType();
289 
290  $points = array();
291  if($type != "pie")
292  {
293  foreach($series->getData() as $point)
294  {
295  $points[] = "[".$point[0].",".$point[1]."]";
296  }
297  $chart->setVariable("SERIES_DATA", "[ ".implode(",", $points)." ]");
298  }
299  else
300  {
301  $has_pie = true;
302  $chart->setVariable("SERIES_DATA", array_pop($series->getData()));
303  }
304  if($idx != $last)
305  {
306  $chart->setVariable("SERIES_END", ",");
307  }
308 
309  $options = array("show: ".($series->isHidden() ? "false" : "true"));
310  if($type != "points")
311  {
312  $width = $series->getLineWidth();
313  if($width !== null)
314  {
315  $options[] = "lineWidth:".$width;
316  }
317  if($type == "bars")
318  {
319  $bar_options = $series->getBarOptions();
320  if($bar_options["width"] !== null)
321  {
322  $options[] = "barWidth:".str_replace(",", ".", $bar_options["width"]);
323  $options[] = "align: \"".$bar_options["align"]."\"";
324  if($bar_options["horizontal"])
325  {
326  $options[] = "horizontal: true";
327  }
328  }
329  }
330  else if($type == "lines")
331  {
332  if($series->getLineSteps())
333  {
334  $options[] = "steps: true";
335  }
336  }
337  }
338  else
339  {
340  $radius = $series->getPointRadius();
341  if($radius !== null)
342  {
343  $options[] = "radius:".$radius;
344  }
345  }
346 
347  if($fill["fill"])
348  {
349  $options[] = "fill: ".$fill["fill"];
350  if($fill["color"])
351  {
352  $options[] = "fillColor: ".self::renderColor($fill["color"], $fill["fill"]);
353  }
354  }
355  $chart->setVariable("SERIES_OPTIONS", implode(", ", $options));
356 
357  $chart->parseCurrentBlock();
358  }
359 
360  if ($has_spider)
361  {
362  $chart->setCurrentBlock("spider");
363  $lab_strings = array();
364  $max_str_len = 0;
365  foreach ($this->getLegLabels() as $l)
366  {
367  $l = ilUtil::shortenText ($l, 80, true);
368  $lab_strings[] = "{label: \"".$l."\"}";
369  $max_str_len = max($max_str_len, strlen($l));
370  }
371  $chart->setVariable("LEG_LABELS", implode($lab_strings, ","));
372  $chart->setVariable("LEG_MAX", $this->getYAxisMax());
373  switch (count($this->getLegLabels()))
374  {
375  case 4:
376  case 6:
377  $chart->setVariable("LEG_START_ANGLE", "10");
378  break;
379 
380  default:
381  $chart->setVariable("LEG_START_ANGLE", "0");
382  break;
383  }
384  if ($max_str_len > 60)
385  {
386  $chart->setVariable("FONT_SIZE", "10");
387  }
388  else if ($max_str_len > 30)
389  {
390  $chart->setVariable("FONT_SIZE", "12");
391  }
392  else
393  {
394  $chart->setVariable("FONT_SIZE", "15");
395  }
396  $chart->parseCurrentBlock();
397 
398  $chart->setCurrentBlock("spider_grid_options");
399  $chart->setVariable("NR_TICKS", $this->getYAxisMax());
400  $chart->parseCurrentBlock();
401  }
402 
403  // global options
404 
405  $chart->setVariable("SHADOW", (int)$this->getShadow());
406  $chart->setVariable("IS_PIE", ($has_pie ? "true" : "false"));
407 
408  $colors = $this->getColors();
409  if($colors)
410  {
411  $tmp = array();
412  foreach($colors as $color)
413  {
414  $tmp[] = self::renderColor($color);
415  }
416  }
417  if(sizeof($tmp))
418  {
419  $chart->setVariable("COLORS", implode(",", $tmp));
420  }
421 
422  // legend
423  if(!$this->legend)
424  {
425  $chart->setVariable("LEGEND", "show: false");
426  }
427  else
428  {
429  $margin = $this->legend->getMargin();
430  $legend = array();
431  $legend[] = "show: true";
432  $legend[] = "noColumns: ".$this->legend->getColumns();
433  $legend[] = "position: \"".$this->legend->getPosition()."\"";
434  $legend[] = "margin: [".$margin["x"].", ".$margin["y"]."]";
435  $legend[] = "backgroundColor: ".self::renderColor($this->legend->getBackground());
436  $legend[] = "backgroundOpacity: ".str_replace(",",".",$this->legend->getOpacity());
437  $legend[] = "labelBoxBorderColor: ".self::renderColor($this->legend->getLabelBorder());
438 
439  $chart->setVariable("LEGEND", implode(", ", $legend));
440  }
441 
442  // axis/ticks
443  $tmp = array();
444  $ticks = $this->getTicks();
445  if($ticks)
446  {
447  foreach($ticks as $axis => $def)
448  {
449  if(is_numeric($def))
450  {
451  $tmp[$axis] = $axis."axis: { ticks: ".$def." }";
452  }
453  else if(is_array($def))
454  {
455  $ttmp = array();
456  foreach($def as $idx => $value)
457  {
458  if($ticks["labeled"])
459  {
460  $ttmp[] = "[".$idx.", \"".$value."\"]";
461  }
462  else
463  {
464  $ttmp[] = $value;
465  }
466  }
467  $tmp[$axis] = $axis."axis: { ticks: [".implode(", ", $ttmp)."] }";
468  }
469  }
470  }
471 
472  // optional: remove decimals
473  if(!isset($tmp["x"]) && $this->integer_axis["x"])
474  {
475  $tmp["x"] = "xaxis: { tickDecimals: 0 }";
476  }
477  if(!isset($tmp["y"]) && $this->integer_axis["y"])
478  {
479  $tmp["y"] = "yaxis: { tickDecimals: 0 }";
480  }
481 
482  if(sizeof($tmp))
483  {
484  $chart->setVariable("AXIS", ",".implode(", ", $tmp));
485  }
486 
487  $ret = $chart->get();
488 //echo htmlentities($ret);
489  return $ret;
490  }
static shortenText($a_str, $a_len, $a_dots=false, $a_next_blank=false, $a_keep_extension=false)
shorten a string to given length.
getYAxisMax()
Get y axis max value.
getTicks()
Get ticks.
if(!is_array($argv)) $options
special template class to simplify handling of ITX/PEAR
if(isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) $tpl
getColors()
Get colors.
static initjQuery($a_tpl=null)
Init jQuery.
getShadow()
Get shadow.
getLegLabels()
Get leg labels.
+ Here is the call graph for this function:

◆ getLegLabels()

ilChart::getLegLabels ( )

Get leg labels.

Returns
array leg labels (array of strings)

Definition at line 237 of file class.ilChart.php.

References $leg_labels.

Referenced by getHTML().

238  {
239  return $this->leg_labels;
240  }
+ Here is the caller graph for this function:

◆ getShadow()

ilChart::getShadow ( )

Get shadow.

Returns
int

Definition at line 169 of file class.ilChart.php.

References $shadow.

Referenced by getHTML().

170  {
171  return $this->shadow;
172  }
+ Here is the caller graph for this function:

◆ getTicks()

ilChart::getTicks ( )

Get ticks.

Returns
array (x, y)

Definition at line 217 of file class.ilChart.php.

References $ticks.

Referenced by getHTML().

218  {
219  return $this->ticks;
220  }
+ Here is the caller graph for this function:

◆ getYAxisMax()

ilChart::getYAxisMax ( )

Get y axis max value.

Returns
float y axis max value

Definition at line 517 of file class.ilChart.php.

References $y_max.

Referenced by getHTML().

518  {
519  return $this->y_max;
520  }
+ Here is the caller graph for this function:

◆ isValidColor()

static ilChart::isValidColor (   $a_value)
static

Validate html color code.

Parameters
string$a_value
Returns
bool

Definition at line 116 of file class.ilChart.php.

Referenced by ilChartLegend\setBackground(), ilChartData\setFill(), and ilChartLegend\setLabelBorder().

117  {
118  if(preg_match("/^#[0-9a-f]{3}$/i", $a_value, $match))
119  {
120  return true;
121  }
122  else if(preg_match("/^#[0-9a-f]{6}$/i", $a_value, $match))
123  {
124  return true;
125  }
126  }
+ Here is the caller graph for this function:

◆ renderColor()

static ilChart::renderColor (   $a_value,
  $a_opacity = 1 
)
staticprotected

Render html color code.

Parameters
string$a_value
float$a_opacity
Returns
string

Definition at line 135 of file class.ilChart.php.

136  {
137  if(self::isValidColor($a_value))
138  {
139  if(strlen($a_value) == 4)
140  {
141  return "\"rgba(".hexdec($a_value[1].$a_value[1]).", ".
142  hexdec($a_value[2].$a_value[2]).", ".
143  hexdec($a_value[3].$a_value[3]).", ".$a_opacity.")\"";
144  }
145  else
146  {
147  return "\"rgba(".hexdec($a_value[1].$a_value[2]).", ".
148  hexdec($a_value[3].$a_value[4]).", ".
149  hexdec($a_value[5].$a_value[6]).", ".$a_opacity.")\"";
150  }
151  }
152  }

◆ setColors()

ilChart::setColors (   $a_values)

Set colors.

Parameters
array$a_values

Definition at line 179 of file class.ilChart.php.

180  {
181  foreach($a_values as $color)
182  {
183  if(self::isValidColor($color))
184  {
185  $this->colors[] = $color;
186  }
187  }
188  }

◆ setLegend()

ilChart::setLegend ( ilChartLegend  $a_legend)

Set chart legend.

Parameters
ilChartLegend$a_legend

Definition at line 105 of file class.ilChart.php.

106  {
107  $this->legend = $a_legend;
108  }

◆ setLegLabels()

ilChart::setLegLabels (   $a_val)

Set leg labels.

Parameters
array$a_valleg labels (array of strings)

Definition at line 227 of file class.ilChart.php.

228  {
229  $this->leg_labels = $a_val;
230  }

◆ setRenderer()

ilChart::setRenderer (   $a_value)

Set renderer.

Parameters
string$a_value

Definition at line 53 of file class.ilChart.php.

References getAllRenderers().

Referenced by __construct().

54  {
55  if(in_array((string)$a_value, $this->getAllRenderers()))
56  {
57  $this->renderer = (string)$a_value;
58  }
59  }
getAllRenderers()
Get all available renderers.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setShadow()

ilChart::setShadow (   $a_value)

Set shadow.

Parameters
int$a_value

Definition at line 159 of file class.ilChart.php.

Referenced by __construct().

160  {
161  $this->shadow = (int)$a_value;
162  }
+ Here is the caller graph for this function:

◆ setSize()

ilChart::setSize (   $a_x,
  $a_y 
)

Set chart size.

Parameters
int$a_x
int$a_y

Definition at line 77 of file class.ilChart.php.

Referenced by __construct().

78  {
79  $this->width = (int)$a_x;
80  $this->height = (int)$a_y;
81  }
+ Here is the caller graph for this function:

◆ setTicks()

ilChart::setTicks (   $a_x,
  $a_y,
  $a_labeled = false 
)

Set ticks.

Parameters
int | array$a_x
int | array$a_y
bool$a_labeled

Definition at line 207 of file class.ilChart.php.

208  {
209  $this->ticks = array("x" => $a_x, "y" => $a_y, "labeled" => (bool)$a_labeled);
210  }

◆ setXAxisToInteger()

ilChart::setXAxisToInteger (   $a_status)

Definition at line 497 of file class.ilChart.php.

Referenced by __construct().

498  {
499  $this->integer_axis["x"] = (bool)$a_status;
500  }
+ Here is the caller graph for this function:

◆ setYAxisMax()

ilChart::setYAxisMax (   $a_val)

Set y axis max value.

Parameters
float$a_valy axis max value

Definition at line 507 of file class.ilChart.php.

508  {
509  $this->y_max = $a_val;
510  }

◆ setYAxisToInteger()

ilChart::setYAxisToInteger (   $a_status)

Definition at line 492 of file class.ilChart.php.

Referenced by __construct().

493  {
494  $this->integer_axis["y"] = (bool)$a_status;
495  }
+ Here is the caller graph for this function:

Field Documentation

◆ $colors

ilChart::$colors
protected

Definition at line 23 of file class.ilChart.php.

Referenced by getColors(), and getHTML().

◆ $data

ilChart::$data
protected

Definition at line 20 of file class.ilChart.php.

Referenced by addData().

◆ $height

ilChart::$height
protected

Definition at line 19 of file class.ilChart.php.

◆ $id

ilChart::$id
protected

Definition at line 16 of file class.ilChart.php.

◆ $integer_axis

ilChart::$integer_axis
protected

Definition at line 25 of file class.ilChart.php.

◆ $leg_labels

ilChart::$leg_labels = array()
protected

Definition at line 26 of file class.ilChart.php.

Referenced by getLegLabels().

◆ $legend

ilChart::$legend
protected

Definition at line 21 of file class.ilChart.php.

Referenced by getHTML().

◆ $renderer

ilChart::$renderer
protected

Definition at line 17 of file class.ilChart.php.

◆ $shadow

ilChart::$shadow
protected

Definition at line 22 of file class.ilChart.php.

Referenced by getShadow().

◆ $ticks

ilChart::$ticks
protected

Definition at line 24 of file class.ilChart.php.

Referenced by getHTML(), and getTicks().

◆ $width

ilChart::$width
protected

Definition at line 18 of file class.ilChart.php.

Referenced by getHTML().

◆ $y_max

ilChart::$y_max = 0
protected

Definition at line 27 of file class.ilChart.php.

Referenced by getYAxisMax().


The documentation for this class was generated from the following file: