|
| 1 | +<?php |
| 2 | + |
| 3 | +/* Icinga Kubernetes Web | (c) 2023 Icinga GmbH | GPLv2 */ |
| 4 | + |
| 5 | +namespace Icinga\Module\Kubernetes; |
| 6 | + |
| 7 | +use ipl\Html\BaseHtmlElement; |
| 8 | +use ipl\Html\Html; |
| 9 | +use ipl\Html\HtmlString; |
| 10 | +use ipl\Html\Table; |
| 11 | + |
| 12 | +class Donut extends BaseHtmlElement |
| 13 | +{ |
| 14 | + protected $tag = 'section'; |
| 15 | + |
| 16 | + protected $defaultAttributes = ['class' => 'donut']; |
| 17 | + |
| 18 | + /** |
| 19 | + * The donut data |
| 20 | + * |
| 21 | + * @var iterable |
| 22 | + */ |
| 23 | + protected $data = []; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $heading; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var int |
| 32 | + */ |
| 33 | + protected $headingLevel = 2; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var callable |
| 37 | + */ |
| 38 | + protected $labelCallback; |
| 39 | + |
| 40 | + /** |
| 41 | + * Set the data to display |
| 42 | + * |
| 43 | + * @param iterable $data |
| 44 | + * |
| 45 | + * @return $this |
| 46 | + */ |
| 47 | + public function setData(iterable $data) |
| 48 | + { |
| 49 | + $this->data = $data; |
| 50 | + |
| 51 | + return $this; |
| 52 | + } |
| 53 | + |
| 54 | + public function setHeading(string $heading, int $level) |
| 55 | + { |
| 56 | + $this->heading = $heading; |
| 57 | + $this->headingLevel = $level; |
| 58 | + |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + public function setLabelCallback(callable $callback) |
| 63 | + { |
| 64 | + $this->labelCallback = $callback; |
| 65 | + |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + public function assemble() |
| 70 | + { |
| 71 | + $donut = new \Icinga\Chart\Donut(); |
| 72 | + $legend = new Table(); |
| 73 | + |
| 74 | + foreach ($this->data as $index => $value) { |
| 75 | + $donut->addSlice((int) $value, ['class' => 'segment-' . $index]); |
| 76 | + $legend->add( |
| 77 | + [ |
| 78 | + Html::tag('span', ['class' => 'badge badge-' . $index]), |
| 79 | + call_user_func($this->labelCallback, $index), |
| 80 | + $value |
| 81 | + ] |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + if ($this->heading === null) { |
| 86 | + $this->addHtml(Html::tag("h{$this->headingLevel}", $this->heading), new HtmlString($donut->render()), $legend); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments