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

178 lines
4.7 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 $GoodsActivityReport = null;
  19. protected $valueData = [];
  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) {
  65. // return params;
  66. // }")
  67. // );
  68. $this->chartOption(
  69. 'tooltip.trigger',
  70. 'axis'
  71. );
  72. $this->chartOption(
  73. 'tooltip.formatter',
  74. JavaScript::make("function (params) {
  75. return 'ff';
  76. }")
  77. );
  78. $this->chartOption(
  79. 'xaxis.type',
  80. 'category'
  81. );
  82. $this->chartOption(
  83. 'xaxis.categories',
  84. $this->categories
  85. );
  86. }
  87. /**
  88. * 处理请求
  89. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  90. *
  91. * @param Request $request
  92. * @return mixed|void
  93. */
  94. public function handle(Request $request)
  95. {
  96. // dd($request);
  97. // 数据查询逻辑
  98. $data = $this->data;
  99. $categories = $this->categories;
  100. $chartData = [
  101. [
  102. 'name' => '销量',
  103. 'data' => $data,
  104. ]
  105. ];
  106. $this->withContent($this->total);
  107. $this->withChart($chartData);
  108. $this->withCategories($categories);
  109. }
  110. /**
  111. * 这里返回需要异步传递到 handler 方法的参数
  112. *
  113. * @return array
  114. */
  115. public function parameters(): array
  116. {
  117. return [
  118. 'id' => $this->id,
  119. 'option' => $this->option,
  120. ];
  121. }
  122. /**
  123. * 设置图表数据.
  124. *
  125. * @param array $data
  126. *
  127. * @return $this
  128. */
  129. public function withChart(array $data)
  130. {
  131. return $this->chart([
  132. 'series' => $data,
  133. ]);
  134. }
  135. /**
  136. * 渲染卡片内容.
  137. *
  138. * @return string
  139. */
  140. public function withContent($total = [])
  141. {
  142. $numberTotal = $this->total['number_total'] ?? 0;
  143. $subsidyTotal = $this->total['subsidy_total'] ?? 0;//d-flex
  144. return $this->content(
  145. <<<HTML
  146. <div class="justify-content-between align-items-center mt-1">
  147. <div class="ml-2">
  148. <div class="card" style="background-color:#4e9876; color:#ffffff;">
  149. <div class="card-header"><span>销售总数量(单)</span></div>
  150. <div class="card-body ml-1"><span>{$numberTotal}</span></div>
  151. </div>
  152. </div>
  153. <div class="ml-2">
  154. <div class="card" style="background-color:#4e9876; color:#ffffff">
  155. <div class="card-header"><span>总补贴金额(元)</span></div>
  156. <div class="card-body ml-1"><span>{$subsidyTotal}</span></div>
  157. </div>
  158. </div>
  159. </div>
  160. HTML
  161. );
  162. }
  163. }