Java后台批量生产echarts图表并保存图片
一个围绕统计分析功能的系统,在最后制作统计分析时需要一个批量点击的功能,用以批量制作echarts图形后生成图片并保存图形和图片。方便后续导出。
publicclassEchartsUtils{
privatestaticfinalStringJSpath="C:\\echarts-convert\\echarts-convert1.js";
publicstaticvoidmain(String[]args){
StringimgName="D:/平台/tes"+UUID.randomUUID().toString().substring(0,4)+".png";
Stringoption="{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'}]}";
//Stringoptions="test";
Stringbase64Img=generateEChart(option,1600,900);
System.out.println(base64Img);
}
publicstaticStringgenerateEChart(Stringoptions,intwidth,intheight){
StringfileName="test-"+UUID.randomUUID().toString().substring(0,8)+".png";
StringimgPath="D:/平台/img/"+fileName;
StringdataPath=writeFile(options);//数据json
try{
Filefile=newFile(imgPath);//文件路径(路径+文件名)
if(!file.exists()){//文件不存在则创建文件,先创建目录
Filedir=newFile(file.getParent());
dir.mkdirs();
file.createNewFile();
}
Stringcmd="phantomjs"+JSpath+"-infile"+dataPath+"-outfile"+imgPath+"-width"+width+"-height"+height;
System.out.println(cmd);
Processprocess=Runtime.getRuntime().exec(cmd);
BufferedReaderinput=newBufferedReader(newInputStreamReader(process.getInputStream()));
Stringline="";
while((line=input.readLine())!=null){
//System.out.println(line);
}
input.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
Stringbase64Img=ImageToBase64(imgPath);
//deleteFile(imgPath);
//deleteFile(dataPath);
returnbase64Img.replaceAll("\\s*","");
}
}
publicstaticStringwriteFile(Stringoptions){
StringdataPath="D:/平台/data/data"+UUID.randomUUID().toString().substring(0,8)+".json";
try{
/*写入Txt文件*/
Filewritename=newFile(dataPath);//相对路径,如果没有则要建立一个新的output.txt文件
if(!writename.exists()){//文件不存在则创建文件,先创建目录
Filedir=newFile(writename.getParent());
dir.mkdirs();
writename.createNewFile();//创建新文件
}
BufferedWriterout=newBufferedWriter(newFileWriter(writename));
out.write(options);//\r\n即为换行
out.flush();//把缓存区内容压入文件
out.close();//最后记得关闭文件
}catch(IOExceptione){
e.printStackTrace();
}
returndataPath;
}
/**
*图片文件转为base64
*@paramimgPath
*/
privatestaticStringImageToBase64(StringimgPath){
byte[]data=null;
//读取图片字节数组
try{
InputStreamin=newFileInputStream(imgPath);
data=newbyte[in.available()];
in.read(data);
in.close();
}catch(IOExceptione){
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoderencoder=newBASE64Encoder();
//返回Base64编码过的字节数组字符串
returnencoder.encode(Objects.requireNonNull(data));
}
/**
*删除文件
*
*@parampathname
*@return
*@throwsIOException
*/
publicstaticbooleandeleteFile(Stringpathname){
booleanresult=false;
Filefile=newFile(pathname);
if(file.exists()){
file.delete();
result=true;
System.out.println("文件已经被成功删除");
}
returnresult;
}
}
因为是需要保存base64图片。所以在生成并读取完毕后将图片删除。
附上图片转base64方法:
/**
*图片文件转为base64
*@paramimgPath
*/
privatestaticStringImageToBase64(StringimgPath){
byte[]data=null;
//读取图片字节数组
try{
InputStreamin=newFileInputStream(imgPath);
data=newbyte[in.available()];
in.read(data);
in.close();
}catch(IOExceptione){
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoderencoder=newBASE64Encoder();
//返回Base64编码过的字节数组字符串
returnencoder.encode(Objects.requireNonNull(data));
}
转换后的编码没有头,需要在保存时手动添加“data:image/png;base64,”
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。