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

133 lines
2.8 KiB

4 years ago
  1. <?php
  2. namespace App\AdminAgent\Metrics\Examples;
  3. use App\Common\DataTime;
  4. use App\Common\OrderStatus;
  5. use App\Models\Order;
  6. use Dcat\Admin\Admin;
  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 OrderStatistics extends Bar
  12. {
  13. /**
  14. * 初始化卡片内容
  15. */
  16. protected function init()
  17. {
  18. parent::init();
  19. $color = Admin::color();
  20. // 卡片内容宽度
  21. $this->contentWidth(0, 12);
  22. // 标题
  23. //$this->title('财务统计');
  24. $this->chartHeight = 500;
  25. // 设置下拉选项
  26. $this->dropdown([
  27. '1' => '日',
  28. '30' => '月',
  29. '365' => '年',
  30. ]);
  31. // 设置图表颜色
  32. $this->chartColors([
  33. $color->green(),
  34. ]);
  35. }
  36. /**
  37. * 处理请求
  38. *
  39. * @param Request $request
  40. *
  41. * @return mixed|void
  42. */
  43. public function handle(Request $request)
  44. {
  45. $query = Order::query()
  46. ->where('agent_id',Admin::user()->id)
  47. ->select('*');
  48. switch ($request->get('option')) {
  49. case '1':
  50. $query->selectRaw("count(id) as sum_price,date(created_at) AS statistics_time");
  51. break;
  52. case '30':
  53. $query->selectRaw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time");
  54. break;
  55. case '365':
  56. $query->selectRaw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time");
  57. break;
  58. default:
  59. $query->selectRaw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time");
  60. }
  61. $order = $query->groupBy('statistics_time')
  62. ->orderBy('created_at')
  63. ->get()
  64. ->toArray();
  65. $this->withChart(
  66. Arr::pluck($order,'sum_price')
  67. );
  68. $this->chartLabels(
  69. Arr::pluck($order,'statistics_time')
  70. );
  71. }
  72. /**
  73. * 设置图表数据.
  74. *
  75. * @param array $data
  76. *
  77. * @return $this
  78. */
  79. public function withChart(array $data)
  80. {
  81. return $this->chart([
  82. 'series' => [[
  83. 'name' => '订单数',
  84. 'data' => $data
  85. ]],
  86. 'chart' => [
  87. //'width' => '180%',
  88. 'type' => 'bar',
  89. 'events' => [
  90. ],
  91. 'toolbar' => ['show' => false],
  92. ],
  93. 'colors' => $this->colors,
  94. 'plotOptions' => [
  95. 'bar' => [
  96. //'columnWidth' => '45%',
  97. 'distributed' => true,
  98. ]
  99. ],
  100. 'dataLabels' => [
  101. 'enabled' => false
  102. ],
  103. 'legend' => [
  104. 'show' => false
  105. ],
  106. 'xaxis' => [
  107. //'categories' =>
  108. // [75, 125, 225, 175, 125, 75, 25]
  109. //,
  110. 'labels' => [
  111. 'show' => true,
  112. 'style' => [
  113. 'colors' => $this->colors,
  114. 'fontSize' => '12px'
  115. ]
  116. ],
  117. ],
  118. 'yaxis' => [
  119. 'show' => true
  120. ],
  121. 'tooltip' => [
  122. 'x' => ['show' => true],
  123. ],
  124. ]);
  125. }
  126. }