Card.php 10 KB

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