123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace App\Admin\Metrics\Statistics;
- use App\Models\Order;
- use App\Repositories\Enums\PayStatusEnum;
- use Carbon\Carbon;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Widgets\ApexCharts\Chart;
- class OrderLine extends Chart
- {
- public function __construct($containerSelector = null, $options = [])
- {
- parent::__construct($containerSelector, $options);
- $this->setUpOptions();
- }
- /**
- * 初始化图表配置
- */
- protected function setUpOptions()
- {
- $color = Admin::color();
- $colors = [$color->primary(), $color->primaryDarker()];
- $this->options([
- 'chart' => [
- 'type' => 'line',
- 'height' => 430,
- 'zoom' => [
- 'enabled' => false
- ]
- ],
- 'dataLabels' => [
- 'enabled' => false,
- ],
- 'stroke' => [
- 'curve' => 'smooth'
- ],
- 'dropShadow' => [
- 'enabled' => true,
- 'color' => '#000',
- 'top' => 18,
- 'left' => 7,
- 'blur' => 10,
- 'opacity' => 0.2
- ],
- 'grid' => [
- 'row' => [
- 'colors' => ['#f3f3f3', 'transparent'], // takes an array which will be repeated on columns
- 'opacity' => 0.5
- ]
- ],
- 'xaxis' => [
- 'categories' => [],
- ],
- 'markers' => [
- 'size' => [4, 7],
- ],
- 'yaxis' => [
- 'title' => ['text' => '订单数'],
- ],
- ]);
- }
- /**
- * 渲染图表
- *
- * @return string
- */
- public function render()
- {
- $this->buildData();
- return parent::render();
- }
- /**
- * 处理图表数据
- */
- protected function buildData()
- {
- $nAges = [];
- for ($i = 30; $i > 0; $i--) {
- $day = Carbon::now()->addDays(-$i)->toDateString();
- $nAges[] = [
- 'day' => $day,
- 'num' => Order::query()->whereDate('pay_time', $day)->where('pay_status', PayStatusEnum::SUCCESS)->count()
- ];
- }
- // 执行你的数据查询逻辑
- $data = [
- [
- 'name' => '订单数',
- 'data' => array_column($nAges, 'num')
- ]
- ];
- $categories = array_column($nAges, 'day');
- $this->withData($data);
- $this->withCategories($categories);
- }
- /**
- * 设置图表数据
- *
- * @param array $data
- *
- * @return $this
- */
- public function withData(array $data)
- {
- return $this->option('series', $data);
- }
- /**
- * 设置图表类别.
- *
- * @param array $data
- *
- * @return $this
- */
- public function withCategories(array $data)
- {
- return $this->option('xaxis.categories', $data);
- }
- }
|