ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilChart.php
Go to the documentation of this file.
1<?php
2
23abstract class ilChart
24{
25 public const TYPE_GRID = 1;
26 public const TYPE_PIE = 2;
27 public const TYPE_SPIDER = 3;
28
30 protected string $id = "";
31 protected string $width = "";
32 protected string $height = "";
33 protected array $data = [];
34 protected ?ilChartLegend $legend = null;
35 protected int $shadow = 0;
36 protected array $colors = [];
37 protected bool $auto_resize = false;
38 protected bool $stacked = false;
39
40 protected function __construct(string $a_id)
41 {
42 global $DIC;
43
44 $this->tpl = $DIC["tpl"];
45 $this->id = $a_id;
46 $this->data = array();
47
48 $this->setShadow(2);
49 }
50
51 public static function getInstanceByType(
52 int $a_type,
53 string $a_id
54 ): ilChart {
55 switch ($a_type) {
56 case self::TYPE_GRID:
57 return new ilChartGrid($a_id);
58
59 case self::TYPE_PIE:
60 return new ilChartPie($a_id);
61
63 return new ilChartSpider($a_id);
64 }
65 throw new ilException("Unknown chart type.");
66 }
67
71 abstract public function getDataInstance(?int $a_type = null): ilChartData;
72
76 abstract protected function isValidDataType(ilChartData $a_series): bool;
77
81 protected function isValid(): bool
82 {
83 if (sizeof($this->data)) {
84 return true;
85 }
86 return false;
87 }
88
92 public function setSize(string $a_x, string $a_y): void
93 {
94 $this->width = $a_x;
95 $this->height = $a_y;
96 }
97
101 public function addData(
102 ilChartData $a_series,
103 ?int $a_idx = null
104 ): ?int {
105 if ($this->isValidDataType($a_series)) {
106 if ($a_idx === null) {
107 $a_idx = sizeof($this->data);
108 }
109 $this->data[$a_idx] = $a_series;
110 return $a_idx;
111 }
112 return null;
113 }
114
115 public function setLegend(ilChartLegend $a_legend): void
116 {
117 $this->legend = $a_legend;
118 }
119
120 public function setColors(array $a_values): void
121 {
122 foreach ($a_values as $color) {
123 if (self::isValidColor($color)) {
124 $this->colors[] = $color;
125 }
126 }
127 }
128
129 public function getColors(): array
130 {
131 return $this->colors;
132 }
133
137 public static function isValidColor(string $a_value): bool
138 {
139 if (preg_match("/^#[0-9a-f]{3}$/i", $a_value, $match)) {
140 return true;
141 } elseif (preg_match("/^#[0-9a-f]{6}$/i", $a_value, $match)) {
142 return true;
143 }
144 return false;
145 }
146
150 public static function renderColor(
151 string $a_value,
152 float $a_opacity = 1
153 ): string {
154 if (self::isValidColor($a_value)) {
155 if (strlen($a_value) == 4) {
156 return "rgba(" . hexdec($a_value[1] . $a_value[1]) . ", " .
157 hexdec($a_value[2] . $a_value[2]) . ", " .
158 hexdec($a_value[3] . $a_value[3]) . ", " . $a_opacity . ")";
159 } else {
160 return "rgba(" . hexdec($a_value[1] . $a_value[2]) . ", " .
161 hexdec($a_value[3] . $a_value[4]) . ", " .
162 hexdec($a_value[5] . $a_value[6]) . ", " . $a_opacity . ")";
163 }
164 }
165 return "";
166 }
167
168 public function setShadow(int $a_value): void
169 {
170 $this->shadow = $a_value;
171 }
172
173 public function getShadow(): int
174 {
175 return $this->shadow;
176 }
177
181 public function setAutoResize(
182 bool $a_value
183 ): void {
184 $this->auto_resize = $a_value;
185 }
186
187 public function setStacked(bool $a_value): void
188 {
189 $this->stacked = $a_value;
190 }
191
195 protected function initJS(): void
196 {
197 $tpl = $this->tpl;
198
200
201 $tpl->addJavaScript("assets/js/excanvas.min.js");
202 $tpl->addJavaScript("assets/js/jquery.flot.min.js");
203
204 if ($this->auto_resize) {
205 // #13108
206 $tpl->addJavaScript("assets/js/jquery.flot.resize.min.js");
207 }
208
209 if ($this->stacked) {
210 $tpl->addJavaScript("assets/js/jquery.flot.stack.min.js");
211 }
212
213 $this->addCustomJS();
214 }
215
219 protected function addCustomJS(): void
220 {
221 }
222
226 public function parseGlobalOptions(stdClass $a_options): void
227 {
228 }
229
233 public function getHTML(): string
234 {
235 if (!$this->isValid()) {
236 return "";
237 }
238
239 $this->initJS();
240
241 $chart = new ilTemplate("tpl.grid.html", true, true, "components/ILIAS/Chart");
242 $chart->setVariable("ID", $this->id);
243
244 if ($this->width) {
245 if (is_numeric($this->width)) {
246 $chart->setVariable("WIDTH", "width:" . $this->width . "px;");
247 } else {
248 $chart->setVariable("WIDTH", "width:" . $this->width . ";");
249 }
250 }
251 if ($this->height) {
252 if (is_numeric($this->height)) {
253 $chart->setVariable("HEIGHT", "height:" . $this->height . "px;");
254 } else {
255 $chart->setVariable("HEIGHT", "height:" . $this->height . ";");
256 }
257 }
258
259
260 // (series) data
261
262 $json_series = array();
263 foreach ($this->data as $series) {
264 $series->parseData($json_series);
265 }
266 $series_str = json_encode($json_series);
267
268
269 // global options
270
271 $json_options = new stdClass();
272 $json_options->series = new stdClass();
273 $json_options->series->shadowSize = $this->getShadow();
274 $json_options->series->lines = new stdClass();
275 $json_options->series->lines->show = false;
276 $json_options->series->stack = $this->stacked;
277
278 foreach ($this->data as $series) {
279 $series->parseGlobalOptions($json_options, $this);
280 }
281
282 $this->parseGlobalOptions($json_options);
283
284 $colors = $this->getColors();
285 if ($colors) {
286 $json_options->colors = array();
287 foreach ($colors as $color) {
288 $json_options->colors[] = self::renderColor($color);
289 }
290 }
291
292 // legend
293 $json_options->legend = new stdClass();
294 if (!$this->legend) {
295 $json_options->legend->show = false;
296 } else {
297 $this->legend->parseOptions($json_options->legend);
298 }
299
300 $options = json_encode($json_options);
301
302 $this->tpl->addOnLoadCode('$.plot($("#ilChart' . $this->id . '"), ' . $series_str . ', ' . $options . ');');
303
304 $ret = $chart->get();
305 return $ret;
306 }
307}
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.
getDataInstance(?int $a_type=null)
Get data series instance.
setAutoResize(bool $a_value)
Toggle auto-resizing on window resize/redraw.
const TYPE_PIE
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)
Base class for ILIAS Exception handling.
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('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26