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.

1155 lines
46 KiB

package com.fr.plugin.third.party.jsdibjj;
import com.fanruan.api.cal.FormulaKit;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.report.chart.BaseChartWithData;
import com.fanruan.api.script.FineCanvas;
import com.fanruan.api.util.AssistKit;
import com.fanruan.api.util.IOKit;
import com.fanruan.api.util.StringKit;
import com.fr.base.BaseFormula;
import com.fr.base.chart.cross.FormulaProcessor;
import com.fr.chart.ChartWebParaProvider;
import com.fr.extended.chart.HyperLinkPara;
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.color.ColorUtils;
import com.fr.plugin.third.party.jsdibjj.data.CustomGanttColumnFieldCollection;
import com.fr.stable.serialize.SerializationUtils;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLableReader;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CustomGanttChart extends BaseChartWithData {
private static final String ID = "CUSTOM_GANTT_CHART_JSD8199";
private BaseFormula titleFormula = FormulaKit.newFormula(StringKit.EMPTY);
private int titleTextStyleColor = Color.BLACK.getRGB();
private String titleTextStyleFontStyle = "normal";
private String titleTextStyleFontWeight = "normal";
private String titleTextStyleFontFamily = "sans-serif";
private int titleTextStyleFontSize = 18;
private String titleLeft = "center";
private int seriesNameTextStyleColor = Color.BLACK.getRGB();
private String seriesNameTextStyleFontFamily = "sans-serif";
private int seriesNameTextStyleFontSize = 18;
private int yAxisAxisLabelColor = Color.BLACK.getRGB();
private String yAxisAxisLabelFontStyle = "normal";
private String yAxisAxisLabelFontWeight = "normal";
private String yAxisAxisLabelFontFamily = "sans-serif";
private int yAxisAxisLabelFontSize = 12;
private long minTimestampValue = 0;
private long maxTimestampValue = 0;
private long currentTimestampValue = 0;
private boolean currentTimeLineVisible = false;
private PieType pieType = PieType.PIE;
private String legendPosition = "left";
private List<Integer> colors = new ArrayList<>();
private boolean fillGapOption = false;
private int fillGapColor = Color.RED.getRGB();
private boolean currentTimeLineOption = true;
private int currentTimeLineColor = Color.RED.getRGB();
private int currentTimeLineWidth = 2;
private int displayScale = 100;
private int displayXScale = 100;
private boolean animationOption = false;
private String symbolType = "rect";
private int symbolWidth = 2;
private int emphasisBorderColor = Color.RED.getRGB();
private int emphasisBorderWidth = 2;
private boolean emphasisOption = false;
/**
* 按项目循环颜色
*/
private boolean projectCycleColor = false;
private boolean dataSort = false;
private int oddBackgroundColor = Color.WHITE.getRGB();
private int evenBackgroundColor = Color.WHITE.getRGB();
public int getOddBackgroundColor() {
return oddBackgroundColor;
}
public void setOddBackgroundColor(int oddBackgroundColor) {
this.oddBackgroundColor = oddBackgroundColor;
}
public int getEvenBackgroundColor() {
return evenBackgroundColor;
}
public void setEvenBackgroundColor(int evenBackgroundColor) {
this.evenBackgroundColor = evenBackgroundColor;
}
public int getSeriesNameTextStyleColor() {
return seriesNameTextStyleColor;
}
public void setSeriesNameTextStyleColor(int seriesNameTextStyleColor) {
this.seriesNameTextStyleColor = seriesNameTextStyleColor;
}
public String getSeriesNameTextStyleFontFamily() {
return seriesNameTextStyleFontFamily;
}
public void setSeriesNameTextStyleFontFamily(String seriesNameTextStyleFontFamily) {
this.seriesNameTextStyleFontFamily = seriesNameTextStyleFontFamily;
}
public int getSeriesNameTextStyleFontSize() {
return seriesNameTextStyleFontSize;
}
public void setSeriesNameTextStyleFontSize(int seriesNameTextStyleFontSize) {
this.seriesNameTextStyleFontSize = seriesNameTextStyleFontSize;
}
public boolean isDataSort() {
return dataSort;
}
public void setDataSort(boolean dataSort) {
this.dataSort = dataSort;
}
public boolean isProjectCycleColor() {
return projectCycleColor;
}
public void setProjectCycleColor(boolean projectCycleColor) {
this.projectCycleColor = projectCycleColor;
}
public int getyAxisAxisLabelColor() {
return yAxisAxisLabelColor;
}
public void setyAxisAxisLabelColor(int yAxisAxisLabelColor) {
this.yAxisAxisLabelColor = yAxisAxisLabelColor;
}
public String getyAxisAxisLabelFontStyle() {
return yAxisAxisLabelFontStyle;
}
public void setyAxisAxisLabelFontStyle(String yAxisAxisLabelFontStyle) {
this.yAxisAxisLabelFontStyle = yAxisAxisLabelFontStyle;
}
public String getyAxisAxisLabelFontWeight() {
return yAxisAxisLabelFontWeight;
}
public void setyAxisAxisLabelFontWeight(String yAxisAxisLabelFontWeight) {
this.yAxisAxisLabelFontWeight = yAxisAxisLabelFontWeight;
}
public String getyAxisAxisLabelFontFamily() {
return yAxisAxisLabelFontFamily;
}
public void setyAxisAxisLabelFontFamily(String yAxisAxisLabelFontFamily) {
this.yAxisAxisLabelFontFamily = yAxisAxisLabelFontFamily;
}
public int getyAxisAxisLabelFontSize() {
return yAxisAxisLabelFontSize;
}
public void setyAxisAxisLabelFontSize(int yAxisAxisLabelFontSize) {
this.yAxisAxisLabelFontSize = yAxisAxisLabelFontSize;
}
public int getTitleTextStyleColor() {
return titleTextStyleColor;
}
public void setTitleTextStyleColor(int titleTextStyleColor) {
this.titleTextStyleColor = titleTextStyleColor;
}
public String getTitleTextStyleFontStyle() {
return titleTextStyleFontStyle;
}
public void setTitleTextStyleFontStyle(String titleTextStyleFontStyle) {
this.titleTextStyleFontStyle = titleTextStyleFontStyle;
}
public String getTitleTextStyleFontWeight() {
return titleTextStyleFontWeight;
}
public void setTitleTextStyleFontWeight(String titleTextStyleFontWeight) {
this.titleTextStyleFontWeight = titleTextStyleFontWeight;
}
public String getTitleTextStyleFontFamily() {
return titleTextStyleFontFamily;
}
public void setTitleTextStyleFontFamily(String titleTextStyleFontFamily) {
this.titleTextStyleFontFamily = titleTextStyleFontFamily;
}
public int getTitleTextStyleFontSize() {
return titleTextStyleFontSize;
}
public void setTitleTextStyleFontSize(int titleTextStyleFontSize) {
this.titleTextStyleFontSize = titleTextStyleFontSize;
}
public String getTitleLeft() {
return titleLeft;
}
public void setTitleLeft(String titleLeft) {
this.titleLeft = titleLeft;
}
public String getSymbolType() {
return symbolType;
}
public void setSymbolType(String symbolType) {
this.symbolType = symbolType;
}
public int getSymbolWidth() {
return symbolWidth;
}
public void setSymbolWidth(int symbolWidth) {
this.symbolWidth = symbolWidth;
}
public boolean isAnimationOption() {
return animationOption;
}
public void setAnimationOption(boolean animationOption) {
this.animationOption = false;
}
public int getEmphasisBorderColor() {
return emphasisBorderColor;
}
public void setEmphasisBorderColor(int emphasisBorderColor) {
this.emphasisBorderColor = emphasisBorderColor;
}
public int getEmphasisBorderWidth() {
return emphasisBorderWidth;
}
public void setEmphasisBorderWidth(int emphasisBorderWidth) {
this.emphasisBorderWidth = emphasisBorderWidth;
}
public boolean isEmphasisOption() {
return emphasisOption;
}
public void setEmphasisOption(boolean emphasisOption) {
this.emphasisOption = emphasisOption;
}
public int getDisplayScale() {
return displayScale;
}
public void setDisplayScale(int scale) {
if (scale <= 0) {
scale = 0;
}
if (scale >= 100) {
scale = 100;
}
this.displayScale = scale;
}
public int getDisplayXScale() {
return displayXScale;
}
public void setDisplayXScale(int scale) {
if (scale <= 0) {
scale = 0;
}
if (scale >= 100) {
scale = 100;
}
this.displayXScale = scale;
}
public boolean isFillGapOption() {
return fillGapOption;
}
public void setFillGapOption(boolean fillGapOption) {
this.fillGapOption = false;
}
public int getFillGapColor() {
return fillGapColor;
}
public void setFillGapColor(int fillGapColor) {
this.fillGapColor = fillGapColor;
}
public boolean isCurrentTimeLineOption() {
return currentTimeLineOption;
}
public void setCurrentTimeLineOption(boolean currentTimeLineOption) {
this.currentTimeLineOption = currentTimeLineOption;
}
public int getCurrentTimeLineColor() {
return currentTimeLineColor;
}
public void setCurrentTimeLineColor(int currentTimeLineColor) {
this.currentTimeLineColor = currentTimeLineColor;
}
public int getCurrentTimeLineWidth() {
return currentTimeLineWidth;
}
public void setCurrentTimeLineWidth(int currentTimeLineWidth) {
this.currentTimeLineWidth = currentTimeLineWidth;
}
public List<Integer> getColors() {
List<Integer> tempColors = new ArrayList<>();
tempColors.add(getColorRGB(25, 79, 151));
tempColors.add(getColorRGB(85, 85, 85));
tempColors.add(getColorRGB(189, 107, 8));
tempColors.add(getColorRGB(0, 104, 107));
tempColors.add(getColorRGB(200, 45, 49));
tempColors.add(getColorRGB(98, 91, 161));
tempColors.add(getColorRGB(137, 137, 137));
tempColors.add(getColorRGB(156, 152, 0));
tempColors.add(getColorRGB(0, 127, 84));
tempColors.add(getColorRGB(161, 149, 197));
tempColors.add(getColorRGB(16, 54, 103));
tempColors.add(getColorRGB(241, 146, 114));
tempColors.add(getColorRGB(193, 140, 0));
tempColors.add(getColorRGB(54, 54, 54));
tempColors.add(getColorRGB(66, 111, 179));
tempColors.add(getColorRGB(102, 191, 127));
tempColors.add(getColorRGB(249, 205, 118));
tempColors.add(getColorRGB(57, 40, 132));
tempColors.add(getColorRGB(0, 174, 113));
tempColors.add(getColorRGB(147, 170, 214));
tempColors.add(getColorRGB(73, 7, 97));
tempColors.add(getColorRGB(250, 241, 75));
tempColors.add(getColorRGB(149, 83, 5));
tempColors.add(getColorRGB(1, 98, 65));
tempColors.add(getColorRGB(115, 136, 193));
tempColors.add(getColorRGB(143, 30, 32));
tempColors.add(getColorRGB(249, 205, 118));
tempColors.add(getColorRGB(214, 155, 1));
tempColors.add(getColorRGB(183, 183, 183));
tempColors.add(getColorRGB(81, 31, 144));
tempColors.add(getColorRGB(153, 208, 185));
tempColors.add(getColorRGB(199, 195, 0));
return tempColors;
}
public void setColors(List<Integer> colors) {
this.colors = colors;
}
public PieType getPieType() {
return pieType;
}
public void setPieType(PieType pieType) {
this.pieType = pieType;
}
public BaseFormula getTitleFormula() {
return titleFormula;
}
public void setTitleFormula(BaseFormula titleFormula) {
this.titleFormula = titleFormula;
}
public String getLegendPosition() {
return legendPosition;
}
public void setLegendPosition(String legendPosition) {
this.legendPosition = legendPosition;
}
private int getColorRGB(int r, int g, int b) {
Color color = new Color(r, g, b);
return color.getRGB();
}
@Override
protected Image designImage(int width, int height, int resolution, ChartWebParaProvider chartWebPara) {
return IOKit.readImageWithCache("com/fr/plugin/third/party/jsdibjj/images/gantt_type.png");
}
@Override
protected Image exportImage(int width, int height, int resolution, ChartWebParaProvider chartWebPara) {
BufferedImage bufferedImage = new BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB);
try {
FineCanvas canvas = new FineCanvas("/com/fr/plugin/demo/echarts-adapter.js", "/com/fr/plugin/demo/echarts.js");
canvas.loadText("canvas.height = " + height, "canvas.width = " + width);
canvas.loadText("var myChart = echarts.init(canvas)");
canvas.loadText("option = " + createAttributeConfig(chartWebPara).toString());
canvas.loadText("myChart.setOption(option);");
bufferedImage = canvas.paint();
} catch (Exception ex) {
LogKit.error(ex.getMessage(), ex);
}
return bufferedImage;
}
@Override
public CustomGanttChart clone() throws CloneNotSupportedException {
CustomGanttChart result = (CustomGanttChart) super.clone();
if (getTitleFormula() != null) {
result.setTitleFormula(this.getTitleFormula().clone());
}
result.setTitleTextStyleColor(this.getTitleTextStyleColor());
result.setTitleTextStyleFontStyle(this.getTitleTextStyleFontStyle());
result.setTitleTextStyleFontWeight(this.getTitleTextStyleFontWeight());
result.setTitleTextStyleFontFamily(this.getTitleTextStyleFontFamily());
result.setTitleTextStyleFontSize(this.getTitleTextStyleFontSize());
result.setTitleLeft(this.getTitleLeft());
result.setyAxisAxisLabelColor(this.getyAxisAxisLabelColor());
result.setyAxisAxisLabelFontStyle(this.getyAxisAxisLabelFontStyle());
result.setyAxisAxisLabelFontWeight(this.getyAxisAxisLabelFontWeight());
result.setyAxisAxisLabelFontFamily(this.getyAxisAxisLabelFontFamily());
result.setyAxisAxisLabelFontSize(this.getyAxisAxisLabelFontSize());
result.setPieType(this.getPieType());
result.setLegendPosition(this.getLegendPosition());
result.setColors(this.getColors());
result.setFillGapOption(this.isFillGapOption());
result.setFillGapColor(this.getFillGapColor());
result.setCurrentTimeLineOption(this.isCurrentTimeLineOption());
result.setCurrentTimeLineColor(this.getCurrentTimeLineColor());
result.setCurrentTimeLineWidth(this.getCurrentTimeLineWidth());
result.setDisplayScale(this.getDisplayScale());
result.setDisplayXScale(this.getDisplayXScale());
result.setAnimationOption(this.isAnimationOption());
result.setSymbolType(this.getSymbolType());
result.setSymbolWidth(this.getSymbolWidth());
result.setEmphasisBorderColor(this.getEmphasisBorderColor());
result.setEmphasisBorderWidth(this.getEmphasisBorderWidth());
result.setEmphasisOption(this.isEmphasisOption());
result.setProjectCycleColor(this.isProjectCycleColor());
result.setDataSort(this.isDataSort());
result.setSeriesNameTextStyleFontFamily(this.getSeriesNameTextStyleFontFamily());
result.setSeriesNameTextStyleFontSize(this.getSeriesNameTextStyleFontSize());
result.setSeriesNameTextStyleColor(this.getSeriesNameTextStyleColor());
return result;
}
@Override
public int hashCode() {
return super.hashCode() + AssistKit.hashCode(this.getTitleFormula(), this.getPieType(), this.getLegendPosition(),
this.getTitleTextStyleColor(), this.getTitleTextStyleFontStyle(), this.getTitleTextStyleFontWeight(),
this.getTitleTextStyleFontFamily(), this.getTitleTextStyleFontSize(), this.getTitleLeft(),
this.getyAxisAxisLabelColor(), this.getyAxisAxisLabelFontStyle(), this.getyAxisAxisLabelFontWeight(), this.getyAxisAxisLabelFontFamily(), this.getyAxisAxisLabelFontSize(),
this.getColors(),
this.isFillGapOption(), this.getFillGapColor(),
this.isCurrentTimeLineOption(), this.getCurrentTimeLineColor(), this.getCurrentTimeLineWidth(),
this.getDisplayScale(), this.getDisplayXScale(),
this.isAnimationOption(), this.getSymbolType(), this.getSymbolWidth(),
this.getSeriesNameTextStyleFontFamily(), this.getSeriesNameTextStyleFontSize(), this.getSeriesNameTextStyleColor(),
this.getEmphasisBorderColor(), this.getEmphasisBorderWidth(), this.isEmphasisOption(), this.isProjectCycleColor(), this.isDataSort());
}
@Override
public boolean equals(Object ob) {
return super.equals(ob)
&& ob instanceof CustomGanttChart
&& AssistKit.equals(this.getTitleFormula(), ((CustomGanttChart) ob).getTitleFormula())
&& AssistKit.equals(this.getTitleTextStyleColor(), ((CustomGanttChart) ob).getTitleTextStyleColor())
&& AssistKit.equals(this.getTitleTextStyleFontStyle(), ((CustomGanttChart) ob).getTitleTextStyleFontStyle())
&& AssistKit.equals(this.getTitleTextStyleFontWeight(), ((CustomGanttChart) ob).getTitleTextStyleFontWeight())
&& AssistKit.equals(this.getTitleTextStyleFontFamily(), ((CustomGanttChart) ob).getTitleTextStyleFontFamily())
&& AssistKit.equals(this.getTitleTextStyleFontSize(), ((CustomGanttChart) ob).getTitleTextStyleFontSize())
&& AssistKit.equals(this.getTitleLeft(), ((CustomGanttChart) ob).getTitleLeft())
&& AssistKit.equals(this.getyAxisAxisLabelColor(), ((CustomGanttChart) ob).getyAxisAxisLabelColor())
&& AssistKit.equals(this.getyAxisAxisLabelFontStyle(), ((CustomGanttChart) ob).getyAxisAxisLabelFontStyle())
&& AssistKit.equals(this.getyAxisAxisLabelFontWeight(), ((CustomGanttChart) ob).getyAxisAxisLabelFontWeight())
&& AssistKit.equals(this.getyAxisAxisLabelFontFamily(), ((CustomGanttChart) ob).getyAxisAxisLabelFontFamily())
&& AssistKit.equals(this.getyAxisAxisLabelFontSize(), ((CustomGanttChart) ob).getyAxisAxisLabelFontSize())
&& AssistKit.equals(this.getPieType(), ((CustomGanttChart) ob).getPieType())
&& AssistKit.equals(this.getLegendPosition(), ((CustomGanttChart) ob).getLegendPosition())
&& AssistKit.equals(this.isFillGapOption(), ((CustomGanttChart) ob).isFillGapOption())
&& AssistKit.equals(this.getFillGapColor(), ((CustomGanttChart) ob).getFillGapColor())
&& AssistKit.equals(this.isCurrentTimeLineOption(), ((CustomGanttChart) ob).isCurrentTimeLineOption())
&& AssistKit.equals(this.getCurrentTimeLineColor(), ((CustomGanttChart) ob).getCurrentTimeLineColor())
&& AssistKit.equals(this.getCurrentTimeLineWidth(), ((CustomGanttChart) ob).getCurrentTimeLineWidth())
&& AssistKit.equals(this.getDisplayScale(), ((CustomGanttChart) ob).getDisplayScale())
&& AssistKit.equals(this.getDisplayXScale(), ((CustomGanttChart) ob).getDisplayXScale())
&& AssistKit.equals(this.isAnimationOption(), ((CustomGanttChart) ob).isAnimationOption())
&& AssistKit.equals(this.getSymbolType(), ((CustomGanttChart) ob).getSymbolType())
&& AssistKit.equals(this.getSymbolWidth(), ((CustomGanttChart) ob).getSymbolWidth())
&& AssistKit.equals(this.getEmphasisBorderColor(), ((CustomGanttChart) ob).getEmphasisBorderColor())
&& AssistKit.equals(this.getEmphasisBorderWidth(), ((CustomGanttChart) ob).getEmphasisBorderWidth())
&& AssistKit.equals(this.isEmphasisOption(), ((CustomGanttChart) ob).isEmphasisOption())
&& AssistKit.equals(this.isProjectCycleColor(), ((CustomGanttChart) ob).isProjectCycleColor())
&& AssistKit.equals(this.isDataSort(), ((CustomGanttChart) ob).isDataSort())
&& AssistKit.equals(this.getSeriesNameTextStyleFontFamily(), ((CustomGanttChart) ob).getSeriesNameTextStyleFontFamily())
&& AssistKit.equals(this.getSeriesNameTextStyleFontSize(), ((CustomGanttChart) ob).getSeriesNameTextStyleFontSize())
&& AssistKit.equals(this.getSeriesNameTextStyleColor(), ((CustomGanttChart) ob).getSeriesNameTextStyleColor());
}
@Override
public JSONObject createAttributeConfig(ChartWebParaProvider chartWebPara) {
JSONObject jsonObject = super.createAttributeConfig(chartWebPara);
CustomGanttColumnFieldCollection columnFieldCollection = getFieldCollection(CustomGanttColumnFieldCollection.class);
jsonObject.put("dataStatus", false);
List<String> projectNames = CustomGanttDataFactory.createProjectNames(columnFieldCollection);
if (projectNames.size() <= 0) {
return jsonObject;
}
jsonObject.put("dataStatus", true);
minTimestampValue = CustomGanttDataFactory.createMinTimestamp(columnFieldCollection);
maxTimestampValue = CustomGanttDataFactory.createMaxTimestamp(columnFieldCollection);
currentTimestampValue = System.currentTimeMillis();
currentTimeLineVisible = false;
if ((currentTimestampValue >= minTimestampValue) && (currentTimestampValue <= maxTimestampValue)) {
currentTimeLineVisible = true;
}
JSONObject currentDateObject = JSONFactory.createJSON(JSON.OBJECT);
currentDateObject.put("visible", currentTimeLineVisible);
currentDateObject.put("value", currentTimestampValue);
JSONObject dateLineObject = JSONFactory.createJSON(JSON.OBJECT);
dateLineObject.put("enable", isCurrentTimeLineOption());
dateLineObject.put("color", CustomGanttDataFactory.rgbToHex(new Color(getCurrentTimeLineColor())));
dateLineObject.put("width", getCurrentTimeLineWidth());
dateLineObject.put("currentDate", currentTimestampValue);
dateLineObject.put("currentDateShow", CustomGanttDataFactory.timestampToDate(currentTimestampValue));
List<CustomGanttData> datas = CustomGanttDataFactory.createData(columnFieldCollection);
List<CustomGanttData> suspendedDatas = new ArrayList<>();
createSeriesDataColor(datas, projectNames, suspendedDatas);
CustomGanttData tempCustomGanttData;
int flag = 0;
JSONArray categoriesData = JSONFactory.createJSON(JSON.ARRAY);
JSONArray suspendedValuesData = JSONFactory.createJSON(JSON.ARRAY);
String category = "";
String suspendedFirstContent = "";
for (int i = 0, max = projectNames.size() - 1; i <= max; i++) {
category = projectNames.get(i);
categoriesData.put(category);
}
//从大到小排序,针对日期有重叠的,日期小的在上面,最后绘制图表
//改成日期大的在上面
Collections.sort(datas, new Comparator<CustomGanttData>() {
@Override
public int compare(CustomGanttData o1, CustomGanttData o2) {
long diff = o1.beginDate - o2.beginDate;
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
return 0;
}
});
//添加奇偶背景颜色
if (isDataSort()) {
long minBeginDate = 0;
long maxEndDate = 0;
if (datas.size() >= 1) {
maxEndDate = datas.get(0).endDate;
minBeginDate = datas.get(datas.size() - 1).beginDate;
}
for (int i = 0, max = projectNames.size() - 1; i <= max; i++) {
tempCustomGanttData = new CustomGanttData();
tempCustomGanttData.index = i;
tempCustomGanttData.beginDate = minBeginDate;
tempCustomGanttData.endDate = maxEndDate;
tempCustomGanttData.showType = CustomGanttData.SHOW_TYPE_BACKGROUND;
if ((i % 2) == 0) {
tempCustomGanttData.completedColor = getOddBackgroundColor();
} else {
tempCustomGanttData.completedColor = getEvenBackgroundColor();
}
tempCustomGanttData.completedColorHex = CustomGanttDataFactory.rgbToHex(new Color(tempCustomGanttData.completedColor));
datas.add(0, tempCustomGanttData);
}
}
List<CustomGanttData> milestoneDatas = CustomGanttDataFactory.createMilestoneDatas(columnFieldCollection);
datas.addAll(milestoneDatas);
JSONArray seriesData = CustomGanttDataFactory.createJsonData(columnFieldCollection, datas, this);
JSONObject ganttDataJsonObject = JSONFactory.createJSON(JSON.OBJECT);
ganttDataJsonObject.put("categories", categoriesData);
ganttDataJsonObject.put("seriesData", seriesData);
ganttDataJsonObject.put("animationOption", isAnimationOption());
ganttDataJsonObject.put("symbolType", getSymbolType());
ganttDataJsonObject.put("symbolWidth", getSymbolWidth());
ganttDataJsonObject.put("minDate", CustomGanttDataFactory.createMinTimestamp(columnFieldCollection));
ganttDataJsonObject.put("maxDate", CustomGanttDataFactory.createMaxTimestamp(columnFieldCollection));
ganttDataJsonObject.put("currentDate", currentDateObject);
ganttDataJsonObject.put("dateLine", dateLineObject);
ganttDataJsonObject.put("displayScale", (100 - getDisplayScale()));
ganttDataJsonObject.put("displayXScale", getDisplayXScale());
JSONArray linesData = createLinesData(datas);
ganttDataJsonObject.put("linesData", linesData);
jsonObject.put("ganttData", ganttDataJsonObject);
JSONObject titleJson = new JSONObject();
String ganttTitle = (String) getTitleFormula().getResult();
if (StringKit.isEmpty(ganttTitle)) {
ganttTitle = "";
}
titleJson.put("titleText", ganttTitle);
titleJson.put("titleTextStyleColor", CustomGanttDataFactory.rgbToHex(new Color(this.titleTextStyleColor)));
titleJson.put("titleTextStyleFontStyle", titleTextStyleFontStyle);
titleJson.put("titleTextStyleFontWeight", titleTextStyleFontWeight);
titleJson.put("titleTextStyleFontFamily", titleTextStyleFontFamily);
titleJson.put("titleTextStyleFontSize", titleTextStyleFontSize);
titleJson.put("titleLeft", titleLeft);
jsonObject.put("ganttTitle", titleJson);
JSONObject seriesNameJson = new JSONObject();
seriesNameJson.put("seriesNameTextStyleFontFamily", seriesNameTextStyleFontFamily);
seriesNameJson.put("seriesNameTextStyleFontSize", seriesNameTextStyleFontSize);
seriesNameJson.put("seriesNameTextStyleColor", CustomGanttDataFactory.rgbToHex(new Color(this.seriesNameTextStyleColor)));
jsonObject.put("seriesNameFont", seriesNameJson);
JSONObject yAxisAxisLabelFontJson = new JSONObject();
yAxisAxisLabelFontJson.put("yAxisAxisLabelColor", CustomGanttDataFactory.rgbToHex(new Color(this.yAxisAxisLabelColor)));
yAxisAxisLabelFontJson.put("yAxisAxisLabelFontStyle", yAxisAxisLabelFontStyle);
yAxisAxisLabelFontJson.put("yAxisAxisLabelFontWeight", yAxisAxisLabelFontWeight);
yAxisAxisLabelFontJson.put("yAxisAxisLabelFontFamily", yAxisAxisLabelFontFamily);
yAxisAxisLabelFontJson.put("yAxisAxisLabelFontSize", yAxisAxisLabelFontSize);
jsonObject.put("yAxisAxisLabelFont", yAxisAxisLabelFontJson);
JSONObject fillGapJson = new JSONObject();
fillGapJson.put("enable", this.fillGapOption);
fillGapJson.put("color", CustomGanttDataFactory.rgbToHex(new Color(this.fillGapColor)));
JSONArray flashingData = JSONFactory.createJSON(JSON.ARRAY);
if (isFillGapOption()) {
flashingData = CustomGanttDataFactory.createFlashingData(datas);
}
fillGapJson.put("flashingData", flashingData);
jsonObject.put("fillGap", fillGapJson);
//jsonObject.put("projectCycleColor", projectCycleColor);
jsonObject.put("projectCycleColor", true);
JSONArray graphDatasConfJson = CustomGanttDataFactory.createMilestoneData(columnFieldCollection);
jsonObject.put("graphDatasConf", graphDatasConfJson);
return jsonObject;
}
@Override
public void dealFormula(FormulaProcessor formulaProcessor) {
if (titleFormula != null) {
formulaProcessor.dealWith(titleFormula);
}
super.dealFormula(formulaProcessor);
}
@Override
public String getID() {
return ID;
}
@Override
public void readAttr(XMLableReader xmLableReader) {
super.readAttr(xmLableReader);
this.setPieType(PieType.parseInt(xmLableReader.getAttrAsInt("pieType", 0)));
this.setTitleFormula(FormulaKit.newFormula(xmLableReader.getAttrAsString("title", "新建图表标题")));
//private int seriesNameTextStyleColor = Color.BLACK.getRGB();
//private String seriesNameTextStyleFontFamily = "sans-serif";
//private int seriesNameTextStyleFontSize = 18;
this.setSeriesNameTextStyleColor(xmLableReader.getAttrAsInt("seriesNameTextStyleColor", Color.BLACK.getRGB()));
this.setSeriesNameTextStyleFontFamily(xmLableReader.getAttrAsString("seriesNameTextStyleFontFamily", "sans-serif"));
this.setSeriesNameTextStyleFontSize(xmLableReader.getAttrAsInt("seriesNameTextStyleFontSize", 18));
this.setTitleTextStyleColor(xmLableReader.getAttrAsInt("titleTextStyleColor", Color.BLACK.getRGB()));
this.setTitleTextStyleFontStyle(xmLableReader.getAttrAsString("titleTextStyleFontStyle", "normal"));
this.setTitleTextStyleFontWeight(xmLableReader.getAttrAsString("titleTextStyleFontWeight", "normal"));
this.setTitleTextStyleFontFamily(xmLableReader.getAttrAsString("titleTextStyleFontFamily", "sans-serif"));
this.setTitleTextStyleFontSize(xmLableReader.getAttrAsInt("titleTextStyleFontSize", 18));
this.setTitleLeft(xmLableReader.getAttrAsString("titleLeft", "center"));
this.setyAxisAxisLabelColor(xmLableReader.getAttrAsInt("yAxisAxisLabelColor", Color.BLACK.getRGB()));
this.setyAxisAxisLabelFontStyle(xmLableReader.getAttrAsString("yAxisAxisLabelFontStyle", "normal"));
this.setyAxisAxisLabelFontWeight(xmLableReader.getAttrAsString("yAxisAxisLabelFontWeight", "normal"));
this.setyAxisAxisLabelFontFamily(xmLableReader.getAttrAsString("yAxisAxisLabelFontFamily", "sans-serif"));
this.setyAxisAxisLabelFontSize(xmLableReader.getAttrAsInt("yAxisAxisLabelFontSize", 12));
this.setLegendPosition(xmLableReader.getAttrAsString("legendPosition", StringKit.EMPTY));
String colorContent = xmLableReader.getAttrAsString("colors", StringKit.EMPTY);
setColors(toColorList(colorContent));
this.setFillGapOption(xmLableReader.getAttrAsBoolean("fillGapOption", false));
this.setFillGapColor(xmLableReader.getAttrAsInt("fillGapColor", Color.RED.getRGB()));
this.setCurrentTimeLineOption(xmLableReader.getAttrAsBoolean("currentTimeLineOption", false));
this.setCurrentTimeLineColor(xmLableReader.getAttrAsInt("currentTimeLineColor", Color.RED.getRGB()));
this.setCurrentTimeLineWidth(xmLableReader.getAttrAsInt("currentTimeLineWidth", 2));
this.setDisplayScale(xmLableReader.getAttrAsInt("displayScale", 100));
this.setDisplayXScale(xmLableReader.getAttrAsInt("displayXScale", 100));
this.setAnimationOption(xmLableReader.getAttrAsBoolean("animationOption", false));
this.setSymbolType(xmLableReader.getAttrAsString("symbolType", "rect"));
this.setSymbolWidth(xmLableReader.getAttrAsInt("symbolWidth", 2));
this.setEmphasisBorderColor(xmLableReader.getAttrAsInt("emphasisBorderColor", Color.RED.getRGB()));
this.setEmphasisBorderWidth(xmLableReader.getAttrAsInt("emphasisBorderWidth", 2));
this.setEmphasisOption(xmLableReader.getAttrAsBoolean("emphasisOption", false));
this.setProjectCycleColor(xmLableReader.getAttrAsBoolean("projectCycleColor", false));
this.setDataSort(xmLableReader.getAttrAsBoolean("dataSort", false));
this.setOddBackgroundColor(xmLableReader.getAttrAsInt("oddBackgroundColor", Color.WHITE.getRGB()));
this.setEvenBackgroundColor(xmLableReader.getAttrAsInt("evenBackgroundColor", Color.WHITE.getRGB()));
}
@Override
public void writeAttr(XMLPrintWriter xmlPrintWriter) {
super.writeAttr(xmlPrintWriter);
String colorContent = getColorContent();
xmlPrintWriter.attr("pieType", pieType.ordinal())
.attr("title", titleFormula.toString())
.attr("titleTextStyleColor", titleTextStyleColor)
.attr("titleTextStyleFontStyle", titleTextStyleFontStyle)
.attr("titleTextStyleFontWeight", titleTextStyleFontWeight)
.attr("titleTextStyleFontFamily", titleTextStyleFontFamily)
.attr("titleTextStyleFontSize", titleTextStyleFontSize)
.attr("titleLeft", titleLeft)
.attr("yAxisAxisLabelColor", yAxisAxisLabelColor)
.attr("yAxisAxisLabelFontStyle", yAxisAxisLabelFontStyle)
.attr("yAxisAxisLabelFontWeight", yAxisAxisLabelFontWeight)
.attr("yAxisAxisLabelFontFamily", yAxisAxisLabelFontFamily)
.attr("yAxisAxisLabelFontSize", yAxisAxisLabelFontSize)
.attr("legendPosition", legendPosition)
.attr("colors", colorContent)
.attr("fillGapOption", fillGapOption)
.attr("fillGapColor", fillGapColor)
.attr("currentTimeLineOption", currentTimeLineOption)
.attr("currentTimeLineColor", currentTimeLineColor)
.attr("currentTimeLineWidth", currentTimeLineWidth)
.attr("displayScale", displayScale)
.attr("displayXScale", displayXScale)
.attr("animationOption", animationOption)
.attr("symbolType", symbolType)
.attr("symbolWidth", symbolWidth)
.attr("emphasisBorderColor", emphasisBorderColor)
.attr("emphasisBorderWidth", emphasisBorderWidth)
.attr("emphasisOption", emphasisOption)
.attr("projectCycleColor", projectCycleColor)
.attr("dataSort", dataSort)
.attr("seriesNameTextStyleColor", seriesNameTextStyleColor)
.attr("seriesNameTextStyleFontFamily", seriesNameTextStyleFontFamily)
.attr("seriesNameTextStyleFontSize", seriesNameTextStyleFontSize)
.attr("oddBackgroundColor", oddBackgroundColor)
.attr("evenBackgroundColor", evenBackgroundColor)
;
}
private JSONArray createLinesData(List<CustomGanttData> ganttDatas) {
JSONArray datas = new JSONArray();
if ((ganttDatas == null) || (ganttDatas.size() <= 0)) {
return datas;
}
/*
{
coords: [
[900, 600],
[1000, 300]
],
lineStyle: {
color: 'red'
}
}*/
CustomGanttData tempGanttData;
JSONObject objJson;
JSONArray coordsJsonArray;
JSONArray coordsData0JsonArray;
JSONArray coordsData1JsonArray;
JSONObject lineStyleJson;
long x0 = 0, x1 = 0;
int y = 0;
for (int i = 0, max = ganttDatas.size() - 1; i <= max; i++) {
tempGanttData = ganttDatas.get(i);
if (CustomGanttData.SHOW_TYPE_FILL.equalsIgnoreCase(tempGanttData.showType)) {
continue;
}
x0 = tempGanttData.beginDate;
x1 = (long) ((tempGanttData.endDate - tempGanttData.beginDate) * tempGanttData.schedule) + tempGanttData.beginDate;
y = tempGanttData.index;
coordsData0JsonArray = new JSONArray();
coordsData0JsonArray.add(x0);
coordsData0JsonArray.add(y);
coordsData1JsonArray = new JSONArray();
coordsData1JsonArray.add(x1);
coordsData1JsonArray.add(y);
coordsJsonArray = new JSONArray();
coordsJsonArray.add(coordsData0JsonArray);
coordsJsonArray.add(coordsData1JsonArray);
lineStyleJson = new JSONObject();
lineStyleJson.put("color", tempGanttData.completedColorHex);
objJson = new JSONObject();
objJson.put("coords", coordsJsonArray);
objJson.put("lineStyle", lineStyleJson);
datas.add(objJson);
}
return datas;
}
private List<Integer> toColorList(String content) {
List<Integer> tempColors = new ArrayList<>();
if (StringKit.isEmpty(content)) {
return tempColors;
}
content = content.trim();
if (StringKit.isEmpty(content)) {
return tempColors;
}
String[] tempArray = content.split(",");
if ((tempArray == null) || (tempArray.length <= 0)) {
return tempColors;
}
String tempValue;
int tempColorValue;
for (int i = 0, max = tempArray.length - 1; i <= max; i++) {
tempValue = tempArray[i];
tempColorValue = Integer.valueOf(tempValue);
tempColors.add(tempColorValue);
}
return tempColors;
}
private String getColorContent() {
if ((this.colors == null) || (this.colors.size() <= 0)) {
return "";
}
String content = "";
for (int i = 0, max = this.colors.size() - 1; i <= max; i++) {
content = content + this.colors.get(i);
if (i < max) {
content = content + ",";
}
}
return content;
}
/**
* 生成进度颜色
*
* @param datas
* @return
*/
private List<CustomGanttData> createSeriesDataColor(List<CustomGanttData> datas, List<String> projectNames, List<CustomGanttData> suspendedDatas) {
if ((datas == null) || (datas.size() <= 0)) {
return datas;
}
if ((projectNames == null) || (projectNames.size() <= 0)) {
return datas;
}
List<List<CustomGanttData>> groups = new ArrayList<>();
for (int i = 0, max = projectNames.size() - 1; i <= max; i++) {
groups.add(new ArrayList<CustomGanttData>());
}
CustomGanttData ganttData;
for (int i = 0, max = datas.size() - 1; i <= max; i++) {
ganttData = datas.get(i);
groups.get(ganttData.index).add(ganttData);
}
List<Color> colors = new ArrayList<>();
List<Color> grayColors = new ArrayList<>();
List<Integer> colorValues = getColors();
Color color, grayColor;
for (int i = 0, max = colorValues.size() - 1; i <= max; i++) {
color = new Color(colorValues.get(i));
colors.add(color);
grayColor = ColorUtils.toDark(color);
grayColors.add(grayColor);
}
if (colors.size() <= 0) {
color = Color.blue;
colors.add(color);
grayColor = ColorUtils.toDark(color);
grayColors.add(grayColor);
}
// Collections.reverse(colors);
int colorSize = colors.size();
List<CustomGanttData> ganttDatas;
int tempValue = 0, tempIndex = 0;
long tempPrevEndDate = 0;
List<CustomGanttData> tempDatas = new ArrayList<>();
List<CustomGanttData> tempGapDatas = new ArrayList<>();
List<CustomGanttData> tempSeriesDatas = new ArrayList<>();
CustomGanttData tempGanttData;
List<Color> tempColors = new ArrayList<>();
Color tempColor;
CustomGanttData tempSeriesGanttData;
CustomGanttData tempSeriesGanttData1;
long diffMinValue = 24 * 60 * 60 * 1000L, diffValue = 0;
for (int i = 0, max = groups.size() - 1; i <= max; i++) {
ganttDatas = groups.get(i);
Collections.sort(ganttDatas, new Comparator<CustomGanttData>() {
@Override
public int compare(CustomGanttData o1, CustomGanttData o2) {
long diff = o1.beginDate - o2.beginDate;
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
return 0;
}
});
tempValue = max - i;
tempIndex = tempValue % colorSize;
color = colors.get(tempIndex);
if (isProjectCycleColor()) {
tempColors = ColorUtils.getCycleColors(colors, ganttDatas.size());
} else {
tempColors = ColorUtils.getGradientColors(color, ganttDatas.size());
}
int fillGapColorValue = this.fillGapColor;
String fillGapColorHexValue = CustomGanttDataFactory.rgbToHex(new Color(this.fillGapColor));
tempPrevEndDate = 0;
tempSeriesDatas.clear();
for (int j = 0, jMax = ganttDatas.size() - 1; j <= jMax; j++) {
tempColor = tempColors.get(j);
ganttData = ganttDatas.get(j);
ganttData.completedColor = tempColor.getRGB();
ganttData.completedColorHex = CustomGanttDataFactory.rgbToHex(tempColor);
ganttData.days = getDiffDays(ganttData);
tempSeriesGanttData = (CustomGanttData) SerializationUtils.clone(ganttData);
if (j == 0) {
tempSeriesDatas.add(tempSeriesGanttData);
} else {
tempSeriesGanttData1 = tempSeriesDatas.get(tempSeriesDatas.size() - 1);
if ((tempSeriesGanttData1.beginDate <= tempSeriesGanttData.beginDate) && (tempSeriesGanttData1.endDate >= tempSeriesGanttData.beginDate)) {
if (tempSeriesGanttData1.endDate < tempSeriesGanttData.endDate) {
tempSeriesGanttData1.endDate = tempSeriesGanttData.endDate;
}
} else {
tempSeriesDatas.add(tempSeriesGanttData);
}
}
}
for (int j = 0, jMax = tempSeriesDatas.size() - 1; j <= jMax; j++) {
ganttData = tempSeriesDatas.get(j);
diffValue = ganttData.beginDate - tempPrevEndDate;
if ((isFillGapOption()) && (j >= 1) && (tempPrevEndDate > 0) && (tempPrevEndDate < ganttData.beginDate) && (diffValue > diffMinValue)) {
tempGanttData = new CustomGanttData();
tempGanttData.index = ganttData.index;
tempGanttData.showType = CustomGanttData.SHOW_TYPE_FILL;
tempGanttData.beginDate = tempPrevEndDate;
tempGanttData.endDate = ganttData.beginDate;
tempGanttData.completedColor = fillGapColorValue;
tempGanttData.completedColorHex = fillGapColorHexValue;
tempGanttData.days = getDiffDays(tempGanttData);
tempGanttData.middleValue = (tempGanttData.endDate + tempGanttData.beginDate) / 2;
tempGanttData.beginDateShow = CustomGanttDataFactory.timestampToDate(tempGanttData.beginDate);
tempGanttData.endDateShow = CustomGanttDataFactory.timestampToDate(tempGanttData.endDate);
tempDatas.add(tempGanttData);
}
if ((isFillGapOption()) && currentTimeLineVisible && (j == 0) && (currentTimestampValue < ganttData.beginDate) && ((ganttData.beginDate - currentTimestampValue) > diffMinValue)) {
tempGanttData = new CustomGanttData();
tempGanttData.index = ganttData.index;
tempGanttData.showType = CustomGanttData.SHOW_TYPE_FILL;
tempGanttData.beginDate = currentTimestampValue;
tempGanttData.endDate = ganttData.beginDate;
tempGanttData.completedColor = fillGapColorValue;
tempGanttData.completedColorHex = fillGapColorHexValue;
tempGanttData.days = getDiffDays(tempGanttData);
tempGanttData.middleValue = (tempGanttData.endDate + tempGanttData.beginDate) / 2;
tempGanttData.beginDateShow = CustomGanttDataFactory.timestampToDate(tempGanttData.beginDate);
tempGanttData.endDateShow = CustomGanttDataFactory.timestampToDate(tempGanttData.endDate);
tempDatas.add(tempGanttData);
}
tempPrevEndDate = ganttData.endDate;
}
}
suspendedDatas.addAll(tempDatas);
datas.addAll(tempDatas);
return datas;
}
private int getDiffDays(CustomGanttData ganttData) {
if (ganttData == null) {
return 0;
}
long diffMinValue = 24 * 60 * 60 * 1000L;
int days = (int) ((ganttData.endDate - ganttData.beginDate) / diffMinValue + 1);
return days;
}
private static final HyperLinkPara categoryHyperLinkPara = new HyperLinkPara() {
@Override
public String getName() {
return "分类";
}
@Override
public String getFormulaContent() {
return "category";
}
@Override
public String[] getProps() {
return new String[]{"data", "category"};
}
};
private static final HyperLinkPara seriesHyperLinkPara = new HyperLinkPara() {
@Override
public String getName() {
return "系列名";
}
@Override
public String getFormulaContent() {
return "series";
}
@Override
public String[] getProps() {
return new String[]{"data", "originalName"};
}
};
private static final HyperLinkPara seriesValueHyperLinkPara = new HyperLinkPara() {
@Override
public String getName() {
return "系列名值";
}
@Override
public String getFormulaContent() {
return "seriesValue";
}
@Override
public String[] getProps() {
return new String[]{"data", "seriesValue"};
}
};
@Override
protected HyperLinkPara[] hyperLinkParas() {
return new HyperLinkPara[]{
categoryHyperLinkPara,
seriesHyperLinkPara,
seriesValueHyperLinkPara
};
}
}