1 Maven依赖

       <!-- Hutool工具包 --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.6.2</version></dependency><!-- JFreeChart图表库 --><dependency><groupId>org.jfree</groupId><artifactId>jfreechart</artifactId><version>1.5.3</version></dependency>

2 GeneratePieChartUtil

饼图生成工具类。

package com.jfreechart;import cn.hutool.core.collection.CollectionUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.PieLabelLinkStyle;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.chart.util.Rotation;import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;/*** 饼图生成工具类*/
public class GeneratePieChartUtil {/*** 生成饼图(返回JFreeChart)** @param chartTitle         图表标题* @param legendNameList     图例名称列表* @param dataList           数据列表* @param theme              主题(null代表默认主题)* @param legendColorList    图例背景颜色列表(为空,使用默认背景颜色)* @param explodePercentList 偏离百分比数据* @return*/public static JFreeChart createPieChart(String chartTitle, List<String> legendNameList, List<Object> dataList, StandardChartTheme theme, List<Color> legendColorList, List<Double> explodePercentList) {//设置主题,防止中文乱码theme = theme == null ? JFreeChartUtil.createChartTheme("") : theme;ChartFactory.setChartTheme(theme);//创建饼图JFreeChart chart = ChartFactory.createPieChart(chartTitle, JFreeChartUtil.createDefaultPieDataset(legendNameList, dataList));// 设置抗锯齿,防止字体显示不清楚chart.setTextAntiAlias(false);PiePlot piePlot = (PiePlot) chart.getPlot();//边框线为白色piePlot.setOutlinePaint(Color.white);//连接线类型为直线piePlot.setLabelLinkStyle(PieLabelLinkStyle.QUAD_CURVE);// 对饼图进行渲染JFreeChartUtil.setPieRender(chart.getPlot());// 设置标注无边框chart.getLegend().setFrame(new BlockBorder(Color.WHITE));// 标注位于右侧chart.getLegend().setPosition(RectangleEdge.RIGHT);//设置图例背景颜色(饼图)if (CollectionUtil.isNotEmpty(legendColorList)) {for (int i = 0; i < legendNameList.size() && i < legendColorList.size(); i++) {Color color = legendColorList.get(i);if (color == null) {continue;}piePlot.setSectionPaint(legendNameList.get(i), color);}}//设置偏离百分比if (CollectionUtil.isNotEmpty(explodePercentList)) {for (int i = 0; i < legendNameList.size() && i < explodePercentList.size(); i++) {piePlot.setExplodePercent(legendNameList.get(i), explodePercentList.get(i));}}return chart;}/*** 生成饼图(返回byte[])** @param chartTitle         图表标题* @param legendNameList     图例名称列表* @param dataList           数据列表* @param width              宽度* @param height             高度* @param theme              主题(null代表默认主题)* @param legendColorList    图例背景颜色列表(为空,使用默认背景颜色)* @param explodePercentList 偏离百分比数据* @return*/public static byte[] createPieChart(String chartTitle, List<String> legendNameList, List<Object> dataList, int width, int height, StandardChartTheme theme, List<Color> legendColorList, List<Double> explodePercentList) {ByteArrayOutputStream bas = new ByteArrayOutputStream();createPieChart(bas, chartTitle, legendNameList, dataList, width, height, theme, legendColorList, explodePercentList);byte[] byteArray = bas.toByteArray();return byteArray;}/*** 生成饼图(返回outputStream)** @param outputStream       输出流* @param chartTitle         图表标题* @param legendNameList     图例名称列表* @param dataList           数据列表* @param width              宽度* @param height             高度* @param theme              主题(null代表默认主题)* @param legendColorList    图例背景颜色列表(为空,使用默认背景颜色)* @param explodePercentList 偏离百分比数据* @return*/public static void createPieChart(OutputStream outputStream, String chartTitle, List<String> legendNameList, List<Object> dataList, int width, int height, StandardChartTheme theme, List<Color> legendColorList, List<Double> explodePercentList) {JFreeChart chart = createPieChart(chartTitle, legendNameList, dataList, theme, legendColorList, explodePercentList);try {ChartUtils.writeChartAsJPEG(outputStream, 1.0f, chart, width, height, null);} catch (IOException e) {e.printStackTrace();}}/*** 生成3D饼图(返回JFreeChart)** @param chartTitle      图表标题* @param legendNameList  图例名称列表* @param dataList        数据列表* @param theme           主题(null代表默认主题)* @param alpha           0.5F为半透明,1为不透明,0为全透明* @param legendColorList 图例背景颜色列表(为空,使用默认背景颜色)* @return*/public static JFreeChart create3DPieChart(String chartTitle, List<String> legendNameList, List<Object> dataList, StandardChartTheme theme, float alpha, List<Color> legendColorList) {//设置主题,防止中文乱码theme = theme == null ? JFreeChartUtil.createChartTheme("") : theme;ChartFactory.setChartTheme(theme);//创建饼图JFreeChart chart = ChartFactory.createPieChart3D(chartTitle, JFreeChartUtil.createDefaultPieDataset(legendNameList, dataList), true, true, true);// 设置抗锯齿,防止字体显示不清楚chart.setTextAntiAlias(false);// 设置标注无边框chart.getLegend().setFrame(new BlockBorder(Color.WHITE));// 标注位于右侧chart.getLegend().setPosition(RectangleEdge.RIGHT);PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot();//设置方向为”顺时针方向“pieplot3d.setDirection(Rotation.CLOCKWISE);//设置透明度,0.5F为半透明,1为不透明,0为全透明pieplot3d.setForegroundAlpha(alpha);//边框线为白色pieplot3d.setOutlinePaint(Color.white);//连接线类型为直线pieplot3d.setLabelLinkStyle(PieLabelLinkStyle.QUAD_CURVE);//设置图例背景颜色(饼图)if (CollectionUtil.isNotEmpty(legendColorList)) {for (int i = 0; i < legendNameList.size() && i < legendColorList.size(); i++) {pieplot3d.setSectionPaint(legendNameList.get(i), legendColorList.get(i));}}// 对饼图进行渲染JFreeChartUtil.setPieRender(chart.getPlot());return chart;}/*** 生成3D饼图(返回byte[])** @param chartTitle      图表标题* @param legendNameList  图例名称列表* @param dataList        数据列表* @param width           宽度* @param height          高度* @param theme           主题(null代表默认主题)* @param alpha           0.5F为半透明,1为不透明,0为全透明* @param legendColorList 图例背景颜色列表(为空,使用默认背景颜色)* @return*/public static byte[] create3DPieChart(String chartTitle, List<String> legendNameList, List<Object> dataList, int width, int height, StandardChartTheme theme, float alpha, List<Color> legendColorList) {ByteArrayOutputStream bas = new ByteArrayOutputStream();create3DPieChart(bas, chartTitle, legendNameList, dataList, width, height, theme, alpha, legendColorList);byte[] byteArray = bas.toByteArray();return byteArray;}/*** 生成3D饼图(返回outputStream)** @param outputStream    输出流* @param chartTitle      图表标题* @param legendNameList  图例名称列表* @param dataList        数据列表* @param width           宽度* @param height          高度* @param theme           主题(null代表默认主题)* @param alpha           0.5F为半透明,1为不透明,0为全透明* @param legendColorList 图例背景颜色列表(为空,使用默认背景颜色)* @return*/public static void create3DPieChart(OutputStream outputStream, String chartTitle, List<String> legendNameList, List<Object> dataList, int width, int height, StandardChartTheme theme, float alpha, List<Color> legendColorList) {JFreeChart chart = create3DPieChart(chartTitle, legendNameList, dataList, theme, alpha, legendColorList);try {ChartUtils.writeChartAsJPEG(outputStream, 1.0f, chart, width, height, null);} catch (IOException e) {e.printStackTrace();}}
}

3 标准饼图

3.1 调试代码

    /*** 饼图** @param response*/@GetMapping("/pieChart")public void pieChart(HttpServletResponse response) throws IOException {//图例名称列表List<String> legendNameList = new ArrayList<>(Arrays.asList("一级", "二级", "三级", "四级", "五级"));//数据列表List<Object> dataList = new ArrayList<>(Arrays.asList(1, 3, 5, 6, 2));//图例背景颜色列表List<Color> legendColorList = new ArrayList<>(Arrays.asList(Color.YELLOW, Color.GRAY, Color.green, Color.cyan, Color.ORANGE));//偏离百分比数据List<Double> explodePercentList = new ArrayList<>(Arrays.asList(0.1, 0.1, 0.1, 0.1, 0.1));GeneratePieChartUtil.createPieChart(response.getOutputStream(), "各级占比情况", legendNameList, dataList, 300, 400, JFreeChartUtil.createChartTheme("宋体"), legendColorList, explodePercentList);}

3.2 调试结果

4 3D饼图

4.1 调试代码

    /*** 3D饼图** @param response*/@GetMapping("/pie3DChart")public void pie3DChart(HttpServletResponse response) throws IOException {//图例名称列表List<String> legendNameList = new ArrayList<>(Arrays.asList("一级", "二级", "三级", "四级", "五级"));//数据列表List<Object> dataList = new ArrayList<>(Arrays.asList(1, 3, 5, 6, 2));//图例背景颜色列表List<Color> legendColorList = new ArrayList<>(Arrays.asList(Color.YELLOW, Color.GRAY, Color.green, Color.cyan, Color.ORANGE));GeneratePieChartUtil.create3DPieChart(response.getOutputStream(), "各级占比情况", legendNameList, dataList, 300, 400, JFreeChartUtil.createChartTheme("宋体"), 1f, legendColorList);}

4.2 调试结果

注:

有关JFreeChartUtil的源码,请查看以下博客。

JFreeChart 生成5种图表(饼图、柱状图、堆叠柱状图、折线图、散点图)_旭东怪的博客-CSDN博客1 Maven依赖 <!-- Hutool工具包 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.6.2</version> </dhttps://blog.csdn.net/qq_38974638/article/details/118704969

JFreeChart 生成饼图(标准饼图、3D饼图)相关推荐

  1. JFreeChart 生成5种图表(饼图、柱状图、堆叠柱状图、折线图、散点图)

    1 Maven依赖 <!-- Hutool工具包 --> <dependency><groupId>cn.hutool</groupId><art ...

  2. JFreeChart(六)之3D饼图/条形图

    转载自   JFreeChart 3D饼图/条形图 三维/3D图表是那些显示在一个三维格式.可以使用这些图表来提供更好的显示效果和清晰的信息.三维/3D饼图是饼图另外一个不错的3D效果. 3D效果可以 ...

  3. Java通过JfreeChart生成转Base64图片字符串(饼图、折线图、柱状图、折线图-多条、3D柱状图、气泡图、时序图、曲线图、区域图、分布图、联合分类图、双X轴图、K线图、柱状图-横向等图)

    工具类 CreatLineChart.java package DrawLine;import java.awt.BasicStroke; import java.awt.Color; import ...

  4. java jfreechart 饼图_JFreeChart 使用一 饼图之高级特性

    原文链接:http://www.cnblogs.com/jtmjx/archive/2013/04/23/jfreechart_advantage.html 本文主要讲解JFreeChart中饼图的一 ...

  5. 使用jfreechart来创建一个简单的饼图

    使用jfreechart来创建一个简单的饼图 代码如下 package com.jfreechart;import java.io.*; import org.jfree.data.general.D ...

  6. android自定义3d饼图,Android使用j4lChartAndroid插件绘制3D饼图

    图表是常见的直观表示数据的途径,目前在android手机上绘制图表基本有两种方法:一是利用java的canvas自己绘制,这种方法自己可操作性强,可以随心所欲地绘制,但是缺点就是工作量大:二是利用第三 ...

  7. echarts 3d饼图

    echarts 3d饼图 效果图: 第一步: 在main.js引入echarts import * as echarts from 'echarts' Vue.prototype.$echarts = ...

  8. vue 使用echarts实现3D饼图和环形图

    记录一下echarts实现3d饼图和环形图功能## 标题 实现效果 首先第一步安装echarts和echarts-gl npm install echarts echarts-gl安装最新版本可能会有 ...

  9. eacharts和eacharts-gl、3d饼图、3d柱状图加折线图、下载gl报错解决

    eacharts-gl下载时版本一定要和eacharts版本对应.否则不出效果!!目前已知可以生效有: 第一种1: npm install echarts@5.1.2 --save npm insta ...

最新文章

  1. css多行超出显示点_CSS实现单行、多行文本溢出显示省略号(…)
  2. 【2018年更新】Sublime text 3安装教程(Linux版本--Ubuntu)
  3. java语言和www技术 阶段性测试一_2018春季【贵州电大】[JAVA语言与WWW技术(省)]04任务阶段性测验(答案)...
  4. vue-cli3 环境设置
  5. URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)
  6. windows7 + vs2008 + oracle + iis7 客户端配置成功
  7. MyBatis笔记——配置文件完成增删改查
  8. 【转】CT层厚、层间距、层间隔的概念是什么,MRI的层厚、层间距、曾间隔是什么
  9. 3dmax批量导入obj_ArcGIS 与 3DMax 结合建模
  10. python200行代码_python代码统计200行
  11. 解决使用redis作为session缓存 报错 Error: no such key 的问题
  12. 2021蓝桥杯C/C++B组真题
  13. HDLBits 答案之Exams/ece241 2014 q7b
  14. mac vim映射esc_如何通过重新映射大写锁定来获取Mac的Esc键
  15. Mendix批量发送邮件给多人待办事项提醒
  16. 我的世界服务器启动显示非正常,大佬们,HMCL启动提示非正常退出,请帮我看看怎么回事。...
  17. windows 安装python3 Eclipse 配置python插件
  18. TexturePacker使用详解
  19. Hexo博客备份方案
  20. HTC Lengend G6上网-彩信设置

热门文章

  1. python|tkinter实现颜色选择器
  2. java语言程序设计教程课后题答案魏永红_《Java语言程序设计基础教程》习题解答...
  3. 小工具分享----Java简单的文字加密解密
  4. 详解prototype、__proto__和constructor
  5. Lifetime Improvement of NAND Flash-based Storage Systems Using Dynamic Program and Erase Scaling
  6. 24|虚实结合:虚拟内存和物理内存
  7. Godot官网新闻翻译 - 2014年
  8. 魔兽争霸之最后的反击
  9. 联想台式机进入BIOS系统,设置u盘启动
  10. 软件工程 实践者的研究方法 第19章答案