NewDevices.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * @return $this
  47. */
  48. public function withChart(array $data)
  49. {
  50. return $this->chart([
  51. 'series' => $data,
  52. ]);
  53. }
  54. /**
  55. * 设置卡片头部内容.
  56. *
  57. * @return $this
  58. */
  59. protected function withContent($desktop, $mobile)
  60. {
  61. $blue = Admin::color()->alpha('blue2', 0.5);
  62. $style = 'margin-bottom: 8px';
  63. $labelWidth = 120;
  64. return $this->content(
  65. <<<HTML
  66. <div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
  67. <div style="width: {$labelWidth}px">
  68. <i class="fa fa-circle text-primary"></i> {$this->labels[0]}
  69. </div>
  70. <div>{$desktop}</div>
  71. </div>
  72. <div class="d-flex pl-1 pr-1" style="{$style}">
  73. <div style="width: {$labelWidth}px">
  74. <i class="fa fa-circle" style="color: $blue"></i> {$this->labels[1]}
  75. </div>
  76. <div>{$mobile}</div>
  77. </div>
  78. HTML
  79. );
  80. }
  81. }