Card.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. namespace Dcat\Admin\Widgets\Metrics;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\Helper;
  5. use Dcat\Admin\Traits\InteractsWithApi;
  6. use Dcat\Admin\Widgets\ApexCharts\Chart;
  7. use Dcat\Admin\Widgets\Widget;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Str;
  10. class Card extends Widget
  11. {
  12. use InteractsWithApi;
  13. /**
  14. * @var string
  15. */
  16. protected $view = 'admin::widgets.metrics.card';
  17. /**
  18. * @var array
  19. */
  20. protected $options = [
  21. 'icon' => null,
  22. 'title' => null,
  23. 'subTitle' => null,
  24. 'header' => null,
  25. 'content' => null,
  26. 'dropdown' => [],
  27. ];
  28. /**
  29. * 图标主题色.
  30. *
  31. * @var string
  32. */
  33. protected $style = 'primary';
  34. /**
  35. * 卡片高度.
  36. *
  37. * @var int|string
  38. */
  39. protected $height = null;
  40. /**
  41. * 图表高度.
  42. *
  43. * @var int
  44. */
  45. protected $chartHeight = 70;
  46. /**
  47. * 图表上间距.
  48. *
  49. * @var int
  50. */
  51. protected $chartMarginTop;
  52. /**
  53. * 图表下间距.
  54. *
  55. * @var int
  56. */
  57. protected $chartMarginBottom;
  58. /**
  59. * 图表配置.
  60. *
  61. * @var array
  62. */
  63. protected $chartOptions = [];
  64. /**
  65. * @var Chart
  66. */
  67. protected $chart;
  68. /**
  69. * @var \Closure
  70. */
  71. protected $chartCallback;
  72. public function __construct($title = null, $icon = null)
  73. {
  74. $this->title($title);
  75. $this->icon($icon);
  76. if ($options = $this->defaultChartOptions()) {
  77. $this->chartOptions = $options;
  78. }
  79. $this->init();
  80. }
  81. /**
  82. * 初始化.
  83. */
  84. protected function init()
  85. {
  86. $this->id('metric-card-'.Str::random(8));
  87. $this->class('card');
  88. }
  89. /**
  90. * 图表默认配置.
  91. *
  92. * @return array
  93. */
  94. protected function defaultChartOptions()
  95. {
  96. return [];
  97. }
  98. /**
  99. * 启用图表.
  100. *
  101. * @return Chart
  102. */
  103. public function useChart()
  104. {
  105. return $this->chart ?: ($this->chart = Chart::make());
  106. }
  107. /**
  108. * 设置图标.
  109. *
  110. * @param string $icon
  111. *
  112. * @return $this
  113. */
  114. public function icon(?string $icon)
  115. {
  116. $this->options['icon'] = $icon;
  117. return $this;
  118. }
  119. /**
  120. * 设置卡片标题.
  121. *
  122. * @param string $title
  123. *
  124. * @return $this
  125. */
  126. public function title(?string $title)
  127. {
  128. $this->options['title'] = $title;
  129. return $this;
  130. }
  131. /**
  132. * 设置卡片子标题.
  133. *
  134. * @param string $title
  135. *
  136. * @return $this
  137. */
  138. public function subTitle(?string $title)
  139. {
  140. $this->options['subTitle'] = $title;
  141. return $this;
  142. }
  143. /**
  144. * 设置卡片头内容.
  145. *
  146. * @param string $contents
  147. *
  148. * @return $this
  149. */
  150. public function header($contents)
  151. {
  152. $this->options['header'] = $contents;
  153. return $this;
  154. }
  155. /**
  156. * 设置卡片内容.
  157. *
  158. * @param string $contents
  159. *
  160. * @return $this
  161. */
  162. public function content($contents)
  163. {
  164. $this->options['content'] = $contents;
  165. return $this;
  166. }
  167. /**
  168. * 设置主题色.
  169. *
  170. * @param string $style
  171. *
  172. * @return $this
  173. */
  174. public function style(string $style)
  175. {
  176. $this->style = $style;
  177. return $this;
  178. }
  179. /**
  180. * 设置卡片的下拉菜单选项.
  181. *
  182. * @param array $items
  183. *
  184. * @return $this
  185. */
  186. public function dropdown(array $items)
  187. {
  188. $this->options['dropdown'] = $items;
  189. return $this;
  190. }
  191. /**
  192. * 设置最小高度.
  193. *
  194. * @param string|int $value
  195. *
  196. * @return $this
  197. */
  198. public function height($value)
  199. {
  200. $this->height = $value;
  201. return $this;
  202. }
  203. /**
  204. * 设置图表配置.
  205. *
  206. * @param string $key
  207. * @param mixed $value
  208. *
  209. * @return $this
  210. */
  211. public function chartOption($key, $value)
  212. {
  213. Arr::set($this->chartOptions, $key, $value);
  214. $this->useChart();
  215. return $this;
  216. }
  217. /**
  218. * 设置图表高度.
  219. *
  220. * @param int $number
  221. *
  222. * @return $this
  223. */
  224. public function chartHeight(int $number)
  225. {
  226. $this->chartHeight = $number;
  227. $this->useChart();
  228. return $this;
  229. }
  230. /**
  231. * 设置图表上间距.
  232. *
  233. * @param int $number
  234. *
  235. * @return $this
  236. */
  237. public function chartMarginTop(int $number)
  238. {
  239. $this->chartMarginBottom = $number;
  240. $this->useChart();
  241. return $this;
  242. }
  243. /**
  244. * 设置图表下间距.
  245. *
  246. * @param int $number
  247. *
  248. * @return $this
  249. */
  250. public function chartMarginBottom(int $number)
  251. {
  252. $this->chartMarginBottom = $number;
  253. $this->useChart();
  254. return $this;
  255. }
  256. /**
  257. * 设置图表label.
  258. *
  259. * @param string|array $label
  260. *
  261. * @return $this
  262. */
  263. public function chartLabels($label)
  264. {
  265. $this->chartOptions['labels'] = (array) $label;
  266. $this->useChart();
  267. return $this;
  268. }
  269. /**
  270. * 设置图表颜色.
  271. *
  272. * @param string|array $colors
  273. *
  274. * @return $this
  275. */
  276. public function chartColors($colors)
  277. {
  278. $this->chartOptions['colors'] = (array) $colors;
  279. $this->useChart();
  280. return $this;
  281. }
  282. /**
  283. * 设置图表.
  284. *
  285. * @param array|\Closure $options
  286. *
  287. * @return $this
  288. */
  289. public function chart($options = [])
  290. {
  291. if ($options instanceof \Closure) {
  292. $this->chartCallback = $options;
  293. } else {
  294. $this->chartOptions = array_merge(
  295. $this->chartOptions,
  296. Helper::array($options)
  297. );
  298. }
  299. $this->useChart();
  300. return $this;
  301. }
  302. /**
  303. * 设置图表.
  304. */
  305. protected function setUpChart()
  306. {
  307. if (! $chart = $this->chart) {
  308. return;
  309. }
  310. $this->setUpChartMargin();
  311. // 设置图表高度
  312. $this->chartOptions['chart']['height'] = $this->chartHeight;
  313. // 颜色
  314. if (empty($this->chartOptions['colors'])) {
  315. $this->chartOptions['colors'] = (array) Admin::color()->get($this->style);
  316. }
  317. // 图表配置选项
  318. $chart->options($this->chartOptions);
  319. if ($callback = $this->chartCallback) {
  320. $callback($chart);
  321. }
  322. }
  323. /**
  324. * 设置图表间距.
  325. *
  326. * @return void
  327. */
  328. protected function setUpChartMargin()
  329. {
  330. if ($this->chartMarginTop !== null) {
  331. $this->chart->style("margin-top: {$this->chartMarginTop}px;");
  332. }
  333. if ($this->chartMarginBottom !== null) {
  334. $this->chart->style("margin-bottom: {$this->chartMarginBottom}px;");
  335. }
  336. }
  337. /**
  338. * @return mixed
  339. */
  340. public function script()
  341. {
  342. if (! $this->allowBuildRequest()) {
  343. return;
  344. }
  345. $id = $this->id();
  346. // 开启loading效果
  347. $this->fetching(
  348. <<<JS
  349. var \$card = $('#{$id}');
  350. \$card.loading();
  351. JS
  352. );
  353. $this->fetched(
  354. <<<'JS'
  355. $card.loading(false);
  356. $card.find('.metric-header').html(response.header);
  357. $card.find('.metric-content').html(response.content);
  358. JS
  359. );
  360. $clickable = "#{$id} .dropdown .select-option";
  361. $cardRequestScript = '';
  362. if ($this->chart) {
  363. // 有图表的情况下,直接使用图表的js代码.
  364. $this->chart->merge($this)->click($clickable);
  365. } else {
  366. // 没有图表,需要构建卡片数据请求js代码.
  367. $cardRequestScript = $this->click($clickable)->buildRequestScript();
  368. }
  369. // 按钮显示选中文本
  370. return <<<JS
  371. $('{$clickable}').click(function () {
  372. $(this).parents('.dropdown').find('.btn').html($(this).text());
  373. });
  374. {$cardRequestScript}
  375. JS;
  376. }
  377. /**
  378. * 渲染卡片头部内容.
  379. *
  380. * @return string
  381. */
  382. public function renderHeader()
  383. {
  384. return Helper::render($this->options['header']);
  385. }
  386. /**
  387. * 渲染卡片主体内容.
  388. *
  389. * @return string
  390. */
  391. public function renderContent()
  392. {
  393. return Helper::render($this->options['content']);
  394. }
  395. /**
  396. * 渲染图表.
  397. *
  398. * @return string
  399. */
  400. public function renderChart()
  401. {
  402. return $this->chart ? $this->chart->render() : '';
  403. }
  404. /**
  405. * 设置卡片高度.
  406. */
  407. protected function setUpCardHeight()
  408. {
  409. if (! $height = $this->height) {
  410. return;
  411. }
  412. if (is_numeric($height)) {
  413. $height .= 'px';
  414. }
  415. $this->appendHtmlAttribute('style', "min-height:{$height};");
  416. }
  417. /**
  418. * @return string
  419. */
  420. public function render()
  421. {
  422. $this->setUpChart();
  423. $this->setUpCardHeight();
  424. $this->script = $this->script();
  425. $this->variables['style'] = $this->style;
  426. $this->variables['header'] = $this->renderHeader();
  427. $this->variables['content'] = $this->renderContent();
  428. return parent::render();
  429. }
  430. /**
  431. * 返回API请求结果.
  432. *
  433. * @return array
  434. */
  435. public function valueResult()
  436. {
  437. $this->setUpChart();
  438. return array_merge(
  439. [
  440. 'status' => 1,
  441. 'header' => $this->renderHeader(),
  442. 'content' => $this->renderContent(),
  443. ],
  444. (array) optional($this->chart)->valueResult()
  445. );
  446. }
  447. }