forked from fanruan/demo-chart-pie
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.
202 lines
7.0 KiB
202 lines
7.0 KiB
package com.fr.plugin.demo; |
|
|
|
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.transform.FunctionRecorder; |
|
import com.fr.stable.xml.XMLPrintWriter; |
|
import com.fr.stable.xml.XMLableReader; |
|
|
|
import java.util.List; |
|
import java.awt.Image; |
|
import java.awt.image.BufferedImage; |
|
|
|
/** |
|
* @author Bjorn |
|
* @version 10.0 |
|
* Created by Bjorn on 2019-09-06 |
|
*/ |
|
@FunctionRecorder |
|
public class DemoChart extends BaseChartWithData { |
|
|
|
private static final String ID = "DEMO_CHART"; |
|
|
|
private BaseFormula titleFormula = FormulaKit.newFormula(StringKit.EMPTY); |
|
|
|
private PieType pieType = PieType.PIE; |
|
|
|
private String legendPosition = "left"; |
|
|
|
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; |
|
} |
|
|
|
@Override |
|
protected Image designImage(int width, int height, int resolution, ChartWebParaProvider chartWebPara) { |
|
switch (pieType) { |
|
case PIE: |
|
return IOKit.readImageWithCache("com/fr/plugin/demo/pieType.png"); |
|
default: |
|
return IOKit.readImageWithCache("com/fr/plugin/demo/ringType.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 DemoChart clone() throws CloneNotSupportedException { |
|
DemoChart result = (DemoChart) super.clone(); |
|
if (getTitleFormula() != null) { |
|
result.setTitleFormula(this.getTitleFormula().clone()); |
|
} |
|
result.setPieType(this.getPieType()); |
|
result.setLegendPosition(this.getLegendPosition()); |
|
return result; |
|
} |
|
|
|
@Override |
|
public int hashCode() { |
|
return super.hashCode() + AssistKit.hashCode(this.getTitleFormula(), this.getPieType(), this.getLegendPosition()); |
|
} |
|
|
|
@Override |
|
public boolean equals(Object ob) { |
|
return super.equals(ob) |
|
&& ob instanceof DemoChart |
|
&& AssistKit.equals(this.getTitleFormula(), ((DemoChart) ob).getTitleFormula()) |
|
&& AssistKit.equals(this.getPieType(), ((DemoChart) ob).getPieType()) |
|
&& AssistKit.equals(this.getLegendPosition(), ((DemoChart) ob).getLegendPosition()); |
|
} |
|
|
|
@Override |
|
public JSONObject createAttributeConfig(ChartWebParaProvider chartWebPara) { |
|
JSONObject jsonObject = super.createAttributeConfig(chartWebPara); |
|
|
|
jsonObject.put("title", JSONFactory.createJSON(JSON.OBJECT).put("text", getTitleFormula().getResult()).put("x", "center")); |
|
jsonObject.put("tooltip", JSONFactory.createJSON(JSON.OBJECT).put("trigger", "item").put("formatter", "{b}: {c} ({d}%)")); |
|
|
|
DemoColumnFieldCollection columnFieldCollection = getFieldCollection(DemoColumnFieldCollection.class); |
|
List<Object> category = columnFieldCollection.getCategory().getValues(); |
|
List<Object> values = columnFieldCollection.getValue().getValues(); |
|
JSONArray legendData = JSONFactory.createJSON(JSON.ARRAY); |
|
JSONArray seriesData = JSONFactory.createJSON(JSON.ARRAY); |
|
for (int i = 0; i < category.size(); i++) { |
|
legendData.put(category.get(i)); |
|
seriesData.put(JSONFactory.createJSON(JSON.OBJECT).put("name", category.get(i)).put("value", values.get(i))); |
|
} |
|
jsonObject.put("legend", JSONFactory.createJSON(JSON.OBJECT).put("orient", "vertical").put("left", getLegendPosition()).put("data", legendData)); |
|
JSONObject series = JSONFactory.createJSON(JSON.OBJECT); |
|
series.put("type", "pie"); |
|
switch (pieType) { |
|
case PIE: |
|
series.put("radius", "55%"); |
|
break; |
|
default: |
|
series.put("radius", JSONFactory.createJSON(JSON.ARRAY).put("35%").put("55%")); |
|
break; |
|
} |
|
series.put("data", seriesData); |
|
jsonObject.put("series", series); |
|
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", "新建图表标题"))); |
|
this.setLegendPosition(xmLableReader.getAttrAsString("legendPosition", StringKit.EMPTY)); |
|
} |
|
|
|
@Override |
|
public void writeAttr(XMLPrintWriter xmlPrintWriter) { |
|
super.writeAttr(xmlPrintWriter); |
|
xmlPrintWriter.attr("pieType", pieType.ordinal()) |
|
.attr("title", titleFormula.toString()) |
|
.attr("legendPosition", legendPosition); |
|
} |
|
|
|
private static final HyperLinkPara Category = new HyperLinkPara() { |
|
@Override |
|
public String getName() { |
|
return "分类"; |
|
} |
|
|
|
@Override |
|
public String getFormulaContent() { |
|
return "Category"; |
|
} |
|
|
|
@Override |
|
public String[] getProps() { |
|
return new String[]{"data", "name"}; |
|
} |
|
}; |
|
|
|
@Override |
|
protected HyperLinkPara[] hyperLinkParas() { |
|
return new HyperLinkPara[]{ |
|
Category, |
|
}; |
|
} |
|
}
|
|
|