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

142 lines
2.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\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. ->where('agent_id', Admin::user()->id)
  66. ->select('*');
  67. $dateTime = request('created_at', 0);
  68. if ($dateTime) {
  69. $query->whereBetween('created_at',$dateTime);
  70. }
  71. switch (request('time_key', 0)) {
  72. case '1':
  73. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  74. break;
  75. case '30':
  76. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  77. break;
  78. case '365':
  79. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  80. break;
  81. default:
  82. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  83. }
  84. $order = $query->groupBy('statistics_time')
  85. ->orderBy('statistics_time')
  86. ->get()
  87. ->toArray();
  88. $this->withData([
  89. [
  90. 'name' => '销量',
  91. 'data' => Arr::pluck($order,'sum_price')
  92. ],
  93. ]
  94. );
  95. $this->withCategories(
  96. Arr::pluck($order,'statistics_time')
  97. );
  98. }
  99. /**
  100. * 设置图表数据
  101. *
  102. * @param array $data
  103. *
  104. * @return \App\AdminAgent\Metrics\Examples\ProductStatistics
  105. */
  106. public function withData(array $data)
  107. {
  108. return $this->option('series', $data);
  109. }
  110. /**
  111. * 设置图表类别.
  112. *
  113. * @param array $data
  114. *
  115. * @return $this
  116. */
  117. public function withCategories(array $data)
  118. {
  119. return $this->option('xaxis.categories', $data);
  120. }
  121. /**
  122. * 渲染图表
  123. *
  124. * @return string
  125. */
  126. public function render()
  127. {
  128. $this->buildData();
  129. return parent::render();
  130. }
  131. }