其他分享
首页 > 其他分享> > JFreeChart

JFreeChart

作者:互联网

星朝博客:jFreeChart利用CategoryDatase,ChartFactory.createBarChart生成的柱状图

/**
     * 创建JFreeChart Bar Chart(柱状图)
     */
    public static void main(String[] args) {
        //步骤1:创建CategoryDataset对象(准备数据)
        CategoryDataset dataset = createDataset();
        //步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置
        JFreeChart freeChart = createChart(dataset);
        //步骤3:将JFreeChart对象输出到文件,Servlet输出流等
        saveAsFile(freeChart, "D:\\资料\\bar.png", 500, 400);
    }

    // 创建CategoryDataset对象
    public static CategoryDataset createDataset() {

        String []rowKeys = {"One", "Two", "Three"};
        String []colKeys = {"1987", "1997", "2007"};

        double [][] data = {
                {100, 20, 50},
                {20, 10.5, 40},
                {40, 30.0008, 38.24},
        };

        //也可以使用以下代码
        DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
        categoryDataset.addValue(688, "小a", "sum");
        categoryDataset.addValue(600, "小b", "sum");
        categoryDataset.addValue(95, "小a", "avg");
        categoryDataset.addValue(80, "小b", "avg");

//        return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data);
        return categoryDataset;
    }

    //根据CategoryDataset生成JFreeChart对象
    public static JFreeChart createChart(CategoryDataset categoryDataset) {
        JFreeChart jfreechart = ChartFactory.createBarChart("测试表",    //标题
                "ss",    //categoryAxisLabel (category轴,横轴,X轴的标签)
                "aa",    //valueAxisLabel(value轴,纵轴,Y轴的标签)
                categoryDataset, // dataset
                PlotOrientation.VERTICAL,
                true, // legend
                false, // tooltips
                false); // URLs

        Font font = new Font("新宋体", Font.BOLD, 15);

        //设置整个图片的标题字体
        jfreechart.getTitle().setFont(font);

        jfreechart.getLegend().setItemFont(font);

        //以下的设置可以由用户定制,也可以省略
        CategoryPlot  plot = (CategoryPlot) jfreechart.getPlot();
        //背景色 透明度
        plot.setBackgroundAlpha(1f);
        //前景色 透明度
        plot.setForegroundAlpha(1f);
        //其它设置可以参考 CategoryPlot

        return jfreechart;
    }

    //保存为文件
    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
                }
            }
        }
    }

 

标签:JFreeChart,CategoryDataset,static,jfreechart,categoryDataset,out
来源: https://www.cnblogs.com/loveDonkey/p/16080420.html