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.
 
 

181 lines
7.5 KiB

package com.fr.plugin.pielinecomb.comp;
import com.fr.base.DeprecatedChartWebUtils;
import com.fr.base.GraphHelper;
import com.fr.base.chart.chartdata.CallbackEvent;
import com.fr.base.chart.chartdata.ChartData;
import com.fr.base.chart.result.WebChartType;
import com.fr.chart.ChartWebPara;
import com.fr.chart.chartattr.ChartsPlot;
import com.fr.chart.chartdata.TopDefinition;
import com.fr.chart.chartglyph.ChartGlyph;
import com.fr.extended.chart.AbstractChart;
import com.fr.extended.chart.AbstractDataConfig;
import com.fr.extended.chart.ToHyperlinkJSONHelper;
import com.fr.general.ComparatorUtils;
import com.fr.general.IOUtils;
import com.fr.js.NameJavaScriptGroup;
import com.fr.json.JSON;
import com.fr.json.JSONException;
import com.fr.json.JSONFactory;
import com.fr.json.JSONObject;
import com.fr.plugin.chart.ToJSONHelper;
import com.fr.plugin.chart.vanchart.VanChartGlyph;
import com.fr.script.Calculator;
import com.fr.stable.CoreGraphHelper;
import com.fr.stable.web.Repository;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
/**
* @author
* @date 2022/11/2
*/
public abstract class AbstractCustomChart<T extends AbstractDataConfig> extends AbstractChart {
private static transient Map<String, BufferedImage> demoImages = new HashMap();
public AbstractCustomChart() {
ChartsPlot plot = new ChartsPlot();
plot.setPlotID(this.getChartID());
this.setPlot(plot);
}
protected BufferedImage getDemoImage() {
if (demoImages.get(this.getChartID()) == null) {
BufferedImage var1 = IOUtils.readImage(this.demoImagePath());
if (var1 == null) {
var1 = CoreGraphHelper.createBufferedImage(100, 100, 6);
}
demoImages.put(this.getChartID(), var1);
}
return (BufferedImage) demoImages.get(this.getChartID());
}
protected BufferedImage getChartImage(Calculator cal, int width, int height) {
if (demoImages.get(this.getChartID()) == null) {
BufferedImage var1 = IOUtils.readImage(this.demoImagePath());
if (var1 == null) {
var1 = CoreGraphHelper.createBufferedImage(100, 100, 6);
}
demoImages.put(this.getChartID(), var1);
}
return (BufferedImage) demoImages.get(this.getChartID());
}
@Override
protected void addJSON(AbstractDataConfig abstractDataConfig, JSONObject jsonObject, Repository repository, AbstractChart.JSONPara jsonPara) throws JSONException {
AbstractCustomChart<T>.JSONPara jsonParaCustom = new AbstractCustomChart<T>.JSONPara(jsonPara.chartWebPara, jsonPara.ecName, jsonPara.sheetIndex);
this.addJSON((T) abstractDataConfig, jsonObject, repository, jsonParaCustom);
}
protected abstract void addJSON(T config, JSONObject json, Repository repo, AbstractCustomChart<T>.JSONPara para) throws JSONException;
@Override
public ChartGlyph createGlyph(ChartData data) {
VanChartGlyph chartGlyph = new VanChartGlyph() {
String ecName;
int sheetIndex;
@Override
public Map toJSONMap(String[] deps, double width, double height, Repository repo, int sheetIdx, String ecName) {
this.ecName = ecName;
this.sheetIndex = sheetIdx;
return super.toJSONMap(deps, width, height, repo, sheetIndex, ecName);
}
@Override
public JSONObject toJSONObject(Repository repo, double width, double height) throws JSONException {
return AbstractCustomChart.this.toJSON(repo, false, AbstractCustomChart.this.new JSONPara(this.getChartWebPara(), this.ecName, this.sheetIndex));
}
@Override
public void draw(Graphics g, int p) {
if (this.getBounds() != null) {
Rectangle2D rect = this.getBounds();
double width = rect.getWidth();
double height = rect.getHeight();
GraphHelper.paintImage(g, (int) width, (int) height, AbstractCustomChart.this.getDemoImage(), 4, -1, -1, -1, -1);
}
}
@Override
public Image toImage(int width, int height, int p, CallbackEvent callback) {
if (this.getChartWebPara().getChartIDInfo().getType() != WebChartType.DESIGNER_CHART) {
return AbstractCustomChart.this.getChartImage((Calculator) this.getChartWebPara().getCalculator(), width, height);
} else {
BufferedImage bi = CoreGraphHelper.createBufferedImage(width, height, 6);
Graphics g = bi.getGraphics();
GraphHelper.paintImage(g, width, height, AbstractCustomChart.this.getDemoImage(), 4, -1, -1, -1, -1);
return bi;
}
}
@Override
public void recordWebPreview() {
AbstractCustomChart.this.recordPreview();
}
};
chartGlyph.setWrapperName(this.wrapperName());
chartGlyph.setRequiredJS(this.requiredJS());
chartGlyph.setRequiredCSS(this.requiredCSS());
return chartGlyph;
}
private JSONObject toJSON(Repository repo, boolean isDes, AbstractCustomChart<T>.JSONPara para) throws JSONException {
JSONObject paramJson = (JSONObject) JSONFactory.createJSON(JSON.OBJECT);
this.checkLicense(paramJson);
ToHyperlinkJSONHelper.addJSON(repo, paramJson, this.getLinkGroup(), this.hyperLinkParas());
paramJson.put("refreshTime", this.isRefreshEnabled() ? this.getAutoRefreshTime() * 1000.0D : 0.0D);
AbstractDataConfig dataConfig = isDes && this.designerDataConfig() != null ? this.designerDataConfig() : this.getDataConfig();
ToJSONHelper.addEmptyDataTip(paramJson, para.chartWebPara);
ToJSONHelper.addStringLocale(repo, paramJson);
this.addJSON((T) dataConfig, paramJson, repo, para);
return paramJson;
}
@Override
public Object clone() throws CloneNotSupportedException {
AbstractCustomChart chart = (AbstractCustomChart) super.clone();
chart.setAutoRefreshTime(this.getAutoRefreshTime());
chart.setRefreshEnabled(this.isRefreshEnabled());
if (this.getLinkGroup() != null) {
chart.setLinkGroup((NameJavaScriptGroup) this.getLinkGroup().clone());
}
if (this.getFilterDefinition() != null) {
chart.setFilterDefinition((TopDefinition) this.getFilterDefinition().clone());
}
return chart;
}
@Override
public boolean equals(Object src) {
return super.equals(src) && src instanceof AbstractCustomChart && ComparatorUtils.equals(this.getChartID(), ((AbstractCustomChart) src).getChartID()) && ComparatorUtils.equals(this.getLinkGroup(), ((AbstractCustomChart) src).getLinkGroup()) && ComparatorUtils.equals(this.isRefreshEnabled(), ((AbstractCustomChart) src).isRefreshEnabled()) && ComparatorUtils.equals(this.getAutoRefreshTime(), ((AbstractCustomChart) src).getAutoRefreshTime());
}
protected class JSONPara {
public ChartWebPara chartWebPara;
@Deprecated
public String webChartID;
public String ecName;
public int sheetIndex;
JSONPara(ChartWebPara webPara, String ecName, int sheetIndex) {
this.chartWebPara = webPara;
this.webChartID = DeprecatedChartWebUtils.createPainterID(webPara.getChartIDInfo());
this.ecName = ecName;
this.sheetIndex = sheetIndex;
}
}
}