java导出JFreeChart图表到PDF文件,SVG图像

一、导出到PDF文件
0,环境搭建所需jar包:
文件 描述
jfreechart-1.0.6.jar JFreeChart类库
jcommon-1.0.9.jar          Jcommon类库
itext-2.0.6.jar           Itext类库
1、硬编码创建一个PDF文件,调用方法将图表写入文件,方法的一个参数是FontMapper对象。iText使用FontMapper接口将java字体对象映射成基本的字体对象。DefaultFontMapper类预先默认映射为java本地化字体,而400和300分别是图表的宽和高
File fileName = new File(System.getProperty("user.home") + "/jfreechart1.pdf");
saveChartAsPDF(fileName, chart, 400, 300, new DefaultFontMapper());

2、saveChartAsPDF方法具体内容
public static void saveChartAsPDF(File file, JFreeChart chart, int width,
			int height, FontMapper mapper) throws IOException {
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
writeChartAsPDF(out, chart, width, height, mapper);
out.close();
}

3、writeChartAsPDF方法具体内容,此方法将图表写入文件输入流中
public static void writeChartAsPDF(OutputStream out, JFreeChart chart,
		int width, int height, FontMapper mapper) throws IOException {
		Rectangle pagesize = new Rectangle(width, height);
		Document document = new Document(pagesize, 50, 50, 50, 50);
		try {
			PdfWriter writer = PdfWriter.getInstance(document, out);
			document.addAuthor("JFreeChart");
			document.addSubject("Demonstration");
			document.open();
			PdfContentByte cb = writer.getDirectContent();
			PdfTemplate tp = cb.createTemplate(width, height);
			Graphics2D g2 = tp.createGraphics(width, height, mapper);
			Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
			chart.draw(g2, r2D);
			g2.dispose();
			cb.addTemplate(tp, 0, 0);
		} catch (DocumentException de) {
			System.err.println(de.getMessage());
		}
		document.close();
}

二、导出到SVG图像文件
1、搭建环境所需jar包
文件 描述
jcommon-1.0.9.jar         JFreeChart的通用类包。
jfreechart-1.0.6.jar     JFreeChart的类包
batik-awt-util.jar     Batik实时运行文件
batik-dom.jar        Batik实时运行文件
batik-svggen.jar        Batik实时运行文件
batik-util.jar        Batik实时运行文件
2、执行代码,完整的代码如下:
public class SVGExportDemo {
	/**
	 * Starting point for the demo.
	 * 
	 * @param args
	 *            ignored.
	 */
	public static void main(String[] args) throws IOException {
		// create a dataset...
		DefaultPieDataset data = new DefaultPieDataset();
		data.setValue("Category 1", new Double(43.2));
		data.setValue("Category 2", new Double(27.9));
		data.setValue("Category 3", new Double(79.5));
		// create a chart
		JFreeChart chart = ChartFactory.createPieChart("Sample Pie Chart",
				data, true, false, false);
		// THE FOLLOWING CODE BASED ON THE EXAMPLE IN THE BATIK DOCUMENTATION...
		// Get a DOMImplementation
		DOMImplementation domImpl = GenericDOMImplementation
				.getDOMImplementation();
		// Create an instance of org.w3c.dom.Document
		Document document = domImpl.createDocument(null, "svg", null);
		// Create an instance of the SVG Generator
		SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
		// set the precision to avoid a null pointer exception in Batik 1.5
		svgGenerator.getGeneratorContext().setPrecision(6);
		// Ask the chart to render into the SVG Graphics2D implementation
		chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);
		// Finally, stream out SVG to a file using UTF-8 character to
		// byte encoding
		boolean useCSS = true;
		Writer out = new OutputStreamWriter(new FileOutputStream(new File(
				"test.svg")), "UTF-8");
		svgGenerator.stream(out, useCSS);
	}
}

3、  Batik类库内包含了一个”Squiggle”的小应用,我们可以使用该工具浏览SVG文件。我们可以使用下面命令打开:java -jar batik-squiggle.jar

你可能感兴趣的:(jfreechart,svg,pdf)