123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package com.jjt.biz.util;
- import com.jjt.biz.vo.ScoreVO;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * DataUtil$
- *
- * @author wukai
- * @date 2024/10/16 01:52
- */
- public class DataUtil {
- /**
- * 补数据
- *
- * @param standTime
- * @param result
- */
- public static void standTimeVO(List<String> standTime, List<ScoreVO> result) {
- for (ScoreVO vo : result) {
- // 创建一个新的 times 和 scores 列表,以确保它们与标准时间序列对齐
- List<String> alignedTimes = new ArrayList<>();
- List<Float> alignedScores = new ArrayList<>();
- // 使用一个Map来存储原始数据,以便快速查找
- Map<String, Float> timeScoreMap = new HashMap<>();
- List<String> times = vo.getXData();
- List<Float> scores = vo.getScores();
- for (int i = 0; i < times.size(); i++) {
- timeScoreMap.put(times.get(i), scores.get(i));
- }
- // 遍历标准时间序列,并根据需要添加空值或实际值
- for (String time : standTime) {
- alignedTimes.add(time);
- // 添加时间
- Float score = timeScoreMap.getOrDefault(time, 100F);
- // 如果没有对应的分数,则添加0.0f
- alignedScores.add(score);
- }
- // 更新原有的 times 和 scores 列表
- times.clear();
- times.addAll(alignedTimes);
- scores.clear();
- scores.addAll(alignedScores);
- }
- }
- /**
- * 补数据
- *
- * @param standTime
- * @param result
- */
- public static void standTimeMap(List<String> standTime, List<Map<String, Object>> result) {
- for (Map<String, Object> map : result) {
- // 创建一个新的 times 和 scores 列表,以确保它们与标准时间序列对齐
- List<String> alignedTimes = new ArrayList<>();
- List<Float> alignedScores = new ArrayList<>();
- // 使用一个Map来存储原始数据,以便快速查找
- Map<String, Float> timeScoreMap = new HashMap<>();
- List<String> times = (List<String>) map.get("time");
- List<Float> scores = (List<Float>) map.get("score");
- for (int i = 0; i < times.size(); i++) {
- timeScoreMap.put(times.get(i), scores.get(i));
- }
- // 遍历标准时间序列,并根据需要添加空值或实际值
- for (String time : standTime) {
- alignedTimes.add(time);
- // 添加时间
- Float score = timeScoreMap.getOrDefault(time, 100F);
- // 如果没有对应的分数,则添加0.0f
- alignedScores.add(score);
- }
- // 更新原有的 times 和 scores 列表
- times.clear();
- times.addAll(alignedTimes);
- scores.clear();
- scores.addAll(alignedScores);
- }
- }
- /**
- * 补数据
- *
- * @param standTime
- * @param result
- */
- public static void standTimeNet(List<Date> standTime, List<Map<String, Object>> result) {
- for (Map<String, Object> map : result) {
- // 创建一个新的 times 和 scores 列表,以确保它们与标准时间序列对齐
- // 使用一个Map来存储原始数据,以便快速查找
- Map<Date, Float> timeScoreMap = new HashMap<>();
- List<Map<String, Object>> trendList = (List<Map<String, Object>>) map.get("data");
- List<Map<String, Object>> newTrend = new ArrayList<>();
- Map<Date, Float> map1 = trendList.stream().collect(Collectors.toMap(mp -> (Date) mp.get("time"), mp -> (Float) mp.get("value")));
- for (Date d : map1.keySet()) {
- timeScoreMap.put(d, map1.get(d));
- }
- // 遍历标准时间序列,并根据需要添加空值或实际值
- for (Date time : standTime) {
- Map<String, Object> map2 = new HashMap<>(16);
- map2.put("time", time);
- map2.put("value", timeScoreMap.getOrDefault(time, 0F));
- newTrend.add(map2);
- }
- map.remove("data");
- map.put("data", newTrend);
- }
- }
- }
|