折线图数据获取及处理

Controller层

 public SegmentChartVo getChartInfo(@PathVariable("id") Long id) {
        Tuple2 tuple2 = getDT(id);//获得开始熏蒸任务开始时间,若结束时间未到获取当前时间,否者则end获取结束时间
        LocalDateTime[] localDateTimes = (LocalDateTime[]) tuple2.second;
        Long segmentId = (Long) tuple2.first;
        SegmentChartVo chartVo = new SegmentChartVo();
        Tuple5 axisData = chartService.getTHMAxisDataById(segmentId, localDateTimes[0], localDateTimes[1], "hour", 1l, 1);//将室内温湿度,ppm浓度,水份按照每一小时为一个间隔返回
        Tuple3 axisData2 = chartService.getWeatherAxisData(localDateTimes[0], localDateTimes[1]);//将室外温湿度按照每五分钟为一个单位返回
        chartVo.setSegment(axisData);
        chartVo.setWeather(axisData2);
        return chartVo;
    }

室内坐标轴获取

因为室内传感器较多,获取数据时间不规律,所以按照整小时为x坐标,对其内数据进行相关处理。

 public Tuple5 getTHMAxisDataById(Long segmentId, LocalDateTime start, LocalDateTime end, String type, Long step,Integer typo) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_FULL);
        List xAxis = generatorXAxis(start, end, type, step);//获取x坐标
        List xAxisFormatter =//获得日期的标准格式
                xAxis.stream().map(localDateTime -> {
                    return parseLocalDateTime(localDateTime, dateTimeFormatter);
                }).collect(Collectors.toList());
        Map> data =
                aggregateService.getDataBetweenDateBySegmentId(segmentId, DateUtils.convertDate(start), DateUtils.convertDate(end),null,typo);//将数据按照deviceid分类
        Tuple4 tuple4 = getY(data,xAxis,"max");//获取y坐标
        return Tuple5.with(xAxisFormatter,tuple4.first,tuple4.second,tuple4.third,tuple4.fourth);//将x,y坐标返回
    }

按照时间间隔取得Y轴坐标

public Tuple4 getY(Map> data,List xAxis,String aggregateType){
        if (data != null && data.size() > 0) {
            List tempAndTempTuple = new ArrayList<>();
            for (int i = 0; i < xAxis.size() - 1; i++) {
                LocalDateTime intervalStart = xAxis.get(i);
                LocalDateTime intervalEnd = xAxis.get(i + 1);
                Tuple4 t = preAchieveYAxis(data, intervalStart, intervalEnd);//获得间隔时间的起始值和终止值,并根据此查出数据
                tempAndTempTuple.add(t);
            }

            Map maxTupleList = realAchieveYAxis(tempAndTempTuple, aggregateType);//获取每个间隔区间的最大Y值
            List tempY = new ArrayList<>();
            List humiY = new ArrayList<>();
            List hzzppmY = new ArrayList<>();
            List moistureY = new ArrayList<>();
            xAxis.forEach(x -> {
                Tuple4 tuple4 = maxTupleList.get(x);
                if (tuple4 != null) {
                    Float temp = (Float) tuple4.first;
                    Float humi = (Float) tuple4.second;
                    Float hzzppm = (Float) tuple4.second;
                    Float moisture = (Float) tuple4.second;
                    tempY.add(temp);
                    humiY.add(humi);
                    hzzppmY.add(hzzppm);
                    moistureY.add(moisture);

                } else {
                    tempY.add(null);
                    humiY.add(null);
                    hzzppmY.add(null);
                    moistureY.add(null);
                }

            });

            return Tuple4.with(tempY, humiY, hzzppmY, moistureY);
        }
        return Tuple4.with(new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
    }

取出间隔时间内的数据

    public Tuple4 preAchieveYAxis(Map> data, LocalDateTime intervalStart, LocalDateTime intervalEnd) {
        List tempList = new ArrayList<>();
        List humiList = new ArrayList<>();
        List hzsppmList = new ArrayList<>();
        List moistureList = new ArrayList<>();
//        temp.put(intervalStart,new ArrayList<>());
        data.forEach((k, v) -> {
            v.forEach(infoHis -> {
                LocalDateTime collectDT = DateUtils.convertLocalDateTime(infoHis.getCollectTime());
                if (collectDT.isBefore(intervalEnd) && collectDT.isAfter(intervalStart)) {//如果收集时间在时间间隔内
                    Float temp = infoHis.getTemp();
                    Float humi = infoHis.getHumidity();
                    Float hzsppm = infoHis.getHzsppm();
                    Float moisture = infoHis.getMoisture();
                    tempList.add(temp);
                    humiList.add(humi);
                    hzsppmList.add(hzsppm);
                    moistureList.add(moisture);
                }
            });
        });
        Tuple2 tempTuple = Tuple2.with(intervalStart, tempList);
        Tuple2 humiTuple = Tuple2.with(intervalStart, humiList);
        Tuple2 hzzppmTuple = Tuple2.with(intervalStart, hzsppmList);
        Tuple2 moistureTuple = Tuple2.with(intervalStart, moistureList);
        return Tuple4.with(tempTuple, humiTuple, hzzppmTuple, moistureTuple);//将在时间间隔内的数据封装并返回
    }

得到间隔时间内数据的最大值

 public Map realAchieveYAxis(List data, String type) {
        if ("max".equals(type)) {
            Map maxTupleList = new HashMap<>();
            data.forEach(tuple4 -> {
                Tuple2 tempTuple =  (Tuple2)tuple4.first;
                LocalDateTime intervalStart = (LocalDateTime) tempTuple.first;
                maxTupleList.put(intervalStart, compute(tuple4,type));//取得区间最大值
            });
            return maxTupleList;
        } else if ("average".equals(type)) {
            Map averTupleList = new HashMap<>();
            data.forEach(tuple4 -> {
                Tuple2 tempTuple =  (Tuple2)tuple4.first;
                LocalDateTime intervalStart = (LocalDateTime) tempTuple.first;
                averTupleList.put(intervalStart, compute(tuple4,type));
            });
            return averTupleList;
        } else {
            return null;
        }


    }
private Tuple4 compute(Tuple4 data, String type){
        Tuple2 tempTuple =  (Tuple2)data.first;
        Tuple2 humiTuple = (Tuple2) data.second;
        Tuple2 hzTuple =  (Tuple2)data.third;
        Tuple2 moiTuple =  (Tuple2)data.fourth;
        LocalDateTime intervalStart = (LocalDateTime) tempTuple.first;
        List tempList = (List) tempTuple.second;//获取区间内所有数值
        List humiList = (List) humiTuple.second;
        List hzsppmList = (List) hzTuple.second;
        List moistureList = (List) moiTuple.second;
        if("max".equals(type)){
           return Tuple4.with(
                    (tempList != null && tempList.size() > 0) ? Collections.max(tempList) : null,//列表不为空取最大值,为空则值为空
                    (humiList != null && humiList.size() > 0) ? Collections.max(humiList) : null,
                    (hzsppmList != null && hzsppmList.size() > 0) ? Collections.max(hzsppmList) : null,
                    (moistureList != null && moistureList.size() > 0) ? Collections.max(moistureList) : null
            );
        }else if("average".equals(type)){
            Double averTemp = tempList.stream().mapToDouble(Float::doubleValue).average().getAsDouble();
            Double humiTemp = humiList.stream().mapToDouble(Float::doubleValue).average().getAsDouble();
            Double hzsppmTemp = hzsppmList.stream().mapToDouble(Float::doubleValue).average().getAsDouble();
            Double moistureTemp = moistureList.stream().mapToDouble(Float::doubleValue).average().getAsDouble();
            return Tuple4.with(averTemp,humiTemp,hzsppmTemp,moistureTemp);
        }else {
            return null;
        }

    }

室外数据获取

 public Tuple3 getWeatherAxisData(Date start, Date end) {
        List weatherInfos = weatherInfoService.getWeatherDataBetweenData(start, end);//查询在时间段内的所有天气数据
        List xAxisFormatter =//获取所有记录的时间,作为x值
                weatherInfos.stream().map(weatherInfo -> {
                    return DateUtils.parseDateToString(weatherInfo.getWeatherDate());
                }).collect(Collectors.toList());
        //weatherInfos的weatherdate十分有规律不考虑再重新x坐标,用原来的就好
        if (weatherInfos != null && weatherInfos.size() > 0) {
            List tempY =
                    weatherInfos.stream().map(weatherInfo -> {
                        return Float.valueOf(weatherInfo.getTempOut());
                    }).collect(Collectors.toList());
            List humiY =
                    weatherInfos.stream().map(weatherInfo -> {
                        return Float.valueOf(weatherInfo.getOutHum());
                    }).collect(Collectors.toList());
            return Tuple3.with(xAxisFormatter, tempY, humiY);//返回数据坐标的x,y值
        }
        return Tuple3.with(xAxisFormatter, new ArrayList<>(), new ArrayList<>());
    }

你可能感兴趣的:(折线图数据获取及处理)