JavaFX中使用JFreeChart方法总结

来源:互联网 发布:vvic搜款网络批发广州 编辑:程序博客网 时间:2024/06/06 07:35

首先介绍一下,JavaFX2自带包括折线图等统计图表,样式可以用css设置,教程http://docs.oracle.com/javafx/2/charts/jfxpub-charts.htmJFreeChartJava平台的一个开放的图表绘制类库,使用广泛。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






0 0
原创粉丝点击