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

132 lines
3.8 KiB

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