OrderLine.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Admin\Metrics\Statistics;
  3. use App\Models\Order;
  4. use App\Repositories\Enums\PayStatusEnum;
  5. use Carbon\Carbon;
  6. use Dcat\Admin\Admin;
  7. use Dcat\Admin\Widgets\ApexCharts\Chart;
  8. class OrderLine extends Chart
  9. {
  10. public function __construct($containerSelector = null, $options = [])
  11. {
  12. parent::__construct($containerSelector, $options);
  13. $this->setUpOptions();
  14. }
  15. /**
  16. * 初始化图表配置
  17. */
  18. protected function setUpOptions()
  19. {
  20. $color = Admin::color();
  21. $colors = [$color->primary(), $color->primaryDarker()];
  22. $this->options([
  23. 'chart' => [
  24. 'type' => 'line',
  25. 'height' => 430,
  26. 'zoom' => [
  27. 'enabled' => false
  28. ]
  29. ],
  30. 'dataLabels' => [
  31. 'enabled' => false,
  32. ],
  33. 'stroke' => [
  34. 'curve' => 'smooth'
  35. ],
  36. 'dropShadow' => [
  37. 'enabled' => true,
  38. 'color' => '#000',
  39. 'top' => 18,
  40. 'left' => 7,
  41. 'blur' => 10,
  42. 'opacity' => 0.2
  43. ],
  44. 'grid' => [
  45. 'row' => [
  46. 'colors' => ['#f3f3f3', 'transparent'], // takes an array which will be repeated on columns
  47. 'opacity' => 0.5
  48. ]
  49. ],
  50. 'xaxis' => [
  51. 'categories' => [],
  52. ],
  53. 'markers' => [
  54. 'size' => [4, 7],
  55. ],
  56. 'yaxis' => [
  57. 'title' => ['text' => '订单数'],
  58. ],
  59. ]);
  60. }
  61. /**
  62. * 渲染图表
  63. *
  64. * @return string
  65. */
  66. public function render()
  67. {
  68. $this->buildData();
  69. return parent::render();
  70. }
  71. /**
  72. * 处理图表数据
  73. */
  74. protected function buildData()
  75. {
  76. $nAges = [];
  77. for ($i = 30; $i > 0; $i--) {
  78. $day = Carbon::now()->addDays(-$i)->toDateString();
  79. $nAges[] = [
  80. 'day' => $day,
  81. 'num' => Order::query()->whereDate('pay_time', $day)->where('pay_status', PayStatusEnum::SUCCESS)->count()
  82. ];
  83. }
  84. // 执行你的数据查询逻辑
  85. $data = [
  86. [
  87. 'name' => '订单数',
  88. 'data' => array_column($nAges, 'num')
  89. ]
  90. ];
  91. $categories = array_column($nAges, 'day');
  92. $this->withData($data);
  93. $this->withCategories($categories);
  94. }
  95. /**
  96. * 设置图表数据
  97. *
  98. * @param array $data
  99. *
  100. * @return $this
  101. */
  102. public function withData(array $data)
  103. {
  104. return $this->option('series', $data);
  105. }
  106. /**
  107. * 设置图表类别.
  108. *
  109. * @param array $data
  110. *
  111. * @return $this
  112. */
  113. public function withCategories(array $data)
  114. {
  115. return $this->option('xaxis.categories', $data);
  116. }
  117. }