链街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.

134 lines
3.9 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($data = [])
  20. {
  21. $this->GoodsActivityReport = new GoodsActivityReport($data);
  22. parent::__construct();
  23. }
  24. protected function init()
  25. {
  26. parent::init();
  27. // 设置标题
  28. $this->title('各市场销售量(单)');
  29. $this->subTitle('各市场销售量的占比图');
  30. $this->chartHeight(170);
  31. $this->chartMarginTop(20);
  32. // 设置下拉菜单
  33. // $this->dropdown([]);
  34. $this->chartLabels($this->labels);
  35. $this->chartColors($this->colors);
  36. }
  37. /**
  38. * 处理请求
  39. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  40. *
  41. * @param Request $request
  42. * @return mixed|void
  43. */
  44. public function handle(Request $request)
  45. {
  46. // 分页的时候不重复查询数据
  47. $currentPage = $request->get('page');
  48. if($currentPage == 1){
  49. $params = $this->parameters();
  50. $data = $this->GoodsActivityReport->getMarketData($params);
  51. $market = MarketModel::getMarketArray();
  52. if(!empty($data) && is_array($data)){
  53. $total = 0;
  54. foreach($data as $key => $value){
  55. $this->data[] = (int)$value['total'];
  56. $this->labels[] = $market[$value['market_id']]??'未知';
  57. $this->colors[] = $this->color[$key];
  58. $total += $value['total'];
  59. }
  60. $this->total['number_total'] = $total;
  61. }
  62. }
  63. $this->chartLabels($this->labels);
  64. $this->chartColors($this->colors);
  65. // 数据查询逻辑
  66. $data = $this->data;
  67. $this->withContent($data);
  68. $this->withChart($data);
  69. }
  70. /**
  71. * 这里返回需要异步传递到 handler 方法的参数
  72. *
  73. * @return array
  74. */
  75. public function parameters(): array
  76. {
  77. $this->marketId = Auth::getMarket();
  78. return [
  79. 'page' => request()->input('page', 1),
  80. 'name' => request()->input('name', ''),
  81. 'market_id' => $this->marketId ? $this->marketId : request()->input('market_id',0),
  82. 'store_id' => request()->input('store_id',0),
  83. 'start_time' => request()->input('start_time',''),
  84. 'end_time' => request()->input('end_time',''),
  85. ];
  86. }
  87. /**
  88. * 设置图表数据.
  89. *
  90. * @param array $data
  91. *
  92. * @return $this
  93. */
  94. public function withChart(array $data)
  95. {
  96. return $this->chart([
  97. 'series' => $data,
  98. ]);
  99. }
  100. /**
  101. * 渲染卡片内容.
  102. *
  103. * @return string
  104. */
  105. public function withContent($data = [])
  106. {
  107. $div = '';
  108. $style = 'margin-bottom: 8px';
  109. if(!empty($data) && is_array($data)){
  110. foreach($data as $key => $value){
  111. $div .= '<div class="d-flex pl-1 pr-1 pt-1" style="'.$style.'"><div style="width: 120px">
  112. <i class="fa fa-circle" style="color:'.$this->colors[$key].'"></i> '.$this->labels[$key].'
  113. </div><div>'.$value.'</div></div>';
  114. }
  115. }
  116. return $this->content(
  117. <<<HTML
  118. {$div}
  119. HTML
  120. );
  121. }
  122. }