|
|
<?php
namespace App\Admin\Metrics\Examples;
use App\Common\OrderStatus;use App\Models\Order;use Dcat\Admin\Admin;use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Support\Facades\DB;
class SalesExamples extends Bar{ protected $labels = []; protected $colors;
/** * 初始化卡片内容 */ protected function init() { parent::init();
$color = Admin::color(); $this->colors = [$color->green(), $color->red(), $color->yellow(), $color->orange1(), $color->dark90(), $color->dark70(), $color->custom()]; $this->contentWidth(3, 9); $this->title('销售排行'); //$this->chartPullRight = false;
//$this->subTitle('Last 30 days');
$this->height = 280; $this->chartHeight = 250; // 设置图表颜色
$this->chartColors($this->colors); }
/** * 渲染模板 * * @return string */ public function render() { $this->fill();
return parent::render(); }
/** * 写入数据. * * @return void */ public function fill() { $warehouse = Order::query() ->with('agentProduct') ->select('*') ->addSelect(DB::raw('count(id) as count_id')) ->where([ 'status' => OrderStatus::SUCCESS, ]) ->orderByDesc('count_id') ->groupBy('agent_product_id') ->limit(5) ->get(); //$warehouse = [];
$data = $categories = []; foreach ($warehouse as $v) { array_push($this->labels, $v->title); array_push($data, $v->count_id); $categories[] = [$v->title]; }
$this->withChart($data, $categories); //$this->chartLabels($this->labels);
$count = Order::query() ->where([ 'status' => OrderStatus::SUCCESS, ]) ->count(); //$count = 0;
if ($count > 0) { array_unshift($data, $count); array_unshift($this->labels, '总销售量'); $color = Admin::color(); array_unshift($this->colors, $color->primary()); } $this->withContent($data); }
/** * 设置图表数据. * * @param array $data * * @return $this */ public function withChart($data, $categories) { return $this->chart([
'series' => [[ 'name' => '销量', 'data' => $data ]], 'chart' => [ //'width' => '180%',
'type' => 'bar', 'events' => [ ], 'toolbar' => ['show' => false], ], 'colors' => $this->colors, 'plotOptions' => [ 'bar' => [ //'columnWidth' => '45%',
'distributed' => true, ] ], 'dataLabels' => [ 'enabled' => false ], 'legend' => [ 'show' => false ], 'xaxis' => [ 'categories' => $categories , 'labels' => [ 'show' => false, 'style' => [ 'colors' => $this->colors, 'fontSize' => '12px' ] ], ], 'yaxis' => [ 'show' => false ], 'tooltip' => [ 'x' => ['show' => true], ],
]); }
/** * 设置卡片头部内容. * * @param mixed $desktop * @param mixed $mobile * * @return $this */ protected function withContent($data) { $content = ''; foreach ($data as $k => $v) { $content .= ' <div class="d-flex pl-1 mt-2"> <div style="width:100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;"> <i class="fa fa-circle" style="color:' . $this->colors[$k] . '"></i> ' . $this->labels[$k] . ' <div style="display:inline-block">' . $v . '</div> </div> </div>
'; } return $this->content( <<<HTML $content HTML ); }}
|