分组统计数据

本文介绍了一种在指定时间范围内,根据用户设备字段进行数据分组和统计的方法。通过将字符串类型的时间转换为长整型毫秒值,从后端获取数据并填充X轴和Y轴的数据,实现了对时间序列数据的可视化展示。文章详细描述了获取时间区间内的所有日期,以及如何根据这些日期和特定字段统计用户设备数量的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在指定时间区间内 根据某字段分组统计数据


    @RequestMapping(value = "getUserMemStatistics", method = RequestMethod.POST)
    public void getUserMemStatistics(HttpServletRequest request, HttpServletResponse response) {
        String startTime = ParamUtil.getParamsTrim(request, "startTime", null);
        String endTime = ParamUtil.getParamsTrim(request, "endTime", null);

        List<String> legend = new ArrayList<String>();//数据分组
        List<String> xAxis = new ArrayList<String>();//x轴数据
        List<Series> series = new ArrayList<Series>();

        // String型时间转成Long型毫秒值
        Long startTimeL = 0L;
        Long endTimeL = 0L;
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        if (startTime != null && endTime != null) {
            try {
                startTimeL = format1.parse(startTime).getTime();
                endTimeL = format1.parse(endTime).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        // 从后端获取数据
        List<UserAcqStatistics> list = new ArrayList<>();
        list = userService.getUserDeviceStatistics(startTimeL, endTimeL);
        
        //--------------- x轴数据(时间)填充 -------------------
        //获取从开始日期到结束日期之间的所有日期
        timeStrs = TimeUtil.listBetweenDays(startTime, endTime, "yyyy-MM-dd");
        
        for(String day : timeStrs){
            xAxis.add(day);
        }

        //--------------- y轴数据填充 -------------------------
        Set<String> mems = new HashSet<String>();
        for(UserAcqStatistics rs :list){
            if(rs.getMem()!=null){
                mems.add(rs.getMem());
            }
        }
        legend.addAll(mems);

        //--------------- series数据 ----------------------
        Map<String, List<Long>> datasMap = new HashMap<String, List<Long>>();
        List<Long> dList = null;
        List<Long> zList = new ArrayList<>();
        for (int i = 0; i < xAxis.size(); i++) {
            long zcount = 0L;
            for (String legendName : legend){
                dList = datasMap.get(legendName)==null?new ArrayList<Long>():(ArrayList<Long>) datasMap.get(legendName);
                long count = 0L;
                if(list != null && list.size() > 0){
                    for(UserAcqStatistics sr : list){
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = new Date(sr.getTraceTime());
                        String res = simpleDateFormat.format(date);
                        if(timeStrs.get(i).equals(res) && legendName.equals(sr.getMem())){
                            count++;
                        }
                    }
                    zcount += count;
                }
                dList.add(count);
                datasMap.put(legendName, dList);
            }
            zList.add(zcount);
        }

        for(String legendName : legend){
            series.add(new Series(legendName,"line",datasMap.get(legendName)));
        }
        
        legend.add("总");
        series.add(new Series("总","line",zList));
        //------------------------------------------------------
        Echarts echarts = new Echarts(legend, xAxis, series);
        HttpUtil.writeResult(response, echarts);
    }

 

    /**
     * 获取两个日期的所有日期 数组
     */
    public static List<String> listBetweenDays(String date1, String date2,String pattern)  {
        List<String> dayStrs = new ArrayList<String>();
        
        SimpleDateFormat simpleDateFormatSrc = new SimpleDateFormat(pattern);
        SimpleDateFormat simpleDateFormatDest = new SimpleDateFormat(pattern);
        
        try {
            int dayCount = differentDay(simpleDateFormatSrc.parse(date1), simpleDateFormatSrc.parse(date2)) + 1;
            long date = simpleDateFormatSrc.parse(date1).getTime();
            for(int i=0 ;i<dayCount;i++){
                dayStrs.add(simpleDateFormatDest.format(date));
                date += ((24*60*60*1000L));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dayStrs;
    }


    /**
     * 计算两个时间相差天数
     */
    public static int differentDay(Date date1, Date date2) throws Exception {
        return (int) ((date2.getTime() - date1.getTime()) / (24*60*60*1000L));
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值