使用jfreechart画饼图,并自定义标签说明

1.导入依赖

       <!-- https://mvnrepository.com/artifact/org.jfree/jcommon -->
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jcommon</artifactId>
            <version>1.0.24</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.5.0</version>
        </dependency>

2.编写测试类

    @Test
    public void piechartJpg() {
        /**
         * 生成统计图
         */
        PieDataset dataset = pieDataSet();
        JFreeChart chart = ChartFactory.createPieChart("项目状态分布", dataset, true, true, false);
        //设置legend字体
        chart.getLegend().setItemFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
        //饼图文字居右显示
        chart.getLegend().setPosition(RectangleEdge.RIGHT);
        //文字说明背景色
        chart.getLegend().setBackgroundPaint(Color.white);

        chart.getLegend().setHorizontalAlignment(HorizontalAlignment.RIGHT);//设置水平对齐 左对齐;
//        chart.getLegend().setMargin(0,0,0,0);//参数是:上,左,下,右. 设置饼图的位置
//        chart.getLegend().setPadding(0, 0, 0, 0);// 设置饼图右边文字的位置
//        chart.getLegend().setFrame(new BlockBorder(0,0,0,0));// 设置饼图右边文字边框的位置

        // 设置图片背景色
        chart.setBackgroundPaint(new Color(255, 255, 128));

//        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        PiePlot plot = (PiePlot) chart.getPlot();
        //设置Label字体
        plot.setLabelFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
        plot.setMaximumLabelWidth(0.2D);//设置标签最长宽度
//        plot.setLabelLinkMargin(0.05D); // 设置分类标签与图的连接线边距
        //设置分类标签与图是否有连接线
        plot.setLabelLinksVisible(true);
        // 图片中显示百分比:默认方式
//        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
        // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
//        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

        HashMap labaeMap = new HashMap();
        labaeMap.put("市场前期", "市场前期市场前期");
        plot.setLegendLabelGenerator(new CustomLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"), labaeMap));
        // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
//        plot.setLegendLabelGenerator(new CustomLabelGenerator("{0}={1}({2})"));
//        plot.setLegendLabelToolTipGenerator();

//        plot.setAutoPopulateSectionOutlineStroke(true);
        // 指定图片的透明度(0.0-1.0)
        plot.setForegroundAlpha(1.0f);
        // 指定显示的饼图上圆形(false)还椭圆形(true)
        plot.setCircular(true);
        //饼图背景色
        plot.setBackgroundPaint(new Color(255, 232, 130));
        // 设置绘图面板外边界线的填充颜色
        plot.setOutlinePaint(new Color(255, 232, 130));
        //忽略0值
        plot.setIgnoreZeroValues(true);
        //忽略空值
        plot.setIgnoreNullValues(true);
        // 设置绘图面板阴影的填充颜色
//        plot.setShadowPaint(Color.pink);
        // 设置分饼颜色
//        plot.setSectionPaint("市场前期", new Color(244, 194, 144));
//        plot.setSectionPaint("执行控制", new Color(144, 233, 144));
        // 设置图标题的字体
        java.awt.Font font = new java.awt.Font("黑体", java.awt.Font.CENTER_BASELINE, 20);
        TextTitle title = new TextTitle("项目状态分布");
        title.setFont(font);
        chart.setTitle(title);
        FileOutputStream fos_jpg = null;
        try {
            fos_jpg = new FileOutputStream("C:\\Users\\E102590\\Desktop\\项目状态分布.jpg");
            ChartUtils.writeChartAsJPEG(fos_jpg, 0.7f, chart, 800, 350, null);
            fos_jpg.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("over");
    }

    private static PieDataset pieDataSet() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("市场前期", new Double(10));
        dataset.setValue("立项", new Double(10));
        dataset.setValue("计划", new Double(10));
        dataset.setValue("需求与设计", new Double(10));
        dataset.setValue("执行控制", new Double(20));
        dataset.setValue("收尾", new Double(10));
        dataset.setValue("运维", new Double(10));
        return dataset;
    }

3.实现PieSectionLabelGenerator类,自定义标签生成器

    //自定义标签生成器
    static class CustomLabelGenerator extends StandardPieSectionLabelGenerator implements PieSectionLabelGenerator {
        private Map labelMap;

        public CustomLabelGenerator() {
            this("{0}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance());
        }

        public CustomLabelGenerator(Locale locale) {
            this("{0}", locale);
        }

        public CustomLabelGenerator(String labelFormat) {
            this(labelFormat, NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance());
        }

        public CustomLabelGenerator(String labelFormat, Locale locale) {
            this(labelFormat, NumberFormat.getNumberInstance(locale), NumberFormat.getPercentInstance(locale));
        }

        public CustomLabelGenerator(String labelFormat, NumberFormat numberFormat, NumberFormat percentFormat) {
            super(labelFormat, numberFormat, percentFormat);
        }

        public CustomLabelGenerator(String labelFormat, NumberFormat numberFormat, NumberFormat percentFormat, HashMap<String, String> labelMap) {
            super(labelFormat, numberFormat, percentFormat);
            this.labelMap = labelMap;
        }

        @Override
        public String generateSectionLabel(PieDataset pieDataset, Comparable comparable) {
            if (null != this.labelMap && null != this.labelMap.get(comparable)) {
                return (String) this.labelMap.get(comparable);
            }
            return super.generateSectionLabel(pieDataset, comparable);
        }

        @Override
        public AttributedString generateAttributedSectionLabel(PieDataset pieDataset, Comparable comparable) {
            return super.generateAttributedSectionLabel(pieDataset, comparable);
        }


    }

4.残留问题,自定义说明文字比较多时换行问题没解决

参考博文:

https://blog.csdn.net/qq_33460264/article/details/79966427?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_utm_term~default-4.pc_relevant_default&spm=1001.2101.3001.4242.3&utm_relevant_index=6

https://blog.csdn.net/qq_36846058/article/details/89203067?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164568419216780269862934%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164568419216780269862934&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-3-89203067.pc_search_result_cache&utm_term=JFreeChart%E9%A5%BC%E5%9B%BE%E8%AE%BE%E7%BD%AE%E4%BD%8D%E7%BD%AE&spm=1018.2226.3001.4187

https://blog.csdn.net/wangcunhuazi/article/details/40455851?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164568983216780274144668%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164568983216780274144668&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-40455851.pc_search_result_cache&utm_term=setLegendLabelGenerator&spm=1018.2226.3001.4187

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值