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

152 lines
4.0 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 $valueData = [];
  17. protected $GoodsActivityReport = null;
  18. protected $showNumber = 7;
  19. public function __construct($data = [])
  20. {
  21. $this->GoodsActivityReport = new GoodsActivityReport();
  22. parent::__construct();
  23. }
  24. protected function init()
  25. {
  26. parent::init();
  27. $color = Admin::color();
  28. // 设置标题
  29. $this->title('销售总数量(单)');
  30. $this->subTitle('其中'.$this->showNumber.'天每天的销量');
  31. // 设置图表颜色
  32. $this->chartColors([$color->primary()]);
  33. $this->chartOption(
  34. 'tooltip.x',
  35. ['show' => true]
  36. );
  37. $this->chartOption(
  38. 'tooltip.y.formatter',
  39. JavaScript::make("function (params,index) {
  40. return params ;
  41. }")
  42. );
  43. $this->chartOption(
  44. 'xaxis.type',
  45. 'category'
  46. );
  47. $this->chartOption(
  48. 'xaxis.categories',
  49. $this->categories
  50. );
  51. }
  52. /**
  53. * 处理请求
  54. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  55. *
  56. * @param Request $request
  57. * @return mixed|void
  58. */
  59. public function handle(Request $request)
  60. {
  61. // 数据查询逻辑
  62. // 分页的时候不重复查询数据
  63. $currentPage = request()->input('page', 1);
  64. if($currentPage == 1){
  65. $params = $this->parameters();
  66. $data = $this->GoodsActivityReport->getCountData($params);
  67. if(!empty($data) && is_array($data)){
  68. $total = 0;
  69. foreach($data as $key => $value){
  70. if($key < $this->showNumber){
  71. $this->data[] = $value['total'];
  72. $this->categories[] = $value['dtime'];
  73. }
  74. $total += $value['total'];
  75. }
  76. $this->total['number_total'] = $total;
  77. }
  78. }
  79. $data = $this->data;
  80. $categories = $this->categories;
  81. $chartData = [
  82. [
  83. 'name' => '销量',
  84. 'data' => $data,
  85. ]
  86. ];
  87. $this->withContent($this->total);
  88. $this->withChart($chartData);
  89. $this->withCategories($categories);
  90. }
  91. /**
  92. * 这里返回需要异步传递到 handler 方法的参数
  93. *
  94. * @return array
  95. */
  96. public function parameters(): array
  97. {
  98. return [
  99. 'page' => request()->input('page', 1),
  100. 'name' => request()->input('name', ''),
  101. 'market_id' => request()->input('market_id',0),
  102. 'store_id' => request()->input('store_id',0),
  103. 'start_time' => request()->input('start_time',''),
  104. 'end_time' => request()->input('end_time',''),
  105. ];
  106. }
  107. /**
  108. * 设置图表数据.
  109. *
  110. * @param array $data
  111. *
  112. * @return $this
  113. */
  114. public function withChart(array $data)
  115. {
  116. return $this->chart([
  117. 'series' => $data,
  118. ]);
  119. }
  120. /**
  121. * 渲染卡片内容.
  122. *
  123. * @return string
  124. */
  125. public function withContent($total = [])
  126. {
  127. $value = $total['number_total'] ?? 0;
  128. $minHeight = '113px';
  129. return $this->content(
  130. <<<HTML
  131. <div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
  132. <div class="text-left">
  133. <h2 class="ml-1 font-lg-1">{$value}</h2>
  134. </div>
  135. </div>
  136. HTML
  137. );
  138. }
  139. }