3 changed files with 200 additions and 0 deletions
-
57app/AdminAgent/Controllers/UserStatisticsController.php
-
142app/AdminAgent/Metrics/Examples/UserStatistics.php
-
1app/AdminAgent/routes.php
@ -0,0 +1,57 @@ |
|||
<?php |
|||
|
|||
namespace App\AdminAgent\Controllers; |
|||
|
|||
use App\AdminAgent\Metrics\Examples\FinanceStatistics; |
|||
use App\AdminAgent\Metrics\Examples\OrderStatistics; |
|||
use App\AdminAgent\Metrics\Examples\UserStatistics; |
|||
use App\Common\OrderStatus; |
|||
use App\Models\Order; |
|||
use App\Models\OrderProductItem; |
|||
use Dcat\Admin\Admin; |
|||
use Dcat\Admin\Layout\Column; |
|||
use Dcat\Admin\Layout\Content; |
|||
use Dcat\Admin\Layout\Row; |
|||
use Dcat\Admin\Http\Controllers\AdminController; |
|||
use Dcat\Admin\Widgets\Card; |
|||
use Illuminate\Support\Arr; |
|||
|
|||
class UserStatisticsController extends AdminController |
|||
{ |
|||
public function index(Content $content) |
|||
{ |
|||
Admin::style( |
|||
<<<CSS |
|||
.col-sm-12.d-flex{ |
|||
display: inline-block !important; |
|||
} |
|||
CSS |
|||
); |
|||
|
|||
//数据
|
|||
|
|||
//订单
|
|||
|
|||
|
|||
return $content |
|||
->body( |
|||
<<<HTML |
|||
<div class="content-header"> |
|||
<section class="content-header breadcrumbs-top"> |
|||
<h1 class=" float-left"> |
|||
<span class="text-capitalize">用户统计</span> |
|||
|
|||
</h1> |
|||
<div class="clearfix"></div> |
|||
|
|||
</section> |
|||
</div> |
|||
HTML |
|||
|
|||
) |
|||
->body(function (Row $row){ |
|||
$row->column(12,new UserStatistics() |
|||
); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,142 @@ |
|||
<?php |
|||
|
|||
namespace App\AdminAgent\Metrics\Examples; |
|||
|
|||
use App\Common\DataTime; |
|||
use App\Common\OrderStatus; |
|||
use App\Models\Order; |
|||
use App\Models\User; |
|||
use Dcat\Admin\Admin; |
|||
use Dcat\Admin\Widgets\Metrics\Bar; |
|||
use Illuminate\Http\Request; |
|||
use Illuminate\Support\Arr; |
|||
use Illuminate\Support\Facades\DB; |
|||
|
|||
class UserStatistics extends Bar |
|||
{ |
|||
/** |
|||
* 初始化卡片内容 |
|||
*/ |
|||
protected function init() |
|||
{ |
|||
parent::init(); |
|||
|
|||
$color = Admin::color(); |
|||
// 卡片内容宽度
|
|||
$this->contentWidth(0, 12); |
|||
// 标题
|
|||
//$this->title('财务统计');
|
|||
$this->chartHeight = 500; |
|||
// 设置下拉选项
|
|||
$this->dropdown([ |
|||
'1' => '日', |
|||
'30' => '月', |
|||
'365' => '年', |
|||
]); |
|||
// 设置图表颜色
|
|||
$this->chartColors([ |
|||
$color->green(), |
|||
]); |
|||
} |
|||
|
|||
/** |
|||
* 处理请求 |
|||
* |
|||
* @param Request $request |
|||
* |
|||
* @return mixed|void |
|||
*/ |
|||
public function handle(Request $request) |
|||
{ |
|||
$query = User::query() |
|||
->where('agent_id',Admin::user()->id) |
|||
->select('*'); |
|||
switch ($request->get('option')) { |
|||
case '1': |
|||
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); |
|||
break; |
|||
case '30': |
|||
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time")); |
|||
break; |
|||
case '365': |
|||
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time")); |
|||
break; |
|||
default: |
|||
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); |
|||
} |
|||
$users = $query->groupBy('statistics_time') |
|||
->orderBy('statistics_time') |
|||
->get(); |
|||
|
|||
$userNum = 0; |
|||
$userArr = []; |
|||
|
|||
foreach ($users as $user) { |
|||
$userNum += $user->sum_price; |
|||
array_push($userArr,$userNum); |
|||
} |
|||
|
|||
$this->withChart( |
|||
$userArr |
|||
); |
|||
$this->chartLabels( |
|||
Arr::pluck($users,'statistics_time') |
|||
); |
|||
} |
|||
|
|||
/** |
|||
* 设置图表数据. |
|||
* |
|||
* @param array $data |
|||
* |
|||
* @return $this |
|||
*/ |
|||
public function withChart(array $data) |
|||
{ |
|||
return $this->chart([ |
|||
'series' => [[ |
|||
'name' => '用户数', |
|||
'data' => $data |
|||
]], |
|||
'chart' => [ |
|||
//'width' => '180%',
|
|||
'type' => 'bar', |
|||
'events' => [ |
|||
], |
|||
'toolbar' => ['show' => false], |
|||
], |
|||
'colors' => $this->colors, |
|||
'plotOptions' => [ |
|||
'bar' => [ |
|||
//'columnWidth' => '45%',
|
|||
'distributed' => true, |
|||
] |
|||
], |
|||
'dataLabels' => [ |
|||
'enabled' => false |
|||
], |
|||
'legend' => [ |
|||
'show' => false |
|||
], |
|||
'xaxis' => [ |
|||
//'categories' =>
|
|||
// [75, 125, 225, 175, 125, 75, 25]
|
|||
//,
|
|||
'labels' => [ |
|||
'show' => true, |
|||
'style' => [ |
|||
'colors' => $this->colors, |
|||
'fontSize' => '12px' |
|||
] |
|||
], |
|||
], |
|||
'yaxis' => [ |
|||
'show' => true |
|||
], |
|||
'tooltip' => [ |
|||
'x' => ['show' => true], |
|||
], |
|||
|
|||
]); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue