海南旅游SAAS
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.

177 lines
3.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminSupplier\Metrics\Examples;
  3. use App\Models\Order;
  4. use App\Models\OrderProductItem;
  5. use Dcat\Admin\Admin;
  6. use Dcat\Admin\Support\JavaScript;
  7. use Dcat\Admin\Widgets\ApexCharts\Chart;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Facades\DB;
  10. class ProductStatistics extends Chart
  11. {
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->setUpOptions();
  16. }
  17. /**
  18. * 初始化图表配置
  19. */
  20. protected function setUpOptions()
  21. {
  22. $this->options([
  23. 'chart' => [
  24. //'width' => '180%',
  25. 'type' => 'bar',
  26. 'events' => [
  27. ],
  28. 'toolbar' => ['show' => false],
  29. ],
  30. 'plotOptions' => [
  31. 'bar' => [
  32. //'columnWidth' => '45%',
  33. 'distributed' => true,
  34. ]
  35. ],
  36. 'dataLabels' => [
  37. 'enabled' => false
  38. ],
  39. 'legend' => [
  40. 'show' => false
  41. ],
  42. 'xaxis' => [
  43. //'categories' =>
  44. // [75, 125, 225, 175, 125, 75, 25]
  45. //,
  46. 'labels' => [
  47. 'show' => true,
  48. 'style' => [
  49. 'fontSize' => '12px'
  50. ]
  51. ],
  52. ],
  53. 'yaxis' => [
  54. 'show' => true
  55. ],
  56. 'tooltip' => [
  57. 'x' => ['show' => true],
  58. ],
  59. ]);
  60. }
  61. /**
  62. * 处理图表数据
  63. */
  64. protected function buildData()
  65. {
  66. $query = OrderProductItem::query()
  67. ->where('supplier_id', Admin::user()->id)
  68. ->select('*');
  69. $dateTime = request('created_at', 0);
  70. if ($dateTime) {
  71. $query->whereBetween('created_at',[$dateTime['start'] . ' 00:00:00',$dateTime['end'].' 23:59:59']);
  72. }
  73. switch (request('time_key', 0)) {
  74. case '1':
  75. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  76. break;
  77. case '30':
  78. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  79. break;
  80. case '365':
  81. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  82. break;
  83. default:
  84. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  85. }
  86. $order = $query->groupBy('statistics_time')
  87. ->orderBy('statistics_time')
  88. ->get()
  89. ->toArray();
  90. $this->withData([
  91. [
  92. 'name' => '销量',
  93. 'data' => Arr::pluck($order,'sum_price')
  94. ],
  95. ]
  96. );
  97. $this->withCategories(
  98. Arr::pluck($order,'statistics_time')
  99. );
  100. }
  101. /**
  102. * 设置图表数据
  103. *
  104. * @param array $data
  105. *
  106. * @return \App\AdminSupplier\Metrics\Examples\ProductStatistics
  107. */
  108. public function withData(array $data)
  109. {
  110. return $this->option('series', $data);
  111. }
  112. /**
  113. * 设置图表类别.
  114. *
  115. * @param array $data
  116. *
  117. * @return $this
  118. */
  119. public function withCategories(array $data)
  120. {
  121. return $this->option('xaxis.categories', $data);
  122. }
  123. /**
  124. * 渲染图表
  125. *
  126. * @return string
  127. */
  128. public function render()
  129. {
  130. $this->buildData();
  131. return parent::render();
  132. }
  133. /**
  134. * 重写初始化JS
  135. * @return string
  136. */
  137. protected function buildDefaultScript()
  138. {
  139. $options = JavaScript::format($this->options);
  140. return <<<JS
  141. (function () {
  142. var options = {$options};
  143. var chart = new ApexCharts(
  144. $("{$this->containerSelector}")[0],
  145. options
  146. );
  147. chart.render();
  148. $(window).resize(function () {
  149. var height = $(window).height() - $('.Dcat_Admin_Widgets_Box').offset().top - 130;
  150. if (height < 320) {
  151. height = 320
  152. }
  153. chart.updateOptions({
  154. 'chart': {
  155. 'height': height
  156. }
  157. });
  158. }).resize();
  159. })();
  160. JS;
  161. }
  162. }