NewUsers.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Admin\Metrics\Examples;
  3. use Dcat\Admin\Widgets\Metrics\Line;
  4. use Illuminate\Http\Request;
  5. class NewUsers extends Line
  6. {
  7. /**
  8. * 初始化卡片内容
  9. *
  10. * @return void
  11. */
  12. protected function init()
  13. {
  14. parent::init();
  15. $this->title('New Users');
  16. $this->dropdown([
  17. '7' => 'Last 7 Days',
  18. '28' => 'Last 28 Days',
  19. '30' => 'Last Month',
  20. '365' => 'Last Year',
  21. ]);
  22. }
  23. /**
  24. * 处理请求
  25. *
  26. * @param Request $request
  27. *
  28. * @return mixed|void
  29. */
  30. public function handle(Request $request)
  31. {
  32. $generator = function ($len, $min = 10, $max = 300) {
  33. for ($i = 0; $i <= $len; $i++) {
  34. yield mt_rand($min, $max);
  35. }
  36. };
  37. switch ($request->get('option')) {
  38. case '365':
  39. // 卡片内容
  40. $this->withContent(mt_rand(1000, 5000).'k');
  41. // 图表数据
  42. $this->withChart(collect($generator(30))->toArray());
  43. break;
  44. case '30':
  45. // 卡片内容
  46. $this->withContent(mt_rand(400, 1000).'k');
  47. // 图表数据
  48. $this->withChart(collect($generator(30))->toArray());
  49. break;
  50. case '28':
  51. // 卡片内容
  52. $this->withContent(mt_rand(400, 1000).'k');
  53. // 图表数据
  54. $this->withChart(collect($generator(28))->toArray());
  55. break;
  56. case '7':
  57. default:
  58. // 卡片内容
  59. $this->withContent('89.2k');
  60. // 图表数据
  61. $this->withChart([28, 40, 36, 52, 38, 60, 55,]);
  62. }
  63. }
  64. /**
  65. * 设置图表数据.
  66. *
  67. * @param array $data
  68. *
  69. * @return $this
  70. */
  71. public function withChart(array $data)
  72. {
  73. return $this->chart([
  74. 'series' => [
  75. [
  76. 'name' => $this->title,
  77. 'data' => $data,
  78. ],
  79. ],
  80. ]);
  81. }
  82. /**
  83. * 设置卡片内容.
  84. *
  85. * @param string $content
  86. *
  87. * @return $this
  88. */
  89. public function withContent($content)
  90. {
  91. return $this->content(
  92. <<<HTML
  93. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  94. <h2 class="ml-1 font-lg-1">{$content}</h2>
  95. <span class="mb-0 mr-1 text-80">{$this->title}</span>
  96. </div>
  97. HTML
  98. );
  99. }
  100. }