|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Asantibanez\LivewireCharts\Models; |
| 5 | + |
| 6 | +/** |
| 7 | + * Class TreeMapChartModel |
| 8 | + * @package Asantibanez\LivewireCharts\Models |
| 9 | + */ |
| 10 | +class TreeMapChartModel extends BaseChartModel |
| 11 | +{ |
| 12 | + use HasTitle; |
| 13 | + use HasColors; |
| 14 | + |
| 15 | + public $data; |
| 16 | + public $distributed; |
| 17 | + public $onBlockClickEventName; |
| 18 | + |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + parent::__construct(); |
| 22 | + |
| 23 | + $this->data = collect(); |
| 24 | + |
| 25 | + $this->distributed = false; |
| 26 | + |
| 27 | + $this->setColors(['#2E93fA']); |
| 28 | + } |
| 29 | + |
| 30 | + public function setDistributed($distributed) |
| 31 | + { |
| 32 | + $this->distributed = $distributed; |
| 33 | + |
| 34 | + return $this; |
| 35 | + } |
| 36 | + |
| 37 | + public function addBlock($title, $value, $extras = []) |
| 38 | + { |
| 39 | + return $this->addSeriesBlock($this->title, $title, $value, $extras); |
| 40 | + } |
| 41 | + |
| 42 | + public function addSeriesBlock($seriesName, $title, $value, $extras = []) |
| 43 | + { |
| 44 | + $series = $this->data->get($seriesName, collect()); |
| 45 | + |
| 46 | + $series->push([ |
| 47 | + 'seriesName' => $seriesName, |
| 48 | + 'title' => $title, |
| 49 | + 'value' => $value, |
| 50 | + 'extras' => $extras, |
| 51 | + ]); |
| 52 | + |
| 53 | + $this->data->put($seriesName, $series); |
| 54 | + |
| 55 | + return $this; |
| 56 | + } |
| 57 | + |
| 58 | + public function withOnBlockClickEvent($onBlockClickEventName) |
| 59 | + { |
| 60 | + $this->onBlockClickEventName = $onBlockClickEventName; |
| 61 | + |
| 62 | + return $this; |
| 63 | + } |
| 64 | + |
| 65 | + public function toArray() |
| 66 | + { |
| 67 | + return array_merge(parent::toArray(), [ |
| 68 | + 'data' => $this->data->toArray(), |
| 69 | + 'distributed' => $this->distributed, |
| 70 | + 'onBlockClickEventName' => $this->onBlockClickEventName, |
| 71 | + ]); |
| 72 | + } |
| 73 | + |
| 74 | + public function fromArray($array) |
| 75 | + { |
| 76 | + parent::fromArray($array); |
| 77 | + |
| 78 | + $this->data = collect(data_get($array, 'data', [])); |
| 79 | + |
| 80 | + $this->distributed = data_get($array, 'distributed', false); |
| 81 | + |
| 82 | + $this->onBlockClickEventName = data_get($array, 'onBlockClickEventName', null); |
| 83 | + } |
| 84 | +} |
0 commit comments