setUpOptions(); } /** * 初始化图表配置 */ protected function setUpOptions() { $this->options([ 'chart' => [ //'width' => '180%', 'height' => 1000, 'type' => 'bar', 'events' => [ ], 'toolbar' => ['show' => false], ], 'plotOptions' => [ 'bar' => [ //'columnWidth' => '45%', 'distributed' => true, ] ], 'dataLabels' => [ 'enabled' => false ], 'legend' => [ 'show' => false ], 'xaxis' => [ //'categories' => // [75, 125, 225, 175, 125, 75, 25] //, 'labels' => [ 'show' => true, 'style' => [ 'fontSize' => '12px' ] ], ], 'yaxis' => [ 'show' => true ], 'tooltip' => [ 'x' => ['show' => true], ], ]); } /** * 处理图表数据 */ protected function buildData() { switch (request('time_key', 0)) { case '1': $time = "DATE_FORMAT(created_at,'%Y-%m-%d')"; break; case '30': $time = "DATE_FORMAT(created_at,'%Y-%m')"; break; case '365': $time = "DATE_FORMAT(created_at,'%Y')"; break; default: $time = "DATE_FORMAT(created_at,'%Y-%m-%d')"; } $model = new User; $table = $model->getTable(); $right = DB::table(function ($query) use ($table,$time){ $query->selectRaw($time." AS tart_dat") ->where('agent_id',Admin::user()->id) ->from($table) ->groupBy('tart_dat'); },'l_tab'); $users = DB::table(function ($query) use ($table,$time) { $query->selectRaw($time ." AS start_date") ->where('agent_id',Admin::user()->id) ->from($table); },'r_tab') ->joinSub($right, 'l_tab', function ($join) { $join->on('l_tab.tart_dat', '>=', 'r_tab.start_date'); }) ->addSelect(DB::raw('tart_dat as statistics_time,count(tart_dat) as count')) ->orderBy('tart_dat') ->groupBy('tart_dat'); $dateTime = request('created_at', 0); if ($dateTime) { $users->whereBetween('tart_dat',$dateTime); } $users = $users->get() ->toArray(); $this->withData([ [ 'name' => '用户数', 'data' => Arr::pluck($users,'count') ], ] ); $this->withCategories( Arr::pluck($users,'statistics_time') ); } 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(); } /** * 重写初始化JS * @return string */ protected function buildDefaultScript() { $options = JavaScript::format($this->options); return <<containerSelector}")[0], options ); chart.render(); $(window).resize(function () { var height = $(window).height() - $('.Dcat_Admin_Widgets_Box').offset().top - 130; if (height < 320) { height = 320 } chart.updateOptions({ 'chart': { 'height': height } }); }).resize(); })(); JS; } }