链街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\Line;
  7. use Illuminate\Http\Request;
  8. class OrderGoodsActivityCountChart extends Line
  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['subsidy_total'];
  35. $this->categories[] = $value['dtime'];
  36. }
  37. $subsidyTotal += $value['subsidy_total'];
  38. }
  39. $this->total['subsidy_total'] = $subsidyTotal;
  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. $this->chartHeight(140);
  52. // 设置图表颜色
  53. $this->chartColors([$color->primary()]);
  54. $this->chartOption(
  55. 'tooltip.x',
  56. ['show' => true]
  57. );
  58. $this->chartOption(
  59. 'tooltip.y.formatter',
  60. JavaScript::make("function (params,index) {
  61. return params ;
  62. }")
  63. );
  64. $this->chartOption(
  65. 'xaxis.type',
  66. 'category'
  67. );
  68. $this->chartOption(
  69. 'xaxis.categories',
  70. $this->categories
  71. );
  72. }
  73. /**
  74. * 处理请求
  75. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  76. *
  77. * @param Request $request
  78. * @return mixed|void
  79. */
  80. public function handle(Request $request)
  81. {
  82. // 数据查询逻辑
  83. $data = $this->data;
  84. $categories = $this->categories;
  85. $chartData = [
  86. [
  87. 'name' => '金额',
  88. 'data' => $data,
  89. ]
  90. ];
  91. $this->withContent($this->total);
  92. $this->withChart($chartData);
  93. $this->withCategories($categories);
  94. }
  95. /**
  96. * 这里返回需要异步传递到 handler 方法的参数
  97. *
  98. * @return array
  99. */
  100. public function parameters(): array
  101. {
  102. return [
  103. 'id' => $this->id,
  104. 'option' => $this->option,
  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. $content = $total['subsidy_total'] ??0;
  128. return $this->content(
  129. <<<HTML
  130. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  131. <h2 class="ml-1 font-lg-1">{$content}</h2>
  132. <!-- <span class="mb-0 mr-1 text-80">{$this->title}</span> -->
  133. </div>
  134. HTML
  135. );
  136. }
  137. }