1、maven引入
<!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.5.5</version>
</dependency>
2、编写java 执行入口
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A4, 25, 25, 30, 55);
String path = "chartView-" + System.currentTimeMillis() + ".pdf";
final String dir = System.getProperty("user.dir") + "/output";
File file = new File(dir);
if (!file.exists()) {
file.mkdir();
}
path = dir + File.separator + path;
FileOutputStream os = new FileOutputStream(path);
PdfWriter writer = PdfWriter.getInstance(document, os);
// 设置页面布局
writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);
document.open();
PdfPTable pdfPTable = ElementUtil.addTable(1, Element.ALIGN_CENTER, Element.ALIGN_CENTER);
com.itextpdf.text.Image image = barChartH("开关触发次数");
//com.itextpdf.text.Image image = barChart("开关触发次数");
PdfPCell pdfPCell = ElementUtil.addTableCellImg(image, 1, 1, Element.ALIGN_CENTER, Element.ALIGN_CENTER, new BaseColor(255, 255, 255));
pdfPTable.addCell(pdfPCell);
document.add(pdfPTable);
// image.scaleToFit(PageSize.A4.getWidth()-60, PageSize.A4.getHeight() - 100);
// document.add(image);
document.close();
}
3、简单PDF生成这里不简述,下面编写生成柱状图图片方法
/**
* 生成柱状图
*/
public static com.itextpdf.text.Image barChartH(String xAxisTitle) throws Exception {
//x轴名称列表
List<String> xAxisNameList = new ArrayList<>(Arrays.asList(
"开关1",
"开关2",
"开关3",
"开关4",
"开关5",
"开关6",
"开关7",
"开关8",
"开关9",
"开关10"));
//图例名称列表
List<String> legendNameList = new ArrayList<>(Arrays.asList("智能保护跳闸次数"));
//数据列表
List<List<Object>> dataList = new ArrayList<>();
dataList.add(new ArrayList<>(Arrays.asList(540, 90, 80, 78,70,50, 45, 0, 0, 0)));
//图例背景颜色
List<Color> legendColorList = new ArrayList<>(Arrays.asList(new Color(232, 83, 6)));
JFreeChart chart = GenerateChartUtil.createBarChartH("智能开关断路保护", legendNameList, xAxisNameList
, dataList, JFreeChartUtil.createChartTheme("宋体"), "跳闸/次", xAxisTitle,
0d, null, legendColorList, true, "{2}");
//在D盘目录下生成图片
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ChartUtils.writeChartAsJPEG(bas, 1.0f, chart, (int)PageSize.A4.getHeight()-10, 475, null);
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(bas.toByteArray());
return image;
}
/**
* 生成柱状图
*/
public static com.itextpdf.text.Image barChart(String xAxisTitle) throws Exception {
//x轴名称列表
List<String> xAxisNameList = new ArrayList<>(Arrays.asList(
"开关1",
"开关2",
"开关3",
"开关4",
"开关5",
"开关6",
"开关7",
"开关8",
"开关9",
"开关10"));
//图例名称列表
List<String> legendNameList = new ArrayList<>(Arrays.asList("智能保护跳闸次数"));
//数据列表
List<List<Object>> dataList = new ArrayList<>();
dataList.add(new ArrayList<>(Arrays.asList(540, 90, 80, 78,70,50, 45, 0, 0, 0)));
//图例背景颜色
List<Color> legendColorList = new ArrayList<>(Arrays.asList(new Color(232, 83, 6)));
JFreeChart chart = GenerateChartUtil.createBarChart("智能开关断路保护", legendNameList, xAxisNameList
, dataList, JFreeChartUtil.createChartTheme("宋体"), "跳闸/次", xAxisTitle,
0d, null, legendColorList, true, "{2}");
//在D盘目录下生成图片
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ChartUtils.writeChartAsJPEG(bas, 1.0f, chart, (int)PageSize.A4.getHeight()-10, 475, null);
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(bas.toByteArray());
return image;
}
4、简单工具类后续再单独文件提供,下面是简单结果展示