dark35(); // 卡片内容宽度 $this->contentWidth(4, 8); $this->chartOption('chart.width', '400'); // 标题 $this->title('订单'); // 设置下拉选项 $this->dropdown([ '7' => '最近7天', '30' => '最近30天', ]); $this->chart->style('float: left;'); // 设置图表颜色 $this->chartColors([ // $dark35, // $dark35, // $dark35, // $dark35, // $dark35, // $dark35, $color->primary(), ]); } /** * 处理请求 * * @param Request $request * * @return mixed|void */ public function handle(Request $request) { switch ($request->get('option')) { case '30': $start_time = Carbon::now()->subDays(29)->startOfDay(); $end_time = Carbon::now()->endOfDay(); // 卡片内容 $this->withContent($this->getOrderCount($start_time, $end_time).'单', '+5.2%'); // 图表数据 $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)); } } /** * 设置图表数据. * * @param array $data * * @return $this */ public function withChart(array $data) { // dd(array_values($data)); return $this->chart([ '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; } /** * 设置卡片内容. * * @param string $title * @param string $value * @param string $style * * @return $this */ public function withContent($title, $value, $style = 'success') { // 根据选项显示 $label = strtolower( $this->dropdown[request()->option] ?? 'last 7 days' ); $minHeight = '183px'; $orderLink = admin_route('orders.index'); return $this->content( <<