java导出JFreeChart图表到PDF文件,SVG图像
一、导出到PDF文件
0,环境搭建所需jar包:
文件描述
jfreechart-1.0.6.jarJFreeChart类库
jcommon-1.0.9.jar Jcommon类库
itext-2.0.6.jar Itext类库
1、硬编码创建一个PDF文件,调用方法将图表写入文件,方法的一个参数是FontMapper对象。iText使用FontMapper接口将java字体对象映射成基本的字体对象。DefaultFontMapper类预先默认映射为java本地化字体,而400和300分别是图表的宽和高
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 chartJFreeChart chart = ChartFactory.createPieChart("Sample Pie Chart",data, true, false, false);// THE FOLLOWING CODE BASED ON THE EXAMPLE IN THE BATIK DOCUMENTATION...// Get a DOMImplementationDOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();// Create an instance of org.w3c.dom.DocumentDocument document = domImpl.createDocument(null, "svg", null);// Create an instance of the SVG GeneratorSVGGraphics2D svgGenerator = new SVGGraphics2D(document);// set the precision to avoid a null pointer exception in Batik 1.5svgGenerator.getGeneratorContext().setPrecision(6);// Ask the chart to render into the SVG Graphics2D implementationchart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);// Finally, stream out SVG to a file using UTF-8 character to// byte encodingboolean useCSS = true;Writer out = new OutputStreamWriter(new FileOutputStream(new File("test.svg")), "UTF-8");svgGenerator.stream(out, useCSS);}}