KvVOMap
package com.包名.路径.vo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.LinkedHashMap;
import java.util.Map;
@Data
@NoArgsConstructor
//@AllArgsConstructor
public class KvVOMap<T> extends LinkedHashMap<String, Object> {
@ApiModelProperty("key")
private String k;
@ApiModelProperty("value")
private T v;
public void setK(String k) {
this.k = k;
this.put("k", k);
}
public void setV(T v) {
this.v = v;
this.put("v", v);
}
//全参构造
public KvVOMap(String k, T v) {
this.k = k;
this.v = v;
this.put("k", k);
this.put("v", v);
}
public KvVOMap<T> extra(Map<String, Object> extra) {
if (CollUtil.isNotEmpty(extra)) {
this.putAll(extra);
}
return this;
}
public KvVOMap<T> extra(String k, Object v) {
if (ObjectUtil.isAllNotEmpty(new Object[]{k, v})) {
this.put(k, v);
}
return this;
}
public String getK() {
return this.k;
}
public T getV() {
return this.v;
}
public String toString() {
return "KvVOMap(k=" + this.getK() + ", v=" + this.getV() + ")";
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof KvVOMap)) {
return false;
} else {
KvVOMap<?> other = (KvVOMap<?>)o;
if (!other.canEqual(this)) {
return false;
} else if (!super.equals(o)) {
return false;
} else {
Object this$k = this.getK();
Object other$k = other.getK();
if (this$k == null) {
if (other$k != null) {
return false;
}
} else if (!this$k.equals(other$k)) {
return false;
}
Object this$v = this.getV();
Object other$v = other.getV();
if (this$v == null) {
if (other$v != null) {
return false;
}
} else if (!this$v.equals(other$v)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof KvVOMap;
}
public int hashCode() {
int result = super.hashCode();
Object $k = this.getK();
result = result * 59 + ($k == null ? 43 : $k.hashCode());
Object $v = this.getV();
result = result * 59 + ($v == null ? 43 : $v.hashCode());
return result;
}
}
最终返给前端的格式,大概率是这样的
Apache ECharts
DataVO
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DataVO<T> {
@ApiModelProperty(value = "数值")
private List<T> data = new ArrayList<>();
@ApiModelProperty(value = "图例")
private String name;
public DataVO(List<T> data) {
this.data = data;
}
}
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.LinkedList;
import java.util.List;
@Accessors(chain = true)
@Data
public class ChartVO<T> {
@ApiModelProperty(value = "标签")
private DataVO<String> legend = new DataVO<>();
@ApiModelProperty(value = "x轴")
@JsonProperty("xAxis")
private DataVO<String> xAxis = new DataVO<>();
@ApiModelProperty(value = "统计图数据")
private List<DataVO<T>> series = new LinkedList<>();
@ApiModelProperty(value = "辅助数据")
private Object obj;
}
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
@NoArgsConstructor
@Data
public class AreaBO {
/**
* 数量
*/
private BigDecimal num;
/**
* 名称
*/
private String name;
/**
* 所属区县
*/
private String homeArea;
public AreaBO(BigDecimal num, String name) {
this.num = num;
this.name = name;
}
public AreaBO(BigDecimal num, String name, String homeArea) {
this.num = num;
this.name = name;
this.homeArea = homeArea;
}
public AreaBO(BigDecimal num, String name, Integer homeArea) {
this.num = num;
this.name = name;
this.homeArea = homeArea.toString();
}
}
import lombok.Data;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
@Accessors(chain = true)
@Data
public class AreaNameBO {
/**
* 数量
*/
private BigDecimal num;
/**
* 名称
*/
private String name;
/**
* 标签
*/
private String homeArea;
/**
* 标签1
*/
private String legend;
public AreaNameBO() {
}
public AreaNameBO(BigDecimal num, String name) {
this.num = num;
this.name = name;
}
public AreaNameBO(BigDecimal num, String name, String homeArea,String legend) {
this.num = num;
this.name = name;
this.homeArea = homeArea;
this.legend = legend;
}
public AreaNameBO(String legend, BigDecimal num) {
this.num = num;
this.legend = legend;
}
}
import lombok.Data;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
@Accessors(chain = true)
@Data
public class LegendAreaBO {
/**
* 数量
*/
private BigDecimal num;
/**
* 名称
*/
private String name;
/**
* 标签
*/
private String legend;
/**
* 所属区县
*/
private String homeArea;
public LegendAreaBO() {
}
public LegendAreaBO(BigDecimal num, String name, String legend) {
this.num = num;
this.name = name;
this.legend = legend;
}
public LegendAreaBO(BigDecimal num, String name, String legend, String homeArea) {
this.num = num;
this.name = name;
this.legend = legend;
this.homeArea = homeArea;
}
}
import lombok.Data;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
@Accessors(chain = true)
@Data
public class LegendNameBO {
/**
* 数量
*/
private BigDecimal num;
/**
* 名称
*/
private String name;
/**
* 标签
*/
private String legend;
public LegendNameBO() {
}
public LegendNameBO(BigDecimal num, String name) {
this.num = num;
this.name = name;
}
public LegendNameBO(BigDecimal num, String name, String legend) {
this.num = num;
this.name = name;
this.legend = legend;
}
public LegendNameBO(String legend,BigDecimal num) {
this.num = num;
this.legend = legend;
}
}
import cn.hutool.core.util.NumberUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
@Accessors(chain = true)
@Data
public class NameBO {
/**
* 数量
*/
private BigDecimal num;
/**
* 名称
*/
private String name;
public NameBO() {
}
public NameBO(BigDecimal num, String name) {
this.num = NumberUtil.round(num, 4);
this.name = name;
}
public NameBO(BigDecimal num) {
this.num = num;
}
public void setNum(BigDecimal num) {
this.num = NumberUtil.round(num, 4);
}
}
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.gas.base.enums.BaseEnum;
import com.gas.bo.*;
import com.gas.enums.AreaEnum;
import com.gas.utils.bean.GasBeanUtil;
import com.gas.utils.collection.GasCollectionUtil;
import com.gas.utils.convert.GasConvertUtil;
import com.gas.utils.enums.AreaBOEnum;
import com.gas.utils.list.GasListUtil;
import com.gas.utils.map.GasMapUtil;
import com.gas.utils.string.GasStringUtil;
import com.gas.vo.*;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
public class EChartsUtil {
public static List<Integer> area(String area) {
if (StrUtil.isBlank(area)) {
return GasListUtil.empty();
}
return StrUtil.split(area, ",")
.stream()
.map(GasConvertUtil::toInt)
.toList();
}
public static List<AreaBO> areaBOMain(List<AreaBO> list) {
if (GasCollectionUtil.isNotEmpty(list)) {
if (list.stream()
.anyMatch(e -> AreaEnum.mainArea.contains(e.getHomeArea()))) {
//汇总主城区数量
BigDecimal sum = list.stream()
.filter(e -> AreaEnum.mainArea.contains(e.getHomeArea()))
.map(AreaBO::getNum)
.reduce(BigDecimal.ZERO, BigDecimal::add);
list.removeIf(e -> AreaEnum.mainArea.contains(e.getHomeArea()));
//添加主城区数量
list.add(0, new AreaBO(sum, AreaEnum.mainAreaName, StrUtil.join(",", AreaEnum.mainArea)));
}
}
return list;
}
public static List<AreaBO> areaBOCityMain(List<AreaBO> list) {
return areaBOMain(list);
}
public static ChartVO<BigDecimal> areaBO(List<AreaBO> bos, AreaBOEnum enums, List<Integer> areas) {
List<AreaBO> list = Arrays.stream(AreaEnum.values())
.filter(e -> {
if (GasCollectionUtil.isNotEmpty(areas)) {
return areas.contains(e.getType());
}
return true;
})
.map(e ->
new AreaBO(bos.stream()
.filter(y -> ObjectUtil.equal(y.getHomeArea(), e.getType().toString()))
.map(AreaBO::getNum)
.findFirst()
.orElse(BigDecimal.ZERO),
e.getName(), e.getType().toString()))
.collect(Collectors.toList());
list.forEach(e -> e.setName(AreaEnum.name(e.getHomeArea())));
list.sort(Comparator.comparing(e -> AreaEnum.sort(e.getHomeArea())));
//组装数据
switch (enums) {
case CITY -> areaBOCityMain(list);
case MAIN -> areaBOMain(list);
}
//组装辅助数据
BigDecimal sum = list.stream()
.map(AreaBO::getNum)
.reduce(BigDecimal.ZERO, BigDecimal::add);
Map<String, List<CityTotalKvVO<BigDecimal, BigDecimal>>> obj = list.stream()
.collect(Collectors.groupingBy(AreaBO::getName,
Collectors.mapping(e ->
new CityTotalKvVO<>(e.getName(), e.getNum(), sum)
, Collectors.toList())));
return areaBO(list).setObj(obj);
}
public static ChartVO<BigDecimal> areaBO(List<AreaBO> bos, AreaBOEnum enums) {
return areaBO(bos, enums, null);
}
public static ChartVO<BigDecimal> areaBO(List<AreaBO> list) {
ChartVO<BigDecimal> chart = new ChartVO<>();
//汇总x轴
List<String> xAxis = list.stream()
.map(AreaBO::getName)
.filter(StrUtil::isNotBlank)
.distinct()
.collect(Collectors.toList());
chart.setXAxis(new DataVO<>(xAxis));
//汇总数据
List<BigDecimal> series = list.stream()
.filter(e -> StrUtil.isNotBlank(e.getName()))
.map(AreaBO::getNum)
.collect(Collectors.toList());
chart.getSeries().add(new DataVO<>(series));
return chart;
}
public static List<LegendAreaBO> legendAreaBOSort(List<LegendAreaBO> list, Map<String, Integer> legendSort) {
//List自带的排序方法
list.sort(
//先按区域排序,再按标签排序
Comparator.comparing((LegendAreaBO e) -> AreaEnum.sort(e.getHomeArea()))
.thenComparing((LegendAreaBO e) -> legendSort.getOrDefault(e.getLegend(), Integer.MAX_VALUE)
));
return list;
}
public static Map<String, Map<String, BigDecimal>> legendNameBOGroup(List<LegendNameBO> bos) {
return bos.stream()
.collect(Collectors.groupingBy(LegendNameBO::getName,
LinkedHashMap::new,
Collectors.groupingBy(LegendNameBO::getLegend,
LinkedHashMap::new,
Collectors.mapping(LegendNameBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add)
)
)
));
}
public static Map<String, Map<String, BigDecimal>> legendAreaBOGroup(List<LegendAreaBO> bos) {
return bos.stream()
.collect(Collectors.groupingBy(LegendAreaBO::getHomeArea,
Collectors.groupingBy(LegendAreaBO::getLegend,
Collectors.mapping(LegendAreaBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add)))));
}
/**
* 主城区 + 标签 + x轴 + obj辅助数据
*/
public static ChartVO legendAreaBO(List<LegendAreaBO> list) {
EChartsUtil.legendAreaBOMain(list);
ChartVO<BigDecimal> chart = new ChartVO<>();
//汇总标签
List<String> legend = list.stream()
.map(LegendAreaBO::getLegend)
.filter(StrUtil::isNotBlank)
.distinct()
.collect(Collectors.toList());
chart.setLegend(new DataVO<>(legend));
//汇总x轴
List<String> xAxis = list.stream()
.map(LegendAreaBO::getName)
.filter(StrUtil::isNotBlank)
.distinct()
.collect(Collectors.toList());
chart.setXAxis(new DataVO<>(xAxis));
//汇总数据
legend.forEach(e -> {
List<BigDecimal> series = list.stream()
.filter(y -> ObjectUtil.equal(e, y.getLegend()) && StrUtil.isNotEmpty(y.getName()))
.map(LegendAreaBO::getNum)
.collect(Collectors.toList());
chart.getSeries().add(new DataVO<>(series, e));
});
Map<String, BigDecimal> legendMap = list.stream()
.collect(Collectors.groupingBy(LegendAreaBO::getLegend,
Collectors.mapping(LegendAreaBO::getNum, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))
));
//组装辅助数据
Map<String, List<CityTotalKvVO<BigDecimal, BigDecimal>>> obj = list.stream()
.collect(Collectors.groupingBy(LegendAreaBO::getName,
Collectors.mapping(e ->
new CityTotalKvVO<>(e.getLegend(), e.getNum(), legendMap.get(e.getLegend()))
, Collectors.toList())));
return chart
.setObj(obj);
}
public static List<LegendAreaBO> legendAreaBOMain(List<LegendAreaBO> list) {
if (CollUtil.isNotEmpty(list)) {
//汇总主城区数量
LinkedHashMap<String, BigDecimal> map = list.stream()
.filter(e -> AreaEnum.mainArea.contains(e.getHomeArea()))
.collect(Collectors.groupingBy(LegendAreaBO::getLegend,
LinkedHashMap::new,
Collectors.mapping(LegendAreaBO::getNum, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))
));
//移除 路南、路北、高新区
list.removeIf(e -> AreaEnum.mainArea.contains(e.getHomeArea()));
//添加主城区数量
List<LegendAreaBO> area = new LinkedList();
map.forEach((k, v) ->
area.add(new LegendAreaBO(v, AreaEnum.mainAreaName, k))
);
list.addAll(0, area);
}
return list;
}
public static ChartVO legendAreaBODictObj(List<LegendAreaBO> list) {
list.forEach(e -> e.setName(AreaEnum.name(e.getHomeArea())));
return EChartsUtil.legendAreaBO(list);
}
public static ChartVO legendAreaBODictObj(List<LegendAreaBO> bos, List<KvBO> dict) {
Map<String, Map<String, BigDecimal>> map = legendAreaBOGroup(bos);
Set<String> areas = bos.stream()
.map(LegendAreaBO::getHomeArea)
.collect(Collectors.toSet());
bos = areas.stream()
.map(e -> dict.stream()
.map(y -> new LegendAreaBO(map.getOrDefault(e, GasMapUtil.empty())
.getOrDefault(y.getKey(), BigDecimal.ZERO),
AreaEnum.name(e), y.getValue(), e))
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.collect(Collectors.toList());
return EChartsUtil.legendAreaBO(bos);
}
public static <T extends BaseEnum> ChartVO legendNameBOEnumObj(List<LegendNameBO> bos,
//X轴
List<T> names,
//标签
List<String> legends) {
Map<String, Map<String, BigDecimal>> map = legendNameBOGroup(bos);
bos = names.stream()
.map(e ->
legends.stream()
.map(y -> new LegendNameBO(map.getOrDefault(e.getValue().toString(), MapUtil.empty())
.getOrDefault(y, BigDecimal.ZERO),
e.getEnumName(), y))
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.collect(Collectors.toList());
return EChartsUtil.legendNameBO(bos);
}
public static <T extends BaseEnum> ChartVO legendAreaBOEnumObj(List<LegendAreaBO> list,
//枚举List
List<T> enums,
//标签排序
Map<String, Integer> legendSort) {
list.forEach(e -> {
//区域名称
e.setName(AreaEnum.name(e.getHomeArea()));
BaseEnum baseEnum = enums.stream()
.filter(y -> ObjectUtil.equal(y.getValue().toString(), e.getLegend()))
.findFirst()
.get();
if (ObjectUtil.isNotNull(baseEnum)) {
e.setLegend(baseEnum.getEnumName());
}
});
Optional.of(list.stream()
.map(LegendAreaBO::getHomeArea)
.toList())
.orElse(new ArrayList<>())
.forEach(e -> {
//补充标签
enums.forEach(y -> {
boolean bool = list.stream()
.anyMatch(t -> ObjectUtil.equal(e + y.getEnumName(),
t.getHomeArea() + t.getLegend()));
if (!bool) {
list.add(new LegendAreaBO(BigDecimal.ZERO, AreaEnum.name(e), y.getEnumName(), e));
}
});
});
//排序数据
EChartsUtil.legendAreaBOSort(list, legendSort);
return EChartsUtil.legendAreaBO(list);
}
public static ChartVO legendNameBO(List<LegendNameBO> list) {
ChartVO chart = new ChartVO<>();
//汇总标签
List<String> legend = list.stream()
.map(LegendNameBO::getLegend)
.filter(StrUtil::isNotBlank)
.distinct()
.collect(Collectors.toList());
chart.setLegend(new DataVO<>(legend));
//汇总x轴
List<String> xAxis = list.stream()
.map(LegendNameBO::getName)
.filter(StrUtil::isNotBlank)
.distinct()
.collect(Collectors.toList());
chart.setXAxis(new DataVO<>(xAxis));
//汇总数据
legend.forEach(e -> {
List<BigDecimal> series = list.stream()
.filter(y -> ObjectUtil.equal(e, y.getLegend()) && StrUtil.isNotEmpty(y.getName()))
.map(LegendNameBO::getNum)
.collect(Collectors.toList());
chart.getSeries().add(new DataVO<>(series, e));
});
return chart;
}
public static List<LegendNameBO> legendNameBOSort(List<LegendNameBO> list, Map<String, Integer> legendSort) {
list.sort(Comparator.comparing(e -> legendSort.get(e.getLegend())));
return list;
}
public static <T extends BaseEnum> ChartVO legendNameBOEnumObj(List<LegendNameBO> list,
List<String> names,
List<T> enums,
Map<String, Integer> legendSort) {
list.forEach(e -> {
BaseEnum baseEnum = enums.stream()
.filter(y -> ObjectUtil.equal(y.getValue().toString(), e.getLegend()))
.findFirst()
.get();
if (ObjectUtil.isNotNull(baseEnum)) {
e.setLegend(baseEnum.getEnumName());
}
});
names.forEach(e -> {
enums.forEach(y -> {
boolean bool = list.stream()
.anyMatch(t -> ObjectUtil.equal(e + y.getEnumName(),
t.getName() + t.getLegend()));
if (!bool) {
list.add(new LegendNameBO(BigDecimal.ZERO, e, y.getEnumName()));
}
});
});
Map<String, BigDecimal> collect = list.stream()
.collect(Collectors.groupingBy(LegendNameBO::getName,
Collectors.mapping(LegendNameBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
collect.forEach((k, v) -> {
if (BigDecimal.ZERO.compareTo(v) == 0) {
list.removeIf(e -> ObjectUtil.equal(k, e.getName()));
}
});
list.sort(Comparator.comparing((LegendNameBO e) -> GasConvertUtil.toInt(e.getName()))
.thenComparing((LegendNameBO e) -> legendSort.getOrDefault(e.getLegend(), Integer.MAX_VALUE)
));
//组装辅助数据
Map<String, List<KvVO<BigDecimal>>> obj = list.stream()
.collect(Collectors.groupingBy(LegendNameBO::getName,
Collectors.mapping(e ->
new KvVO<>(e.getLegend(), e.getNum())
, Collectors.toList())));
return EChartsUtil.legendNameBO(list).setObj(obj);
}
public static <T extends BaseEnum> ChartVO legendNameBOEnumObj(List<LegendNameBO> bos,
List<T> enums) {
Set<String> names = bos.stream()
.map(LegendNameBO::getName)
.collect(Collectors.toSet());
Map<String, BigDecimal> map = bos.stream()
.collect(Collectors.toMap(e -> e.getName() + e.getLegend(), LegendNameBO::getNum));
List<LegendNameBO> list = names.stream()
.map(e ->
enums.stream()
.map(y -> new LegendNameBO(map.getOrDefault(e + y.getValue(), BigDecimal.ZERO),
e, y.getEnumName()))
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.toList();
Map<String, BigDecimal> collect = list.stream()
.collect(Collectors.groupingBy(LegendNameBO::getName,
Collectors.mapping(LegendNameBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
collect.forEach((k, v) -> {
if (BigDecimal.ZERO.compareTo(v) == 0) {
list.removeIf(e -> ObjectUtil.equal(k, e.getName()));
}
});
//组装辅助数据
Map<String, List<KvVO<BigDecimal>>> obj = list.stream()
.collect(Collectors.groupingBy(LegendNameBO::getName,
Collectors.mapping(e ->
new KvVO<>(e.getLegend(), e.getNum())
, Collectors.toList())));
return EChartsUtil.legendNameBO(list).setObj(obj);
}
public static ChartVO legendNameBOAreaEnterprise(List<LegendNameBO> list, List<LegendNameBO> listArea, String area) {
Map<String, BigDecimal> collect = list.stream()
.collect(Collectors.groupingBy(LegendNameBO::getName,
Collectors.mapping(LegendNameBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
collect.forEach((k, v) -> {
if (BigDecimal.ZERO.compareTo(v) == 0) {
list.removeIf(e -> ObjectUtil.equal(k, e.getName()));
}
});
Map<String, BigDecimal> map = list.stream()
.collect(Collectors.groupingBy(LegendNameBO::getLegend,
Collectors.mapping(LegendNameBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
String areaName = AreaEnum.getAreaName(area);
listArea.forEach(e ->
e.setName(areaName)
.setNum(map.getOrDefault(e.getLegend(), BigDecimal.ZERO))
);
list.addAll(0, listArea);
return legendNameBO(list);
}
public static <T extends BaseEnum> ChartVO legendNameBOAreaEnterpriseEnum(List<LegendNameBO> bos,
String areaCode,
List<T> enums,
Map<String, Integer> legendSort) {
String areaName = AreaEnum.getAreaName(areaCode);
List<LegendNameBO> areaList = enums.stream()
.map(e ->
new LegendNameBO(bos.stream()
.filter(y -> ObjectUtil.equal(e.getValue().toString(), y.getLegend()))
.map(LegendNameBO::getNum)
.reduce(BigDecimal.ZERO, BigDecimal::add), areaName, e.getValue().toString()))
.collect(Collectors.toList());
Set<String> names = bos.stream()
.map(LegendNameBO::getName)
.collect(Collectors.toSet());
List<LegendNameBO> list = names.stream()
.map(e ->
enums.stream()
.map(y -> {
LegendNameBO legendNameBO = bos.stream()
.filter(k -> ObjectUtil.equal(e, k.getName()))
.filter(k -> ObjectUtil.equal(y.getValue().toString(), k.getLegend()))
.findFirst()
.orElse(null);
if (ObjectUtil.isNull(legendNameBO)) {
legendNameBO = new LegendNameBO(BigDecimal.ZERO, e, y.getValue().toString());
}
return legendNameBO;
})
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.collect(Collectors.toList());
list.addAll(0, areaList);
//补第一列轴
// enums.forEach(e -> {
// LegendNameBO legendNameBO = areaList.stream()
// .filter(y -> ObjectUtil.equal(e.getValue().toString(), y.getLegend()))
// .findFirst()
// .orElse(null);
// if (ObjectUtil.isNull(legendNameBO)) {
// areaList.add(new LegendNameBO(BigDecimal.ZERO, areaName, e.getEnumName()));
// } else {
// legendNameBO.setName(areaName)
// .setLegend(e.getEnumName());
// }
// });
//
list.forEach(e -> {
BaseEnum baseEnum = enums.stream()
.filter(y -> ObjectUtil.equal(y.getValue().toString(), e.getLegend()))
.findFirst()
.get();
if (ObjectUtil.isNotNull(baseEnum)) {
e.setLegend(baseEnum.getEnumName());
}
});
//补x轴
// Optional.of(list.stream()
// .map(LegendNameBO::getName)
// .toList())
// .orElse(new ArrayList<>())
// .forEach(e -> {
// enums.forEach(y -> {
// boolean bool = list.stream()
// .anyMatch(t -> ObjectUtil.equal(e + y.getEnumName(),
// t.getName() + t.getLegend()));
// if (!bool) {
// list.add(new LegendNameBO(BigDecimal.ZERO, e, y.getEnumName()));
// }
// });
// });
// Map collect = list.stream()
// .collect(Collectors.groupingBy(LegendNameBO::getName,
// Collectors.mapping(LegendNameBO::getNum,
// Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
// collect.forEach((k, v) -> {
// if (BigDecimal.ZERO.compareTo(v) == 0) {
// list.removeIf(e -> ObjectUtil.equal(k, e.getName()));
// }
// });
legendNameBOSort(list, legendSort);
return legendNameBO(list);
}
public static ChartVO legendNameBOAreaEnterpriseDict(List<LegendNameBO> bos, List<KvBO> dict, String area) {
Map<String, BigDecimal> areaMap = bos.stream()
.collect(Collectors.groupingBy(LegendNameBO::getLegend,
Collectors.mapping(LegendNameBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
List<LegendNameBO> areaBO = dict.stream()
.map(e -> new LegendNameBO(
areaMap.getOrDefault(e.getKey(), BigDecimal.ZERO),
AreaEnum.getAreaName(area),
e.getValue()))
.collect(Collectors.toList());
Map<String, Map<String, BigDecimal>> map = legendNameBOGroup(bos);
Set<String> names = bos.stream()
.map(LegendNameBO::getName)
.collect(Collectors.toSet());
bos = names.stream()
.map(e -> dict.stream()
.map(y -> new LegendNameBO(
map.getOrDefault(e, GasMapUtil.empty())
.getOrDefault(y.getKey(), BigDecimal.ZERO),
e, y.getValue()))
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.collect(Collectors.toList());
bos.addAll(0, areaBO);
//组装辅助数据
Map<String, List<KvVO<BigDecimal>>> obj = bos.stream()
.collect(Collectors.groupingBy(LegendNameBO::getName,
Collectors.mapping(e ->
new KvVO<>(e.getLegend(), e.getNum())
, Collectors.toList())));
return legendNameBO(bos).setObj(obj);
}
public static ChartVO nameBODateScope(List<NameBO> bos,
List<String> dateScope) {
List<NameBO> list = new ArrayList<>();
Map<String, BigDecimal> map = bos.stream()
.collect(Collectors.toMap(NameBO::getName, NameBO::getNum));
for (int i = 0; i < dateScope.size(); i++) {
String e = dateScope.get(i);
list.add(new NameBO(map.getOrDefault(e, BigDecimal.ZERO), e));
}
//若这里出问题,就把这段代码删了 或 注释了,再把下边的代码 解开注释
List<KvBO> obj = list.stream()
.map(e -> new KvBO(e.getName(),e.getNum().toString()))
.collect(Collectors.toList());
kvBOPercent(obj);
obj.forEach(e -> e.setValue(e.getValue() + "%"));
// Map collect = list.stream()
// .collect(Collectors.groupingBy(NameBO::getName, Collectors.mapping(NameBO::getNum,
// Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
// BigDecimal sum = list.stream()
// .map(NameBO::getNum)
// .reduce(BigDecimal.ZERO, BigDecimal::add);
//
// List obj = new ArrayList<>();
// for (int i = 0; i < dateScope.size(); i++) {
// String e = dateScope.get(i);
// BigDecimal total = collect.getOrDefault(e, BigDecimal.ZERO);
// KvBO bo = new KvBO(e, "0%");
// if (BigDecimal.ZERO.compareTo(total) != 0) {
// bo.setValue(NumberUtil.round(
// NumberUtil.mul(
// NumberUtil.div(total, sum), EChartsUtil.ONE_HUNDRED)
// , 2)
// + "%");
// }
// obj.add(bo);
// }
return nameBO(list).setObj(obj);
}
public static ChartVO nameBOAreaEnterprise(List<NameBO> list, NameBO bo, String areaCode) {
bo.setName(AreaEnum.getAreaName(areaCode));
list.add(0, bo);
return nameBO(list);
}
public static ChartVO nameBOAreaEnterprise(List<NameBO> list, String areaCode) {
list.add(0, new NameBO(list.stream()
.map(NameBO::getNum)
.reduce(BigDecimal.ZERO, BigDecimal::add), AreaEnum.getAreaName(areaCode)));
return nameBO(list);
}
public static ChartVO nameBO(List<NameBO> list) {
ChartVO chart = new ChartVO<>();
//汇总x轴
List<String> xAxis = list.stream()
.map(NameBO::getName)
.filter(StrUtil::isNotBlank)
.distinct()
.collect(Collectors.toList());
chart.setXAxis(new DataVO<>(xAxis));
//汇总数据
List<BigDecimal> series = list.stream()
.map(NameBO::getNum)
.collect(Collectors.toList());
chart.getSeries().add(new DataVO<>(series));
return chart;
}
public static final BigDecimal ONE_HUNDRED = new BigDecimal("100");
public static ChartVO kvBOPiePercent(List<KvBO> list, Map<String, String> map) {
return kvBOPie(kvBOPercent(list), map);
}
public static BigDecimal percent(String divisor, String dividend) {
return percent(StrUtil.isNotBlank(divisor) ? new BigDecimal(divisor) : null,
StrUtil.isNotBlank(dividend) ? new BigDecimal(dividend) : null);
}
public static BigDecimal percent(BigDecimal divisor, BigDecimal dividend) {
if (ObjectUtil.isAllNotEmpty(divisor, dividend) && dividend.compareTo(BigDecimal.ZERO) != 0) {
return NumberUtil.round(
NumberUtil.mul(
NumberUtil.div(divisor, dividend), EChartsUtil.ONE_HUNDRED), 2);
}
return BigDecimal.ZERO;
}
public static ChartVO kvBOPiePercent(List<KvBO> list) {
return kvBOPie(kvBOPercent(list));
}
private static List<KvBO> kvBOPercent(List<KvBO> list) {
BigDecimal sum = list.stream()
.map(e -> new BigDecimal(e.getValue()))
.reduce(BigDecimal.ZERO, BigDecimal::add);
list.forEach(e -> {
BigDecimal value = new BigDecimal(e.getValue());
if (BigDecimal.ZERO.compareTo(value) != 0) {
e.setValue(
NumberUtil.round(
NumberUtil.mul(
NumberUtil.div(e.getValue(), StrUtil.toString(sum)), ONE_HUNDRED)
, 2).toString());
} else {
e.setValue("0");
}
});
return list;
}
public static ChartVO kvBOPie(List<KvBO> list) {
ChartVO chart = new ChartVO();
//汇总标签
List<String> legend = list.stream()
.map(KvBO::getKey)
.collect(Collectors.toList());
chart.setLegend(new DataVO<>(legend));
List<PieDataVO> pieData = list.stream()
.map(e -> new PieDataVO<>(e.getKey(), e.getValue()))
.collect(Collectors.toList());
chart.setSeries(GasListUtil.toList(new DataVO<>(pieData)));
return chart.setObj(list);
}
public static ChartVO kvBOPie(List<KvBO> list, Map<String, String> map, List<String> names) {
ChartVO chartVO = kvBOPie(list, map);
chartVO.getLegend().setData(names);
return chartVO;
}
public static ChartVO kvBOPie(List<KvBO> list, Map<String, String> map) {
map.forEach((k, v) -> {
KvBO kvBO = list.stream()
.filter(e -> ObjectUtil.equal(e.getKey(), k))
.findFirst()
.orElse(null);
if (ObjectUtil.isNull(kvBO)) {
list.add(new KvBO(v, "0"));
} else {
kvBO.setKey(v);
}
});
return kvBOPie(list);
}
/**
* KvBO 柱形图 折线图
*/
public static ChartVO kvVO(List<KvVO<BigDecimal>> bo) {
ChartVO chart = new ChartVO<>();
//汇总x轴
List<String> xAxis = bo.stream()
.filter(e -> StrUtil.isNotBlank(e.getK()))
.map(KvVO::getK)
.distinct()
.collect(Collectors.toList());
chart.setXAxis(new DataVO<>(xAxis));
//汇总数据
List<BigDecimal> series = bo.stream()
.filter(e -> StrUtil.isNotBlank(e.getK()))
.map(KvVO::getV)
.collect(Collectors.toList());
chart.getSeries().add(new DataVO<>(series));
return chart;
}
/**
* KvBO 转换为饼图->处理数据
*
* @param bo
* @param div 被除数
* @return
*/
public static ChartVO kvBOPie(List<KvBO> bo, String div) {
if (StrUtil.isNotBlank(div) && GasConvertUtil.toInt(div) != 0) {
bo.forEach(e -> {
e.setValue(
NumberUtil.div(e.getValue(), div, 2).toString()
);
});
}
return kvBOPie(bo);
}
public static ChartVO legendNameBODateScope(List<LegendNameBO> bos, List<String> legends, List<String> dateScope) {
List<LegendNameBO> list = new ArrayList<>();
Map<String, Map<String, BigDecimal>> map = legendNameBOGroup(bos);
for (int i = 0; i < dateScope.size(); i++) {
String e = dateScope.get(i);
for (int k = 0; k < legends.size(); k++) {
String y = legends.get(k);
list.add(new LegendNameBO(
map.getOrDefault(e, GasMapUtil.empty())
.getOrDefault(y, BigDecimal.ZERO), e, y));
}
}
return legendNameBO(list);
}
public static ChartVO legendNameBOAreaEnterprise(List<LegendNameBO> bos, String area) {
String areaName = AreaEnum.getAreaName(area);
bos.addAll(0, bos.stream()
.collect(Collectors.groupingBy(LegendNameBO::getLegend,
LinkedHashMap::new,
Collectors.mapping(LegendNameBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))))
.entrySet()
.stream()
.map(e -> new LegendNameBO(e.getValue(), areaName, e.getKey()))
.collect(Collectors.toList()));
return legendNameBO(bos);
}
public static <T extends BaseEnum> ChartVO legendNameBOEnumDateScope(List<LegendNameBO> bos,
List<T> enums,
Map<String, Integer> legendSort,
//日期偏移量
List<String> dateScope) {
HashMap<String, Integer> dateScopeSort = new HashMap<>();
for (int i = 0; i < dateScope.size(); i++) {
dateScopeSort.put(dateScope.get(i), i);
}
Map<String, Map<String, BigDecimal>> map = legendNameBOGroup(bos);
bos = dateScope.stream()
.map(e -> enums.stream()
.map(y -> new LegendNameBO(map.getOrDefault(e, GasMapUtil.empty())
.getOrDefault(y.getValue().toString(), BigDecimal.ZERO),
e, y.getEnumName()))
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.collect(Collectors.toList());
bos.sort(Comparator.comparing((LegendNameBO e) -> dateScopeSort.get(e.getName()))
.thenComparing((LegendNameBO e) -> legendSort.getOrDefault(e.getLegend(), Integer.MAX_VALUE)
));
return legendNameBO(bos);
}
private static final List<AreaEnum> AREA_LIST = GasListUtil.toList(AreaEnum.values());
public static Map<String, Map<String, BigDecimal>> areaNameBOGroup(List<AreaNameBO> bos) {
return bos.stream()
.collect(Collectors.groupingBy(AreaNameBO::getName,
LinkedHashMap::new,
Collectors.groupingBy(AreaNameBO::getHomeArea,
LinkedHashMap::new,
Collectors.mapping(AreaNameBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add)
)
)
));
}
;
public static ChartVO areaNameBORadarEnumObj(List<AreaNameBO> bos,
List<KvBO> nameBOS) {
Map<String, Map<String, BigDecimal>> map = areaNameBOGroup(bos);
List<AreaNameBO> list = nameBOS.stream()
.map(e ->
AREA_LIST.stream()
.map(y -> new AreaNameBO(map.getOrDefault(e.getKey(), GasMapUtil.empty())
.getOrDefault(y.getValue().toString(), BigDecimal.ZERO),
e.getValue(), y.getType().toString(), y.getEnumName()))
.collect(Collectors.toList()))
.flatMap(Collection::stream)
.collect(Collectors.toList());
String main = GasStringUtil.join(",", AreaEnum.mainArea);
List<AreaNameBO> mainBOS = list.stream()
.filter(e -> AreaEnum.mainArea.contains(e.getHomeArea()))
.collect(Collectors.groupingBy(AreaNameBO::getName,
LinkedHashMap::new,
Collectors.mapping(AreaNameBO::getNum,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))))
.entrySet()
.stream()
.map(e -> new AreaNameBO(e.getValue(),
e.getKey(), main, AreaEnum.mainAreaName))
.collect(Collectors.toList());
list.removeIf(e -> AreaEnum.mainArea.contains(e.getHomeArea()));
list.addAll(mainBOS);
list.sort(Comparator.comparing(e -> AreaEnum.LEGEND_SORT.getOrDefault(e.getLegend(), Integer.MAX_VALUE)));
//组装辅助数据
Map<String, List<KvVO<BigDecimal>>> obj = list.stream()
.collect(Collectors.groupingBy(AreaNameBO::getLegend,
Collectors.mapping(e ->
new KvVO<>(e.getName(), e.getNum())
, Collectors.toList())));
return EChartsUtil.legendNameBO(GasBeanUtil.copyPropertiesList(list, LegendNameBO.class)).setObj(obj);
}
}
public interface BaseEnum<T> {
T getValue();
default String getEnumName() {
return null;
}
}
package com.gas.enums;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.gas.base.enums.BaseEnum;
import com.gas.utils.convert.GasConvertUtil;
import com.gas.utils.obj.GasObjectUtil;
import com.gas.utils.string.GasStringUtil;
import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* 区县类型
* 1:路北区 2:路南区 3:古冶区 4:开平区 5:丰南区 6:丰润区 7:高新开发区 8:迁安市 9:遵化市
* 10:滦州市 11:滦南县 12:乐亭县 13:迁西县 14:玉田县 15:海港开发区 16:国际旅游岛 17:芦台开发区
* 18:汉沽管理区 19:曹妃甸区 20:南堡开发区
*
* @author zhaoyang
*/
@Getter
public enum AreaEnum implements BaseEnum<Integer> {
_130203(130203, 1, "路北区", 1),
_130202(130202, 2, "路南区", 2),
_130204(130204, 3, "古冶区", 3),
_130205(130205, 4, "开平区", 4),
_130207(130207, 5, "丰南区", 5),
_130208(130208, 6, "丰润区", 6),
_130273(130273, 7, "高新开发区", 7),
_130283(130283, 8, "迁安市", 8),
_130281(130281, 9, "遵化市", 9),
_130284(130284, 10, "滦州市", 10),
_130224(130224, 11, "滦南县", 11),
_130225(130225, 12, "乐亭县", 12),
_130227(130227, 13, "迁西县", 13),
_130229(130229, 14, "玉田县", 14),
_130274(130274, 15, "海港开发区", 15),
_130290(130290, 16, "国际旅游岛", 16),
_130271(130271, 17, "芦台开发区", 17),
_130272(130272, 18, "汉沽管理区", 18),
_130209(130209, 19, "曹妃甸区", 19),
//南堡 暂时属于 曹妃甸
_20(130209, 20, "南堡开发区", 20),
;
private final Integer region;
private final Integer type;
private final String name;
private final Integer sort;
public static final Map<String, Integer> LEGEND_SORT = Arrays.stream(AreaEnum.values())
.collect(Collectors.toMap(AreaEnum::getName, AreaEnum::getSort));
public static final String mainAreaName = "主城区";
public static final String allAreaName = "全市";
AreaEnum(Integer region, Integer type, String name, Integer sort) {
this.region = region;
this.type = type;
this.name = name;
this.sort = sort;
}
public static final List<String> mainArea = Arrays.stream(AreaEnum.values())
.filter(e -> GasObjectUtil.equals(e, _130203, _130202, _130273))
.map(AreaEnum::getType)
.map(GasStringUtil::toString)
.toList();
public static final List<Integer> allArea = Arrays.stream(AreaEnum.values())
.map(AreaEnum::getType)
.toList();
public static AreaEnum of(Integer type) {
return Arrays.stream(AreaEnum.values())
.filter(e -> GasObjectUtil.equals(e.getType(), type))
.findFirst()
.orElse(null);
}
public static String name(String type) {
return name(GasConvertUtil.toInt(type));
}
public static String name(Integer type) {
return Arrays.stream(AreaEnum.values())
.filter(e -> ObjectUtil.equals(e.getType(), type))
.findFirst()
.map(AreaEnum::getName)
.orElse(GasStringUtil.EMPTY);
}
public static Integer sort(String type) {
return sort(GasConvertUtil.toInt(type));
}
public static Integer sort(Integer type) {
return Arrays.stream(AreaEnum.values())
.filter(e -> ObjectUtil.equals(e.getType(), type))
.findFirst()
.map(AreaEnum::getSort)
.orElse(0);
}
public static Integer type(String name) {
return Arrays.stream(AreaEnum.values())
.filter(e -> ObjectUtil.equals(e.getName(), name))
.findFirst()
.map(AreaEnum::getType)
.orElse(null);
}
public static boolean isMain(String area) {
return StrUtil.contains(area, ",");
}
public static String getAreaName(String area) {
return isMain(area) ? AreaEnum.mainAreaName : name(area);
}
public static Integer getRegionByAreaType(String type) {
return Arrays.stream(AreaEnum.values())
.filter(e -> Objects.equals(e.getType(), Integer.valueOf(type)))
.findFirst()
.map(AreaEnum::getRegion)
.orElse(null);
}
@Override
public Integer getValue() {
return this.type;
}
@Override
public String getEnumName() {
return this.name;
}
public Integer handleSort(String type) {
return sort(type);
}
}
记录个大概