RadialBar.php 4.1 KB

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