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