JFreeChart is a free Java class library for generating charts. After creating a nice graph you might want to use it in a report. JFreeChart already allows you to save your graph to PNG and JPEG, but obviously you would rather have it in vector format, so the chart can be scaled to any size.
Just to save you the effort here's the Java code to export your chart to SVG (Scalable Vector Graphics) format.
First download the Batik SVG toolkit. Batik comes in a large number of jar files. You only need the following ones:
batik-dom.jar
batik-svggen.jar
batik-awt-util.jar
batik-util.jar
batik-xml.jar
Create a JFreeChart as you normally do. Use the following function to save the graph to a file.
/**
* Exports a JFreeChart to a SVG file.
*
* @param chart JFreeChart to export
* @param bounds the dimensions of the viewport
* @param svgFile the output file.
* @throws IOException if writing the svgFile fails.
*/
void exportChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
// Get a DOMImplementation and create an XML document
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
Document document = domImpl.createDocument(null, "svg", null);
// Create an instance of the SVG Generator
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// draw the chart in the SVG generator
chart.draw(svgGenerator, bounds);
// Write svg file
OutputStream outputStream = new FileOutputStream(svgFile);
Writer out = new OutputStreamWriter(outputStream, "UTF-8");
svgGenerator.stream(out, true /* use css */);
outputStream.flush();
outputStream.close();
}
One small tip if you want to use the SVG in your LaTeX document: You can convert the SVG file to EPS using Inkscape.
If you don't know how to use this code, you can also download a zip file containing a runnable version of the code (including the required batik and jfreechart libraries) here (compiled with Java SDK 1.5).
分享到:
相关推荐
纯Java动态生成SVG饼图与JFreeChart超强功能生成SVG图表
java生成饼图svg,代码实现感觉有点复杂,个人认为不如用JFreeChart,这篇文章主要介绍java生成饼图svg及JFreeChart生成svg图表,有需要的小伙伴可以参考下
jfreechart-1.5.2.jar,jfreechart|jfreechart
JFreeChart 指南 JFreeChart Guide JFreeChart 示例 JFreeChart demo
JfreeChart学习总结JfreeChart学习总结
Jfreechart 乱码分析和解决方法
jfreechart Java图形报表开源代码
JFreeChart示例 JAVA_JFreeChart的应用 JFreeChart 教程
这是JFReeChart官方例子,它展示了JFreeChart所有图形。
JFreeChart的使用,需要导入的2个jar文件: jcommon-1.0.23.jar和jfreechart-1.0.19.jar。 可以去官网下载:http://sourceforge.net/projects/jfreechart/files/ 上传以作后续使用时备份.
jFreeChart文档,了解jFreeChart的使用,快速入门和上手
jfreechart
JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包...
JFreeChart相关jar + JFreeChart帮助文档 + JFreeChart详解
jfreechart折线图所需jar包
jfreechart开发jar包,真正要引用到eclipse build path的是jfreechart-1.0.14\lib目录下的gnujaxp.jar、jcommon-1.017.jar和jfreechart-1.0.14.jar三个包,我用的就是这3个jar包。。
JFreeChart中文api (JFreeChart-1.0.5-Ch.chm) JFreeChart中文api (JFreeChart-1.0.5-Ch.chm)