海南旅游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
  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\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 OrderStatistics 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 = OrderProductItem::query()
  69. ->where('supplier_id',Admin::user()->id)
  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("count(id) 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("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  81. break;
  82. case '365':
  83. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  84. break;
  85. default:
  86. $query->addSelect(DB::raw("count(id) 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. }