海南旅游SAAS
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.
 
 
 

141 lines
2.7 KiB

<?php
namespace App\Admin\Metrics\Examples;
use App\Models\Order;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\ApexCharts\Chart;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
class ProductStatistics 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()
{
$query = Order::query()
->select('*');
$dateTime = request('created_at', 0);
if ($dateTime) {
$query->whereBetween('created_at',[$dateTime['start'] . ' 00:00:00',$dateTime['end'].' 23:59:59']);
}
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('statistics_time')
->get()
->toArray();
$this->withData([
[
'name' => '销量',
'data' => Arr::pluck($order,'sum_price')
],
]
);
$this->withCategories(
Arr::pluck($order,'statistics_time')
);
}
/**
* 设置图表数据
*
* @param array $data
*
* @return \App\Admin\Metrics\Examples\ProductStatistics
*/
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();
}
}