|
@@ -0,0 +1,727 @@
|
|
|
+package com.sunwin.swismp.util;
|
|
|
+
|
|
|
+import com.sunwin.swismp.constant.ChartsParam;
|
|
|
+import com.sunwin.swismp.dto.ChartsResult;
|
|
|
+//import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation;
|
|
|
+//import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.logging.Level;
|
|
|
+import java.util.logging.Logger;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <br>
|
|
|
+ * Copyright (c) 2019/9/18 All Rights Reserved By SUNWIN<br>
|
|
|
+ * <br>
|
|
|
+ * Package: com.sunwin.swbd.utils <br>
|
|
|
+ * FileName: ChartUtil <br>
|
|
|
+ * <br>
|
|
|
+ * 图表操作工具类。
|
|
|
+ *
|
|
|
+ * @author duanmin
|
|
|
+ * @created 2019/9/18 18:13
|
|
|
+ * @last Modified
|
|
|
+ * @history
|
|
|
+ */
|
|
|
+public class ChartUtil {
|
|
|
+ /**
|
|
|
+ * 简单直方图
|
|
|
+ */
|
|
|
+ public static final Integer HISTOGRAM_CHART = 0;
|
|
|
+ /**
|
|
|
+ * 饼图
|
|
|
+ */
|
|
|
+ public static final Integer PIE_CHART = 1;
|
|
|
+ /**
|
|
|
+ * 全国区域名称列表
|
|
|
+ */
|
|
|
+ public static final String[] AREA_NAMES = new String[]{"江苏", "北京", "天津", "上海", "重庆", "河北", "河南", "云南", "辽宁", "黑龙江", "湖南", "安徽", "山东", "新疆", "浙江", "江西", "湖北",
|
|
|
+ "广西", "甘肃", "山西", "内蒙古", "陕西", "吉林", "福建", "贵州", "广东", "青海", "西藏", "四川", "宁夏", "海南", "台湾", "香港", "澳门"};
|
|
|
+
|
|
|
+ private static Logger logger = Logger.getLogger("ChartUtil");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 封装name 、value结果
|
|
|
+ *
|
|
|
+ * @param name
|
|
|
+ * @param value
|
|
|
+ * @param ratio
|
|
|
+ * @param data
|
|
|
+ */
|
|
|
+ public static void setMapList(String name, Object value, String ratio, List<Map<String, Object>> data) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("name", name);
|
|
|
+ map.put("value", value);
|
|
|
+ if (ratio != null) {
|
|
|
+ map.put("ratio", ratio + "%");
|
|
|
+ }
|
|
|
+ data.add(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理查询的图表结果参数,比如折线图,如果结果包括两条线的结果
|
|
|
+ * 则需要对两个结果的x轴进行补齐,并且给y轴补齐
|
|
|
+ *
|
|
|
+ * @param resList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static ChartsResult makeChartResult(List<ChartsParam> resList) {
|
|
|
+ if (resList == null || resList.isEmpty()) {
|
|
|
+ return new ChartsResult();
|
|
|
+ }
|
|
|
+ String title = null;
|
|
|
+ //初始化与legend数量相同的map,x轴并集去重
|
|
|
+ Map<String, Map> maps = new LinkedHashMap<>();
|
|
|
+ Set<String> xSet = new LinkedHashSet<>();
|
|
|
+ for (ChartsParam param : resList) {
|
|
|
+ if (title == null) {
|
|
|
+ title = param.getTitle();
|
|
|
+ }
|
|
|
+ xSet.add(param.getxAxis());
|
|
|
+ if (!maps.containsKey(param.getLegend())) {
|
|
|
+ Map map = new LinkedHashMap();
|
|
|
+ map.put(param.getxAxis(), param.getyAxis());
|
|
|
+ maps.put(param.getLegend(), map);
|
|
|
+ } else {
|
|
|
+ if (!maps.get(param.getLegend()).containsKey(param.getxAxis())) {
|
|
|
+ maps.get(param.getLegend()).put(param.getxAxis(), param.getyAxis());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //如果legend只有一个,无需补全
|
|
|
+ if (maps.keySet().size() == 1) {
|
|
|
+ ChartsResult chartsResult = new ChartsResult();
|
|
|
+ List<String> legend = new ArrayList<>();
|
|
|
+ List<String> xList = new ArrayList<>();
|
|
|
+ List yList = new ArrayList();
|
|
|
+ legend.add(TimeUtils.dateFormat(resList.get(0).getLegend(), TimeUtils.YYYYMMDD, TimeUtils.YYYY_MM_DD));
|
|
|
+ for (ChartsParam param : resList) {
|
|
|
+ xList.add(param.getxAxis());
|
|
|
+ yList.add(param.getyAxis());
|
|
|
+ }
|
|
|
+ chartsResult.setLegend(legend);
|
|
|
+ chartsResult.setCategory(xList);
|
|
|
+ chartsResult.setData(yList);
|
|
|
+ chartsResult.setTitle(title);
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+ //构造x轴,重新排序
|
|
|
+ List<String> xList = new ArrayList();
|
|
|
+ Iterator<String> it = xSet.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ xList.add(it.next());
|
|
|
+ }
|
|
|
+ Collections.sort(xList);
|
|
|
+ //构造y轴数据,根据x轴数据补全,没有的补null
|
|
|
+ List<List> yList = new ArrayList<>();
|
|
|
+ List<String> legend = new ArrayList<>();
|
|
|
+ for (String key : maps.keySet()) {
|
|
|
+ legend.add(key);
|
|
|
+ List list = new ArrayList();
|
|
|
+ for (String x : xList) {
|
|
|
+ if (!maps.get(key).containsKey(x)) {
|
|
|
+ //如果对应的数值没有,用null补全
|
|
|
+ list.add(0);
|
|
|
+ } else {
|
|
|
+ list.add(maps.get(key).get(x));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ yList.add(list);
|
|
|
+ }
|
|
|
+ ChartsResult chartsResult = new ChartsResult();
|
|
|
+ chartsResult.setLegend(legend);
|
|
|
+ chartsResult.setCategory(xList);
|
|
|
+ chartsResult.setData(yList);
|
|
|
+ if (title != null) {
|
|
|
+ chartsResult.setTitle(title);
|
|
|
+ }
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理查询的图表结果参数,比如折线图,如果结果包括两条线的结果
|
|
|
+ * 则需要对两个结果的x轴进行补齐,并且给y轴补齐
|
|
|
+ *
|
|
|
+ * @param resList
|
|
|
+ * @param beginDate
|
|
|
+ * @param endDate
|
|
|
+ * @param dayOrMonthOrHour
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static ChartsResult makeChartResultByBeginEndDate(List<ChartsParam> resList, String beginDate, String endDate, Integer dayOrMonthOrHour) {
|
|
|
+ if (CollectionUtils.isEmpty(resList)) {
|
|
|
+ return new ChartsResult();
|
|
|
+ }
|
|
|
+ String title = null;
|
|
|
+ //初始化与legend数量相同的map,x轴并集去重
|
|
|
+ Map<String, Map> maps = new LinkedHashMap<>();
|
|
|
+ maps.put("早高峰", new LinkedHashMap());
|
|
|
+ maps.put("晚高峰", new LinkedHashMap());
|
|
|
+ maps.put("平峰", new LinkedHashMap());
|
|
|
+
|
|
|
+ for (ChartsParam param : resList) {
|
|
|
+ if (title == null) {
|
|
|
+ title = param.getTitle();
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(param.getLegend())) {
|
|
|
+ maps.clear();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!maps.get(param.getLegend()).containsKey(param.getxAxis())) {
|
|
|
+ maps.get(param.getLegend()).put(param.getxAxis(), param.getyAxis());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //构造x轴,重新排序
|
|
|
+ List<String> xList = getBeginEndDateList(beginDate, endDate, dayOrMonthOrHour);
|
|
|
+
|
|
|
+ //构造y轴数据,根据x轴数据补全,没有的补null
|
|
|
+ ChartsResult chartsResult = getChartsResult(maps, xList);
|
|
|
+ if (title != null) {
|
|
|
+ chartsResult.setTitle(title);
|
|
|
+ }
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param maps
|
|
|
+ * @param xList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static ChartsResult getChartsResult(Map<String, Map> maps, List<String> xList) {
|
|
|
+ List<List> yList = new ArrayList<>();
|
|
|
+ List<String> legend = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, Map> entry : maps.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ Map value = entry.getValue();
|
|
|
+ legend.add(key);
|
|
|
+ List list = new ArrayList();
|
|
|
+ for (String x : xList) {
|
|
|
+ if (!value.containsKey(x)) {
|
|
|
+ //如果对应的数值没有,用null补全
|
|
|
+ list.add(0);
|
|
|
+ } else {
|
|
|
+ list.add(value.get(x));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ yList.add(list);
|
|
|
+ }
|
|
|
+ ChartsResult chartsResult = new ChartsResult();
|
|
|
+ chartsResult.setLegend(legend);
|
|
|
+ chartsResult.setCategory(xList);
|
|
|
+ chartsResult.setData(yList);
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param beginDate
|
|
|
+ * @param endDate
|
|
|
+ * @param dayOrMonthOrHour
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static List<String> getBeginEndDateList(String beginDate, String endDate, Integer dayOrMonthOrHour) {
|
|
|
+ List<String> xList = new ArrayList();
|
|
|
+ Calendar beginCalendar = Calendar.getInstance();
|
|
|
+ Calendar endCalendar = Calendar.getInstance();
|
|
|
+ if (dayOrMonthOrHour == 1) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ try {
|
|
|
+ beginCalendar.setTime(format.parse(beginDate));
|
|
|
+ endCalendar.setTime(format.parse(endDate));
|
|
|
+ Calendar tempCalendar = beginCalendar;
|
|
|
+ do {
|
|
|
+ xList.add(format1.format(tempCalendar.getTime()));
|
|
|
+ tempCalendar.add(Calendar.DATE, 1);
|
|
|
+ } while (tempCalendar.before(endCalendar) || tempCalendar.equals(endCalendar));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ logger.log(Level.INFO, e.getMessage());
|
|
|
+ }
|
|
|
+ } else if (dayOrMonthOrHour == 0) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
|
|
|
+ SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM");
|
|
|
+ try {
|
|
|
+ beginCalendar.setTime(format.parse(beginDate.substring(0, 6)));
|
|
|
+ endCalendar.setTime(format.parse(endDate.substring(0, 6)));
|
|
|
+ Calendar tempCalendar = beginCalendar;
|
|
|
+ do {
|
|
|
+ xList.add(format1.format(tempCalendar.getTime()));
|
|
|
+ tempCalendar.add(Calendar.MONTH, 1);
|
|
|
+ } while (tempCalendar.before(endCalendar) || tempCalendar.equals(endCalendar));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ logger.log(Level.INFO, e.getMessage());
|
|
|
+ }
|
|
|
+ } else if (dayOrMonthOrHour == 3) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("HH:mm");
|
|
|
+
|
|
|
+ try {
|
|
|
+ beginCalendar.setTime(format.parse("00:00"));
|
|
|
+ endCalendar.setTime(format.parse("23:00"));
|
|
|
+ Calendar tempCalendar = beginCalendar;
|
|
|
+ do {
|
|
|
+ xList.add(format.format(tempCalendar.getTime()));
|
|
|
+ tempCalendar.add(Calendar.HOUR, 1);
|
|
|
+ } while (tempCalendar.before(endCalendar) || tempCalendar.equals(endCalendar));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ logger.log(Level.INFO, e.getMessage());
|
|
|
+ }
|
|
|
+ } else if (dayOrMonthOrHour == 4) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy");
|
|
|
+ SimpleDateFormat format1 = new SimpleDateFormat("yyyy");
|
|
|
+ try {
|
|
|
+ beginCalendar.setTime(format.parse(beginDate.substring(0, 4)));
|
|
|
+ endCalendar.setTime(format.parse(endDate.substring(0, 4)));
|
|
|
+ Calendar tempCalendar = beginCalendar;
|
|
|
+ do {
|
|
|
+ xList.add(format1.format(tempCalendar.getTime()));
|
|
|
+ tempCalendar.add(Calendar.YEAR, 1);
|
|
|
+ } while (tempCalendar.before(endCalendar) || tempCalendar.equals(endCalendar));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ logger.log(Level.INFO, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return xList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按照legend补齐
|
|
|
+ * http://echarts.baidu.com/examples/editor.html?c=bar-label-rotation
|
|
|
+ *
|
|
|
+ * @param resList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static ChartsResult makeChartResultX2(List<ChartsParam> resList) {
|
|
|
+ if (resList == null || resList.isEmpty()) {
|
|
|
+ return new ChartsResult();
|
|
|
+ }
|
|
|
+ List<String> legend = new ArrayList<>();
|
|
|
+ List<String> xAxis = new ArrayList<>();
|
|
|
+ List<List> yAxis = new ArrayList<>();
|
|
|
+
|
|
|
+ Map<String, Map<String, Double>> mapMap = new HashMap<>();
|
|
|
+ for (ChartsParam param : resList) {
|
|
|
+
|
|
|
+ if (!xAxis.contains(param.getxAxis())) {
|
|
|
+ xAxis.add(param.getxAxis());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!legend.contains(param.getLegend())) {
|
|
|
+ legend.add(param.getLegend());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!mapMap.containsKey(param.getLegend())) {
|
|
|
+ Map<String, Double> map = new HashMap<>();
|
|
|
+ map.put(param.getxAxis(), param.getyAxis());
|
|
|
+ mapMap.put(param.getLegend(), map);
|
|
|
+ } else {
|
|
|
+ mapMap.get(param.getLegend()).put(param.getxAxis(), param.getyAxis());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String string : legend) {
|
|
|
+ List<Double> data = new ArrayList<>();
|
|
|
+ for (String x : xAxis) {
|
|
|
+ if (mapMap.get(string).containsKey(x)) {
|
|
|
+ data.add(mapMap.get(string).get(x));
|
|
|
+ } else {
|
|
|
+ data.add(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ yAxis.add(data);
|
|
|
+ }
|
|
|
+ ChartsResult chartsResult = new ChartsResult(legend, xAxis, yAxis);
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改日期字符串格式,yyyyMMdd -> yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param dateList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> dateFormatTransform(List<String> dateList) {
|
|
|
+ if (dateList == null || dateList.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<String> newDateList = new ArrayList<>();
|
|
|
+ for (int i = 0; i < dateList.size(); i++) {
|
|
|
+ String date = dateList.get(i);
|
|
|
+ newDateList.add(date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6));
|
|
|
+ }
|
|
|
+ return newDateList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据给定的x轴和legend补齐数据,
|
|
|
+ *
|
|
|
+ * @param chartsParams
|
|
|
+ * @param legend
|
|
|
+ * @param xAxis
|
|
|
+ * @param type 0: 返回 legend.size * xAxis.size的矩阵, 否则返回 xAxis.size * legend.size
|
|
|
+ * @return ms.size * ns.size 的矩阵
|
|
|
+ */
|
|
|
+ public static List<List<Double>> completeChart(List<ChartsParam> chartsParams, List<String> legend, List<String> xAxis, Integer type) {
|
|
|
+ Map<String, Map<String, Double>> data = new HashMap<>();
|
|
|
+ if (type == null || type == 0) {
|
|
|
+ if (chartsParams != null && !chartsParams.isEmpty()) {
|
|
|
+ for (ChartsParam chartsParam : chartsParams) {
|
|
|
+ if (data.containsKey(chartsParam.getLegend())) {
|
|
|
+ data.get(chartsParam.getLegend()).put(chartsParam.getxAxis(), chartsParam.getyAxis());
|
|
|
+ } else {
|
|
|
+ Map<String, Double> temp = new HashMap<>();
|
|
|
+ temp.put(chartsParam.getxAxis(), chartsParam.getyAxis());
|
|
|
+ data.put(chartsParam.getLegend(), temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (chartsParams != null && !chartsParams.isEmpty()) {
|
|
|
+ for (ChartsParam chartsParam : chartsParams) {
|
|
|
+ if (data.containsKey(chartsParam.getxAxis())) {
|
|
|
+ data.get(chartsParam.getxAxis()).put(chartsParam.getLegend(), chartsParam.getyAxis());
|
|
|
+ } else {
|
|
|
+ Map<String, Double> temp = new HashMap<>();
|
|
|
+ temp.put(chartsParam.getLegend(), chartsParam.getyAxis());
|
|
|
+ data.put(chartsParam.getxAxis(), temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<List<Double>> result = new ArrayList<>();
|
|
|
+ for (String m : legend) {
|
|
|
+ List<Double> temp = new ArrayList<>();
|
|
|
+ for (String n : xAxis) {
|
|
|
+ if (data.containsKey(m) && data.get(m).containsKey(n)) {
|
|
|
+ temp.add(data.get(m).get(n));
|
|
|
+ } else {
|
|
|
+ temp.add(0.0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result.add(temp);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从map中构造图表的数据结构
|
|
|
+ *
|
|
|
+ * @param keyValue 键值对
|
|
|
+ * @param chartType 图表类型 0:直方图,折线图 1:饼图,评分Top图
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T extends Number> ChartsResult constructChartResult1(LinkedHashMap<String, T> keyValue,
|
|
|
+ Integer chartType) {
|
|
|
+ ChartsResult chartsResult = new ChartsResult();
|
|
|
+ List<String> xList = new ArrayList<>();
|
|
|
+ if (keyValue == null || keyValue.isEmpty()) {
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+ if (chartType == 0) { /** 简单直方图*/
|
|
|
+ List<Object> data = new ArrayList<>();
|
|
|
+ for (String key : keyValue.keySet()) {
|
|
|
+ xList.add(key);
|
|
|
+ data.add(keyValue.get(key));
|
|
|
+ }
|
|
|
+ chartsResult.setCategory(xList);
|
|
|
+ chartsResult.setData(data);
|
|
|
+ } else if (chartType == 1) {/** 饼图\ 评分Top图*/
|
|
|
+ List<Map<String, Object>> data = new ArrayList<>();
|
|
|
+ List<Double> ratioList = new ArrayList<>();
|
|
|
+ for (String key : keyValue.keySet()) {
|
|
|
+ xList.add(key);
|
|
|
+ Map<String, Object> tmp = new HashMap<>();
|
|
|
+ tmp.put("name", key);
|
|
|
+ tmp.put("value", keyValue.get(key));
|
|
|
+ data.add(tmp);
|
|
|
+ ratioList.add(keyValue.get(key).doubleValue());
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取占比并填充
|
|
|
+ */
|
|
|
+ int index = 0;
|
|
|
+ for (Map map : data) {
|
|
|
+ Double sum = 0.0;
|
|
|
+ for (int i = 0; i < ratioList.size(); i++) {
|
|
|
+ sum += ratioList.get(i);
|
|
|
+ }
|
|
|
+ map.put("ratio", sum == 0.0 ? 0.0 : NumberUtils.getPercentValue(ratioList, index, 2));
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ chartsResult.setLegend(xList);
|
|
|
+ chartsResult.setData(data);
|
|
|
+ }
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param keyValues 雷达图
|
|
|
+ * 多维度:radarvalue = {
|
|
|
+ * legend:['评分'],
|
|
|
+ * data:[
|
|
|
+ * { value: [100, 899, 200, 400], name: '小区综合评分1'},//value长度对应维度个数
|
|
|
+ * { value: [100, 899, 200, 400], name: '小区综合评分2'}
|
|
|
+ * ],
|
|
|
+ * category: [
|
|
|
+ * { name: '流动性', max: 1000,min: 0 },
|
|
|
+ * { name: '有利',max: 1000, min: 0 },
|
|
|
+ * { name: '周边配套', max: 1000, min: 0 }
|
|
|
+ * { name: '周边交通', max: 1000, min: 0 }
|
|
|
+ * ]
|
|
|
+ * }
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T extends Number> ChartsResult constructChartResult2(LinkedHashMap<String, LinkedHashMap<String, T>> keyValues, Integer max, Integer min) {
|
|
|
+ ChartsResult chartsResult = new ChartsResult();
|
|
|
+ List<String> legends = new ArrayList<>();
|
|
|
+ List<String> xList = new ArrayList<>();
|
|
|
+ List<List<Map<String, Object>>> categorysList = new ArrayList<>();
|
|
|
+ //data
|
|
|
+ List<Map<String, Object>> dataList = new ArrayList<>();
|
|
|
+ for (String key1 : keyValues.keySet()) {
|
|
|
+ Map<String, Object> dataMap = new HashMap<>();
|
|
|
+ List<Double> data = new ArrayList<>();
|
|
|
+ dataMap.put("name", key1);
|
|
|
+ //category
|
|
|
+ List<Map<String, Object>> categoryList = new ArrayList<>();
|
|
|
+ for (String key2 : keyValues.get(key1).keySet()) {
|
|
|
+ data.add((Double) keyValues.get(key1).get(key2));
|
|
|
+ Map<String, Object> categoryMap = new HashMap<>();
|
|
|
+ categoryMap.put("name", key2);
|
|
|
+ categoryMap.put("max", max);
|
|
|
+ categoryMap.put("min", min);
|
|
|
+ categoryList.add(categoryMap);
|
|
|
+ }
|
|
|
+ dataMap.put("value", data);
|
|
|
+ dataList.add(dataMap);
|
|
|
+ categorysList.add(categoryList);
|
|
|
+ }
|
|
|
+ chartsResult.setLegend(legends);
|
|
|
+ chartsResult.setCategory(categorysList);
|
|
|
+ chartsResult.setData(dataList);
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param keyValues 雷达图
|
|
|
+ * Map<String,Integer> key \ max \ min
|
|
|
+ * LinkedHashMap<String,List<T></>> key 维度, 值
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T extends Number> ChartsResult constructChartResult3(List<Map<String, Object>> mapList, LinkedHashMap<String, List<T>> keyValues) {
|
|
|
+ ChartsResult chartsResult = new ChartsResult();
|
|
|
+ //data
|
|
|
+ List<Map<String, Object>> dataList = new ArrayList<>();
|
|
|
+ for (String key1 : keyValues.keySet()) {
|
|
|
+ Map<String, Object> dataMap = new HashMap<>();
|
|
|
+ dataMap.put("name", key1);
|
|
|
+ dataMap.put("value", keyValues.get(key1));
|
|
|
+ dataList.add(dataMap);
|
|
|
+ }
|
|
|
+ chartsResult.setData(dataList);
|
|
|
+ //category
|
|
|
+ chartsResult.setCategory(mapList);
|
|
|
+
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造堆叠直方图 (未排序)
|
|
|
+ *
|
|
|
+ * @param keyValues 两个分桶统计值,第一分桶作为legend,第二分桶作为x轴
|
|
|
+ * @param defaultValue
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T extends Number> ChartsResult constructChartResult4(LinkedHashMap<String,
|
|
|
+ LinkedHashMap<String, T>> keyValues, T defaultValue) {
|
|
|
+ ChartsResult chartsResult = new ChartsResult();
|
|
|
+ List<String> legends = new ArrayList<>();
|
|
|
+ List<String> xList = new ArrayList<>();
|
|
|
+ for (String key1 : keyValues.keySet()) {
|
|
|
+ legends.add(key1);
|
|
|
+ for (String key2 : keyValues.get(key1).keySet()) {
|
|
|
+ if (!xList.contains(key2)) {
|
|
|
+ xList.add(key2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //TODO Collections.sort(xList);
|
|
|
+ List<List<T>> data = new ArrayList<>();
|
|
|
+ for (String key1 : keyValues.keySet()) {
|
|
|
+ LinkedHashMap<String, T> tmpMap = keyValues.get(key1);
|
|
|
+ List<T> tmpDataList = new ArrayList<>();
|
|
|
+ for (String key2 : xList) {
|
|
|
+ if (tmpMap.containsKey(key2)) {
|
|
|
+ tmpDataList.add(tmpMap.get(key2));
|
|
|
+ } else {
|
|
|
+ tmpDataList.add(defaultValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ data.add(tmpDataList);
|
|
|
+ }
|
|
|
+ chartsResult.setLegend(legends);
|
|
|
+ chartsResult.setCategory(xList);
|
|
|
+ chartsResult.setData(data);
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ //将chartparam中的成员两个list合并成一个map类型的list
|
|
|
+ public static List<Map> listPairToMapList(List name, List value) {
|
|
|
+ List<Map> result = new ArrayList<Map>();
|
|
|
+ for (int i = 0; i < name.size(); i++) {
|
|
|
+ Map map = new HashMap();
|
|
|
+ map.put(MapKeysName.keyName, name.get(i));
|
|
|
+ map.put(MapKeysName.valueName, value.get(i));
|
|
|
+ result.add(map);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ interface MapKeysName {
|
|
|
+ String keyName = "name";
|
|
|
+ String valueName = "value";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将chartparam中图例的其他放到末尾或去除
|
|
|
+ * 判断data中是否有值
|
|
|
+ *
|
|
|
+ * @param o
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isPresentData(Object o) {
|
|
|
+ if (o instanceof ChartsResult) {
|
|
|
+ if (((ChartsResult) o).getData() == null) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ List data = ((ChartsResult) o).getData();
|
|
|
+ if (data.size() == 0) return false;
|
|
|
+ if (data.size() > 0) {
|
|
|
+ Object obj = data.get(0);
|
|
|
+ return obj != null && (!(obj instanceof List) || ((List) obj).size() != 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 两个分桶统计
|
|
|
+// *
|
|
|
+// * @param agg
|
|
|
+// * @param secondAggName 第二个分桶名称
|
|
|
+// * @return linkedHashMap 保证键值对的顺序正确,Map<第一分桶,<第二分桶,值>>
|
|
|
+// */
|
|
|
+// public static LinkedHashMap<String, LinkedHashMap<String, Long>> getKeyValueFromMultiAggrestion(InternalMultiBucketAggregation agg, String secondAggName) {
|
|
|
+// LinkedHashMap<String, LinkedHashMap<String, Long>> resMap = new LinkedHashMap<>();
|
|
|
+// List<MultiBucketsAggregation.Bucket> buckets = (List<MultiBucketsAggregation.Bucket>) agg.getBuckets();
|
|
|
+// for (MultiBucketsAggregation.Bucket bucket : buckets) {
|
|
|
+// String key = bucket.getKeyAsString();
|
|
|
+// LinkedHashMap<String, Long> pMap = new LinkedHashMap<>(); //当前分类,每个时间点的统计值
|
|
|
+// InternalMultiBucketAggregation internalMultiBucketAggregation = bucket.getAggregations().get(secondAggName);
|
|
|
+// List<MultiBucketsAggregation.Bucket> bucketList = (List<MultiBucketsAggregation.Bucket>) internalMultiBucketAggregation.getBuckets();
|
|
|
+// for (MultiBucketsAggregation.Bucket bucket2 : bucketList) {
|
|
|
+// pMap.put(bucket2.getKeyAsString(), bucket2.getDocCount());
|
|
|
+// }
|
|
|
+// resMap.put(key, pMap);
|
|
|
+// }
|
|
|
+// return resMap;
|
|
|
+// }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 从es单桶统计结果中转换成键值对
|
|
|
+// *
|
|
|
+// * @param agg
|
|
|
+// * @return linkedHashMap 保证键值对的顺序正确
|
|
|
+// */
|
|
|
+// public static LinkedHashMap<String, Long> getKeyValueFromAggrestion(InternalMultiBucketAggregation agg) {
|
|
|
+// LinkedHashMap<String, Long> cMap = new LinkedHashMap<>();
|
|
|
+// List<MultiBucketsAggregation.Bucket> buckets = (List<MultiBucketsAggregation.Bucket>) agg.getBuckets();
|
|
|
+// for (MultiBucketsAggregation.Bucket bucket : buckets) {
|
|
|
+// cMap.put(bucket.getKeyAsString(), bucket.getDocCount());
|
|
|
+// }
|
|
|
+// return cMap;
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改图表x坐标轴的时间格式
|
|
|
+ *
|
|
|
+ * @param chartResult
|
|
|
+ * @param format
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static ChartsResult categoryDateFormat(ChartsResult chartResult, String format) throws ParseException {
|
|
|
+ DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ DateFormat df2 = new SimpleDateFormat(format);
|
|
|
+ List<String> newXAxis = new ArrayList<>();
|
|
|
+ for (String x : (List<String>) chartResult.getCategory()) {
|
|
|
+ newXAxis.add(df2.format(df1.parse(x)));
|
|
|
+ }
|
|
|
+ chartResult.setCategory(newXAxis);
|
|
|
+ return chartResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造堆叠直方图
|
|
|
+ *
|
|
|
+ * @param keyValues 两个分桶统计值,第一分桶作为legend,第二分桶作为x轴
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T extends Number> ChartsResult constructChartResult5(LinkedHashMap<String, LinkedHashMap<String, T>> keyValues, T defaultValue) {
|
|
|
+ ChartsResult chartResult = new ChartsResult();
|
|
|
+ List<String> legends = new ArrayList<>();
|
|
|
+ List<String> xList = new ArrayList<>();
|
|
|
+ for (String key1 : keyValues.keySet()) {
|
|
|
+ legends.add(key1);
|
|
|
+ for (String key2 : keyValues.get(key1).keySet()) {
|
|
|
+ if (!xList.contains(key2)) {
|
|
|
+ xList.add(key2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Collections.sort(xList);
|
|
|
+ List<List<T>> data = new ArrayList<>();
|
|
|
+ for (String key1 : keyValues.keySet()) {
|
|
|
+ LinkedHashMap<String, T> tmpMap = keyValues.get(key1);
|
|
|
+ List<T> tmpDataList = new ArrayList<>();
|
|
|
+ for (String key2 : xList) {
|
|
|
+ if (tmpMap.containsKey(key2)) {
|
|
|
+ tmpDataList.add(tmpMap.get(key2));
|
|
|
+ } else {
|
|
|
+ tmpDataList.add(defaultValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ data.add(tmpDataList);
|
|
|
+ }
|
|
|
+ chartResult.setLegend(legends);
|
|
|
+ chartResult.setCategory(xList);
|
|
|
+ chartResult.setData(data);
|
|
|
+ return chartResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param size size必须大于1
|
|
|
+ * @Description : 初始化具有多个图例的图表对象
|
|
|
+ * @Return :
|
|
|
+ * @Author : yangdong
|
|
|
+ * @Date : 2020/4/16 16:53
|
|
|
+ */
|
|
|
+ public static ChartsResult intialMultiLegendChart(Integer size) {
|
|
|
+ List<String> category = new ArrayList<>();
|
|
|
+ List<String> legend = new ArrayList<>();
|
|
|
+ List data = new ArrayList();
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ List dataMem = new ArrayList();
|
|
|
+ data.add(dataMem);
|
|
|
+ }
|
|
|
+ ChartsResult chartsResult = new ChartsResult(legend, category, data);
|
|
|
+ return chartsResult;
|
|
|
+ }
|
|
|
+}
|