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.
|
|
<?php
namespace App\Admin\Widgets\Charts;
use App\Admin\Common\Auth;use App\Admin\Repositories\v3\GoodsActivityReport;use Dcat\Admin\Widgets\Metrics\Donut;use Illuminate\Http\Request;use App\Models\v3\Market as MarketModel;
class OrderGoodsActivityMarketChart extends Donut{ /** * 活动商品总数 */ protected $labels = []; protected $data = []; protected $total = []; protected $colors = [];
protected $GoodsActivityReport = null; protected $color = ['#33b2df', '#546E7A', '#d4526e', '#13d8aa', '#A5978B', '#2b908f', '#f9a3a4', '#90ee7e', '#f48024', '#69d2e7']; public function __construct($params = []) { parent::__construct();
// $params = $this->parameters($params);
$data = $params['list'] ?? []; $market = $params['markets'] ?? [];
if(!empty($data) && is_array($data)){ $total = 0; foreach($data as $key => $value){ $this->data[] = (int)$value['total']; $this->labels[] = $market[$value['market_id']]??'未知'; $this->colors[] = $this->color[$key]; $total += $value['total']; } $this->total['number_total'] = $total; }
$this->chartLabels($this->labels); $this->chartColors($this->colors); // 数据查询逻辑
$data = $this->data; $this->withContent($data); $this->withChart($data); }
protected function init() { parent::init(); // 设置标题
$this->title('各市场销售量(单)'); $this->subTitle('各市场销售量的占比图'); $this->chartHeight(170); $this->chartMarginTop(20); } /** * 处理请求 * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求 * * @param Request $request * @return mixed|void */ public function handleBar(Request $request) { // $this->GoodsActivityReport = new GoodsActivityReport($params);
// 分页的时候不重复查询数据
// $currentPage = $params['page'] ?? 1;
// if($currentPage == 1){
// $data = $this->GoodsActivityReport->getMarketData($params);
// $market = MarketModel::getMarketArray();
// }else{
// }
// 分页的时候不重复查询数据
$currentPage = $request->get('page'); if($currentPage == 1){ $params = $this->parameters(); $data = $this->GoodsActivityReport->getMarketData($params); $market = MarketModel::getMarketArray(); if(!empty($data) && is_array($data)){ $total = 0; foreach($data as $key => $value){ $this->data[] = (int)$value['total']; $this->labels[] = $market[$value['market_id']]??'未知'; $this->colors[] = $this->color[$key]; $total += $value['total']; } $this->total['number_total'] = $total; } } $this->chartLabels($this->labels); $this->chartColors($this->colors); // 数据查询逻辑
$data = $this->data; $this->withContent($data); $this->withChart($data); }
/** * 这里返回需要异步传递到 handler 方法的参数 * * @return array */ public function parameters($data = []): array { $this->marketId = Auth::getMarket(); return [ 'page' => request()->input('page', 1), 'name' => request()->input('name', ''), 'market_id' => $this->marketId ? $this->marketId : request()->input('market_id',0), 'store_id' => request()->input('store_id',0), 'start_time' => request()->input('start_time',''), 'end_time' => request()->input('end_time',''), 'data' => $data ]; }
/** * 设置图表数据. * * @param array $data * * @return $this */ public function withChart(array $data) { return $this->chart([ 'series' => $data, ]); }
/** * 渲染卡片内容. * * @return string */ public function withContent($data = []) { $div = ''; $style = 'margin-bottom: 8px'; if(!empty($data) && is_array($data)){ foreach($data as $key => $value){ $div .= '<div class="d-flex pl-1 pr-1 pt-1" style="'.$style.'"><div style="width: 120px">
<i class="fa fa-circle" style="color:'.$this->colors[$key].'"></i> '.$this->labels[$key].' </div><div>'.$value.'</div></div>'; } } return $this->content( <<<HTML {$div}HTML ); }
}
|