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