123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace App\Admin\Metrics\Charts;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Widgets\ApexCharts\Chart;
- use Dcat\Admin\Widgets\Metrics\Bar;
- use Dcat\Admin\Widgets\Metrics\Line;
- use Illuminate\Http\Request;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\DB;
- class Orders 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([
- 'colors' => $colors,
- 'chart' => [
- 'type' => 'area',
- 'height' => 430
- ],
- 'plotOptions' => [
- 'bar' => [
- 'horizontal' => true,
- 'dataLabels' => [
- 'position' => 'top',
- ],
- ]
- ],
- 'dataLabels' => [
- 'enabled' => true,
- 'offsetX' => -6,
- 'style' => [
- 'fontSize' => '12px',
- 'colors' => ['#ddd']
- ]
- ],
- 'stroke' => [
- 'show' => true,
- 'width' => 1,
- 'colors' => ['#fff']
- ],
- 'xaxis' => [
- 'categories' => [],
- ],
- ]);
- $this->dropdown([
- '7' => 'Last 7 Days',
- '28' => 'Last 28 Days',
- '30' => 'Last Month',
- '365' => 'Last Year',
- ]);
- }
- /**
- * 处理图表数据
- */
- protected function buildData()
- {
- $count = DB::table('orders')->whereBetween('created_at', [date('Y-m-d', strtotime("-30 day")), date('Y-m-d')])
- ->selectRaw('DATE(created_at) as date,COUNT(*) as value')
- ->groupBy('date')->get()->toArray();
- $data = [
- [
- 'data' => Arr::pluck($count, 'value')
- ]
- ];
- $categories = Arr::pluck($count, 'date');
- $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);
- }
- /**
- * 渲染图表
- *
- * @return string
- */
- public function render()
- {
- $this->buildData();
- return parent::render();
- }
- }
|