Spring Boot 集成JFreeChart 生成图表

 添加依赖

<!-- JFreeChart图表库 -->
		<dependency>
			<groupId>org.jfree</groupId>
			<artifactId>jfreechart</artifactId>
			<version>1.5.3</version>
		</dependency>

直接上代码吧


import java.awt.*;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import cn.hutool.core.date.DateUtil;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.general.PieDataset;
import org.springframework.util.ResourceUtils;

/**
 * @author XUWEICHAO
 */
public class JfreeChartUtil {
    private static PieDataset pieDataSet() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("市场前期", new Double(10));
        dataset.setValue("立项", new Double(15));
        dataset.setValue("计划", new Double(10));
        dataset.setValue("需求与设计", new Double(10));
        dataset.setValue("执行控制", new Double(55));
        dataset.setValue("收尾", new Double(10));
        dataset.setValue("运维", new Double(10));
        return dataset;
    }

    private static CategoryDataset getDataSet() {
        final DefaultCategoryDataset ds = new DefaultCategoryDataset();
        ds.addValue(1.34, "失业率", "2018");
        ds.addValue(0.65, "公平性", "2018");
        ds.addValue(1.11, "失业率", "2019");
        ds.addValue(0.72, "公平性", "2019");
        ds.addValue(1.17, "失业率", "2020");
        ds.addValue(0.60, "公平性", "2020");
        ds.addValue(1.53, "失业率", "2021");
        ds.addValue(0.65, "公平性", "2021");
        ds.addValue(1.66, "失业率", "2022");
        ds.addValue(0.53, "公平性", "2022");
        return ds;
    }

    /**
     * 生成饼状图
     *
     * @param title
     * @param dataset
     * @return
     */
    public static String createPieChart(String title, PieDataset dataset) {

        JFreeChart chart = ChartFactory.createPieChart(title, dataset, true, true, false);
        // 设置抗锯齿,防止字体显示不清楚
        chart.setTextAntiAlias(false);
        PiePlot plot = (PiePlot) chart.getPlot();
        //边框线为白色
        plot.setOutlinePaint(Color.white);
        //连接线类型为直线
        plot.setLabelLinkStyle(PieLabelLinkStyle.QUAD_CURVE);
        //设置Label字体
        plot.setLabelFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
        //设置legend字体
        chart.getLegend().setItemFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
        // 图片中显示百分比:默认方式
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
        // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})",
                NumberFormat.getNumberInstance(),
                new DecimalFormat("0.00%")));
        // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
        // 指定图片的透明度(0.0-1.0)
        plot.setForegroundAlpha(1.0f);
        // 指定显示的饼图上圆形(false)还椭圆形(true)
        plot.setCircular(true);
        //设置图表背景透明度
        plot.setBackgroundAlpha(0f);
        // 设置背景色为白色
        chart.setBackgroundPaint(Color.WHITE);
        // 设置标注无边框
        chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
        // 标注位于右侧
        chart.getLegend().setPosition(RectangleEdge.RIGHT);

        // 设置图标题的字体
        java.awt.Font font = new java.awt.Font("黑体", java.awt.Font.CENTER_BASELINE, 20);
        TextTitle textTitle = new TextTitle(title);
        textTitle.setFont(font);
        chart.setTitle(textTitle);

        //图片存放位置
        try {
            String imgPath = ResourceUtils.getURL("classpath:").getPath() + "static/img/" + DateUtil.current() + ".jpg";

            FileOutputStream outputStream = new FileOutputStream(imgPath);

            ChartUtils.writeChartAsJPEG(outputStream, 1.0f, chart, 800, 450, null);
            outputStream.close();
            return imgPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public static String createBarChart(String title, CategoryDataset dataset) {

        final JFreeChart chart = ChartFactory.createBarChart(
                title,
                "年份", // 目录轴的显示标签
                "数值", // 数值轴的显示标签
                dataset, // 数据集
                PlotOrientation.VERTICAL, // 图表方向
                true, // 是否显示图例,对于简单的柱状图必须为false
                true, // 是否生成提示工具
                false); // 是否生成url链接

        final CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();

        final NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();

        final CategoryAxis domainAxis = categoryplot.getDomainAxis();
        // 设置背景透明度
        categoryplot.setBackgroundAlpha(0f);
        /*------设置X轴坐标上的文字-----------*/
        domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));

        /*------设置X轴的标题文字------------*/
        domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));

        /*------设置Y轴坐标上的文字-----------*/
        numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));

        /*------设置Y轴的标题文字------------*/
        numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

        /*------这句代码解决了底部汉字乱码的问题-----------*/
        chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));

        /******* 这句代码解决了标题汉字乱码的问题 ********/
        chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));


        try {
            String imgPath = ResourceUtils.getURL("classpath:").getPath() + "static/img/" + DateUtil.current() + ".jpg";
            FileOutputStream out = new FileOutputStream(imgPath);
            ChartUtils.writeChartAsJPEG(out, 1.0f, chart, 800, 450, null);
            out.close();
            return imgPath;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    public static void main(String[] args) {

        //PieDataset dataset = JfreeChartUtil.pieDataSet();
        //JfreeChartUtil.createPieChart("项目进度分布", dataset);

        CategoryDataset dataSet = JfreeChartUtil.getDataSet();
        JfreeChartUtil.createBarChart("柱状图标题", dataSet);
    }
}

查看效果

就是这么简单,有时间会补充一下折线图和柱状图的代码,欢迎评论交流!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值