RadialBarCard.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 RadialBarCard 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. * @var int
  21. */
  22. protected $chartHeight = 150;
  23. /**
  24. * 内容宽度.
  25. *
  26. * @var array
  27. */
  28. protected $contentWidth = [2, 10];
  29. /**
  30. * @var int
  31. */
  32. protected $chartMarginBottom = -10;
  33. /**
  34. * 初始化
  35. */
  36. public function init()
  37. {
  38. parent::init();
  39. $this->useChart();
  40. $this->chart->style("margin-bottom: {$this->chartMarginBottom}px;");
  41. }
  42. /**
  43. * 图表默认配置.
  44. *
  45. * @return array
  46. */
  47. protected function defaultChartOptions()
  48. {
  49. $gradientColor = Admin::color()->success();
  50. $labelColor = '#99a2ac';
  51. return [
  52. 'chart' => [
  53. 'type' => 'radialBar',
  54. ],
  55. 'plotOptions' => [
  56. 'radialBar' => [
  57. 'size' => 150,
  58. 'startAngle' => -150,
  59. 'endAngle' => 150,
  60. 'offsetY' => 20,
  61. 'hollow' => [
  62. 'size' => '65%',
  63. ],
  64. 'track' => [
  65. 'background' => '#fff',
  66. 'strokeWidth' => '100%',
  67. ],
  68. 'dataLabels' => [
  69. 'value' => [
  70. 'offsetY' => 30,
  71. 'color' => $labelColor,
  72. 'fontSize' => '2rem'
  73. ]
  74. ]
  75. ],
  76. ],
  77. 'fill' => [
  78. 'type' => 'gradient',
  79. 'gradient' => [
  80. 'shade' => 'dark',
  81. 'type' => 'horizontal',
  82. 'shadeIntensity' => 0.5,
  83. 'gradientToColors' => [$gradientColor],
  84. 'inverseColors' => true,
  85. 'opacityFrom' => 1,
  86. 'opacityTo' => 1,
  87. 'stops' => [0, 100]
  88. ],
  89. ],
  90. 'stroke' => [
  91. 'dashArray' => 8
  92. ],
  93. ];
  94. }
  95. /**
  96. * 设置卡片底部内容.
  97. *
  98. * @param string|\Closure|Renderable $value
  99. *
  100. * @return $this
  101. */
  102. public function footer($value)
  103. {
  104. $this->options['footer'] = $value;
  105. return $this;
  106. }
  107. /**
  108. * 设置内容宽度.
  109. *
  110. * @param int $left
  111. * @param int $right
  112. *
  113. * @return $this
  114. */
  115. public function contentWidth(int $left, int $right)
  116. {
  117. $this->contentWidth = [$left, $right];
  118. return $this;
  119. }
  120. /**
  121. * @return string
  122. */
  123. public function renderFooter()
  124. {
  125. return Helper::render($this->options['footer']);
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function renderContent()
  131. {
  132. $content = parent::renderContent();
  133. return <<<HTML
  134. <div class="card-content">
  135. <div class="card-body pt-0">
  136. <div class="row">
  137. <div class="metric-content col-sm-{$this->contentWidth[0]} col-12 d-flex flex-column flex-wrap text-center">
  138. {$content}
  139. </div>
  140. <div class="col-sm-{$this->contentWidth[1]} col-12 d-flex justify-content-center">
  141. {$this->renderChart()}
  142. </div>
  143. </div>
  144. <div class="chart-info metric-footer d-flex justify-content-between">
  145. {$this->renderFooter()}
  146. </div>
  147. </div>
  148. </div>
  149. HTML;
  150. }
  151. }