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

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