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

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