|
|
<?php
namespace App\AdminSupplier\Metrics\Examples;
use App\Models\Order;use App\Models\OrderProductItem;use Dcat\Admin\Admin;use Dcat\Admin\Support\JavaScript;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 = OrderProductItem::query() ->where('supplier_id', Admin::user()->id) ->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\AdminSupplier\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(); }
/** * 重写初始化JS * @return string */ protected function buildDefaultScript() { $options = JavaScript::format($this->options);
return <<<JS(function () { var options = {$options};
var chart = new ApexCharts( $("{$this->containerSelector}")[0], options ); chart.render();
$(window).resize(function () { var height = $(window).height() - $('.Dcat_Admin_Widgets_Box').offset().top - 130; if (height < 320) { height = 320 } chart.updateOptions({ 'chart': { 'height': height } }); }).resize();})();JS; }}
|