首先介绍一下,JavaFX2自带包括折线图等统计图表,样式可以用css设置,教程http://docs.oracle.com/javafx/2/charts/jfxpub-charts.htm。JFreeChart是Java平台的一个开放的图表绘制类库,使用广泛。JavaFX虽然在不断发展,取得一定成绩,但是图表比久未更新的JFreeChart在专业性上还是差不少。另外JFreeChart通过适当设置也可以很美观。
在JavaFX中使用JFreeChart,有如下两种方法,欢迎补充:
1、生成图片加载,可设置大小,输出格式,关联EXCEL
public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) {
FileOutputStream out = null;
try {
File outFile = new File(outputPath);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
out = new FileOutputStream(outputPath);
// 保存为PNG
ChartUtilities.writeChartAsPNG(out, chart, weight, height);
// 保存为JPEG
// ChartUtilities.writeChartAsJPEG(out, chart, weight, height);
out.flush();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// do nothing
}
}
}
}
2、包装成SwingNode,默认大小,可设置node的横纵向拉伸调整
public SwingNode toNode(JFreeChart chart){
SwingNode node=new SwingNode();
ChartPanel c=new ChartPanel(chart);
node.setContent(c);
return node;
}
如有错误,欢迎纠正!
参考资料
http://premier9527.iteye.com/blog/1575018
http://stackoverflow.com/questions/16691320/how-i-can-use-jfreechart-with-javafx2