You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
2.9 KiB
155 lines
2.9 KiB
<?php
|
|
|
|
namespace App\Admin\Metrics\Examples;
|
|
|
|
use App\Common\OrderStatus;
|
|
use App\Models\Order;
|
|
use App\Models\User;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Widgets\ApexCharts\Chart;
|
|
use Dcat\Admin\Widgets\Metrics\Bar;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class UserStatistics extends Chart
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->setUpOptions();
|
|
}
|
|
|
|
/**
|
|
* 初始化图表配置
|
|
*/
|
|
protected function setUpOptions()
|
|
{
|
|
$this->options([
|
|
'chart' => [
|
|
//'width' => '180%',
|
|
'type' => 'bar',
|
|
'events' => [
|
|
],
|
|
'toolbar' => ['show' => false],
|
|
],
|
|
'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' => [
|
|
'fontSize' => '12px'
|
|
]
|
|
],
|
|
],
|
|
'yaxis' => [
|
|
'show' => true
|
|
],
|
|
'tooltip' => [
|
|
'x' => ['show' => true],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 处理图表数据
|
|
*/
|
|
protected function buildData()
|
|
{
|
|
switch (request('time_key', 0)) {
|
|
case '1':
|
|
$time = "DATE_FORMAT(created_at,'%Y-%m-%d')";
|
|
break;
|
|
case '30':
|
|
$time = "DATE_FORMAT(created_at,'%Y-%m')";
|
|
break;
|
|
case '365':
|
|
$time = "DATE_FORMAT(created_at,'%Y')";
|
|
break;
|
|
default:
|
|
$time = "DATE_FORMAT(created_at,'%Y-%m-%d')";
|
|
}
|
|
$model = new User;
|
|
$table = $model->getTable();
|
|
$right = DB::table(function ($query) use ($table,$time){
|
|
$query->selectRaw($time." AS tart_dat")
|
|
->from($table)
|
|
->groupBy('tart_dat');
|
|
},'l_tab');
|
|
|
|
$users = DB::table(function ($query) use ($table,$time) {
|
|
$query->selectRaw($time ." AS start_date")
|
|
->from($table);
|
|
},'r_tab')
|
|
->joinSub($right, 'l_tab', function ($join) {
|
|
$join->on('l_tab.tart_dat', '>=', 'r_tab.start_date');
|
|
})
|
|
->addSelect(DB::raw('tart_dat as statistics_time,count(tart_dat) as count'))
|
|
->orderBy('tart_dat')
|
|
->groupBy('tart_dat');
|
|
$dateTime = request('created_at', 0);
|
|
if ($dateTime) {
|
|
$users->whereBetween('tart_dat',$dateTime);
|
|
}
|
|
|
|
$users = $users->get()
|
|
->toArray();
|
|
|
|
|
|
$this->withData([
|
|
[
|
|
'name' => '用户数',
|
|
'data' => Arr::pluck($users,'count')
|
|
],
|
|
]
|
|
);
|
|
$this->withCategories(
|
|
Arr::pluck($users,'statistics_time')
|
|
);
|
|
|
|
}
|
|
|
|
public function withData(array $data)
|
|
{
|
|
return $this->option('series', $data);
|
|
}
|
|
|
|
/**
|
|
* 设置图表类别.
|
|
*
|
|
* @param array $data
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function withCategories(array $data)
|
|
{
|
|
return $this->option('xaxis.categories', $data);
|
|
}
|
|
|
|
/**
|
|
* 渲染图表
|
|
*
|
|
* @return string
|
|
*/
|
|
public function render()
|
|
{
|
|
$this->buildData();
|
|
|
|
return parent::render();
|
|
}
|
|
}
|