jFreeChart+itext生成带统计图的pdf文件

jar包依赖

<!-- iText start-->
		<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
		<dependency>
		    <groupId>com.itextpdf</groupId>
		    <artifactId>itextpdf</artifactId>
		    <version>5.5.12</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
		<!-- itext 中文支持 -->
		<dependency>
		    <groupId>com.itextpdf</groupId>
		    <artifactId>itext-asian</artifactId>
		    <version>5.2.0</version>
		</dependency>
		<!-- iText end-->
		
		<!-- https://mvnrepository.com/artifact/org.jfree/jcommon -->
		<dependency>
		    <groupId>org.jfree</groupId>
		    <artifactId>jcommon</artifactId>
		    <version>1.0.24</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
		<dependency>
		    <groupId>org.jfree</groupId>
		    <artifactId>jfreechart</artifactId>
		    <version>1.5.0</version>
		</dependency>

关键代码

package com.nk.common;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFTest {
	private static final String FILE_URL = "C:\\Users\\jj\\Desktop\\NIKEY\\";
	private static final int alarmNum = 123;
	public static void main(String[] args) {
		Document document = new Document();
		try {
			PdfWriter.getInstance(document, new FileOutputStream(FILE_URL+"hello01.pdf"));
			document.open();
			//设置中文样式(不设置,中文将不会显示)
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font fontChinese_title = new Font(bfChinese, 20, Font.BOLD, BaseColor.BLACK);
			Font fontChinese_content = new Font(bfChinese, 10, Font.NORMAL, BaseColor.BLACK);
			Paragraph paragraph_title = new Paragraph("这是一个标题", fontChinese_title);
			paragraph_title.setAlignment(Paragraph.ALIGN_CENTER);
			Paragraph paragraph_title_1 = new Paragraph("01总概括", fontChinese_content);
			paragraph_title_1.setAlignment(Paragraph.ALIGN_JUSTIFIED);
			Paragraph paragraph_content = new Paragraph("本期发生告警数量一共"+alarmNum+"次", fontChinese_content);
			paragraph_content.setFirstLineIndent(20);
			document.add(paragraph_title);
			document.add(paragraph_title_1);
			document.add(paragraph_content);
			document.newPage();
			Image img = Image.getInstance(FILE_URL+"N03S{0KBD3NLW8(M}G0{UMR.png");
			img.setAlignment(Image.ALIGN_CENTER);
			img.scaleAbsolute(328, 370);
			document.add(img);
			/**
			 * 生成统计图
			 */
			PieDataset dataset = pieDataSet();
			JFreeChart chart = ChartFactory.createPieChart3D(
															" 项目进度分布", // chart title
															dataset,// data
															true,// include legend
															true,
															false
			);
			PiePlot3D plot=(PiePlot3D)chart.getPlot();
			//设置Label字体
			plot.setLabelFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
			//设置legend字体
			chart.getLegend().setItemFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
			// 图片中显示百分比:默认方式
			//plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
			// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
			plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
			// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
			plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
			// 设置背景色为白色
			chart.setBackgroundPaint(Color.white);
			// 指定图片的透明度(0.0-1.0)
			plot.setForegroundAlpha(1.0f);
			// 指定显示的饼图上圆形(false)还椭圆形(true)
			plot.setCircular(true);
			// 设置图标题的字体
			java.awt.Font font = new java.awt.Font(" 黑体",java.awt.Font.CENTER_BASELINE,20);
			TextTitle title = new TextTitle("项目状态分布");
			title.setFont(font);
			chart.setTitle(title);
			FileOutputStream fos_jpg = null;
			try {
				fos_jpg=new FileOutputStream("D:\\项目状态分布.jpg");
				ChartUtils.writeChartAsJPEG(fos_jpg,0.7f,chart,800,1000,null);
				fos_jpg.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			document.newPage();
			Paragraph pieParagraph = new Paragraph("02、饼状图测试", fontChinese_content);
			pieParagraph.setAlignment(Paragraph.ALIGN_LEFT);
			document.add(pieParagraph);
			Image pieImage = Image.getInstance("D:\\项目状态分布.jpg");
			pieImage.setAlignment(Image.ALIGN_CENTER);
			pieImage.scaleAbsolute(328, 370);
			document.add(pieImage);
			
			/**
			 * 折线图
			 */
			CategoryDataset lineDataset = lineDataset();
			JFreeChart lineChart = ChartFactory.createLineChart("java图书销量",
										"java图书", 
										"销量", 
										lineDataset, 
										PlotOrientation.VERTICAL, 
										true, 
										true, 
										true
										);
			lineChart.getLegend().setItemFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 16));
			//获取title
			lineChart.getTitle().setFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 16));
			
			//获取绘图区对象
			CategoryPlot linePlot = lineChart.getCategoryPlot();
			linePlot.setBackgroundAlpha(0.1f);
			
			//获取坐标轴对象
			CategoryAxis lineAxis = linePlot.getDomainAxis();
			//设置坐标轴字体
			lineAxis.setLabelFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
			//设置坐标轴标尺值字体(x轴)
			lineAxis.setTickLabelFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
			//获取数据轴对象(y轴)
			ValueAxis rangeAxis = linePlot.getRangeAxis();
			rangeAxis.setLabelFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
			
			
			/*
			 * 生成图片
			 */
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream("D:\\java图书销量.jpg");
				ChartUtils.writeChartAsJPEG(fos, 0.7f, lineChart, 600, 300);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			Paragraph lineParagraph = new Paragraph("02、折线图测试", fontChinese_content);
			lineParagraph.setAlignment(Paragraph.ALIGN_LEFT);
			document.add(lineParagraph);
			Image image = Image.getInstance("D:\\java图书销量.jpg");
			image.setAlignment(Image.ALIGN_CENTER);
			image.scaleAbsolute(600, 300);
			document.add(image);
			
			System.out.println("over");
			document.close();
		} catch (FileNotFoundException | DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static CategoryDataset lineDataset() {
		DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
		//添加第一季度数据
        dataSet.addValue(6000, "第一季度", "J2SE类");
        dataSet.addValue(3000, "第一季度", "J2ME类");
        dataSet.addValue(12000, "第一季度", "J2EE类");
        //添加第二季度数据
        dataSet.addValue(8000, "第二季度", "J2SE类");
        dataSet.addValue(4000, "第二季度", "J2ME类");
        dataSet.addValue(6000, "第二季度", "J2EE类");
        //添加第三季度数据
        dataSet.addValue(5000, "第三季度", "J2SE类");
        dataSet.addValue(4000, "第三季度", "J2ME类");
        dataSet.addValue(8000, "第三季度", "J2EE类");
        //添加第四季度数据
        dataSet.addValue(8000, "第四季度", "J2SE类");
        dataSet.addValue(2000, "第四季度", "J2ME类");
        dataSet.addValue(9000, "第四季度", "J2EE类");
		return dataSet;
	}

	private static PieDataset pieDataSet() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue(" 市场前期", new Double(10));
		dataset.setValue(" 立项", new Double(15));
		dataset.setValue(" 计划", new Double(10));
		dataset.setValue(" 需求与设计", new Double(10));
		dataset.setValue(" 执行控制", new Double(35));
		dataset.setValue(" 收尾", new Double(10));
		dataset.setValue(" 运维",new Double(10));
		return dataset;
	}
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭