diff --git a/app/Admin/Metrics/NewUsers.php b/app/Admin/Metrics/NewUsers.php index c442107f..c13d6a7f 100644 --- a/app/Admin/Metrics/NewUsers.php +++ b/app/Admin/Metrics/NewUsers.php @@ -35,12 +35,6 @@ class NewUsers extends Line */ public function handle(Request $request) { - $generator = function ($len, $min = 10, $max = 300) { - for ($i = 0; $i <= $len; $i++) { - yield mt_rand($min, $max); - } - }; - switch ($request->get('option')) { case '90': // 卡片内容 @@ -82,9 +76,12 @@ class NewUsers extends Line 'series' => [ [ 'name' => $this->title, - 'data' => $data, + 'data' => array_values($data), ], ], + 'xaxis' => [ + 'categories'=> array_keys($data), + ], ]); } @@ -101,14 +98,14 @@ class NewUsers extends Line $list = User::selectRaw("COUNT(DISTINCT id) AS userNum, DATE_FORMAT(created_at,'%Y-%m-%d') dDay") ->whereBetween('created_at', [$start_time, $end_time]) ->groupBy('dDay')->pluck('userNum', 'dDay')->toArray(); - for ($i = 0; $i<=$days; $i++) { + for ($i = 0; $i<$days; $i++) { $time = Carbon::now()->subDay($i)->toDateString(); if (!isset($list[Carbon::now()->subDay($i)->toDateString()])) { $list[$time] = 0; } } ksort($list); - return array_values($list); + return $list; } /** diff --git a/app/Admin/Metrics/Orders.php b/app/Admin/Metrics/Orders.php index 4fdad338..042e290e 100644 --- a/app/Admin/Metrics/Orders.php +++ b/app/Admin/Metrics/Orders.php @@ -2,6 +2,8 @@ namespace App\Admin\Metrics; +use App\Models\Order; +use Carbon\Carbon; use Dcat\Admin\Admin; use Dcat\Admin\Widgets\Metrics\Bar; use Illuminate\Http\Request; @@ -20,14 +22,16 @@ class Orders extends Bar $dark35 = $color->dark35(); // 卡片内容宽度 - $this->contentWidth(5, 7); + $this->contentWidth(4, 8); + $this->chartOption('chart.width', '400'); // 标题 $this->title('订单'); // 设置下拉选项 $this->dropdown([ '7' => '最近7天', - '28' => '最近30天', + '30' => '最近30天', ]); + $this->chart->style('float: left;'); // 设置图表颜色 $this->chartColors([ $dark35, @@ -50,18 +54,24 @@ class Orders extends Bar public function handle(Request $request) { switch ($request->get('option')) { - case '7': - default: + case '30': + $start_time = Carbon::now()->subDays(29)->startOfDay(); + $end_time = Carbon::now()->endOfDay(); // 卡片内容 - $this->withContent('2.7k', '+5.2%'); + $this->withContent($this->getOrderCount($start_time, $end_time).'单', '+5.2%'); // 图表数据 - $this->withChart([ - [ - 'name' => 'Sessions', - 'data' => [75, 125, 225, 250, 125, 75, 25], - ], - ]); + $this->withChart($this->getOrderGenerator($start_time, $end_time, 30)); + break; + case '7': + default: + $start_time = Carbon::now()->subDays(6)->startOfDay(); + $end_time = Carbon::now()->endOfDay(); + // 卡片内容 + $this->withContent($this->getOrderCount($start_time, $end_time).'单', '+5.2%'); + + // 图表数据 + $this->withChart($this->getOrderGenerator($start_time, $end_time, 7)); } } @@ -74,11 +84,40 @@ class Orders extends Bar */ public function withChart(array $data) { + // dd(array_values($data)); return $this->chart([ - 'series' => $data, + 'series' => [ + [ + 'name' => $this->title, + 'data' => array_values($data), + ], + ], ]); } + /** + * 获取指定时间段用户数量 + */ + public function getOrderCount($start_time, $end_time) + { + return Order::whereBetween('created_at', [$start_time, $end_time])->count(); + } + + public function getOrderGenerator($start_time, $end_time, $days, $groupBy='d') + { + $list = Order::selectRaw("COUNT(DISTINCT id) AS orderNum, DATE_FORMAT(created_at,'%Y-%m-%d') dDay") + ->whereBetween('created_at', [$start_time, $end_time]) + ->groupBy('dDay')->pluck('orderNum', 'dDay')->toArray(); + for ($i = 0; $i<$days; $i++) { + $time = Carbon::now()->subDay($i)->toDateString(); + if (!isset($list[Carbon::now()->subDay($i)->toDateString()])) { + $list[$time] = 0; + } + } + ksort($list); + return $list; + } + /** * 设置卡片内容. * diff --git a/app/Admin/Metrics/Users.php b/app/Admin/Metrics/Users.php index 805db470..03f50099 100644 --- a/app/Admin/Metrics/Users.php +++ b/app/Admin/Metrics/Users.php @@ -26,7 +26,7 @@ class Users extends Donut $this->chartMarginBottom(50); $this->chart->style('float: none;'); $color = Admin::color(); - $dark35 = $color->dark35(); + // $dark35 = $color->dark35(); $this->chartColors([ $color->primary(), $color->warning(),