最近领导让倒腾java生成pdf报告,里面有几个图表需要生成,雷达图柱状图之类的。。。
百度了一会有好几种方式,我一开始用的JFreeChart效果大概是这样
贴一哈代码
package cn.exrick.xboot.common.utils;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* @MethodName: 生成雷达图、柱状图工具类
* @Description: TODO
* @Param:
* @Return:
* @Author: Jiangsy
* @Date: 2019/8/7
**/
public class JFrameUtils {
public static void main(String args[]) {
JFrame jf = new JFrame();
jf.add(erstelleSpinnenDiagramm());
jf.pack();
jf.setVisible(true);//true会弹出预览的弹层,false就不弹出了
savePic(jf);
// jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//退出应用程序
//你关了窗口线程停止,但是实际项目里,不会弹出图片,只是生成,这个是终止线程
System.exit(0);
}
public static JPanel erstelleSpinnenDiagramm() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
String group1 = "企业信用风险指数(ECRI)";
dataset.addValue(70, group1, "商业信息(70%)");
dataset.addValue(80, group1, "财务信息(80%)");
dataset.addValue(40, group1, "付款信息(40%)");
dataset.addValue(30, group1, "公共记录(30%)");
dataset.addValue(65, group1, "经营信息(65%)");
//SpiderWebPlot spiderwebplot1 = new SpiderWebPlot(dataset);
MySpiderWebPlot mySpiderWebPlot = new MySpiderWebPlot(dataset);
JFreeChart jfreechart = new JFreeChart("企业信用风险指数", TextTitle.DEFAULT_FONT,mySpiderWebPlot, false);
LegendTitle legendtitle = new LegendTitle(mySpiderWebPlot);
legendtitle.setPosition(RectangleEdge.BOTTOM);
jfreechart.addSubtitle(legendtitle);
ChartPanel chartpanel = new ChartPanel(jfreechart);
return chartpanel;
}
public static void savePic(JFrame jf){
//得到窗口内容面板
Container content=jf.getContentPane();
//创建缓冲图片对象
BufferedImage img=new BufferedImage(
jf.getWidth(),jf.getHeight(),BufferedImage.TYPE_INT_RGB);
//得到图形对象
Graphics2D g2d = img.createGraphics();
//将窗口内容面板输出到图形对象中
content.printAll(g2d);
//保存为图片
File f=new File("/project/111.jpg");
try {
ImageIO.write(img, "jpg", f);
} catch (IOException e) {
e.printStackTrace();
}
//释放图形对象
g2d.dispose();
}
}
pom依赖
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
</dependency>
效果不太好看吧,第二种方式是利用phantomjs执行js代码,调用echarts生成图表
PhantomJS是一个基于webkit的javascript API。它使用QtWebKit作为它核心浏览器的功能,使用webkit来编译解释执行JavaScript代码
echarts官网:https://www.echartsjs.com/
PhantomJS:https://phantomjs.org/download.html
先看一下雷达图的demo
我是mac,配置一下PhantomJS环境变量先
1.执行 phantomjs echarts-convert.js -infile /project/222.json -outfile /project/echarts-chart1.png -scale 0.01 -width 600
echarts-convert.js --这是需要用到的echarts转换的js
/project/222.json -- 从echarts官网把左边的options复制出来新建txt,json文件都可以(自己修改options内容)
/project/echarts-chart1.png -- 生成图表存储的路径
-width 600 -- 图片宽度
需要jquerymin.js和echarts的js,放同级目录就好。
用java实现 思路就是调起cmd命令,执行,生成的图片保存到指定路径,后续逻辑可自行读取图片,大概这个意思
String cmd = (代码里需要写完整路径)"phantomjs " + convert.js的路径 + " -infile " + option内容的文件路径 + " -outfile " + 生成图片保存的路径;
Process process = Runtime.getRuntime().exec(cmd);
如果图片出现方框乱码的话安装一下字体支持
yum install bitmap-fonts bitmap-fonts-cjk