调整表格数据及折线图数据
This commit is contained in:
@@ -83,7 +83,7 @@ function updateChart1() {
|
||||
|
||||
const option = {
|
||||
title: {
|
||||
text: '折线图1',
|
||||
text: '今日盈亏数据',
|
||||
left: 'center'
|
||||
},
|
||||
tooltip: {
|
||||
@@ -106,7 +106,7 @@ function updateChart1() {
|
||||
data: chartLabels.value,
|
||||
axisLabel: {
|
||||
rotate: 45,
|
||||
interval: 11, // 每12个标签显示一个,避免重叠
|
||||
interval: chartLabels.value.length > 12 ? Math.floor(chartLabels.value.length / 12) : 0, // 根据标签数量动态调整间隔,确保至少显示一些标签
|
||||
fontSize: 10
|
||||
}
|
||||
},
|
||||
@@ -163,8 +163,15 @@ async function fetchChartData() {
|
||||
console.log('获取折线图数据');
|
||||
const response = await axios.get('http://localhost:8080/api/charts/line1');
|
||||
console.log('折线图数据响应:', response.data);
|
||||
if (response.data && response.data.data) {
|
||||
chartData1.value = response.data.data;
|
||||
if (response.data) {
|
||||
// 更新数据
|
||||
if (response.data.data) {
|
||||
chartData1.value = response.data.data;
|
||||
}
|
||||
// 更新标签
|
||||
if (response.data.labels) {
|
||||
chartLabels.value = response.data.labels;
|
||||
}
|
||||
// 确保数据长度与标签长度一致
|
||||
if (chartData1.value.length !== chartLabels.value.length) {
|
||||
console.warn('数据长度与标签长度不一致,使用默认数据');
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user