NewUsers.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * @return mixed|void
  27. */
  28. public function handle(Request $request)
  29. {
  30. $generator = function ($len, $min = 10, $max = 300) {
  31. for ($i = 0; $i <= $len; $i++) {
  32. yield mt_rand($min, $max);
  33. }
  34. };
  35. switch ($request->get('option')) {
  36. case '365':
  37. // 卡片内容
  38. $this->withContent(mt_rand(1000, 5000) . 'k');
  39. // 图表数据
  40. $this->withChart(collect($generator(30))->toArray());
  41. break;
  42. case '30':
  43. // 卡片内容
  44. $this->withContent(mt_rand(400, 1000) . 'k');
  45. // 图表数据
  46. $this->withChart(collect($generator(30))->toArray());
  47. break;
  48. case '28':
  49. // 卡片内容
  50. $this->withContent(mt_rand(400, 1000) . 'k');
  51. // 图表数据
  52. $this->withChart(collect($generator(28))->toArray());
  53. break;
  54. case '7':
  55. default:
  56. // 卡片内容
  57. $this->withContent('89.2k');
  58. // 图表数据
  59. $this->withChart([28, 40, 36, 52, 38, 60, 55]);
  60. }
  61. }
  62. /**
  63. * 设置图表数据.
  64. *
  65. * @return $this
  66. */
  67. public function withChart(array $data)
  68. {
  69. return $this->chart([
  70. 'series' => [
  71. [
  72. 'name' => $this->title,
  73. 'data' => $data,
  74. ],
  75. ],
  76. ]);
  77. }
  78. /**
  79. * 设置卡片内容.
  80. *
  81. * @param string $content
  82. *
  83. * @return $this
  84. */
  85. public function withContent($content)
  86. {
  87. return $this->content(
  88. <<<HTML
  89. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  90. <h2 class="ml-1 font-lg-1">{$content}</h2>
  91. <span class="mb-0 mr-1 text-80">{$this->title}</span>
  92. </div>
  93. HTML
  94. );
  95. }
  96. }