Java后端如何处理柱状图关于筛选日期范围内没数据的情况下,返回所有日期,没数据返回0,有数据则填充对应的值

效果图如下:

在这里插入图片描述

代码如下:

		//统计时间范围的数组
        List<String>  datList = RangeDateUtil.allRangeDate(startDate, endDate);
        if(CollectionUtil.isEmpty(datList)){
            return new ArrayList<>();
        }
        //查询时间范围内复合条件的所有数据
        List<TrusteeshipInfoVO> trusteeshipInfoVOS =  wxInsureTrusteeshipInfoMapper.getTreatmentNumber(startDate,endDate);
        //new一个返回的结果数组
        List<TrusteeshipInfoVO> result = new ArrayList<>();
        //当日期数组不为空时,则开始遍历所有日期的每一天
        for(String date : datList){
            //判断一下查询的结果数组是否为空
            if(CollectionUtil.isEmpty(trusteeshipInfoVOS)){
                //当开始的第一天数据为空时,则将数据对象里面的值除了日期,其他全部初始化为0,然后添加到结果返回数组中
                result.add(initTrusteeshipInfoVO(date));
                continue;
            }
            //遍历查出来的数据,填充进日期
            boolean hasValue = false;
            for(TrusteeshipInfoVO trusteeshipInfoVO : trusteeshipInfoVOS){
                if(date.equals(trusteeshipInfoVO.getDateTime().substring(0,10))){
                    trusteeshipInfoVO.setDateTime(trusteeshipInfoVO.getDateTime().substring(0,10));
                    result.add(trusteeshipInfoVO);
                    hasValue = true;
                    break;
                }
            }
            if(!hasValue) {
                result.add(initTrusteeshipInfoVO(date));
            }
        }
        return result;
    }

    /**
     *   将为空的日期的对象数据全部初始化为0
     */
     private TrusteeshipInfoVO initTrusteeshipInfoVO(String date){
         TrusteeshipInfoVO trusteeshipInfoVO = new TrusteeshipInfoVO();
         trusteeshipInfoVO.setCustomerCount(0);
         trusteeshipInfoVO.setEmployeeCount(0);
         trusteeshipInfoVO.setDateTime(date);
         return trusteeshipInfoVO;
    }


用到的工具类如下:


    /**
     * @param startDateStr 开始日期
     * @param endDateStr 结束日期
     * @return  从开始到结束的所有日期全部统计出来
     */
    public static List<String> allRangeDate(String startDateStr, String endDateStr) {
        List<String> listDate = new ArrayList<>();
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            LocalDateTime startDate = LocalDateTime.parse(startDateStr, df1);
            LocalDateTime endDate = LocalDateTime.parse(endDateStr, df1);
            LocalDateTime tempDate = null;
            while (!(LocalDateTime.of(startDate.plusDays(-1).toLocalDate(), LocalTime.MIN)
                    .equals(LocalDateTime.of(endDate.toLocalDate(), LocalTime.MIN)))) {
                tempDate = startDate;
                String format = tempDate.format(df2);
                listDate.add(format);
                startDate = startDate.plusDays(1);
            }
            System.out.println(listDate.toString());
            return listDate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值