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

141 lines
2.7 KiB

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