/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.faster.buzz;
import com.faster.util.Time;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import tech.qting.bcore.mo.Record;
import tech.qting.bcore.mo.RecordSet;
import tech.qting.qson.JSONArray;
import tech.qting.qson.JSONObject;
/**
*
* 用于图表数据补零
*
* @author 沧月
*/
public class ZeroSupplement {
private final RecordSet<Record> data;
private final ZeroMapping mapping;
private List<String> list = new ArrayList<>();
private final List<String> listType = new ArrayList<>();
/**
*
* @param array 需要构建补零的数据
* @param mapping 映射关系
*/
public ZeroSupplement(RecordSet<Record> array, ZeroMapping mapping) {
this.data = array;
this.mapping = mapping;
}
/**
* 默认支持时间区间值设置
*
* @param startTime 开始时间 yyyy-MM-dd
* @param endTime 结束时间 yyyy-MM-dd
*/
public void sectionDate(String startTime, String endTime) {
list.add(startTime);
if (Objects.equals(startTime, endTime)) {
return;
}
String _t = startTime;
for (;;) {
_t = Time.getAfterDay(_t);
if (Objects.equals(_t, endTime)) {
list.add(_t);
break;
} else {
list.add(_t);
}
}
}
/**
* 自定义区间值
*
* @param list 自定义区间值列表
* @return
*/
public ZeroSupplement sectionList(List<String> list) {
this.list = list;
return this;
}
/**
* 自定义区间值
*
* @param list 自定义区间值列表
* @return
*/
public ZeroSupplement sectionList(Set<String> list) {
list.forEach(_a -> {
this.list.add(_a);
});
return this;
}
/**
* 创建数据模板
*/
private Map<String, Object> createTemplate() {
Map<String, Object> object = new LinkedHashMap<>();
list.forEach(_t -> {
object.put(_t, 0);
});
return object;
}
/**
* 设置数据分类
*
* @param type 类型名称 ps:在多类型数据结构中如果不设置类型,无法完成 data 为空的补零策略
* @return
*/
public ZeroSupplement addType(String type) {
this.listType.add(type);
return this;
}
/**
* 没有类型方式数据构建
*/
private JSONObject noTypeBuild() {
Map<String, Object> dataTemplate = this.createTemplate();
data.forEach(action -> {
dataTemplate.put(action.getString(this.mapping.getKey()), action.getInteger(this.mapping.getValue()));
});
JSONArray value = new JSONArray(new ArrayList());
dataTemplate.forEach((k, v) -> {
value.fluentAdd(v);
});
return new JSONObject().fluentPut("data", value).fluentPut("xAxis", list);
}
/**
* 有类型方式构建
*/
private JSONObject TypeBuild() {
Map<String, Map<String, Object>> _tList = new LinkedHashMap<>();
this.listType.forEach(type -> {
_tList.put(type, this.createTemplate());
});
data.forEach(action -> {
Map<String, Object> _tep = _tList.get(action.getString(this.mapping.getType()));
if (_tep == null) {
_tep = this.createTemplate();
_tList.put(action.getString(this.mapping.getType()), _tep);
}
_tep.put(action.getString(this.mapping.getKey()), action.getInteger(this.mapping.getValue()));
});
Set<String> type = _tList.keySet();
JSONArray array = new JSONArray();
type.forEach(key -> {
JSONArray value = new JSONArray();
_tList.get(key).forEach((k, v) -> {
value.fluentAdd(v);
});
array.fluentAdd(new JSONObject().fluentPut("value", value).fluentPut("name", key));
});
return new JSONObject().fluentPut("data", array).fluentPut("xAxis", this.list).fluentPut("legend", type);
}
/**
* 构建数据
*
* @return
*/
public JSONObject build() {
String type = mapping.getType();
if (type == null) {
return this.noTypeBuild();
}
return this.TypeBuild();
}
}
映射类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.faster.buzz;
/**
*
* @author 沧月
*/
public class ZeroMapping {
private String type;
private String key;
private String value;
public ZeroMapping(String type, String key, String value) {
this.type = type;
this.key = key;
this.value = value;
}
public ZeroMapping(String key, String value) {
this(null, key, value);
}
public String getType() {
return type;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
使用Demo:
public static void main(String[] args) {
RecordSet<Record> jsonArray = new RecordSet<>();
jsonArray.fluentAdd(new Record().fluentPut("type", "男").fluentPut("month", "1 月").fluentPut("value", 20));
ZeroSupplement supplement = new ZeroSupplement(jsonArray, new ZeroMapping("type", "month", "value"));
supplement.addType("男")
.addType("女")
.sectionList(new JSONArray<>().fluentAdd("1 月").fluentAdd("2 月").fluentAdd("3 月").fluentAdd("4 月").fluentAdd("5 月").fluentAdd("6 月").fluentAdd("7 月"));
JSONObject data = supplement.build();
System.out.println(data);
}
输出数据:
{"xAxis":["1 月","2 月","3 月","4 月","5 月","6 月","7 月"],"data":[{"name":"男","value":[20,0,0,0,0,0,0]},{"name":"女","value":[0,0,0,0,0,0,0]}],"legend":["男","女"]}