NewUsers.php 2.6 KB

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