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('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(); } }