1.maven的pom.xml引入相关依赖

  <properties><poi-version>3.17</poi-version></properties><dependencies><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>${poi-version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi-version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>${poi-version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.1</version></dependency><!-- https://mvnrepository.com/artifact/jfree/jcommon --><dependency><groupId>jfree</groupId><artifactId>jcommon</artifactId><version>1.0.16</version></dependency><!-- https://mvnrepository.com/artifact/jfree/jfreechart --><dependency><groupId>jfree</groupId><artifactId>jfreechart</artifactId><version>1.0.13</version></dependency></dependencies>

2.Demo


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.IntervalBarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.*;public class JfreeChartUtils {public static void main(String[] args) throws Exception {// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)HSSFWorkbook wb = new HSSFWorkbook();//创建工作表HSSFSheet sheet = wb.createSheet("Sheet 1");//如果不使用Font,中文将显示不出来Font font = new Font("新宋体", Font.BOLD, 15);HSSFPatriarch patriarch = sheet.createDrawingPatriarch();//1.创建饼图数据List<NameValueVO> datas1 = new ArrayList<>();datas1.add(new NameValueVO("录播主机",(double) 1000));datas1.add(new NameValueVO("编码器",(double) 1000));datas1.add(new NameValueVO("流转码服务器",(double) 1000));datas1.add(new NameValueVO("设备网络掉线4",(double) 1000));datas1.add(new NameValueVO("流转码服务器1",(double) 1000));datas1.add(new NameValueVO("流转码服务器2",(double) 1000));datas1.add(new NameValueVO("流转码服务器3",(double) 1000));datas1.add(new NameValueVO("流转码服务器4",(double) 1000));datas1.add(new NameValueVO("流转码服务器5",(double) 1000));//2.创建棒图数据List<NameValueVO> datas2 = new ArrayList<>();datas2.add(new NameValueVO("设备掉线1",(double) 1000));datas2.add(new NameValueVO("设备掉线2",(double) 1000));datas2.add(new NameValueVO("设备掉线3",(double) 1000));datas2.add(new NameValueVO("设备掉线4",(double) 1000));datas2.add(new NameValueVO("设备掉线5",(double) 1000));datas2.add(new NameValueVO("设备掉线6",(double) 1000));datas2.add(new NameValueVO("设备掉线7",(double) 1000));datas2.add(new NameValueVO("设备掉线8",(double) 1000));datas2.add(new NameValueVO("设备掉线9",(double) 1000));datas2.add(new NameValueVO("设备掉线10",(double) 1000));//3.画饼图testPieChart(wb,patriarch,font,datas1);//4.画棒图testBarChart(wb,patriarch,font,datas2);//5.折线图testLineChart(wb,patriarch,font);File file =new File("D://excel-demo/1.xlsx");if(file.exists()){file.delete();}FileOutputStream fileOut = new FileOutputStream(new File("D://excel-demo/1.xlsx"));wb.write(fileOut);fileOut.close();}/*** 创建饼图* @throws Exception*/public static void testPieChart(HSSFWorkbook wb,HSSFPatriarch patriarch,Font font,List<NameValueVO> datas){try {//1.指定图片位置JFreeChart chart = createPieChart("故障设备类型比例", datas, font);//读取chart信息至字节输出流ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();ChartUtilities.writeChartAsPNG(byteArrayOut, chart, 1250, 680);//anchor主要用于设置图片的属性,指定图片位置HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 2, (short) 1, (short) 19, (short) 40);anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);//2.插入图片patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));}catch (Exception e){e.printStackTrace();}}/*** 创建棒图* @throws Exception*/public static void testBarChart(HSSFWorkbook wb,HSSFPatriarch patriarch,Font font,List<NameValueVO> datas){try {//1.构建JFreeChartJFreeChart chart = createBarChart("故障类型比例",datas,"告警数量(个)",font);//读取chart信息至字节输出流ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();ChartUtilities.writeChartAsPNG(byteArrayOut, chart, 1250, 680);//anchor主要用于设置图片的属性,指定图片位置HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 2, (short) 45, (short) 19, (short) 84);anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);//2.插入图片patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));}catch (Exception e){e.printStackTrace();}}/*** 创建折线图* @param wb* @param patriarch* @param font*/public static void testLineChart(HSSFWorkbook wb, HSSFPatriarch patriarch, Font font){try {String[] rowKeys = {"A平台","B平台"};String[] colKeys = {"0:00", "1:00", "2:00", "7:00", "8:00", "9:00","10:00", "11:00", "12:00", "13:00", "16:00", "20:00", "21:00","23:00"};double[][] data = {{4, 3, 1, 1, 1, 1, 2, 2, 2, 1, 8, 2, 1, 1},{6, 3, 8, 1, 1, 19, 2, 2, 5, 1, 8, 12, 1, 51}};String categoryAxisLabel = "时间";String valueAxisLabel = "告警数量(个)";CategoryDataset categoryDataset = DatasetUtilities.createCategoryDataset(rowKeys,colKeys, data);//1.指定图片位置JFreeChart chart = createLineChart("折线图",categoryAxisLabel,valueAxisLabel,font,categoryDataset);//读取chart信息至字节输出流ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();ChartUtilities.writeChartAsPNG(byteArrayOut, chart, 1250, 680);//anchor主要用于设置图片的属性,指定图片位置HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 2, (short) 99, (short) 19, (short) 128);anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);//插入图片patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));}catch (Exception e){e.printStackTrace();}}/*** JFreeChart-棒图* @param title* @param datas* @param type* @param font* @return*/public static JFreeChart createBarChart(String title, List<NameValueVO> datas, String type,Font font){try {Map<String, String> dataMap = tranBeanToMap(datas);DefaultCategoryDataset ds = new DefaultCategoryDataset();dataMap.forEach((key,value)->{ds.setValue(Double.parseDouble(value.toString()), "", key.toString());});//创建柱状图,柱状图分水平显示和垂直显示两种JFreeChart chart = ChartFactory.createBarChart(title, null, type, ds, PlotOrientation.VERTICAL, true, true, true);//设置整个图片的标题字体chart.getTitle().setFont(new Font("新宋体", Font.BOLD, 12));//设置提示条字体chart.getLegend().setItemFont(font);//得到绘图区CategoryPlot plot = (CategoryPlot) chart.getPlot();// 自定义柱状图中柱子的样式CustomRender brender = new CustomRender();brender.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());brender.setBaseItemLabelsVisible(true);brender.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));brender.setShadowVisible(false);brender.setIncludeBaseInRange(true);brender.setBarPainter(new StandardBarPainter());brender.setMaximumBarWidth(0.05);brender.setMinimumBarLength(0.05);plot.setRenderer(brender);//得到绘图区的域轴(横轴),设置标签的字体plot.getDomainAxis().setLabelFont(font);//设置横轴标签项字体plot.getDomainAxis().setTickLabelFont(font);//设置范围轴(纵轴)字体plot.getRangeAxis().setLabelFont(font);plot.setForegroundAlpha(1.0f);//设置plot的背景色透明度plot.setBackgroundAlpha(0.0f);return chart;} catch (Exception e) {e.printStackTrace();return null;}}/*** JFreeChart-饼图* @param title* @param datas* @param font* @return*/public static JFreeChart createPieChart(String title, List<NameValueVO> datas, Font font) {try {Map<String, String> dataMap = tranBeanToMap(datas);Set<Map.Entry<String, String>> set = dataMap.entrySet();DefaultPieDataset pds = new DefaultPieDataset();Iterator iterator = set.iterator();Map.Entry entry;while (iterator.hasNext()) {entry = (Map.Entry) iterator.next();pds.setValue(entry.getKey().toString(), Double.parseDouble(entry.getValue().toString()));}//生成一个饼图的图表:显示图表的标题、组装的数据、是否显示图例、是否生成贴士以及是否生成URL链接JFreeChart chart = ChartFactory.createPieChart(title, pds, true, false, true);// 设置图片标题的字体chart.getTitle().setFont(font);// 得到图块,准备设置标签的字体PiePlot plot = (PiePlot) chart.getPlot();// 设置标签字体plot.setLabelFont(font);// 设置图例项目字体chart.getLegend().setItemFont(font);//设置plot的前景色透明度plot.setForegroundAlpha(0.7f);//设置plot的背景色透明度plot.setBackgroundAlpha(0.0f);//设置标签生成器(默认{0})//{0}:key {1}:value {2}:百分比 {3}:sumplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}{1}({2})"));  // 一般在{1}后面加单位,如:{0}({1}次)/{2}return chart;} catch (Exception e) {e.printStackTrace();return null;}}/***JFreeChart-折线图* @param categoryDataset 数据集* @param title 标题* @param categoryAxisLabel x轴名称* @param valueAxisLabel y轴名称* @param font** @return*/public static JFreeChart createLineChart(String title,String categoryAxisLabel, String valueAxisLabel,Font font,CategoryDataset categoryDataset) {try {JFreeChart chart = ChartFactory.createLineChart(title,categoryAxisLabel,valueAxisLabel,categoryDataset, PlotOrientation.VERTICAL, true, false, false);//设置整个图片的标题字体chart.getTitle().setFont(font);//设置提示条字体chart.getLegend().setItemFont(font);CategoryPlot plot = (CategoryPlot)chart.getPlot();LineAndShapeRenderer renderer = new LineAndShapeRenderer();renderer.setBaseShapesVisible(true); // series 点(即数据点)可见renderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见renderer.setUseSeriesOffset(true); // 设置偏移量renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer.setBaseItemLabelsVisible(true);plot.setRenderer(renderer);//得到绘图区的域轴(横轴),设置标签的字体plot.getDomainAxis().setLabelFont(font);//设置横轴标签项字体plot.getDomainAxis().setTickLabelFont(font);//设置范围轴(纵轴)字体plot.getRangeAxis().setLabelFont(font);plot.setForegroundAlpha(1.0f);//设置plot的背景色透明度plot.setBackgroundAlpha(0.0f);return chart;}catch (Exception e){e.printStackTrace();return null;}}/*** 构建折线图数据* @param rowKeys* @param colKeys* @param data* @return*/public static CategoryDataset createCategoryDataset(String[] rowKeys,String[] colKeys, double[][] data){return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data);}/*** 数据机构转换,NameValueVO转map* @param data* @return*/public static Map<String, String> tranBeanToMap(List<NameValueVO> data){if(CollectionUtils.isEmpty(data)){return null;}Map<String, String> map = new LinkedHashMap<>();for (NameValueVO nameValueVO : data){map.put(nameValueVO.getName().toString(), nameValueVO.getValue().toString());}return map;}static class CustomRender extends IntervalBarRenderer {private Paint[] colors;//初始化柱子颜色private String[] colorValues = {"#FF0000", "#0070C0", "#00AF4E", "#7030A0"};public CustomRender() {colors = new Paint[colorValues.length];for (int i = 0; i < colorValues.length; i++) {colors[i] = Color.decode(colorValues[i]);}}//每根柱子以初始化的颜色不断轮循public Paint getItemPaint(int i, int j) {return colors[j % colors.length];}}@Data@NoArgsConstructor@AllArgsConstructorpublic class NameValueVO<T> {private String name;private T value;}
}

JFreeChart导出excel图表工具类相关推荐

  1. mykit-excel之——这是我用过的最好用的导出Excel的工具类

    mykit-excel的github链接地址为:https://github.com/sunshinelyz/mykit-excel  欢迎各位Star和Fork源码,也欢迎大家pr你牛逼哄哄的代码. ...

  2. 导出EXCEL常用工具类

    1.接口 package com.yihaodian.pis.common.util.export;import java.io.OutputStream; import java.util.List ...

  3. java导出excel报表工具类

    自己写的导出excel报表的公共方法,该公共方法主要用于非横向流动性报表导出 1.创立excel基本初始步骤 XSSFWorkbook wb = new XSSFWorkbook();XSSFCell ...

  4. Python导出Excel图表

    Python自动化办公的过程,部分涉及到导出Excel图表:本篇主要讲下使用python代码将excel中的图表导出为图片的开发过程: Python  版本: C:\Users>python P ...

  5. [转载] Python导出Excel图表

    参考链接: Python | 使用openpyxl模块在Excel工作表中绘制图表 1 Python自动化办公的过程,部分涉及到导出Excel图表:本篇主要讲下使用python代码将excel中的图表 ...

  6. 使用POI在Excel中动态生成图表工具类(支持柱状、组合、环状图、折线图、等常用图)

    使用POI在Excel中动态生成图表工具类 使用POI在Excel中动态生成图表工具类 由于公司是一个生成报表的机构,之前一直使用pageOffice,但是公司领导就是不买,你说公司那样有钱磨磨唧唧干 ...

  7. 由需求而产生的一款db导出excel的工具

    代码地址如下: http://www.demodashi.com/demo/12062.html 程序员最大的毛病可能就是懒,因为懒所以做出了许许多多提高自己工作效率的工具. 起因于我是商业开发,既然 ...

  8. 解决导出Excel报COM类工厂错误的办法--修改版

    解决导出Excel报COM类工厂错误的办法 1.问题现象 (1)第一种:销售模板的一些报表(如日报.月报等)导出时提示如下信息 (2)第二种:有些模块的一些报表导出时,没有反应,前后台也没报错 如成本 ...

  9. 【EasyExcel】在SpringBoot+VUE项目中引入EasyExcel实现对数据的导出(封装工具类)

    在SpringBoot+VUE项目中引入EasyExcel实现导入导出 一.引入EasyExcel 通过maven引入,坐标如下: <dependency><groupId>c ...

最新文章

  1. 再见了,收费的 Navicat!
  2. 怎样 获取 ios的系统版本
  3. Java关键字synchronized详解
  4. tomcat基本知识点与实例
  5. 如何在未来的大数据和机器学习领域,获得一份不错的工作?
  6. jmeter 加密解密_使用Jmeter对SHA1加密接口进行性能测试
  7. Turtlebot入门-配置网络
  8. eureka自我保护功能
  9. 计算机网络 —— 计算机网络的性能指标
  10. Map-Reduce原理
  11. http接口返回对象的方法
  12. 关于参数的写法规则,参数引用几种写法
  13. 钝化 会钝化 订单审批流程 码一会er
  14. 华为数通HCNA学习资料
  15. spark入门demo
  16. Keep in mind用法
  17. Python分析并绘制可视化动态地图,实时查询全球疫情数据(11月最新...)
  18. Tkinter教程之Label篇
  19. 面试中的SQL分析二
  20. 手把手教你在Linux上上搭建BitTorrent服务器

热门文章

  1. 计算机软件女生适合的工作,适合女生的长久职业_最有前途的十大职业(2)
  2. Android实现隐藏导航栏
  3. 为什么视频网站的视频链接地址是blob
  4. 盘点那些“越老越值钱”的职业,你知道几个?
  5. C#+WPF+MaterialDesign图片瀑布流 和 一键深色模式
  6. 王权富贵:使用终端向服务器传送文件
  7. 邮储银行新一代个人业务核心系统国际汇款业务上线,openGauss核心应用再创新高度
  8. 计算机毕业设计 SSM+Vue医药商城平台系统 购药商城平台 医药用品商城平台 中药店商城平台
  9. 湖南凤凰古城--美丽的湘西小城
  10. Unity App 接入IOS-Swift原生教程(注意事项)