NewDevices.php 2.0 KB

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