在指定时间区间内 根据某字段分组统计数据
@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));
}