Card.php 7.8 KB

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