RadialBarChartCard.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Dcat\Admin\Widgets\Metrics;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Contracts\Support\Renderable;
  6. class RadialBarChartCard extends Card
  7. {
  8. /**
  9. * @var array
  10. */
  11. protected $options = [
  12. 'icon' => null,
  13. 'title' => null,
  14. 'header' => null,
  15. 'content' => null,
  16. 'footer' => null,
  17. 'dropdown' => [],
  18. ];
  19. /**
  20. * 卡片高度.
  21. *
  22. * @var int
  23. */
  24. protected $height = 250;
  25. /**
  26. * 图表高度.
  27. *
  28. * @var int
  29. */
  30. protected $chartHeight = 150;
  31. /**
  32. * 内容宽度.
  33. *
  34. * @var array
  35. */
  36. protected $contentWidth = [2, 10];
  37. /**
  38. * 图表下间距.
  39. *
  40. * @var int
  41. */
  42. protected $chartMarginBottom = -10;
  43. /**
  44. * 图表位置是否靠右.
  45. *
  46. * @var bool
  47. */
  48. protected $chartPullRight = false;
  49. /**
  50. * 初始化.
  51. */
  52. public function init()
  53. {
  54. parent::init();
  55. $this->useChart();
  56. $this->chart->style("margin-bottom: {$this->chartMarginBottom}px;");
  57. }
  58. /**
  59. * 图表默认配置.
  60. *
  61. * @return array
  62. */
  63. protected function defaultChartOptions()
  64. {
  65. $gradientColor = Admin::color()->success();
  66. $labelColor = '#99a2ac';
  67. return [
  68. 'chart' => [
  69. 'type' => 'radialBar',
  70. ],
  71. 'plotOptions' => [
  72. 'radialBar' => [
  73. 'size' => 150,
  74. 'startAngle' => -150,
  75. 'endAngle' => 150,
  76. 'offsetY' => 20,
  77. 'hollow' => [
  78. 'size' => '65%',
  79. ],
  80. 'track' => [
  81. 'background' => '#fff',
  82. 'strokeWidth' => '100%',
  83. ],
  84. 'dataLabels' => [
  85. 'value' => [
  86. 'offsetY' => 30,
  87. 'color' => $labelColor,
  88. 'fontSize' => '2rem',
  89. ],
  90. ],
  91. ],
  92. ],
  93. 'fill' => [
  94. 'type' => 'gradient',
  95. 'gradient' => [
  96. 'shade' => 'dark',
  97. 'type' => 'horizontal',
  98. 'shadeIntensity' => 0.5,
  99. 'gradientToColors' => [$gradientColor],
  100. 'inverseColors' => true,
  101. 'opacityFrom' => 1,
  102. 'opacityTo' => 1,
  103. 'stops' => [0, 100],
  104. ],
  105. ],
  106. 'stroke' => [
  107. 'dashArray' => 8,
  108. ],
  109. ];
  110. }
  111. /**
  112. * 设置卡片底部内容.
  113. *
  114. * @param string|\Closure|Renderable $value
  115. *
  116. * @return $this
  117. */
  118. public function footer($value)
  119. {
  120. $this->options['footer'] = $value;
  121. return $this;
  122. }
  123. /**
  124. * 设置内容宽度.
  125. *
  126. * @param int $left
  127. * @param int $right
  128. *
  129. * @return $this
  130. */
  131. public function contentWidth(int $left, int $right)
  132. {
  133. $this->contentWidth = [$left, $right];
  134. return $this;
  135. }
  136. /**
  137. * 图表位置靠右.
  138. *
  139. * @param bool $value
  140. *
  141. * @return $this
  142. */
  143. public function chartPullRight(bool $value = true)
  144. {
  145. $this->chartPullRight = $value;
  146. return $this;
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function renderFooter()
  152. {
  153. return Helper::render($this->options['footer']);
  154. }
  155. /**
  156. * @return string
  157. */
  158. public function renderContent()
  159. {
  160. $content = null;
  161. if ($this->contentWidth[0]) {
  162. $content = parent::renderContent();
  163. $content = <<<HTML
  164. <div class="metric-content col-sm-{$this->contentWidth[0]}">
  165. {$content}
  166. </div>
  167. HTML;
  168. }
  169. $justifyClass = $this->chartPullRight ? 'justify-content-between' : 'justify-content-center';
  170. return <<<HTML
  171. <div class="card-content">
  172. <div class="card-body pt-0">
  173. <div class="row">
  174. {$content}
  175. <div class="col-sm-{$this->contentWidth[1]} d-flex {$justifyClass}">
  176. <div></div>
  177. <div>{$this->renderChart()}</div>
  178. </div>
  179. </div>
  180. <div class="metric-footer d-flex justify-content-between">
  181. {$this->renderFooter()}
  182. </div>
  183. </div>
  184. </div>
  185. HTML;
  186. }
  187. }