From ec13968717c6af528f08186e752d90d2ea908b0e Mon Sep 17 00:00:00 2001
From: lemon <15040771@qq.com>
Date: Mon, 6 Sep 2021 21:39:02 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86=E5=95=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Controllers/OrderStatisticsController.php | 62 +++++-
.../Controllers/UserStatisticsController.php | 55 +++++-
.../Metrics/Examples/OrderStatistics.php | 170 ++++++++--------
.../Metrics/Examples/UserStatistics.php | 186 +++++++++---------
4 files changed, 292 insertions(+), 181 deletions(-)
diff --git a/app/AdminAgent/Controllers/OrderStatisticsController.php b/app/AdminAgent/Controllers/OrderStatisticsController.php
index 14d7fd5..dbfa4e1 100755
--- a/app/AdminAgent/Controllers/OrderStatisticsController.php
+++ b/app/AdminAgent/Controllers/OrderStatisticsController.php
@@ -12,8 +12,11 @@ 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\Box;
use Dcat\Admin\Widgets\Card;
+use Dcat\Admin\Widgets\Dropdown;
use Illuminate\Support\Arr;
+use Illuminate\Support\Str;
class OrderStatisticsController extends AdminController
{
@@ -48,10 +51,22 @@ class OrderStatisticsController extends AdminController
HTML
)
- ->body(function (Row $row) {
- $orders = Order::query()->where('agent_id',Admin::user()->id)->select('*')->selectRaw("count(id) as count_id")->groupBy('status')->get();
- $count = Order::query()->where('agent_id',Admin::user()->id)->count();
+ ->body(function (Row $row){
+ $row->column(6,function (Column $column){
+ $column->row(new \App\AdminAgent\Tools\DataReportDate('data_report'));
+ });
+ })
+ ->body(function (Row $row) {
+ $orders = Order::query()->where('agent_id',Admin::user()->id)->select('*')->selectRaw("count(id) as count_id")->groupBy('status');
+ $count = Order::query()->where('agent_id',Admin::user()->id);
+ $dateTime = request('created_at', 0);
+ if ($dateTime) {
+ $orders->whereBetween('created_at',$dateTime);
+ $count->whereBetween('created_at',$dateTime);
+ }
+ $orders = $orders->get();
+ $count = $count->count();
$row->column(3, function (Column $column) use ($count) {
$column->row(Card::make('总数', function () use ($count) {
return <<body(function (Row $row){
- $row->column(12,new OrderStatistics()
- );
- });
+ // 构建下拉菜单,当点击菜单时发起请求获取数据重新渲染图表
+ $menu = [
+ '1' => '日',
+ '30' => '月',
+ '365' => '年',
+ ];
+ $buttonName = '日';
+ if (Arr::exists($menu, \request()->input('time_key', ''))) {
+ $buttonName = $menu[\request()->input('time_key')];
+ }
+
+ $dropdown = Dropdown::make($menu)
+ ->button(current($menu))
+ ->button($buttonName)
+ ->click()
+ ->map(function ($v, $k) {
+ $querys = \request()->all();
+ $querys['time_key'] = $k;
+ $queryString = http_build_query($querys);
+ $str = Str::replaceFirst('admin-agent/','',request()->path());
+ $url = admin_url($str.'?'.$queryString);
+ // 此处设置的 data-xxx 属性会作为post数据发送到后端api
+ return "{$v}";
+ });
+
+ // 传递自定义参数
+
+ $bar = OrderStatistics::make()
+ ->fetching('$("#my-box").loading()') // 设置loading效果
+ ->fetched('$("#my-box").loading(false)') // 移除loading效果
+ ->click('.switch-bar'); // 设置图表点击菜单则重新发起请求,且被点击的目标元素上的 data-xxx 属性会被作为post数据发送到后端API
+
+ $box = Box::make('成本总额图表', $bar)
+ ->id('my-box') // 设置盒子的ID
+ ->tool($dropdown); // 设置下拉菜单按钮
+
+ $row->column(12, $box);
+ });
}
}
diff --git a/app/AdminAgent/Controllers/UserStatisticsController.php b/app/AdminAgent/Controllers/UserStatisticsController.php
index 11d00bd..fd739ad 100755
--- a/app/AdminAgent/Controllers/UserStatisticsController.php
+++ b/app/AdminAgent/Controllers/UserStatisticsController.php
@@ -13,8 +13,11 @@ 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\Box;
use Dcat\Admin\Widgets\Card;
+use Dcat\Admin\Widgets\Dropdown;
use Illuminate\Support\Arr;
+use Illuminate\Support\Str;
class UserStatisticsController extends AdminController
{
@@ -49,9 +52,51 @@ class UserStatisticsController extends AdminController
HTML
)
- ->body(function (Row $row){
- $row->column(12,new UserStatistics()
- );
- });
- }
+ ->body(function (Row $row) {
+ $row->column(6, function (Column $column) {
+ $column->row(new \App\AdminAgent\Tools\DataReportDate('data_report'));
+ });
+
+ })
+ ->body(function (Row $row) {
+ // 构建下拉菜单,当点击菜单时发起请求获取数据重新渲染图表
+ $menu = [
+ '1' => '日',
+ '30' => '月',
+ '365' => '年',
+ ];
+ $buttonName = '日';
+ if (Arr::exists($menu, \request()->input('time_key', ''))) {
+ $buttonName = $menu[\request()->input('time_key')];
+ }
+
+ $dropdown = Dropdown::make($menu)
+ ->button(current($menu))
+ ->button($buttonName)
+ ->click()
+ ->map(function ($v, $k) {
+ $querys = \request()->all();
+ $querys['time_key'] = $k;
+ $queryString = http_build_query($querys);
+ $str = Str::replaceFirst('admin-agent/', '', request()->path());
+ $url = admin_url($str . '?' . $queryString);
+ // 此处设置的 data-xxx 属性会作为post数据发送到后端api
+ return "{$v}";
+ });
+
+ // 传递自定义参数
+
+ $bar = UserStatistics::make()
+ ->fetching('$("#my-box").loading()') // 设置loading效果
+ ->fetched('$("#my-box").loading(false)') // 移除loading效果
+ ->click('.switch-bar'); // 设置图表点击菜单则重新发起请求,且被点击的目标元素上的 data-xxx 属性会被作为post数据发送到后端API
+
+ $box = Box::make('成本总额图表', $bar)
+ ->id('my-box') // 设置盒子的ID
+ ->tool($dropdown); // 设置下拉菜单按钮
+
+ $row->column(12, $box);
+ });
+
+ }
}
diff --git a/app/AdminAgent/Metrics/Examples/OrderStatistics.php b/app/AdminAgent/Metrics/Examples/OrderStatistics.php
index 09326cb..0c2e839 100644
--- a/app/AdminAgent/Metrics/Examples/OrderStatistics.php
+++ b/app/AdminAgent/Metrics/Examples/OrderStatistics.php
@@ -2,93 +2,29 @@
namespace App\AdminAgent\Metrics\Examples;
-use App\Common\DataTime;
use App\Common\OrderStatus;
use App\Models\Order;
use Dcat\Admin\Admin;
+use Dcat\Admin\Widgets\ApexCharts\Chart;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
-class OrderStatistics extends Bar
+class OrderStatistics extends Chart
{
- /**
- * 初始化卡片内容
- */
- protected function init()
- {
- parent::init();
+ public function __construct()
+ {
+ parent::__construct();
+ $this->setUpOptions();
+ }
- $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 = Order::query()
- ->where('agent_id',Admin::user()->id)
- ->select('*');
- switch ($request->get('option')) {
- case '1':
- $query->selectRaw("count(id) as sum_price,date(created_at) AS statistics_time");
- break;
- case '30':
- $query->selectRaw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time");
- break;
- case '365':
- $query->selectRaw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time");
- break;
- default:
- $query->selectRaw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time");
- }
- $order = $query->groupBy('statistics_time')
- ->orderBy('created_at')
- ->get()
- ->toArray();
- $this->withChart(
- Arr::pluck($order,'sum_price')
- );
- $this->chartLabels(
- Arr::pluck($order,'statistics_time')
- );
- }
-
- /**
- * 设置图表数据.
- *
- * @param array $data
- *
- * @return $this
- */
- public function withChart(array $data)
- {
- return $this->chart([
- 'series' => [[
- 'name' => '订单数',
- 'data' => $data
- ]],
+ /**
+ * 初始化图表配置
+ */
+ protected function setUpOptions()
+ {
+ $this->options([
'chart' => [
//'width' => '180%',
'type' => 'bar',
@@ -96,7 +32,6 @@ class OrderStatistics extends Bar
],
'toolbar' => ['show' => false],
],
- 'colors' => $this->colors,
'plotOptions' => [
'bar' => [
//'columnWidth' => '45%',
@@ -116,7 +51,6 @@ class OrderStatistics extends Bar
'labels' => [
'show' => true,
'style' => [
- 'colors' => $this->colors,
'fontSize' => '12px'
]
],
@@ -127,7 +61,81 @@ class OrderStatistics extends Bar
'tooltip' => [
'x' => ['show' => true],
],
-
]);
- }
+ }
+
+ /**
+ * 处理图表数据
+ */
+ protected function buildData()
+ {
+ $query = Order::query()
+ ->where('agent_id',Admin::user()->id)
+ ->select('*');
+
+ $dateTime = request('created_at', 0);
+ if ($dateTime) {
+ $query->whereBetween('created_at',$dateTime);
+ }
+
+ switch (request('time_key', 0)) {
+ 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"));
+ }
+ $order = $query->groupBy('statistics_time')
+ ->orderBy('created_at')
+ ->get()
+ ->toArray();
+
+
+ $this->withData([
+ [
+ 'name' => '订单数',
+ 'data' => Arr::pluck($order,'sum_price')
+ ],
+ ]
+ );
+ $this->withCategories(
+ Arr::pluck($order,'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();
+ }
}
diff --git a/app/AdminAgent/Metrics/Examples/UserStatistics.php b/app/AdminAgent/Metrics/Examples/UserStatistics.php
index c5b708e..2d8a249 100644
--- a/app/AdminAgent/Metrics/Examples/UserStatistics.php
+++ b/app/AdminAgent/Metrics/Examples/UserStatistics.php
@@ -2,102 +2,30 @@
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\ApexCharts\Chart;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
-class UserStatistics extends Bar
+class UserStatistics extends Chart
{
- /**
- * 初始化卡片内容
- */
- 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 = [];
+ public function __construct()
+ {
+ parent::__construct();
+ $this->setUpOptions();
+ }
- 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
- ]],
+ /**
+ * 初始化图表配置
+ */
+ protected function setUpOptions()
+ {
+ $this->options([
'chart' => [
//'width' => '180%',
'type' => 'bar',
@@ -105,7 +33,6 @@ class UserStatistics extends Bar
],
'toolbar' => ['show' => false],
],
- 'colors' => $this->colors,
'plotOptions' => [
'bar' => [
//'columnWidth' => '45%',
@@ -125,7 +52,6 @@ class UserStatistics extends Bar
'labels' => [
'show' => true,
'style' => [
- 'colors' => $this->colors,
'fontSize' => '12px'
]
],
@@ -136,7 +62,89 @@ class UserStatistics extends Bar
'tooltip' => [
'x' => ['show' => true],
],
-
]);
- }
+ }
+
+ /**
+ * 处理图表数据
+ */
+ protected function buildData()
+ {
+ $query = User::query()
+ ->where('agent_id',Admin::user()->id)
+ ->select('*');
+
+
+ $dateTime = request('created_at', 0);
+ if ($dateTime) {
+ $query->whereBetween('created_at',$dateTime);
+ }
+
+ switch (request('time_key', 0)) {
+ 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->withData([
+ [
+ 'name' => '用户数',
+ 'data' => $userArr
+ ],
+ ]
+ );
+ $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();
+ }
}