链街Dcat后台
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.

159 lines
4.8 KiB

  1. <?php
  2. namespace App\Admin\Widgets\Charts;
  3. use App\Admin\Common\Auth;
  4. use App\Admin\Repositories\v3\GoodsActivityReport;
  5. use Dcat\Admin\Widgets\Metrics\Donut;
  6. use Illuminate\Http\Request;
  7. use App\Models\v3\Market as MarketModel;
  8. class OrderGoodsActivityMarketChart extends Donut
  9. {
  10. /**
  11. * 活动商品总数
  12. */
  13. protected $labels = [];
  14. protected $data = [];
  15. protected $total = [];
  16. protected $colors = [];
  17. protected $GoodsActivityReport = null;
  18. protected $color = ['#33b2df', '#546E7A', '#d4526e', '#13d8aa', '#A5978B', '#2b908f', '#f9a3a4', '#90ee7e', '#f48024', '#69d2e7'];
  19. public function __construct($params = [])
  20. {
  21. parent::__construct();
  22. // $params = $this->parameters($params);
  23. $data = $params['list'] ?? [];
  24. $market = $params['markets'] ?? [];
  25. if(!empty($data) && is_array($data)){
  26. $total = 0;
  27. foreach($data as $key => $value){
  28. $this->data[] = (int)$value['total'];
  29. $this->labels[] = $market[$value['market_id']]??'未知';
  30. $this->colors[] = $this->color[$key];
  31. $total += $value['total'];
  32. }
  33. $this->total['number_total'] = $total;
  34. }
  35. $this->chartLabels($this->labels);
  36. $this->chartColors($this->colors);
  37. // 数据查询逻辑
  38. $data = $this->data;
  39. $this->withContent($data);
  40. $this->withChart($data);
  41. }
  42. protected function init()
  43. {
  44. parent::init();
  45. // 设置标题
  46. $this->title('各市场销售量(单)');
  47. $this->subTitle('各市场销售量的占比图');
  48. $this->chartHeight(170);
  49. $this->chartMarginTop(20);
  50. }
  51. /**
  52. * 处理请求
  53. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  54. *
  55. * @param Request $request
  56. * @return mixed|void
  57. */
  58. public function handleBar(Request $request)
  59. {
  60. // $this->GoodsActivityReport = new GoodsActivityReport($params);
  61. // 分页的时候不重复查询数据
  62. // $currentPage = $params['page'] ?? 1;
  63. // if($currentPage == 1){
  64. // $data = $this->GoodsActivityReport->getMarketData($params);
  65. // $market = MarketModel::getMarketArray();
  66. // }else{
  67. // }
  68. // 分页的时候不重复查询数据
  69. $currentPage = $request->get('page');
  70. if($currentPage == 1){
  71. $params = $this->parameters();
  72. $data = $this->GoodsActivityReport->getMarketData($params);
  73. $market = MarketModel::getMarketArray();
  74. if(!empty($data) && is_array($data)){
  75. $total = 0;
  76. foreach($data as $key => $value){
  77. $this->data[] = (int)$value['total'];
  78. $this->labels[] = $market[$value['market_id']]??'未知';
  79. $this->colors[] = $this->color[$key];
  80. $total += $value['total'];
  81. }
  82. $this->total['number_total'] = $total;
  83. }
  84. }
  85. $this->chartLabels($this->labels);
  86. $this->chartColors($this->colors);
  87. // 数据查询逻辑
  88. $data = $this->data;
  89. $this->withContent($data);
  90. $this->withChart($data);
  91. }
  92. /**
  93. * 这里返回需要异步传递到 handler 方法的参数
  94. *
  95. * @return array
  96. */
  97. public function parameters($data = []): array
  98. {
  99. $this->marketId = Auth::getMarket();
  100. return [
  101. 'page' => request()->input('page', 1),
  102. 'name' => request()->input('name', ''),
  103. 'market_id' => $this->marketId ? $this->marketId : request()->input('market_id',0),
  104. 'store_id' => request()->input('store_id',0),
  105. 'start_time' => request()->input('start_time',''),
  106. 'end_time' => request()->input('end_time',''),
  107. 'data' => $data
  108. ];
  109. }
  110. /**
  111. * 设置图表数据.
  112. *
  113. * @param array $data
  114. *
  115. * @return $this
  116. */
  117. public function withChart(array $data)
  118. {
  119. return $this->chart([
  120. 'series' => $data,
  121. ]);
  122. }
  123. /**
  124. * 渲染卡片内容.
  125. *
  126. * @return string
  127. */
  128. public function withContent($data = [])
  129. {
  130. $div = '';
  131. $style = 'margin-bottom: 8px';
  132. if(!empty($data) && is_array($data)){
  133. foreach($data as $key => $value){
  134. $div .= '<div class="d-flex pl-1 pr-1 pt-1" style="'.$style.'"><div style="width: 120px">
  135. <i class="fa fa-circle" style="color:'.$this->colors[$key].'"></i> '.$this->labels[$key].'
  136. </div><div>'.$value.'</div></div>';
  137. }
  138. }
  139. return $this->content(
  140. <<<HTML
  141. {$div}
  142. HTML
  143. );
  144. }
  145. }