123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- namespace Dcat\Admin\Widgets\ApexCharts;
- use Dcat\Admin\Support\Helper;
- use Dcat\Admin\Traits\FromApi;
- use Dcat\Admin\Widgets\Widget;
- use Illuminate\Support\Str;
- class Chart extends Widget
- {
- use FromApi;
- public static $js = '@apex-charts';
- protected $containerSelector;
- protected $options = [];
- protected $built = false;
- protected $scripts = [
- 'extend' => 'return options',
- ];
- public function __construct($containerSelector = null, $options = [])
- {
- if ($containerSelector && ! is_string($containerSelector)) {
- $options = $containerSelector;
- $containerSelector = null;
- }
- $this->selector($containerSelector);
- $this->options($options);
- }
- /**
- * 设置或获取图表容器选择器
- *
- * @param string|null $selector
- *
- * @return $this|string|null
- */
- public function selector(?string $selector = null)
- {
- if ($selector === null) {
- return $this->containerSelector;
- }
- $this->containerSelector = $selector;
- if ($selector && ! $this->built) {
- $this->autoRender();
- }
- return $this;
- }
- /**
- * @param string|array $title
- *
- * @return $this
- */
- public function title($title)
- {
- if (is_string($title)) {
- $options = ['text' => $title];
- } else {
- $options = Helper::array($title);
- }
- $this->options['title'] = $options;
- return $this;
- }
- /**
- * @param array $series
- *
- * @return $this
- */
- public function series($series)
- {
- $this->options['series'] = Helper::array($series);
- return $this;
- }
- /**
- * @param array $value
- *
- * @return $this
- */
- public function labels($value)
- {
- $this->options['labels'] = Helper::array($value);
- return $this;
- }
- /**
- * @param string|array $colors
- *
- * @return $this
- */
- public function colors($colors)
- {
- $this->options['colors'] = Helper::array($colors);
- return $this;
- }
- /**
- * @param array $value
- *
- * @return $this
- */
- public function stroke($value)
- {
- $this->options['stroke'] = Helper::array($value);
- return $this;
- }
- /**
- * @param array $value
- *
- * @return $this
- */
- public function xaxis($value)
- {
- $this->options['xaxis'] = Helper::array($value);
- return $this;
- }
- /**
- * @param array $value
- *
- * @return $this
- */
- public function tooltip($value)
- {
- $this->options['tooltip'] = Helper::array($value);
- return $this;
- }
- /**
- * @param array $value
- *
- * @return $this
- */
- public function yaxis($value)
- {
- $this->options['yaxis'] = Helper::array($value);
- return $this;
- }
- /**
- * @param array $value
- *
- * @return $this
- */
- public function fill($value)
- {
- $this->options['fill'] = Helper::array($value);
- return $this;
- }
- /**
- * @param array $value
- *
- * @return $this
- */
- public function chart($value)
- {
- $this->options['chart'] = Helper::array($value);
- return $this;
- }
- /**
- * @param array|bool $value
- *
- * @return $this
- */
- public function dataLabels($value)
- {
- if (is_bool($value)) {
- $value = ['enabled' => $value];
- }
- $this->options['dataLabels'] = Helper::array($value);
- return $this;
- }
- /**
- * @param string|\Closure $script
- *
- * @return $this
- */
- public function extendOptions($script)
- {
- $this->scripts['extend'] = value($script);
- return $this;
- }
- /**
- * @param string $options
- *
- * @return string
- */
- protected function buildDefaultScript($options)
- {
- return <<<JS
- (function () {
- var options = {$options}, extend = function (options) {
- {$this->scripts['extend']}
- };
- var chart = new ApexCharts(
- $("{$this->containerSelector}")[0],
- $.extend(options, extend(options))
- );
- chart.render();
- })();
- JS;
- }
- /**
- * @return string
- */
- public function script()
- {
- $options = json_encode($this->options);
- if (! $this->allowBuildRequest()) {
- return $this->buildDefaultScript($options);
- }
- $this->fetched(
- <<<JS
- if (! response.status) {
- return Dcat.error(response.message || 'Server internal error.');
- }
- var container = $('{$this->containerSelector}');
- container.html('');
- setTimeout(function () {
- var chart = new ApexCharts(container[0], response.options);
-
- chart.render();
- }, 50);
- JS
- );
- return $this->buildRequestScript();
- }
- /**
- * @return string
- */
- public function render()
- {
- if ($this->built) {
- return;
- }
- $this->built = true;
- $hasSelector = $this->containerSelector ? true : false;
- if (! $hasSelector) {
- $id = $this->generateId();
- $this->selector('#'.$id);
- }
- $this->script = $this->script();
- $this->collectAssets();
- if ($hasSelector) {
- return;
- }
- // 没有指定容器选择器,则需自动生成
- $this->setHtmlAttribute([
- 'id' => $id,
- ]);
- return <<<HTML
- <div {$this->formatHtmlAttributes()}></div>
- HTML;
- }
- /**
- * 返回请求结果.
- *
- * @return array
- */
- public function result()
- {
- return [
- 'status' => 1,
- 'options' => $this->options,
- ];
- }
- protected function generateId()
- {
- return 'apex-chart-'.Str::random(8);
- }
- }
|