6
0
Fork 0

调整订单统计

release
vine_liutk 2021-12-31 16:21:01 +08:00
parent 5fffcf0fc3
commit 7812cca9b1
3 changed files with 58 additions and 22 deletions

View File

@ -35,12 +35,6 @@ class NewUsers extends Line
*/ */
public function handle(Request $request) 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')) { switch ($request->get('option')) {
case '90': case '90':
// 卡片内容 // 卡片内容
@ -82,9 +76,12 @@ class NewUsers extends Line
'series' => [ 'series' => [
[ [
'name' => $this->title, '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") $list = User::selectRaw("COUNT(DISTINCT id) AS userNum, DATE_FORMAT(created_at,'%Y-%m-%d') dDay")
->whereBetween('created_at', [$start_time, $end_time]) ->whereBetween('created_at', [$start_time, $end_time])
->groupBy('dDay')->pluck('userNum', 'dDay')->toArray(); ->groupBy('dDay')->pluck('userNum', 'dDay')->toArray();
for ($i = 0; $i<=$days; $i++) { for ($i = 0; $i<$days; $i++) {
$time = Carbon::now()->subDay($i)->toDateString(); $time = Carbon::now()->subDay($i)->toDateString();
if (!isset($list[Carbon::now()->subDay($i)->toDateString()])) { if (!isset($list[Carbon::now()->subDay($i)->toDateString()])) {
$list[$time] = 0; $list[$time] = 0;
} }
} }
ksort($list); ksort($list);
return array_values($list); return $list;
} }
/** /**

View File

@ -2,6 +2,8 @@
namespace App\Admin\Metrics; namespace App\Admin\Metrics;
use App\Models\Order;
use Carbon\Carbon;
use Dcat\Admin\Admin; use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Bar; use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -20,14 +22,16 @@ class Orders extends Bar
$dark35 = $color->dark35(); $dark35 = $color->dark35();
// 卡片内容宽度 // 卡片内容宽度
$this->contentWidth(5, 7); $this->contentWidth(4, 8);
$this->chartOption('chart.width', '400');
// 标题 // 标题
$this->title('订单'); $this->title('订单');
// 设置下拉选项 // 设置下拉选项
$this->dropdown([ $this->dropdown([
'7' => '最近7天', '7' => '最近7天',
'28' => '最近30天', '30' => '最近30天',
]); ]);
$this->chart->style('float: left;');
// 设置图表颜色 // 设置图表颜色
$this->chartColors([ $this->chartColors([
$dark35, $dark35,
@ -50,18 +54,24 @@ class Orders extends Bar
public function handle(Request $request) public function handle(Request $request)
{ {
switch ($request->get('option')) { switch ($request->get('option')) {
case '7': case '30':
default: $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([ $this->withChart($this->getOrderGenerator($start_time, $end_time, 30));
[ break;
'name' => 'Sessions', case '7':
'data' => [75, 125, 225, 250, 125, 75, 25], 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) public function withChart(array $data)
{ {
// dd(array_values($data));
return $this->chart([ 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;
}
/** /**
* 设置卡片内容. * 设置卡片内容.
* *

View File

@ -26,7 +26,7 @@ class Users extends Donut
$this->chartMarginBottom(50); $this->chartMarginBottom(50);
$this->chart->style('float: none;'); $this->chart->style('float: none;');
$color = Admin::color(); $color = Admin::color();
$dark35 = $color->dark35(); // $dark35 = $color->dark35();
$this->chartColors([ $this->chartColors([
$color->primary(), $color->primary(),
$color->warning(), $color->warning(),