FarmLandTrendChart.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Module\Farm\AdminControllers\Metrics;
  3. use Dcat\Admin\Widgets\Metrics\Line;
  4. use Illuminate\Http\Request;
  5. use App\Module\Farm\Models\FarmDailyStats;
  6. use Carbon\Carbon;
  7. /**
  8. * 农场土地类型趋势图 - 多线折线图
  9. *
  10. * 参考UCore\DcatAdmin\Metrics\Examples\NewUsersDou实现
  11. * 每个土地类型一条线,支持时间范围选择
  12. */
  13. class FarmLandTrendChart extends Line
  14. {
  15. /**
  16. * 图表默认高度
  17. *
  18. * @var int
  19. */
  20. protected $chartHeight = 200;
  21. /**
  22. * 土地类型名称映射
  23. */
  24. protected $landTypeNames = [
  25. 1 => '普通土地',
  26. 2 => '红土地',
  27. 3 => '黑土地',
  28. 4 => '金色特殊土地',
  29. 5 => '蓝色特殊土地',
  30. 6 => '紫色特殊土地',
  31. ];
  32. /**
  33. * 初始化卡片内容
  34. *
  35. * @return void
  36. */
  37. protected function init()
  38. {
  39. parent::init();
  40. $this->title('土地类型趋势图');
  41. $this->dropdown([
  42. '7' => '最近7天',
  43. '15' => '最近15天',
  44. '30' => '最近30天',
  45. ]);
  46. }
  47. /**
  48. * 处理请求,获取数值
  49. *
  50. * @param Request $request
  51. *
  52. * @return mixed|void
  53. */
  54. public function handle(Request $request)
  55. {
  56. $days = $request->get('option', '7');
  57. // 获取指定天数的统计数据
  58. $stats = FarmDailyStats::where('stats_date', '>=', Carbon::now()->subDays($days))
  59. ->orderBy('stats_date', 'asc')
  60. ->get();
  61. if ($stats->isEmpty()) {
  62. // 如果没有数据,显示提示
  63. $this->withContent('暂无数据');
  64. $this->withChart([]);
  65. return;
  66. }
  67. // 准备图表数据
  68. $chartData = [];
  69. $totalLands = 0;
  70. // 遍历每种土地类型
  71. for ($type = 1; $type <= 6; $type++) {
  72. $field = "land_type_{$type}";
  73. $typeData = [];
  74. $hasData = false;
  75. foreach ($stats as $stat) {
  76. $value = $stat->$field ?? 0;
  77. $typeData[] = $value;
  78. if ($value > 0) {
  79. $hasData = true;
  80. }
  81. }
  82. // 只添加有数据的土地类型
  83. if ($hasData) {
  84. $chartData[$this->landTypeNames[$type]] = $typeData;
  85. $totalLands += array_sum($typeData);
  86. }
  87. }
  88. // 计算总土地数(取最新一天的数据)
  89. $latestStats = $stats->last();
  90. $currentTotal = 0;
  91. for ($type = 1; $type <= 6; $type++) {
  92. $field = "land_type_{$type}";
  93. $currentTotal += $latestStats->$field ?? 0;
  94. }
  95. // 设置卡片内容
  96. $this->withContent($currentTotal . '块');
  97. // 设置图表数据
  98. $this->withChart($chartData);
  99. }
  100. /**
  101. * 设置卡片内容
  102. *
  103. * @param string $content
  104. *
  105. * @return $this
  106. */
  107. public function withContent($content)
  108. {
  109. return $this->content(
  110. <<<HTML
  111. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  112. <h2 class="ml-1 font-lg-1">{$content}</h2>
  113. <span class="mb-0 mr-1 text-80">总土地数</span>
  114. </div>
  115. HTML
  116. );
  117. }
  118. }