Episodes.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Admin\Metrics;
  3. use App\Models\Episode;
  4. use App\Models\EpisodesList;
  5. use Dcat\Admin\Admin;
  6. use Dcat\Admin\Widgets\Metrics\Donut;
  7. class Episodes extends Donut
  8. {
  9. protected $labels = ['短剧总数量', '剧集总数量'];
  10. private $episodeCount = 0;
  11. private $episodeListCount = 0;
  12. /**
  13. * 初始化卡片内容.
  14. */
  15. protected function init()
  16. {
  17. parent::init();
  18. $color = Admin::color();
  19. $colors = [$color->primary(), $color->alpha('blue2', 0.5)];
  20. $this->title('短剧/剧集');
  21. $this->chartLabels($this->labels);
  22. // 设置图表颜色
  23. $this->chartColors($colors);
  24. $this->episodeCount = $this->getEpisodeCount();
  25. $this->episodeListCount = $this->getEpisodeListCount();
  26. }
  27. private function getEpisodeCount()
  28. {
  29. return Episode::count();
  30. }
  31. private function getEpisodeListCount()
  32. {
  33. return EpisodesList::count();
  34. }
  35. /**
  36. * 渲染模板
  37. *
  38. * @return string
  39. */
  40. public function render()
  41. {
  42. $this->fill();
  43. return parent::render();
  44. }
  45. /**
  46. * 写入数据.
  47. *
  48. * @return void
  49. */
  50. public function fill()
  51. {
  52. $this->withContent($this->episodeCount, $this->episodeListCount);
  53. // 图表数据
  54. $this->withChart([$this->episodeCount, $this->episodeListCount]);
  55. }
  56. /**
  57. * 设置图表数据.
  58. *
  59. * @return $this
  60. */
  61. public function withChart(array $data)
  62. {
  63. return $this->chart([
  64. 'series' => $data,
  65. ]);
  66. }
  67. /**
  68. * 设置卡片头部内容.
  69. *
  70. * @return $this
  71. */
  72. protected function withContent($episode, $episodeList)
  73. {
  74. $blue = Admin::color()->alpha('blue2', 0.5);
  75. $style = 'margin-bottom: 8px';
  76. $labelWidth = 120;
  77. return $this->content(
  78. <<<HTML
  79. <div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
  80. <div style="width: {$labelWidth}px">
  81. <i class="fa fa-circle text-primary"></i> {$this->labels[0]}
  82. </div>
  83. <div>{$episode}</div>
  84. </div>
  85. <div class="d-flex pl-1 pr-1" style="{$style}">
  86. <div style="width: {$labelWidth}px">
  87. <i class="fa fa-circle" style="color: $blue"></i> {$this->labels[1]}
  88. </div>
  89. <div>{$episodeList}</div>
  90. </div>
  91. HTML
  92. );
  93. }
  94. }