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.
148 lines
5.7 KiB
148 lines
5.7 KiB
2 years ago
|
package com.fr.plugin.pielinecomb.comp;
|
||
|
|
||
|
import com.fr.general.IOUtils;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.os.OperatingSystem;
|
||
|
import com.fr.third.org.apache.commons.io.FileUtils;
|
||
|
|
||
|
import java.io.BufferedReader;
|
||
|
import java.io.ByteArrayInputStream;
|
||
|
import java.io.File;
|
||
|
import java.io.InputStreamReader;
|
||
|
|
||
|
/**
|
||
|
* @author duan.jingliang
|
||
|
* @date 2022/9/28
|
||
|
*/
|
||
|
public class WebPageRender {
|
||
|
private static String CHART_PATH = "";
|
||
|
private static WebPageRender webPage = new WebPageRender();
|
||
|
|
||
|
private WebPageRender() {
|
||
|
}
|
||
|
public static WebPageRender getInstance() {
|
||
|
return webPage;
|
||
|
}
|
||
|
|
||
|
public String makeChartImage(JSONObject options, int width, int height) {
|
||
|
Process process = null;
|
||
|
try {
|
||
|
prepareChartDir(options);
|
||
|
|
||
|
String cmd = "";
|
||
|
if (OperatingSystem.isWindows()) {
|
||
|
cmd += "cmd /c cd " + CHART_PATH + " && phantomjs ";
|
||
|
} else {
|
||
|
cmd += "cd " + CHART_PATH + " && phantomjs ";
|
||
|
}
|
||
|
cmd += "export.js -chartname PieLineCombChartWrapper -width " + width + " -height " + height;
|
||
|
|
||
|
process = Runtime.getRuntime().exec(cmd);
|
||
|
/**
|
||
|
* 获取控制台输出信息
|
||
|
* 通过JS中使用console.log()打印输出base64编码
|
||
|
* 获取进程输入流,进行base64编码获取
|
||
|
*/
|
||
|
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||
|
|
||
|
String line = "";
|
||
|
StringBuffer base64Img = new StringBuffer();
|
||
|
while ((line = input.readLine()) != null) {
|
||
|
base64Img.append(line);
|
||
|
}
|
||
|
|
||
|
String imgStr = base64Img.toString();
|
||
|
if (imgStr.indexOf(",") > 0) {
|
||
|
imgStr = imgStr.substring(imgStr.indexOf(",") + 1);
|
||
|
}
|
||
|
|
||
|
return imgStr;
|
||
|
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
} finally {
|
||
|
if (null != process) {
|
||
|
process.destroy();
|
||
|
}
|
||
|
}
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
private void prepareChartDir(JSONObject options) {
|
||
|
try {
|
||
|
options.put("isLoadAnimal", false);
|
||
|
String optionStr = options.toString();
|
||
|
optionStr = "window.chartOptions = " + optionStr;
|
||
|
|
||
|
if (StringUtils.isEmpty(CHART_PATH)) {
|
||
|
String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
|
||
|
path = java.net.URLDecoder.decode(path, "UTF-8");
|
||
|
System.out.println("path: " + path);
|
||
|
File pluginDir = new File(path);
|
||
|
CHART_PATH = pluginDir.getParent() + File.separator + "chartexport";
|
||
|
}
|
||
|
|
||
|
File chartPath = new File(CHART_PATH);
|
||
|
chartPath.mkdirs();
|
||
|
|
||
|
File delFile = new File(CHART_PATH + File.separator + "chartdata.js");
|
||
|
if (delFile.exists()) {
|
||
|
delFile.delete();
|
||
|
}
|
||
|
|
||
|
/*if (OperatingSystem.isWindows()) {
|
||
|
if (!new File(CHART_PATH + File.separator + "phantomjs.exe").exists()) {
|
||
|
IOUtils.copy(this.getClass().getResourceAsStream("/com/fr/plugin/boxplot/web/phantomjs.exe"), "phantomjs.exe", chartPath);
|
||
|
}
|
||
|
} else if (OperatingSystem.isMacos()) {
|
||
|
if (!new File(CHART_PATH + File.separator + "phantomjs").exists()) {
|
||
|
IOUtils.copy(this.getClass().getResourceAsStream("/com/fr/plugin/boxplot/web/phantomjs-mac"), "phantomjs", chartPath);
|
||
|
}
|
||
|
} else {
|
||
|
if (!new File(CHART_PATH + File.separator + "phantomjs").exists()) {
|
||
|
IOUtils.copy(this.getClass().getResourceAsStream("/com/fr/plugin/boxplot/web/phantomjs-linux"), "phantomjs", chartPath);
|
||
|
}
|
||
|
}*/
|
||
|
|
||
|
if (!new File(CHART_PATH + File.separator + "fr.js").exists()) {
|
||
|
IOUtils.copy(this.getClass().getResourceAsStream("/com/fr/plugin/pielinecomb/web/fr.js"), "fr.js", chartPath);
|
||
|
}
|
||
|
if (!new File(CHART_PATH + File.separator + "jquery.js").exists()) {
|
||
|
IOUtils.copy(this.getClass().getResourceAsStream("/com/fr/plugin/pielinecomb/web/jquery.js"), "jquery.js", chartPath);
|
||
|
}
|
||
|
if (!new File(CHART_PATH + File.separator + "echarts.js").exists()) {
|
||
|
IOUtils.copy(this.getClass().getResourceAsStream("/com/fr/plugin/pielinecomb/web/echarts.min.js"), "echarts.js", chartPath);
|
||
|
}
|
||
|
IOUtils.copy(this.getClass().getResourceAsStream("/com/fr/plugin/pielinecomb/web/export.js"), "export.js", chartPath);
|
||
|
IOUtils.copy(this.getClass().getResourceAsStream("/com/fr/plugin/pielinecomb/web/PieLineCombChartWrapper.js"), "chartwrapper.js", chartPath);
|
||
|
IOUtils.copy(new ByteArrayInputStream(optionStr.getBytes("UTF-8")), "chartdata.js", chartPath);
|
||
|
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public boolean isDataChanged(JSONObject options) {
|
||
|
try {
|
||
|
if (StringUtils.isEmpty(CHART_PATH)) {
|
||
|
return true;
|
||
|
}
|
||
|
String optionStr = options.toString();
|
||
|
optionStr = "window.chartOptions = " + optionStr;
|
||
|
|
||
|
File dataFile = new File(CHART_PATH + File.separator + "chartdata.js");
|
||
|
if (dataFile.exists()) {
|
||
|
String oldData = FileUtils.readFileToString(dataFile, "UTF-8");
|
||
|
if (StringUtils.isNotEmpty(oldData) && oldData.equals(optionStr)) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
}
|