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

128 lines
3.4 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->chartHeight(160);
  48. // 设置下拉菜单
  49. // $this->dropdown([]);
  50. $this->chartLabels($this->labels);
  51. // 设置图表颜色
  52. $this->chartColors($this->colors);
  53. }
  54. /**
  55. * 处理请求
  56. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  57. *
  58. * @param Request $request
  59. * @return mixed|void
  60. */
  61. public function handle(Request $request)
  62. {
  63. // 数据查询逻辑
  64. $data = $this->data;
  65. $this->withContent($data);
  66. $this->withChart($data);
  67. }
  68. /**
  69. * 这里返回需要异步传递到 handler 方法的参数
  70. *
  71. * @return array
  72. */
  73. public function parameters(): array
  74. {
  75. return [
  76. 'id' => $this->id,
  77. 'option' => $this->option,
  78. ];
  79. }
  80. /**
  81. * 设置图表数据.
  82. *
  83. * @param array $data
  84. *
  85. * @return $this
  86. */
  87. public function withChart(array $data)
  88. {
  89. return $this->chart([
  90. 'series' => $data,
  91. ]);
  92. }
  93. /**
  94. * 渲染卡片内容.
  95. *
  96. * @return string
  97. */
  98. public function withContent($data = [])
  99. {
  100. $div = '';
  101. $style = 'margin-bottom: 8px';
  102. if(!empty($data) && is_array($data)){
  103. foreach($data as $key => $value){
  104. $div .= '<div class="d-flex pl-1 pr-1 pt-1" style="'.$style.'"><div style="width: 120px">
  105. <i class="fa fa-circle" style="color:'.$this->colors[$key].'"></i> '.$this->labels[$key].'
  106. </div><div>'.$value.'</div></div>';
  107. }
  108. }
  109. return $this->content(
  110. <<<HTML
  111. {$div}
  112. HTML
  113. );
  114. }
  115. }