NewUsersDou.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 NewUsersDou extends Line
  10. {
  11. /**
  12. * 图表默认高度.
  13. *
  14. * @var int
  15. */
  16. protected $chartHeight = 100;
  17. /**
  18. * 初始化卡片内容
  19. *
  20. * @return void
  21. */
  22. protected function init()
  23. {
  24. parent::init();
  25. $this->title('新用户折线图');
  26. $this->dropdown([
  27. '7' => 'Last 7 Days',
  28. '28' => 'Last 28 Days',
  29. '30' => 'Last Month',
  30. '365' => 'Last Year',
  31. ]);
  32. }
  33. /**
  34. * 处理请求,获取数值
  35. *
  36. * @param Request $request
  37. *
  38. * @return mixed|void
  39. */
  40. public function handle(Request $request)
  41. {
  42. $generator = function ($len, $min = 10, $max = 300) {
  43. for ($i = 0; $i <= $len; $i++) {
  44. yield mt_rand($min, $max);
  45. }
  46. };
  47. switch ($request->get('option')) {
  48. case '365':
  49. // 卡片内容
  50. $this->withContent(mt_rand(1000, 5000).'k');
  51. // 图表数据 - 多条线
  52. $this->withChart([
  53. '新用户注册' => collect($generator(30, 50, 200))->toArray(),
  54. '活跃用户' => collect($generator(30, 80, 300))->toArray(),
  55. '付费用户' => collect($generator(30, 10, 100))->toArray(),
  56. ]);
  57. break;
  58. case '30':
  59. // 卡片内容
  60. $this->withContent(mt_rand(400, 1000).'k');
  61. // 图表数据 - 多条线
  62. $this->withChart([
  63. '新用户注册' => collect($generator(30, 40, 150))->toArray(),
  64. '活跃用户' => collect($generator(30, 60, 250))->toArray(),
  65. '付费用户' => collect($generator(30, 8, 80))->toArray(),
  66. ]);
  67. break;
  68. case '28':
  69. // 卡片内容
  70. $this->withContent(mt_rand(400, 1000).'k');
  71. // 图表数据 - 多条线
  72. $this->withChart([
  73. '新用户注册' => collect($generator(28, 35, 120))->toArray(),
  74. '活跃用户' => collect($generator(28, 55, 200))->toArray(),
  75. '付费用户' => collect($generator(28, 5, 60))->toArray(),
  76. ]);
  77. break;
  78. case '7':
  79. default:
  80. // 卡片内容
  81. $this->withContent('89.2k');
  82. // 图表数据 - 多条线
  83. $this->withChart([
  84. '新用户注册' => [28, 40, 36, 52, 38, 60, 55],
  85. '活跃用户' => [45, 55, 48, 65, 52, 75, 68],
  86. '付费用户' => [8, 12, 10, 15, 11, 18, 16],
  87. ]);
  88. }
  89. }
  90. /**
  91. * 设置图表数据.
  92. *
  93. * @param array $data 多条线数据,格式:['线名称' => [数据数组], ...]
  94. *
  95. * @return $this
  96. */
  97. public function withChart(array $data)
  98. {
  99. $series = [];
  100. // 定义不同线条的颜色
  101. $colors = [
  102. '#1890ff', // 蓝色 - 新用户注册
  103. '#52c41a', // 绿色 - 活跃用户
  104. '#fa8c16', // 橙色 - 付费用户
  105. '#722ed1', // 紫色
  106. '#eb2f96', // 粉色
  107. '#13c2c2', // 青色
  108. ];
  109. // 如果是单维数组,保持原有兼容性
  110. if (is_numeric(array_keys($data)[0])) {
  111. $series[] = [
  112. 'name' => $this->title,
  113. 'data' => $data,
  114. ];
  115. } else {
  116. // 多条线数据处理
  117. $colorIndex = 0;
  118. foreach ($data as $name => $lineData) {
  119. $series[] = [
  120. 'name' => $name,
  121. 'data' => $lineData,
  122. ];
  123. $colorIndex++;
  124. }
  125. }
  126. return $this->chart([
  127. 'series' => $series,
  128. 'colors' => array_slice($colors, 0, count($series)), // 根据线条数量设置颜色
  129. 'legend' => [
  130. 'show' => true,
  131. 'position' => 'bottom',
  132. 'horizontalAlign' => 'center'
  133. ]
  134. ]);
  135. }
  136. /**
  137. * 设置卡片内容.
  138. *
  139. * @param string $content
  140. *
  141. * @return $this
  142. */
  143. public function withContent($content)
  144. {
  145. return $this->content(
  146. <<<HTML
  147. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  148. <h2 class="ml-1 font-lg-1">{$content}</h2>
  149. <span class="mb-0 mr-1 text-80">{$this->title}</span>
  150. </div>
  151. HTML
  152. );
  153. }
  154. }