前端代码:
var option = {
//其他省略
//关闭动画,否则有些统计图表只有背景(如扇形统计图)
animation:false,
}
//初始化echarts,注意:main HTML元素必须指定固定宽高(导出图片的大小),并且不能为隐藏
//如果是隐藏,也可以通过echarts.init(document.getElementById('main'),'',{width:600,height:200})来初始化 容器的宽高
var myChart = echarts.init(document.getElementById('main'));
// 为echarts对象加载数据
myChart.setOption(option);
//获取echarts图的base64编码,为png格式。
var picBase64Info = myChart.getDataURL();
//传输后台方式随意 我用的表单post ajaxSubmit();
java后台接收:
@ResponseBody
@RequestMapping(value = "/export", method = RequestMethod.POST)
public String export(String picBase64Info){
//获取图片的base64字符串,转换为数组
String [] picBase64InfoArray = picBase64Info.split(",");
byte[] imgByte = new BASE64Decoder().decodeBuffer(picBase64InfoArray[1]);
//对异常的数据进行处理
for(int i=0 ;i<imgByte.length;i++){
if(imgByte[i]<0){
imgByte[i] += 256;
}
}
//生成图片的地址
String filePath = "文件保存位置" + ".PNG";
//保存图片 写入本地磁盘中
OutputStream out = new FileOutputStream(filePath);
out.write(imgByte );
out.flush();
out.close();
}