折线图多数据处理

发布于:2025-07-02 ⋅ 阅读:(28) ⋅ 点赞:(0)

前言:

skline1有年份和新申请单位数,skline2有年份和有效期内单位数,我想要把1和2的年份放在一起从小到大放,没有重复的,新申请单位数和有效期内单位数和年份的排列顺序一致

实现:

// 获取原始数据
List<Map<String, Object>> skLine1 = bmzgConfidentQualifyManageService.getSkLine1();
List<Map<String, Object>> skLine2 = bmzgConfidentQualifyManageService.getSkLine2();

// 转换为年份->数据的Map便于查询
Map<String, String> yearToCount1 = skLine1.stream()
    .collect(Collectors.toMap(
        map -> map.get("年份").toString(),
        map -> map.get("新申请单位数").toString()
    ));

Map<String, String> yearToCount2 = skLine2.stream()
    .collect(Collectors.toMap(
        map -> map.get("年份").toString(),
        map -> map.get("有效期内单位数").toString()
    ));

// 合并年份并排序
Set<String> mergedYears = Stream.concat(
        skLine1.stream().map(map -> map.get("年份").toString()),
        skLine2.stream().map(map -> map.get("年份").toString()))
    .collect(Collectors.toCollection(TreeSet::new)); // 自动排序去重

// 构建最终结果
List<String> sortedYears = new ArrayList<>(mergedYears);
List<String> newApplications = new ArrayList<>();
List<String> validUnits = new ArrayList<>();

for (String year : sortedYears) {
    newApplications.add(yearToCount1.getOrDefault(year, "0")); // 没有数据默认为0
    validUnits.add(yearToCount2.getOrDefault(year, "0"));
}

// 最终合并结果
Map<String, Object> result = new HashMap<>();
result.put("xData", sortedYears);      // 排序后的年份列表
result.put("yData1", newApplications); // 对应年份的新申请单位数
result.put("yData2", validUnits);      // 对应年份的有效期内单位数

图片:


网站公告

今日签到

点亮在社区的每一天
去签到