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

129 lines
3.5 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 $id;
  16. protected $option;
  17. protected $GoodsActivityReport = null;
  18. protected $color = ['#33b2df', '#546E7A', '#d4526e', '#13d8aa', '#A5978B', '#2b908f', '#f9a3a4', '#90ee7e', '#f48024', '#69d2e7'];
  19. protected $colors = [];
  20. public function __construct($data = [])
  21. {
  22. $this->GoodsActivityReport = new GoodsActivityReport();
  23. $this->option = $this->id = 7;
  24. // 分页的时候不重复查询数据
  25. $currentPage = request()->input('page', 1);
  26. if($currentPage == 1){
  27. $data = $this->GoodsActivityReport->getMarketData();
  28. $market = MarketModel::getMarketArray();
  29. if(!empty($data) && is_array($data)){
  30. $total = 0;
  31. foreach($data as $key => $value){
  32. $this->data[] = (int)$value['total'];
  33. $this->labels[] = $market[$value['market_id']]??'未知';
  34. $this->colors[] = $this->color[$key];
  35. $total += $value['total'];
  36. }
  37. $this->total['number_total'] = $total;
  38. }
  39. }
  40. parent::__construct();
  41. }
  42. protected function init()
  43. {
  44. parent::init();
  45. // 设置标题
  46. $this->title('各市场销售量(单)');
  47. $this->subTitle('各市场销售量的占比图');
  48. $this->chartHeight(170);
  49. // 设置下拉菜单
  50. // $this->dropdown([]);
  51. $this->chartLabels($this->labels);
  52. // 设置图表颜色
  53. $this->chartColors($this->colors);
  54. }
  55. /**
  56. * 处理请求
  57. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  58. *
  59. * @param Request $request
  60. * @return mixed|void
  61. */
  62. public function handle(Request $request)
  63. {
  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. 'id' => $this->id,
  78. 'option' => $this->option,
  79. ];
  80. }
  81. /**
  82. * 设置图表数据.
  83. *
  84. * @param array $data
  85. *
  86. * @return $this
  87. */
  88. public function withChart(array $data)
  89. {
  90. return $this->chart([
  91. 'series' => $data,
  92. ]);
  93. }
  94. /**
  95. * 渲染卡片内容.
  96. *
  97. * @return string
  98. */
  99. public function withContent($data = [])
  100. {
  101. $div = '';
  102. $style = 'margin-bottom: 8px';
  103. if(!empty($data) && is_array($data)){
  104. foreach($data as $key => $value){
  105. $div .= '<div class="d-flex pl-1 pr-1 pt-1" style="'.$style.'"><div style="width: 120px">
  106. <i class="fa fa-circle" style="color:'.$this->colors[$key].'"></i> '.$this->labels[$key].'
  107. </div><div>'.$value.'</div></div>';
  108. }
  109. }
  110. return $this->content(
  111. <<<HTML
  112. {$div}
  113. HTML
  114. );
  115. }
  116. }