DoughnutChartCard.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Dcat\Admin\Widgets\DataCard;
  3. use Dcat\Admin\Widgets\Chart\Doughnut;
  4. use Dcat\Admin\Widgets\Colors;
  5. use Illuminate\Contracts\Support\Arrayable;
  6. class DoughnutChartCard extends Card
  7. {
  8. protected $view = 'admin::widgets.data-card.chart';
  9. /**
  10. * @var Doughnut
  11. */
  12. protected $chart;
  13. /**
  14. * @var array
  15. */
  16. public $dotColors = [];
  17. /**
  18. * @var array
  19. */
  20. protected $dots = [];
  21. public function __construct($title = null, $description = null)
  22. {
  23. $this->setupChart();
  24. $this->setupDotColors();
  25. parent::__construct($title, $description);
  26. }
  27. protected function setupChart()
  28. {
  29. $this->options['chart'] = $this->chart =
  30. Doughnut::make()
  31. ->cutoutPercentage(65)
  32. ->responsive(false) // 去掉自适应,这里固定大小即可,否则手机端显示会有问题
  33. ->height('85px')
  34. ->width('85px')
  35. ->setHtmlAttribute('width', '85px')
  36. ->setHtmlAttribute('height', '85px')
  37. ->disableLegend();
  38. }
  39. protected function setupDotColors()
  40. {
  41. $this->dotColors = $this->chart->colors;
  42. }
  43. /**
  44. * @param \Closure|array $builder
  45. * @param array $data
  46. * @return $this
  47. */
  48. public function chart($builder, array $data = [])
  49. {
  50. if ($builder) {
  51. if ($builder instanceof \Closure) {
  52. $builder($this->chart);
  53. } elseif (is_array($builder) || $builder instanceof Arrayable) {
  54. $this->chart->labels($builder);
  55. }
  56. }
  57. $data && $this->chart->add($data);
  58. return $this;
  59. }
  60. public function orange()
  61. {
  62. $this->chart->orange();
  63. $this->setupDotColors();
  64. return $this;
  65. }
  66. public function green()
  67. {
  68. $this->chart->green();
  69. $this->setupDotColors();
  70. return $this;
  71. }
  72. public function purple()
  73. {
  74. $this->chart->purple();
  75. $this->setupDotColors();
  76. return $this;
  77. }
  78. public function blue()
  79. {
  80. $this->chart->blue();
  81. $this->setupDotColors();
  82. return $this;
  83. }
  84. public function dot($content)
  85. {
  86. $this->dots[] = function () use (&$content) {
  87. $color = array_shift($this->dotColors);
  88. $content = $this->toString($content);
  89. $point = "<i class='fa fa-circle' style='font-size:14px;color:$color;'></i>";
  90. $this->options['content']['left'] .= <<<HTML
  91. <div style='margin:-16px 0 5px;'>
  92. <span style="font-size:13px;color:#414750" >
  93. {$point} &nbsp; $content
  94. </span>
  95. </div>
  96. HTML;
  97. };
  98. return $this;
  99. }
  100. protected function script()
  101. {
  102. $this->buildDots();
  103. if (!$this->allowBuildFetchingScript()) {
  104. return;
  105. }
  106. $this->setupFetchScript();
  107. $this->chart->copy($this);
  108. }
  109. protected function buildDots()
  110. {
  111. foreach ($this->dots as $dot) {
  112. $dot();
  113. }
  114. }
  115. /**
  116. * Return JsonResponse instance.
  117. *
  118. * @param array $data
  119. * @return \Illuminate\Http\JsonResponse
  120. */
  121. public function toJsonResponse(array $data = [])
  122. {
  123. $this->buildDots();
  124. return $this->chart->toJsonResponse(
  125. true,
  126. array_merge($this->buildJsonResponseArray(), $data)
  127. );
  128. }
  129. }