JFreeChart简介及使用

1.所需jar包

JFreeChart1.0.18.jar
jcommon-1.0.22.jar 

下载链接 jar包

2.JFreeChart架构

类层次架构

类层次架构解释了如何把不同阶层的相互库交互,以创建不同类型的图表。

类层次架构 - 单元 - 描述 - 文件 - 所用的用户输入为源,用于创建该文件中的数据集。 - 数据库 - 所用的用户输入为源,用于创建在数据库中的数据集。 - 创建数据集 - 接受数据集中存储和数据集中到数据集对象。
- 通用数据集 - 这种类型的数据集主要用于饼图。 - 分类数据集 - 这种类型的数据集,用于柱状图,折线图等等。 - 系列数据集 - 这种类型的数据集被用于存储一系列数据和构建线图表。 - 系列采集数据集 - 不同类别的一系列数据集添加系列集合数据集。这种类型的数据集,用于xy折线图表。 - 创建图表 - 这是被执行以创建最终的图表的方法。 - 帧、图片 - 该图显示在一个Swing框架或创建映像。 应用层框架

应用层框架

3.JFreeChart API

JFreeChart API

4.生成各种图表的图片代码

Jfreechart打造专业漂亮图表

4.1生成饼状图
        ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());

        DefaultPieDataset data = new DefaultPieDataset();
        // 饼状图分割数据
        data.setValue("使用率", 40);
        data.setValue("未使用率", 60);

        JFreeChart chart = ChartFactory.createPieChart(
                            title,   //标题
                            data,    //数据
                            true,    //是否生成图例
                            true,    //是否生成工具
                            false);  //是否生成链接
    
        // 图例标题
        LegendTitle lt = chart.getLegend();
        // 字体设定
        Font font = new Font(FONT_MSGOTHIC, Font.PLAIN, 13);
        lt.setItemFont(font);
        // 标题的外边框设定
        chart.setBorderVisible(false);
        // 背景色设定
        chart.setBackgroundPaint(Color.WHITE);
        if ( title != null ) {
            chart.getTitle().setFont( new Font( FONT_MSGOTHIC, Font.PLAIN, 14 ) );
        }

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setInteriorGap(0.03);
        // 标题的内框设定
        plot.setOutlineVisible(false);

        // 每个图例的颜色设定
        plot.setSectionPaint(data.getKey(0), new Color(255, 201, 14));
        plot.setSectionPaint(data.getKey(1), new Color(144, 160, 255));

        plot.setLabelLinksVisible(false);
        plot.setSimpleLabels(true);
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}"));
        plot.setLabelPaint(Color.BLACK);
        plot.setLabelBackgroundPaint(null);
        plot.setLabelOutlineStroke(null);
        plot.setShadowGenerator(new DefaultShadowGenerator(1, Color.WHITE, 1f, 0, 0d));
        plot.setShadowPaint(null);

        // 图例位置 边框设定
        LegendTitle legend = chart.getLegend();
        legend.setPosition(RectangleEdge.RIGHT);
        legend.setBorder(0, 0, 0, 0);

        // 输出方式1:PNG文件
        ChartUtilities.saveChartAsPNG(imgFile, chart, width, height);
        
        // 输出方式2:流,通过response传送到jsp页面
	response.setContentType("image/png");
	ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 210, 80);
	response.getOutputStream().flush();
4.2生成柱状图
        ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());

        DefaultCategoryDataset data = new DefaultCategoryDataset();
        data.addValue(
                    value1,    //柱状图的数值
                      str1,   //某一个分类的柱状图名字
                      str2   //某一分类名字
         );

        JFreeChart chart = ChartFactory.createBarChart(
                        title,   //柱状图标题
                        null,    //横轴标题
                        null,    //纵轴标题
                        data,    //数据
  PlotOrientation.HORIZONTAL,    //图表方向:横向
                        false,   //是否生成图例
                        true,    //是否生成工具
                        false    //是否生成链接
        );
        // 标题外边框线是否显示
        chart.setBorderVisible(false);
        // 图表的背景色
        chart.setBackgroundPaint(Color.WHITE);
        if ( title != null ) {
            chart.getTitle().setFont( new Font( FONT_MSGOTHIC, Font.PLAIN, 14 ) );
        }

        //获取绘图区对象
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setInteriorGap(0.03);
        plot.setOutlineVisible(false);   //>>> 内边线设定
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);  //>>> 轴的位置设定

        // 横轴相关设定
        CategoryAxis cAxis = plot.getDomainAxis();
        cAxis.setTickLabelFont(new Font(FONT_MSGOTHIC, Font.PLAIN, 10));

        // 纵轴相关设定
        ValueAxis vAxis = plot.getRangeAxis();
        vAxis.setTickLabelFont(new Font(FONT_MSGOTHIC, Font.PLAIN, 11));

        // bar的设定
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setShadowVisible(false);
        renderer.setItemMargin(0);  //>>> Category间的分割距离

        // 柱的宽度(ItemMargin优先)
        double barWidth = 0.08;
        switch (size) {
        case 1:
            barWidth = 0.45; break;
        case 2:
            barWidth = 0.25; break;
        }
        renderer.setMaximumBarWidth(barWidth);

        // 每个柱子的颜色设定
        renderer.setSeriesPaint(0, new Color(255, 201, 14));
        renderer.setSeriesPaint(1, new Color(255, 174, 201));

        // 横轴的设定
        CategoryAxis domAxis = plot.getDomainAxis();
        domAxis.setLowerMargin(0.04); 
        domAxis.setUpperMargin(0.04); 
        // 间隔设定
        domAxis.setCategoryMargin(0.08);
        // 文字的角度
        domAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
        // label最大行数
        if (strgData.size() < 4)
            domAxis.setMaximumCategoryLabelLines(2);
        else
            domAxis.setMaximumCategoryLabelLines(1);
        
        domAxis.setMaximumCategoryLabelWidthRatio(0.4f);

        // 纵轴设定
        NumberAxis valueAxis = (NumberAxis)plot.getRangeAxis();
        valueAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());  //>>> 固定整数
        valueAxis.setAutoRange(false); //>>> 值轴的范围
        valueAxis.setRange(0, 100);

        // 输出方式1:PNG文件
        ChartUtilities.saveChartAsPNG(imgFile, chart, width, height);
        
        // 输出方式2:流,通过response传送到jsp页面
	response.setContentType("image/png");
	ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 210, 80);
	response.getOutputStream().flush();
4.3生成折线图
        ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());

        DefaultCategoryDataset data = new DefaultCategoryDataset();
        data.addValue(
                    value1,    //值域的数值
                      str1,   //某一折线的名字
                      str2   //范围域的值
         );

        JFreeChart chart = ChartFactory.createLineChart(
                        title,   //折线图标题
                        null,    //横轴标题
                        null,    //纵轴标题
                        data,    //数据
  PlotOrientation.HORIZONTAL,    //图表方向:横向
                        false,   //是否生成图例
                        true,    //是否生成工具
                        false    //是否生成链接
        );
        // 标题外边框
        chart.setBorderVisible(false);
        // 背景色设定
        chart.setBackgroundPaint(Color.WHITE);
        if ( title != null ) {
            chart.getTitle().setFont( new Font( FONT_MSGOTHIC, Font.PLAIN, 14 ) );
        }

        //获取绘图区域
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setOutlineVisible(false);  //>>> 标题内边线设定
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); //>>> 轴的位置设定

        // 横轴设定
        CategoryAxis cAxis = plot.getDomainAxis();
        cAxis.setTickLabelFont(new Font(FONT_MSGOTHIC, Font.PLAIN, 10)); //字体设定

        // 纵轴设定
        ValueAxis vAxis = plot.getRangeAxis();
        vAxis.setTickLabelFont(new Font(FONT_MSGOTHIC, Font.PLAIN, 11)); //字体设定

        // 横轴设定
        CategoryAxis domAxis = plot.getDomainAxis();  
    domAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));  // label文字角度

         // 输出方式1:PNG文件
        ChartUtilities.saveChartAsPNG(imgFile, chart, width, height);
        
        // 输出方式2:流,通过response传送到jsp页面
	response.setContentType("image/png");
	ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 210, 80);
	response.getOutputStream().flush();

转载于:https://my.oschina.net/u/2462423/blog/528599

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值