java echarts 生成图片_Java后台批量生产echarts图表并保存图片

一个围绕统计分析功能的系统,在最后制作统计分析时需要一个批量点击的功能,用以批量制作echarts图形后生成图片并保存图形和图片。方便后续导出。

public class EchartsUtils {

private static final String JSpath = "C:\\echarts-convert\\echarts-convert1.js";

public static void main(String[] args) {

String imgName = "D:/平台/tes" + UUID.randomUUID().toString().substring(0, 4) + ".png ";

String option = "{xAxis: {type: 'category',data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']},yAxis: {type: 'value'},series: [{data: [820, 932, 901, 934, 1290, 1330, 1320],type: 'line'}]}";

//String options = "test";

String base64Img = generateEChart(option,1600,900);

System.out.println(base64Img);

}

public static String generateEChart(String options,int width,int height) {

String fileName= "test-"+UUID.randomUUID().toString().substring(0, 8) + ".png";

String imgPath = "D:/平台/img/" +fileName;

String dataPath = writeFile(options);//数据json

try {

File file = new File(imgPath); //文件路径(路径+文件名)

if (!file.exists()) { //文件不存在则创建文件,先创建目录

File dir = new File(file.getParent());

dir.mkdirs();

file.createNewFile();

}

String cmd = "phantomjs " + JSpath + " -infile " + dataPath + " -outfile " + imgPath + " -width " + width + " -height " + height;

System.out.println(cmd);

Process process = Runtime.getRuntime().exec(cmd);

BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line = "";

while ((line = input.readLine()) != null) {

//System.out.println(line);

}

input.close();

} catch (IOException e) {

e.printStackTrace();

}finally{

String base64Img = ImageToBase64(imgPath);

//deleteFile(imgPath);

//deleteFile(dataPath);

return base64Img.replaceAll("\\s*", "");

}

}

public static String writeFile(String options) {

String dataPath="D:/平台/data/data"+ UUID.randomUUID().toString().substring(0, 8) +".json";

try {

/* 写入Txt文件 */

File writename = new File(dataPath); // 相对路径,如果没有则要建立一个新的output.txt文件

if (!writename.exists()) { //文件不存在则创建文件,先创建目录

File dir = new File(writename.getParent());

dir.mkdirs();

writename.createNewFile(); // 创建新文件

}

BufferedWriter out = new BufferedWriter(new FileWriter(writename));

out.write(options); // \r\n即为换行

out.flush(); // 把缓存区内容压入文件

out.close(); // 最后记得关闭文件

} catch (IOException e) {

e.printStackTrace();

}

return dataPath;

}

/**

* 图片文件转为base64

* @param imgPath

*/

private static String ImageToBase64(String imgPath) {

byte[] data = null;

// 读取图片字节数组

try {

InputStream in = new FileInputStream(imgPath);

data = new byte[in.available()];

in.read(data);

in.close();

} catch (IOException e) {

e.printStackTrace();

}

// 对字节数组Base64编码

BASE64Encoder encoder = new BASE64Encoder();

// 返回Base64编码过的字节数组字符串

return encoder.encode(Objects.requireNonNull(data));

}

/**

* 删除文件

*

* @param pathname

* @return

* @throws IOException

*/

public static boolean deleteFile(String pathname){

boolean result = false;

File file = new File(pathname);

if (file.exists()) {

file.delete();

result = true;

System.out.println("文件已经被成功删除");

}

return result;

}

}

因为是需要保存base64图片。所以在生成并读取完毕后将图片删除。

附上图片转base64方法:

/**

* 图片文件转为base64

* @param imgPath

*/

private static String ImageToBase64(String imgPath) {

byte[] data = null;

// 读取图片字节数组

try {

InputStream in = new FileInputStream(imgPath);

data = new byte[in.available()];

in.read(data);

in.close();

} catch (IOException e) {

e.printStackTrace();

}

// 对字节数组Base64编码

BASE64Encoder encoder = new BASE64Encoder();

// 返回Base64编码过的字节数组字符串

return encoder.encode(Objects.requireNonNull(data));

}

转换后的编码没有头,需要在保存时手动添加“data:image/png;base64,”

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java生成echarts图片,可以使用Java的开源库echarts-java。该库提供了Javaecharts的集成,可以通过Java代码生成echarts图表,并将其保存为图片。 首先,需要在项目中导入echarts-java的依赖,可以使用Maven或Gradle进行管理。 在代码中,首先创建一个echarts对象,通过设置不同的属性来配置图表的内容和样式。例如,可以设置图表类型、标题、横纵坐标等。 然后,可以创建一个echarts图片生成器对象,将echarts对象作为参数传递给生成器。可以设置生成图片的格式、大小和保存路径等。 最后,调用生成器的generate方法,即可根据echarts对象生成相应的图片并保存。 以下是一个简单的示例代码: ```java import com.github.abel533.echarts.Option; import com.github.abel533.echarts.json.GsonOption; import com.github.abel533.echarts.utils.EchartsUtils; import com.github.abel533.echarts.util.EnhancedOption; import com.github.abel533.echarts.image.ZEChartsConfig; import com.github.abel533.echarts.image.ZEChartsRenderTool; public class EchartsImageGenerator { public static void main(String[] args) { // 创建echarts对象 EnhancedOption option = new EnhancedOption(); option.title().text("示例图表"); option.legend().data("A", "B"); option.xAxis().data("1月", "2月", "3月", "4月", "5月"); option.yAxis().name("销量"); // 添加数据系列 option.series("A", "bar", new Integer[]{10, 20, 30, 40, 50}); option.series("B", "line", new Integer[]{5, 10, 15, 20, 25}); // 创建echarts图片生成器对象 ZEChartsConfig config = new ZEChartsConfig(); config.setRenderTool(ZEChartsConfig.getRenderToolOrInstance()); config.setImagePath("data:image/png;base64"); config.setCharsetName("UTF-8"); ZEChartsRenderTool renderTool = new ZEChartsRenderTool(); renderTool.setConfig(config); // 生成图片并保存 String outputPath = "path/to/output/image.png"; renderTool.renderToPath(EchartsUtils.getInstanceFromOption(option, GsonOption.class), outputPath); } } ``` 通过上述代码,我们可以利用Java调用echarts-java库来生成echarts图片。可以根据实际需求,使用不同的图表类型和数据来定制生成图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值