title('农场统计图表') ->description('农场模块数据统计分析') ->row(function ($row) { $row->column(6, new FarmLandLevelStatsCard()); $row->column(6, $this->getLandStatsCards()); }) ->row($this->getLandTrendsChart()) ->row($this->getHouseRankingCard()); } /** * 获取土地等级统计卡片 */ protected function getLandStatsCards() { $latestStats = FarmDailyStats::orderBy('stats_date', 'desc')->first(); if (!$latestStats) { return new Card('土地统计', '
暂无统计数据
'); } $landTypeNames = [ 1 => '普通土地', 2 => '红土地', 3 => '黑土地', 4 => '金色特殊土地', 5 => '蓝色特殊土地', 6 => '紫色特殊土地', ]; $cards = '
'; for ($type = 1; $type <= 6; $type++) { $field = "land_type_{$type}"; $count = $latestStats->$field ?? 0; $typeName = $landTypeNames[$type]; // 根据土地类型设置不同的颜色 $colors = [ 1 => 'bg-secondary', // 普通土地 - 灰色 2 => 'bg-danger', // 红土地 - 红色 3 => 'bg-dark', // 黑土地 - 黑色 4 => 'bg-warning', // 金色特殊土地 - 黄色 5 => 'bg-primary', // 蓝色特殊土地 - 蓝色 6 => 'bg-info', // 紫色特殊土地 - 紫色 ]; $color = $colors[$type]; $cards .= "
{$typeName} {$count}块
"; } $cards .= '
'; return new Card('土地类型统计', $cards); } /** * 获取土地趋势图表 */ protected function getLandTrendsChart() { // 获取最近30天的统计数据 $stats = FarmDailyStats::where('stats_date', '>=', Carbon::now()->subDays(30)) ->orderBy('stats_date', 'asc') ->get(); if ($stats->isEmpty()) { return new Card('土地趋势图', '
暂无趋势数据
'); } $dates = []; $landData = [ 1 => [], // 普通土地 2 => [], // 红土地 3 => [], // 黑土地 4 => [], // 金色特殊土地 5 => [], // 蓝色特殊土地 6 => [], // 紫色特殊土地 ]; foreach ($stats as $stat) { $dates[] = $stat->stats_date->format('m-d'); for ($type = 1; $type <= 6; $type++) { $field = "land_type_{$type}"; $landData[$type][] = $stat->$field ?? 0; } } $landTypeNames = [ 1 => '普通土地', 2 => '红土地', 3 => '黑土地', 4 => '金色特殊土地', 5 => '蓝色特殊土地', 6 => '紫色特殊土地', ]; // 构建图表数据 $series = []; $colors = ['#6c757d', '#dc3545', '#343a40', '#ffc107', '#007bff', '#17a2b8']; for ($type = 1; $type <= 6; $type++) { $series[] = [ 'name' => $landTypeNames[$type], 'type' => 'line', 'data' => $landData[$type], 'itemStyle' => [ 'color' => $colors[$type - 1] ] ]; } $chartId = 'land-trends-chart'; $chartScript = " "; $chartHtml = "
{$chartScript} "; // 添加ECharts库 Admin::script('https://cdn.jsdelivr.net/npm/echarts@5.4.0/dist/echarts.min.js'); return new Card('土地类型趋势图', $chartHtml); } /** * 获取房屋等级排名卡片 */ protected function getHouseRankingCard() { $latestStats = FarmDailyStats::orderBy('stats_date', 'desc')->first(); if (!$latestStats) { return new Card('房屋等级排名', '
暂无统计数据
'); } $houseData = []; for ($level = 1; $level <= 10; $level++) { $field = "house_level_{$level}"; $count = $latestStats->$field ?? 0; if ($count > 0) { $houseData[] = [ 'level' => $level, 'label' => "{$level}级房屋", 'value' => $count, 'ratio' => 0 // 这里可以计算占比 ]; } } // 按数量排序 usort($houseData, function($a, $b) { return $b['value'] - $a['value']; }); // 计算占比 $total = array_sum(array_column($houseData, 'value')); foreach ($houseData as &$item) { $item['ratio'] = $total > 0 ? round($item['value'] / $total * 100, 1) : 0; } $rankingHtml = '
'; $rankingHtml .= ''; $rankingHtml .= ''; $rankingHtml .= ''; if (empty($houseData)) { $rankingHtml .= ''; } else { foreach ($houseData as $index => $item) { $rank = $index + 1; $rankingHtml .= ""; $rankingHtml .= ""; $rankingHtml .= ""; $rankingHtml .= ""; $rankingHtml .= ""; $rankingHtml .= ""; } } $rankingHtml .= '
排名房屋等级数量占比
暂无数据
{$rank}{$item['label']}{$item['value']}个{$item['ratio']}%
'; return new Card('房屋等级排名', $rankingHtml); } }