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

168 lines
4.4 KiB

  1. <?php
  2. namespace App\Admin\Widgets\Charts;
  3. use App\Admin\Repositories\v3\GoodsActivityReport;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Support\JavaScript;
  6. use Dcat\Admin\Widgets\Metrics\Bar;
  7. use Illuminate\Http\Request;
  8. class OrderGoodsActivityTotalChart extends Bar
  9. {
  10. /**
  11. * 活动商品总数
  12. */
  13. protected $categories = [];
  14. protected $data = [];
  15. protected $total = [];
  16. protected $id;
  17. protected $option;
  18. protected $valueData = [];
  19. protected $GoodsActivityReport = null;
  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->getCountData();
  28. if(!empty($data) && is_array($data)){
  29. $total = 0;
  30. $subsidyTotal = 0;
  31. foreach($data as $key => $value){
  32. if($key < 10){
  33. $this->data[] = $value['total'];
  34. $this->categories[] = $value['dtime'];
  35. $this->valueData[] = $value['subsidy_total'];
  36. }
  37. $total += $value['total'];
  38. $subsidyTotal += $value['subsidy_total'];
  39. }
  40. $this->total['number_total'] = $total;
  41. $this->total['subsidy_total'] = $subsidyTotal;
  42. }
  43. }
  44. parent::__construct();
  45. }
  46. protected function init()
  47. {
  48. parent::init();
  49. $color = Admin::color();
  50. // 设置标题
  51. $this->title('');
  52. // 设置下拉菜单
  53. // $this->dropdown([]);
  54. // $this->chartBarColumnWidth('50%');
  55. // $this->style('display:block');
  56. // 设置图表颜色
  57. $this->chartColors([$color->primary()]);
  58. $this->chartOption(
  59. 'tooltip.x',
  60. ['show' => true]
  61. );
  62. $this->chartOption(
  63. 'tooltip.y.formatter',
  64. JavaScript::make("function (params,index) {
  65. return params ;
  66. }")
  67. );
  68. $this->chartOption(
  69. 'xaxis.type',
  70. 'category'
  71. );
  72. $this->chartOption(
  73. 'xaxis.categories',
  74. $this->categories
  75. );
  76. }
  77. /**
  78. * 处理请求
  79. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  80. *
  81. * @param Request $request
  82. * @return mixed|void
  83. */
  84. public function handle(Request $request)
  85. {
  86. // 数据查询逻辑
  87. $data = $this->data;
  88. $categories = $this->categories;
  89. $chartData = [
  90. [
  91. 'name' => '销量',
  92. 'data' => $data,
  93. ]
  94. ];
  95. $this->withContent($this->total);
  96. $this->withChart($chartData);
  97. $this->withCategories($categories);
  98. }
  99. /**
  100. * 这里返回需要异步传递到 handler 方法的参数
  101. *
  102. * @return array
  103. */
  104. public function parameters(): array
  105. {
  106. return [
  107. 'id' => $this->id,
  108. 'option' => $this->option,
  109. ];
  110. }
  111. /**
  112. * 设置图表数据.
  113. *
  114. * @param array $data
  115. *
  116. * @return $this
  117. */
  118. public function withChart(array $data)
  119. {
  120. return $this->chart([
  121. 'series' => $data,
  122. ]);
  123. }
  124. /**
  125. * 渲染卡片内容.
  126. *
  127. * @return string
  128. */
  129. public function withContent($total = [])
  130. {
  131. $numberTotal = $this->total['number_total'] ?? 0;
  132. $subsidyTotal = $this->total['subsidy_total'] ?? 0;//d-flex
  133. return $this->content(
  134. <<<HTML
  135. <div class="justify-content-between align-items-center mt-1">
  136. <div class="ml-2">
  137. <div class="card" style="background-color:#4e9876; color:#ffffff;">
  138. <div class="card-header"><span>销售总数量(单)</span></div>
  139. <div class="card-body ml-1"><span>{$numberTotal}</span></div>
  140. </div>
  141. </div>
  142. <div class="ml-2">
  143. <div class="card" style="background-color:#4e9876; color:#ffffff">
  144. <div class="card-header"><span>总补贴金额(元)</span></div>
  145. <div class="card-body ml-1"><span>{$subsidyTotal}</span></div>
  146. </div>
  147. </div>
  148. </div>
  149. HTML
  150. );
  151. }
  152. }