19declare(strict_types=1);
48 RendererInterface $default_renderer
55 $tpl->setVariable(
"LIST", $default_renderer->render($a11y_list));
59 $dimensions = $component->getDataset()->getDimensions();
60 $x_labels = json_encode($this->
reformatValueLabels($dimensions[key($dimensions)]->getLabels()));
61 $tooltips = json_encode($component->getDataset()->getAlternativeInformation());
63 $component = $component->withAdditionalOnLoadCode(
65 return "il.UI.chart.bar.horizontal.init(
75 $tpl->setVariable(
"ID",
$id);
82 RendererInterface $default_renderer
84 $tpl = $this->getTemplate(
"tpl.bar_vertical.html",
true,
true);
86 $this->renderBasics($component, $tpl);
88 $options = json_encode($this->getParsedOptions($component));
89 $data = json_encode($this->getParsedData($component));
90 $dimensions = $component->getDataset()->getDimensions();
91 $y_labels = json_encode($this->reformatValueLabels($dimensions[key($dimensions)]->getLabels()));
92 $tooltips = json_encode($component->getDataset()->getAlternativeInformation());
94 $a11y_list = $this->getAccessibilityList($component);
95 $tpl->setVariable(
"LIST", $default_renderer->render($a11y_list));
97 $component = $component->withAdditionalOnLoadCode(
99 return "il.UI.chart.bar.vertical.init(
108 $id = $this->bindJavaScript($component);
109 $tpl->setVariable(
"ID",
$id);
116 $tpl->
setVariable(
"TITLE", $this->convertSpecialCharacters($component->getTitle()));
119 $height = $this->determineHeightForHorizontal($component);
121 $height = $this->determineHeightForVertical($component);
130 $item_count = count($component->getDataset()->getPoints());
131 $height = $min_height + ($item_count - 2) * 50;
132 if ($height < $min_height) {
133 $height = $min_height;
135 if ($height > $max_height) {
136 $height = $max_height;
139 return (
string) (
int) $height;
146 $data_max = $this->getHighestValueOfChart($component) - $this->getLowestValueOfChart($component);
147 $height = $min_height + ($data_max / 10) * 50;
148 if ($height > $max_height) {
149 $height = $max_height;
152 return (
string) (
int) $height;
158 $ui_fac = $this->getUIFactory();
160 $points_per_dimension = $component->getDataset()->getPointsPerDimension();
161 $tooltips_per_dimension = $component->getDataset()->getAlternativeInformationPerDimension();
162 $dimensions = $component->getDataset()->getDimensions();
163 $value_labels = $dimensions[key($dimensions)]->getLabels();
164 $lowest = $this->getLowestValueOfChart($component);
167 foreach ($points_per_dimension as $dimension_name => $item_points) {
169 foreach ($item_points as $measurement_item_label => $point) {
171 if (isset($tooltips_per_dimension[$dimension_name][$measurement_item_label])) {
173 $entry = $measurement_item_label .
": " . $tooltips_per_dimension[$dimension_name][$measurement_item_label];
174 } elseif (is_array($point)) {
177 foreach ($point as $p) {
181 $entry = $measurement_item_label .
": " .
$range;
182 } elseif (is_null($point)) {
184 $entry = $measurement_item_label .
": -";
185 } elseif (!empty($value_labels) && is_int($point) && !empty($value_labels[$point - $lowest])) {
187 $entry = $measurement_item_label .
": " . $value_labels[$point - $lowest];
190 $entry = $measurement_item_label .
": " . $point;
192 $entries[] = $this->convertSpecialCharacters($entry);
194 $list_items[$this->convertSpecialCharacters($dimension_name)] = $ui_fac->listing()->unordered($entries);
197 $list = $ui_fac->listing()->descriptive($list_items);
206 foreach ($component->getDataset()->getDimensions() as $dimension_name => $dimension) {
207 $new_min = floor($component->getDataset()->getMinValueForDimension($dimension_name));
208 if (is_null($min) || $new_min < $min) {
214 $min = $component->getXAxis()->getMinValue() ?? $min;
216 $min = $component->getYAxis()->getMinValue() ?? $min;
226 foreach ($component->getDataset()->getDimensions() as $dimension_name => $dimension) {
227 $new_max = ceil($component->getDataset()->getMaxValueForDimension($dimension_name));
228 if (is_null($max) || $new_max > $max) {
234 $max = $component->getXAxis()->getMaxValue() ?? $max;
236 $max = $component->getYAxis()->getMaxValue() ?? $max;
246 foreach ($labels as $label) {
247 $new_labels[$index] = $label;
257 $options->indexAxis = $component->getIndexAxis();
259 $options->maintainAspectRatio =
false;
261 $options->plugins->legend =
new stdClass();
262 $options->plugins->legend->display = $component->isLegendVisible();
263 $options->plugins->legend->position = $component->getLegendPosition();
264 $options->plugins->tooltip =
new stdClass();
265 $options->plugins->tooltip->enabled = $component->isTooltipsVisible();
266 $options->plugins->tooltip->callbacks =
new stdClass();
267 $options->plugins->title =
new stdClass();
268 $options->plugins->title->display = $component->isTitleVisible();
269 $options->plugins->title->text = $component->getTitle();
272 $options->scales = $this->getParsedOptionsForHorizontal($component);
274 $options->scales = $this->getParsedOptionsForVertical($component);
282 $scales =
new stdClass();
283 $scales->y =
new stdClass();
284 $x_axis = $component->getXAxis();
285 $scales->x =
new stdClass();
286 $scales->x->axis = $x_axis->getAbbreviation();
287 $scales->x->type = $x_axis->getType();
288 $scales->x->display = $x_axis->isDisplayed();
289 $scales->x->position = $x_axis->getPosition();
290 $scales->x->beginAtZero = $x_axis->isBeginAtZero();
291 $scales->x->ticks =
new stdClass();
292 $scales->x->ticks->callback =
null;
293 $scales->x->ticks->stepSize = $x_axis->getStepSize();
294 if ($x_axis->getMinValue()) {
295 $scales->x->min = $x_axis->getMinValue();
297 if ($x_axis->getMaxValue()) {
298 $scales->x->max = $x_axis->getMaxValue();
300 $dimension_groups = $component->getDataset()->getDimensionGroups();
301 foreach ($component->getGroupConfigs() as $group_name => $config) {
302 if (!isset($dimension_groups[$group_name]) || !$config->isStacked()) {
305 $scales->y->stacked =
true;
306 $scales->x->stacked =
true;
311 $dimension_scales = $component->getDataset()->getDimensions();
312 foreach ($dimension_scales as $scale_id) {
313 $scales->{get_class($scale_id)} =
new stdClass();
314 $scales->{get_class($scale_id)}->axis =
"y";
315 $scales->{get_class($scale_id)}->display =
false;
323 $scales =
new stdClass();
324 $scales->x =
new stdClass();
325 $y_axis = $component->getYAxis();
326 $scales->y =
new stdClass();
327 $scales->y->axis = $y_axis->getAbbreviation();
328 $scales->y->type = $y_axis->getType();
329 $scales->y->display = $y_axis->isDisplayed();
330 $scales->y->position = $y_axis->getPosition();
331 $scales->y->beginAtZero = $y_axis->isBeginAtZero();
332 $scales->y->ticks =
new stdClass();
333 $scales->y->ticks->callback =
null;
334 $scales->y->ticks->stepSize = $y_axis->getStepSize();
335 if ($y_axis->getMinValue()) {
336 $scales->y->min = $y_axis->getMinValue();
338 if ($y_axis->getMaxValue()) {
339 $scales->y->max = $y_axis->getMaxValue();
341 $dimension_groups = $component->getDataset()->getDimensionGroups();
342 foreach ($component->getGroupConfigs() as $group_name => $config) {
343 if (!isset($dimension_groups[$group_name]) || !$config->isStacked()) {
346 $scales->x->stacked =
true;
347 $scales->y->stacked =
true;
352 $dimension_scales = $component->getDataset()->getDimensions();
353 foreach ($dimension_scales as $scale_id) {
354 $scales->{get_class($scale_id)} =
new stdClass();
355 $scales->{get_class($scale_id)}->axis =
"x";
356 $scales->{get_class($scale_id)}->display =
false;
364 $data =
new stdClass();
365 $data->datasets =
new stdClass();
367 $user_data = $this->getUserData($component);
369 foreach ($user_data as $set) {
371 $datasets[] = $dataset;
373 $data->datasets = $datasets;
374 $data->labels = array_keys($component->getDataset()->getPoints());
381 $points_per_dimension = $component->getDataset()->getPointsPerDimension();
382 $dimensions = $component->getDataset()->getDimensions();
383 $dimension_groups = $component->getDataset()->getDimensionGroups();
384 $bar_configs = $component->getBarConfigs();
385 $group_configs = $component->getGroupConfigs();
387 $stacking_groups = [];
389 foreach ($group_configs as $group_name => $config) {
390 if (!isset($dimension_groups[$group_name]) || !$config->isStacked()) {
393 foreach ($dimension_groups[$group_name]->getDimensionKeys() as $dimension_name) {
394 $stacking_groups[$dimension_name] = $group_name;
398 foreach ($points_per_dimension as $dimension_name => $item_points) {
399 $data[$dimension_name][
"label"] = $dimension_name;
400 if (isset($bar_configs[$dimension_name]) && $bar_configs[$dimension_name]->getColor()) {
401 $data[$dimension_name][
"backgroundColor"] = $bar_configs[$dimension_name]->getColor()->asHex();
403 if (isset($bar_configs[$dimension_name]) && $bar_configs[$dimension_name]->getRelativeWidth()) {
404 $data[$dimension_name][
"barPercentage"] = $bar_configs[$dimension_name]->getRelativeWidth();
406 if (isset($stacking_groups[$dimension_name])) {
407 $data[$dimension_name][
"stack"] = $stacking_groups[$dimension_name];
410 $points_as_objects = [];
412 foreach ($item_points as $y_point => $x_point) {
413 $datasets =
new stdClass();
414 $datasets->data =
new stdClass();
415 $datasets->data->y = $y_point;
416 $datasets->data->x = $x_point;
417 $points_as_objects[] = $datasets->data;
419 $data[$dimension_name][
"data"] = $points_as_objects;
420 $data[$dimension_name][
"yAxisID"] = get_class($dimensions[$dimension_name]);
422 foreach ($item_points as $x_point => $y_point) {
423 $datasets =
new stdClass();
424 $datasets->data =
new stdClass();
425 $datasets->data->x = $x_point;
426 $datasets->data->y = $y_point;
427 $points_as_objects[] = $datasets->data;
429 $data[$dimension_name][
"data"] = $points_as_objects;
430 $data[$dimension_name][
"xAxisID"] = get_class($dimensions[$dimension_name]);
442 parent::registerResources($registry);
443 $registry->
register(
'assets/js/chart.umd.js');
444 $registry->
register(
'assets/js/bar.js');
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
render(Component\Component $component, RendererInterface $default_renderer)
determineHeightForHorizontal(Bar\Bar $component)
determineHeightForVertical(Bar\Bar $component)
getAccessibilityList(Bar\Bar $component)
getParsedOptionsForVertical(Bar\Bar $component)
getUserData(Bar\Bar $component)
getParsedOptionsForHorizontal(Bar\Bar $component)
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
getHighestValueOfChart(Bar\Bar $component)
reformatValueLabels(array $labels)
getLowestValueOfChart(Bar\Bar $component)
renderBasics(Bar\Bar $component, Template $tpl)
renderVertical(Bar\Vertical $component, RendererInterface $default_renderer)
getParsedOptions(Bar\Bar $component)
getParsedData(Bar\Bar $component)
renderHorizontal(Bar\Horizontal $component, RendererInterface $default_renderer)
Base class for all component renderers.
cannotHandleComponent(Component $component)
This method MUST be called by derived component renderers, if.
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
Registry for resources required by rendered output like Javascript or CSS.
register(string $name)
Add a dependency.
Interface to templating as it is used in the UI framework.
setVariable(string $name, $value)
Set a variable in the current block.
An entity that renders components to a string output.