调整表格数据及折线图数据

This commit is contained in:
2026-01-23 15:16:17 +08:00
parent 9fea9d86ed
commit 92a3762ce1
3 changed files with 78 additions and 11 deletions

View File

@@ -1,6 +1,8 @@
package com.tem.bocai.controller;
import com.tem.bocai.entity.CompletedToday;
import com.tem.bocai.entity.LotteryResult;
import com.tem.bocai.repository.CompletedTodayRepository;
import com.tem.bocai.repository.LotteryResultRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@@ -15,18 +17,76 @@ public class ChartController {
@Autowired
private LotteryResultRepository lotteryResultRepository;
@Autowired
private CompletedTodayRepository completedTodayRepository;
// 获取折线图1数据
@GetMapping("/charts/line1")
public Map<String, Object> getLineChartData1() {
Map<String, Object> response = new HashMap<>();
// 模拟数据 - 实际项目中应该从数据库获取
List<Integer> data = Arrays.asList(65, 59, 80, 81, 56, 55, 40);
List<String> labels = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月");
response.put("data", data);
response.put("labels", labels);
response.put("title", "折线图1数据");
try {
// 获取今日日期的字符串表示格式yyyy-MM-dd
String today = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
System.out.println("今日日期: " + today);
// 从数据库获取今日的CompletedToday数据
List<CompletedToday> completedTodays = completedTodayRepository.findAll();
System.out.println("获取到" + completedTodays.size() + "条CompletedToday数据");
// 过滤出今日的数据
List<CompletedToday> todayData = new ArrayList<>();
for (CompletedToday item : completedTodays) {
if (item.getTime() != null && item.getTime().contains(today)) {
todayData.add(item);
}
}
System.out.println("过滤后获取到" + todayData.size() + "条今日数据");
// 按时间排序
todayData.sort(Comparator.comparing(CompletedToday::getTime));
// 提取数据
List<Double> data = new ArrayList<>();
List<String> labels = new ArrayList<>();
for (CompletedToday item : todayData) {
// 使用resultAmount作为折线图数据
data.add(item.getResultAmount());
// 使用时间作为标签(只保留时分部分)
String time = item.getTime();
if (time != null && time.length() >= 16) {
labels.add(time.substring(11, 16)); // 提取时分部分
} else {
labels.add(time);
}
}
// 如果没有今日数据,使用默认数据
if (data.isEmpty()) {
data = Arrays.asList(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
labels = Arrays.asList("00:00", "04:00", "08:00", "12:00", "16:00", "20:00", "23:59");
}
response.put("data", data);
response.put("labels", labels);
response.put("title", "今日盈亏数据");
System.out.println("返回的数据长度: " + data.size());
System.out.println("返回的标签长度: " + labels.size());
System.out.println("返回的标签: " + labels);
} catch (Exception e) {
System.err.println("获取折线图1数据失败: " + e.getMessage());
e.printStackTrace();
// 异常时使用默认数据
List<Integer> data = Arrays.asList(65, 59, 80, 81, 56, 55, 40);
List<String> labels = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月");
response.put("data", data);
response.put("labels", labels);
response.put("title", "折线图1数据");
}
return response;
}