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

134 lines
2.9 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 App\Models\User;
  7. use Dcat\Admin\Admin;
  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 ProductStatistics extends Bar
  13. {
  14. /**
  15. * 初始化卡片内容
  16. */
  17. protected function init()
  18. {
  19. parent::init();
  20. $color = Admin::color();
  21. // 卡片内容宽度
  22. $this->contentWidth(0, 12);
  23. // 标题
  24. //$this->title('财务统计');
  25. $this->chartHeight = 500;
  26. // 设置下拉选项
  27. $this->dropdown([
  28. '1' => '日',
  29. '30' => '月',
  30. '365' => '年',
  31. ]);
  32. // 设置图表颜色
  33. $this->chartColors([
  34. $color->green(),
  35. ]);
  36. }
  37. /**
  38. * 处理请求
  39. *
  40. * @param Request $request
  41. *
  42. * @return mixed|void
  43. */
  44. public function handle(Request $request)
  45. {
  46. $query = Order::query()
  47. ->where('agent_id',Admin::user()->id)
  48. ->select('*');
  49. switch ($request->get('option')) {
  50. case '1':
  51. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  52. break;
  53. case '30':
  54. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  55. break;
  56. case '365':
  57. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  58. break;
  59. default:
  60. $query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  61. }
  62. $order = $query->groupBy('statistics_time')
  63. ->orderBy('statistics_time')
  64. ->get();
  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. }