JSD-8199 甘特图扩展
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

510 lines
18 KiB

package com.fr.plugin.third.party.jsdibjj;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.StringKit;
import com.fr.general.GeneralUtils;
import com.fr.json.JSON;
import com.fr.json.JSONArray;
import com.fr.json.JSONFactory;
import com.fr.json.JSONObject;
import com.fr.plugin.third.party.jsdibjj.data.CustomGanttColumnFieldCollection;
import java.awt.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
public class CustomGanttDataFactory {
public static synchronized List<String> createProjectNames(CustomGanttColumnFieldCollection dataSource) {
List<String> projectList = new ArrayList<>();
if (dataSource == null) {
return projectList;
}
List<Object> projectNames = dataSource.getProjectName().getValues();
int size = projectNames.size();
if (size <= 0) {
return projectList;
}
String tempValue;
Object tempObj;
for (int i = 0, max = size - 1; i <= max; i++) {
tempObj = projectNames.get(i);
if (tempObj == null) {
tempValue = "";
addNotRepeatingObj(tempValue, projectList);
continue;
}
tempValue = GeneralUtils.objectToString(tempObj);
addNotRepeatingObj(tempValue, projectList);
}
Collections.reverse(projectList);
return projectList;
}
public static synchronized JSONArray createJsonData(CustomGanttColumnFieldCollection dataSource, CustomGanttChart ganttChartConf) {
List<CustomGanttData> datas = createData(dataSource);
return createJsonData(dataSource, datas, ganttChartConf);
}
public static synchronized JSONArray createFlashingData(List<CustomGanttData> datas) {
JSONArray jsonContent = JSONFactory.createJSON(JSON.ARRAY);
if ((datas == null) || (datas.size() <= 0)) {
return jsonContent;
}
JSONArray jsonArray;
CustomGanttData ganttData;
for (int i = 0, max = datas.size() - 1; i <= max; i++) {
ganttData = datas.get(i);
if (!StringKit.equals(ganttData.showType, CustomGanttData.SHOW_TYPE_FILL)) {
continue;
}
jsonArray = JSONFactory.createJSON(JSON.ARRAY);
jsonArray.put(ganttData.index);
jsonArray.put(ganttData.middleValue);
JSONObject valueJson = JSONFactory.createJSON(JSON.OBJECT);
valueJson.put("value", jsonArray);
jsonContent.put(valueJson);
}
return jsonContent;
}
public static synchronized JSONArray createJsonData(CustomGanttColumnFieldCollection dataSource, List<CustomGanttData> datas, CustomGanttChart ganttChartConf) {
JSONArray jsonContent = JSONFactory.createJSON(JSON.ARRAY);
if ((datas == null) || (datas.size() <= 0)) {
return jsonContent;
}
JSONObject jsonObject;
JSONArray jsonArray;
CustomGanttData ganttData;
JSONObject emphasisJson = JSONFactory.createJSON(JSON.OBJECT);
if (ganttChartConf.isEmphasisOption()) {
JSONObject itemStyleJson = JSONFactory.createJSON(JSON.OBJECT);
itemStyleJson.put("borderColor", rgbToHex(new Color(ganttChartConf.getEmphasisBorderColor())));
itemStyleJson.put("borderWidth", ganttChartConf.getEmphasisBorderWidth());
itemStyleJson.put("borderType", "solid");
//itemStyleJson.put("borderWidth",2);
emphasisJson.put("itemStyle", itemStyleJson);
}
/*
JSONObject itemStyleJson = JSONFactory.createJSON(JSON.OBJECT).put("normal", JSONFactory.createJSON(JSON.OBJECT).put("color", "#7b9ce1"));
JSONObject emphasisJson = JSONFactory.createJSON(JSON.OBJECT);
emphasisJson.put("borderColor", "red");
emphasisJson.put("borderWidth", 20);
emphasisJson.put("borderType", "solid");
itemStyleJson.put("","")*/
for (int i = 0, max = datas.size() - 1; i <= max; i++) {
ganttData = datas.get(i);
jsonObject = JSONFactory.createJSON(JSON.OBJECT);
jsonObject.put("category", ganttData.category);
jsonObject.put("name", ganttData.showName);
jsonObject.put("originalName", ganttData.originalName);
jsonObject.put("showType", ganttData.showType);
jsonObject.put("days", ganttData.days);
jsonObject.put("beginDateShow", ganttData.beginDateShow);
jsonObject.put("endDateShow", ganttData.endDateShow);
jsonObject.put("middleValue", ganttData.middleValue);
jsonObject.put("seriesValue", ganttData.seriesValue);
jsonArray = JSONFactory.createJSON(JSON.ARRAY);
jsonArray.put(ganttData.index);
jsonArray.put(ganttData.beginDate);
jsonArray.put(ganttData.endDate);
jsonArray.put(ganttData.showName);
jsonArray.put(ganttData.milestoneDate);
jsonArray.put(ganttData.schedule);
jsonArray.put(ganttData.completedColorHex);
jsonArray.put(ganttData.incompleteColorHex);
jsonArray.put(ganttData.scheduleShow);
jsonArray.put(ganttData.showType);
jsonArray.put(ganttData.middleValue);
jsonArray.put(ganttData.seriesValue);
jsonObject.put("value", jsonArray);
jsonObject.put("itemStyle", JSONFactory.createJSON(JSON.OBJECT).put("normal", JSONFactory.createJSON(JSON.OBJECT).put("color", "#7b9ce1")));
jsonObject.put("emphasis", emphasisJson);
jsonContent.put(jsonObject);
}
return jsonContent;
}
public static synchronized long createMinTimestamp(CustomGanttColumnFieldCollection dataSource) {
List<Object> beginDates = dataSource.getBeginDate().getValues();
int size = beginDates.size();
if (size <= 0) {
return 0;
}
Calendar cal = Calendar.getInstance();
cal.set(2999, 1, 1);
long minTimestamp = cal.getTimeInMillis();
;
long tempTimestamp = 0;
int count = 0;
for (int i = 0, max = size - 1; i <= max; i++) {
tempTimestamp = objectToTimestamp(beginDates.get(i));
if (minTimestamp > tempTimestamp) {
minTimestamp = tempTimestamp;
count++;
}
}
if (count <= 0) {
return 0;
}
return minTimestamp;
}
public static synchronized long createMaxTimestamp(CustomGanttColumnFieldCollection dataSource) {
List<Object> endDates = dataSource.getEndDate().getValues();
int size = endDates.size();
if (size <= 0) {
return 0;
}
Calendar cal = Calendar.getInstance();
cal.set(1000, 1, 1);
long maxTimestamp = cal.getTimeInMillis();
long tempTimestamp = 0;
int count = 0;
for (int i = 0, max = size - 1; i <= max; i++) {
tempTimestamp = objectToTimestamp(endDates.get(i));
if (maxTimestamp < tempTimestamp) {
maxTimestamp = tempTimestamp;
count++;
}
}
if (count <= 0) {
return 0;
}
return maxTimestamp;
}
public static synchronized List<CustomGanttData> createData(CustomGanttColumnFieldCollection dataSource) {
List<CustomGanttData> datas = new ArrayList<>();
List<String> projectList = createProjectNames(dataSource);
if (projectList.size() <= 0) {
return datas;
}
List<Object> projectNames = dataSource.getProjectName().getValues();
List<Object> seriesNames = dataSource.getSeriesName().getValues();
List<Object> seriesValues = dataSource.getSeriesValue().getValues();
List<Object> beginDates = dataSource.getBeginDate().getValues();
List<Object> endDates = dataSource.getEndDate().getValues();
List<Object> schedules = dataSource.getSchedule().getValues();
int size = projectNames.size();
if (size <= 0) {
return datas;
}
String projectName, seriesName;
CustomGanttData ganttData;
for (int i = 0, max = size - 1; i <= max; i++) {
projectName = objectToString(projectNames.get(i));
seriesName = objectToString(seriesNames.get(i));
ganttData = new CustomGanttData();
ganttData.category = projectName;
ganttData.index = getIndex(projectName, projectList);
ganttData.showName = createSeriesName(seriesName);
ganttData.originalName = seriesName;
ganttData.schedule = objectToRate(schedules.get(i));
ganttData.scheduleShow = objectToRateShow(ganttData.schedule);
ganttData.beginDate = objectToTimestamp(beginDates.get(i));
ganttData.endDate = objectToTimestamp(endDates.get(i));
ganttData.beginDateShow = String.valueOf(beginDates.get(i));
ganttData.endDateShow = String.valueOf(endDates.get(i));
ganttData.seriesValue = String.valueOf(seriesValues.get(i));
if ((ganttData.beginDate <= 0) || (ganttData.endDate <= 0)) {
LogKit.error("甘特图定制,解析开始时间或结束时间出错");
return new ArrayList<CustomGanttData>();
}
datas.add(ganttData);
}
return datas;
}
public static synchronized List<CustomGanttData> createMilestoneDatas(CustomGanttColumnFieldCollection dataSource) {
List<CustomGanttData> datas = new ArrayList<>();
List<String> projectList = createProjectNames(dataSource);
if (projectList.size() <= 0) {
return datas;
}
List<Object> projectNames = dataSource.getProjectName().getValues();
List<Object> seriesNames = dataSource.getSeriesName().getValues();
List<Object> seriesValues = dataSource.getSeriesValue().getValues();
List<Object> beginDates = dataSource.getBeginDate().getValues();
List<Object> endDates = dataSource.getEndDate().getValues();
List<Object> schedules = dataSource.getSchedule().getValues();
List<Object> milestoneDates = dataSource.getMilestoneDate().getValues();
List<Object> milestoneIcons = dataSource.getMilestoneIcon().getValues();
List<Object> milestoneInfos = dataSource.getMilestoneInfo().getValues();
int size = projectNames.size();
if (size <= 0) {
return datas;
}
if (milestoneDates.size() <= 0) {
return datas;
}
if (milestoneIcons.size() <= 0) {
return datas;
}
String projectName, seriesName;
CustomGanttData ganttData;
String path;
for (int i = 0, max = size - 1; i <= max; i++) {
projectName = objectToString(projectNames.get(i));
seriesName = objectToString(seriesNames.get(i));
ganttData = new CustomGanttData();
ganttData.category = projectName;
ganttData.index = getIndex(projectName, projectList);
ganttData.showName = createSeriesName(seriesName);
ganttData.originalName = seriesName;
ganttData.schedule = 0;
ganttData.scheduleShow = getIconPath(i, milestoneInfos);
ganttData.beginDate = objectToTimestamp(milestoneDates.get(i));
ganttData.endDate = objectToTimestamp(milestoneDates.get(i));
ganttData.beginDateShow = String.valueOf(milestoneDates.get(i));;
ganttData.endDateShow = "";
path = getIconPath(i, milestoneIcons);
if (StringKit.isEmpty(path)) {
continue;
}
ganttData.seriesValue = path;
if ((ganttData.beginDate <= 0) || (ganttData.endDate <= 0)) {
continue;
}
ganttData.showType = CustomGanttData.SHOW_TYPE_IMAGE;
datas.add(ganttData);
}
return datas;
}
public static synchronized JSONArray createMilestoneData(CustomGanttColumnFieldCollection dataSource) {
JSONArray dataArray = new JSONArray();
List<String> projectList = createProjectNames(dataSource);
if (projectList.size() <= 0) {
return dataArray;
}
List<Object> projectNames = dataSource.getProjectName().getValues();
List<Object> milestoneDates = dataSource.getMilestoneDate().getValues();
List<Object> milestoneIcons = dataSource.getMilestoneIcon().getValues();
int size = projectNames.size();
if (size <= 0) {
return dataArray;
}
if (milestoneDates.size() <= 0) {
return dataArray;
}
String projectName;
int x;
long y;
String type = "";
String path = "";
JSONObject milestoneJson;
for (int i = 0, max = size - 1; i <= max; i++) {
projectName = objectToString(projectNames.get(i));
x = getIndex(projectName, projectList);
y = objectToTimestamp(milestoneDates.get(i));
if (y <= 0) {
continue;
}
type = "diamond";
path = getIconPath(i, milestoneIcons);
if (StringKit.isNotEmpty(path)) {
type = "image";
}
milestoneJson = new JSONObject();
milestoneJson.put("x", x);
milestoneJson.put("y", y);
milestoneJson.put("type", type);
milestoneJson.put("path", path);
dataArray.add(milestoneJson);
}
return dataArray;
}
private static String getIconPath(int index, List<Object> icons) {
if (index < 0) {
return "";
}
if ((icons == null) || (icons.size() <= 0) || (index >= icons.size())) {
return "";
}
String path = String.valueOf(icons.get(index));
path = StringKit.trim(path);
if (StringKit.isEmpty(path)) {
return "";
}
return path;
}
/**
* 针对echarts的bug处理,若系列名称有数字,有中文,会默认按数字处理,有中文会显示NaN,故在前面加中文处理
*
* @param name
* @return
*/
private static String createSeriesName(String name) {
String tempName = name;
if (StringKit.isEmpty(name)) {
tempName = "";
}
return "甘特图" + tempName;
}
private static synchronized String objectToString(Object obj) {
String tempValue = GeneralUtils.objectToString(obj);
return tempValue;
}
private static ThreadLocal<SimpleDateFormat> threadLocalFormat = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
private static ThreadLocal<SimpleDateFormat> threadLocalFormat1 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public static synchronized String timestampToDate(long value) {
Date date = new Date(value);
return threadLocalFormat.get().format(date);
}
/**
* 转为时间戳
*
* @param obj
* @return
*/
private static synchronized long objectToTimestamp(Object obj) {
if (obj == null) {
return 0;
}
Date tempDate = null;
if (obj instanceof Date) {
tempDate = (Date) obj;
return tempDate.getTime();
}
String tempValue = GeneralUtils.objectToString(obj);
if (StringKit.isEmpty(tempValue)) {
return 0;
}
int length = tempValue.length();
try {
if (length == 10) {
tempDate = threadLocalFormat.get().parse(tempValue);
} else if (length == 19) {
tempDate = threadLocalFormat1.get().parse(tempValue);
}
} catch (Exception e) {
return 0;
}
if (tempDate != null) {
return tempDate.getTime();
}
return 0;
}
/**
* 转为0-1的四位小数
*
* @param obj
* @return
*/
private static synchronized double objectToRate(Object obj) {
if (obj == null) {
return 0;
}
double tempValue = 0;
if (obj instanceof Double) {
tempValue = (Double) obj;
} else {
String tempContent = objectToString(obj);
if (StringKit.isEmpty(tempContent)) {
return 0;
}
tempContent = tempContent.trim();
if (StringKit.isEmpty(tempContent)) {
return 0;
}
tempValue = Double.valueOf(tempContent);
}
if (tempValue <= 0) {
return 0;
}
if (tempValue >= 1) {
return 1;
}
BigDecimal bigDecimal = new BigDecimal(String.valueOf(tempValue));
tempValue = bigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
return tempValue;
}
private static synchronized String objectToRateShow(double value) {
BigDecimal bigDecimal = new BigDecimal(String.valueOf(value));
BigDecimal bigDecimal1 = new BigDecimal("100");
String tempValue = bigDecimal.multiply(bigDecimal1).doubleValue() + "%";
return tempValue;
}
private static synchronized void addNotRepeatingObj(String value, List<String> values) {
String tempValue = value;
if (StringKit.isEmpty(tempValue)) {
tempValue = "";
}
if (values.contains(tempValue)) {
return;
}
values.add(tempValue);
}
private static synchronized int getIndex(String value, List<String> values) {
int index = values.indexOf(value);
return index;
}
public static String rgbToHex(Color color) {
if (color == null) {
return "#000000";
}
String value = "#" + String.format("%02X", color.getRed()) + String.format("%02X", color.getGreen()) + String.format("%02X", color.getBlue());
return value;
}
public static Color colorToGray(Color color) {
if (color == null) {
return new Color(0);
}
int gray = (int) ((color.getRed() * 30 + color.getGreen() * 59 + color.getBlue() * 11 + 50) * 0.01);
return new Color(gray);
}
}