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

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