ILIAS  release_8 Revision v8.24
class.ilChart.php
Go to the documentation of this file.
1<?php
2
20abstract class ilChart
21{
22 public const TYPE_GRID = 1;
23 public const TYPE_PIE = 2;
24 public const TYPE_SPIDER = 3;
25
27 protected string $id = "";
28 protected string $width = "";
29 protected string $height = "";
30 protected array $data = [];
31 protected ?ilChartLegend $legend = null;
32 protected int $shadow = 0;
33 protected array $colors = [];
34 protected bool $auto_resize = false;
35 protected bool $stacked = false;
36
37 protected function __construct(string $a_id)
38 {
39 global $DIC;
40
41 $this->tpl = $DIC["tpl"];
42 $this->id = $a_id;
43 $this->data = array();
44
45 $this->setShadow(2);
46 }
47
48 public static function getInstanceByType(
49 int $a_type,
50 string $a_id
51 ): ilChart {
52 switch ($a_type) {
53 case self::TYPE_GRID:
54 return new ilChartGrid($a_id);
55
56 case self::TYPE_PIE:
57 return new ilChartPie($a_id);
58
60 return new ilChartSpider($a_id);
61 }
62 throw new ilException("Unknown chart type.");
63 }
64
68 abstract public function getDataInstance(int $a_type = null): ilChartData;
69
73 abstract protected function isValidDataType(ilChartData $a_series): bool;
74
78 protected function isValid(): bool
79 {
80 if (sizeof($this->data)) {
81 return true;
82 }
83 return false;
84 }
85
89 public function setSize(string $a_x, string $a_y): void
90 {
91 $this->width = $a_x;
92 $this->height = $a_y;
93 }
94
98 public function addData(
99 ilChartData $a_series,
100 ?int $a_idx = null
101 ): ?int {
102 if ($this->isValidDataType($a_series)) {
103 if ($a_idx === null) {
104 $a_idx = sizeof($this->data);
105 }
106 $this->data[$a_idx] = $a_series;
107 return $a_idx;
108 }
109 return null;
110 }
111
112 public function setLegend(ilChartLegend $a_legend): void
113 {
114 $this->legend = $a_legend;
115 }
116
117 public function setColors(array $a_values): void
118 {
119 foreach ($a_values as $color) {
120 if (self::isValidColor($color)) {
121 $this->colors[] = $color;
122 }
123 }
124 }
125
126 public function getColors(): array
127 {
128 return $this->colors;
129 }
130
134 public static function isValidColor(string $a_value): bool
135 {
136 if (preg_match("/^#[0-9a-f]{3}$/i", $a_value, $match)) {
137 return true;
138 } elseif (preg_match("/^#[0-9a-f]{6}$/i", $a_value, $match)) {
139 return true;
140 }
141 return false;
142 }
143
147 public static function renderColor(
148 string $a_value,
149 float $a_opacity = 1
150 ): string {
151 if (self::isValidColor($a_value)) {
152 if (strlen($a_value) == 4) {
153 return "rgba(" . hexdec($a_value[1] . $a_value[1]) . ", " .
154 hexdec($a_value[2] . $a_value[2]) . ", " .
155 hexdec($a_value[3] . $a_value[3]) . ", " . $a_opacity . ")";
156 } else {
157 return "rgba(" . hexdec($a_value[1] . $a_value[2]) . ", " .
158 hexdec($a_value[3] . $a_value[4]) . ", " .
159 hexdec($a_value[5] . $a_value[6]) . ", " . $a_opacity . ")";
160 }
161 }
162 return "";
163 }
164
165 public function setShadow(int $a_value): void
166 {
167 $this->shadow = $a_value;
168 }
169
170 public function getShadow(): int
171 {
172 return $this->shadow;
173 }
174
178 public function setAutoResize(
179 bool $a_value
180 ): void {
181 $this->auto_resize = $a_value;
182 }
183
184 public function setStacked(bool $a_value): void
185 {
186 $this->stacked = $a_value;
187 }
188
192 protected function initJS(): void
193 {
195
197
198 $tpl->addJavaScript("Services/Chart/js/flot/excanvas.min.js");
199 $tpl->addJavaScript("Services/Chart/js/flot/jquery.flot.min.js");
200
201 if ($this->auto_resize) {
202 // #13108
203 $tpl->addJavaScript("Services/Chart/js/flot/jquery.flot.resize.min.js");
204 }
205
206 if ($this->stacked) {
207 $tpl->addJavaScript("Services/Chart/js/flot/jquery.flot.stack.min.js");
208 }
209
210 $this->addCustomJS();
211 }
212
216 protected function addCustomJS(): void
217 {
218 }
219
223 public function parseGlobalOptions(stdClass $a_options): void
224 {
225 }
226
230 public function getHTML(): string
231 {
232 if (!$this->isValid()) {
233 return "";
234 }
235
236 $this->initJS();
237
238 $chart = new ilTemplate("tpl.grid.html", true, true, "Services/Chart");
239 $chart->setVariable("ID", $this->id);
240
241 if ($this->width) {
242 if (is_numeric($this->width)) {
243 $chart->setVariable("WIDTH", "width:" . $this->width . "px;");
244 } else {
245 $chart->setVariable("WIDTH", "width:" . $this->width . ";");
246 }
247 }
248 if ($this->height) {
249 if (is_numeric($this->height)) {
250 $chart->setVariable("HEIGHT", "height:" . $this->height . "px;");
251 } else {
252 $chart->setVariable("HEIGHT", "height:" . $this->height . ";");
253 }
254 }
255
256
257 // (series) data
258
259 $json_series = array();
260 foreach ($this->data as $series) {
261 $series->parseData($json_series);
262 }
263 $series_str = json_encode($json_series);
264
265
266 // global options
267
268 $json_options = new stdClass();
269 $json_options->series = new stdClass();
270 $json_options->series->shadowSize = $this->getShadow();
271 $json_options->series->lines = new stdClass();
272 $json_options->series->lines->show = false;
273 $json_options->series->stack = $this->stacked;
274
275 foreach ($this->data as $series) {
276 $series->parseGlobalOptions($json_options, $this);
277 }
278
279 $this->parseGlobalOptions($json_options);
280
281 $colors = $this->getColors();
282 if ($colors) {
283 $json_options->colors = array();
284 foreach ($colors as $color) {
285 $json_options->colors[] = self::renderColor($color);
286 }
287 }
288
289 // legend
290 $json_options->legend = new stdClass();
291 if (!$this->legend) {
292 $json_options->legend->show = false;
293 } else {
294 $this->legend->parseOptions($json_options->legend);
295 }
296
297 $options = json_encode($json_options);
298
299 $this->tpl->addOnLoadCode('$.plot($("#ilChart' . $this->id . '"), ' . $series_str . ', ' . $options . ');');
300
301 $ret = $chart->get();
302 return $ret;
303 }
304}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isValidDataType(ilChartData $a_series)
Validate data series.
array $colors
ilChartLegend $legend
getHTML()
Render.
__construct(string $a_id)
const TYPE_SPIDER
initJS()
Init JS script files.
static getInstanceByType(int $a_type, string $a_id)
setShadow(int $a_value)
setLegend(ilChartLegend $a_legend)
array $data
bool $auto_resize
string $height
ilGlobalTemplateInterface $tpl
static renderColor(string $a_value, float $a_opacity=1)
Render html color code.
setAutoResize(bool $a_value)
Toggle auto-resizing on window resize/redraw.
const TYPE_PIE
getDataInstance(int $a_type=null)
Get data series instance.
string $width
const TYPE_GRID
setColors(array $a_values)
isValid()
Basic validation.
addData(ilChartData $a_series, ?int $a_idx=null)
Add data series.
bool $stacked
static isValidColor(string $a_value)
Validate html color code.
parseGlobalOptions(stdClass $a_options)
Convert (global) properties to flot config.
setSize(string $a_x, string $a_y)
Set chart size.
addCustomJS()
Add type-specific JS script.
string $id
setStacked(bool $a_value)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
special template class to simplify handling of ITX/PEAR
static initjQuery(ilGlobalTemplateInterface $a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41