itextpdf 添加折线图、饼图、柱状图

时隔三年,又要对PDF一顿改版了,此次改版加入了饼图、折线图、柱状图等图形可视化。

文章目录

  • itextpdf 添加折线图、饼图、柱状图
    • 依赖包
    • 效果图
    • 步骤
      • 生成折线图
      • 生成柱状图
      • 生成饼图
      • 添加到PDF中
    • 生成图表工具类

依赖包

  • itextpdf 5.5.6
  • jfreechart 1.5.3

效果图

  • 折线图
  • 柱状图
  • 饼图

步骤

  1. 生成图表
  2. 插入到PDF

生成折线图

主要代码 (完整代码文末贴出来):

public static JFreeChart lineChart(String title, String categoryAxisLabel, String valueAxisLabel, DefaultCategoryDataset dataset){ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createLineChart(title,categoryAxisLabel,valueAxisLabel,dataset,PlotOrientation.VERTICAL,true,true,false);CategoryPlot plot = chart.getCategoryPlot();LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();// 折现点显示数值renderer.setDefaultItemLabelsVisible(true);renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());// 更改线条颜色for (int i = 0; i < dataset.getRowKeys().size(); i++) {renderer.setSeriesPaint(i, LINE_COLORS[i]);}return chart;
}

生成柱状图

public static JFreeChart barChart(String title, String categoryAxisLabel, String valueAxisLabel, DefaultCategoryDataset dataset){ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createBarChart(title,categoryAxisLabel,valueAxisLabel,dataset,PlotOrientation.VERTICAL,true,true,false);CategoryPlot plot = chart.getCategoryPlot();BarRenderer renderer = (BarRenderer) plot.getRenderer();// 纯色显示renderer.setBarPainter(new StandardBarPainter());// 柱子上显示小数字renderer.setDefaultItemLabelsVisible(true);renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());// 设置柱子间隔renderer.setItemMargin(0.0);for (int i = 0; i < dataset.getRowKeys().size(); i++) {renderer.setSeriesPaint(i, BAR_COLORS[i]);}return chart;
}

生成饼图

public static JFreeChart pieChart(String title, DefaultPieDataset dataset){ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createPieChart(title,dataset,true,true,false);PiePlot plot = (PiePlot) chart.getPlot();for (int i = 0; i < dataset.getKeys().size(); i++) {plot.setSectionPaint(dataset.getKey(i), PIE_COLORS[i]);}// 设置扇区的线条颜色plot.setDefaultSectionOutlinePaint(new Color(255, 255, 255));// 设置扇区的线条大小plot.setDefaultSectionOutlineStroke(new BasicStroke(3));// 设置标签颜色plot.setLabelLinkPaint(new Color(255,255,255, 0));// 设置标签背景色plot.setLabelBackgroundPaint(new Color(255, 255, 255,0));// 设置标签线条颜色plot.setLabelOutlinePaint(new Color(255, 255, 255, 0));// 设置标签阴影颜色plot.setLabelShadowPaint(new Color(255, 255, 255, 0));// 设置饼图阴影颜色plot.setShadowPaint(new Color(255, 255, 255, 0));// 添加标签数字百分比显示plot.setLabelGenerator(new StandardPieSectionLabelGenerator(("{0}{2}"), NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));return chart;
}

添加到PDF中

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ChartUtils.writeChartAsJPEG(bos, ChartUtil.lineChart("月度应税销售收入对比图", "", "", dataset), 850, 440);Image image=Image.getInstance(bos.toByteArray());
image.scalePercent(60);
doc.add(image);

生成图表工具类

static class ChartUtil{private static final Color[] BAR_COLORS = new Color[]{new Color(79,129,189),new Color(192, 80, 77),new Color(155, 187, 89),};private static final Color[] LINE_COLORS = new Color[]{new Color(237, 123, 46),new Color(90, 154, 213),new Color(164, 164, 164),};private static final Color[] PIE_COLORS = new Color[]{new Color(75, 172, 198),new Color(128, 100, 162),new Color(155, 187, 89),new Color(192, 80, 77),new Color(79, 129, 189),new Color(44, 77, 117),new Color(247, 150, 70),new Color(165, 165, 165),};private static StandardChartTheme initChartTheme(){StandardChartTheme currentTheme = new StandardChartTheme("JFree");// 横轴纵轴标题文字大小currentTheme.setLargeFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 15));// 横轴纵轴数值文字大小currentTheme.setRegularFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 13));currentTheme.setExtraLargeFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 20));// 背景颜色currentTheme.setPlotBackgroundPaint(new Color(255, 255, 204, 0));// 边框线条currentTheme.setPlotOutlinePaint(new Color(0, 0, 0, 0));// 网格线条currentTheme.setRangeGridlinePaint(new Color(78, 74, 74));return currentTheme;}/*** 线图** @param title 标题* @param categoryAxisLabel 分类标签* @param valueAxisLabel 数值标签* @param dataset 数据集* @return org.jfree.chart.JFreeChart* @author Hou_fx* @date 2021.8.4 10:39*/public static JFreeChart lineChart(String title, String categoryAxisLabel, String valueAxisLabel, DefaultCategoryDataset dataset){ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createLineChart(title,categoryAxisLabel,valueAxisLabel,dataset,PlotOrientation.VERTICAL,true,true,false);CategoryPlot plot = chart.getCategoryPlot();LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();// 折现点显示数值renderer.setDefaultItemLabelsVisible(true);renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());// 更改线条颜色for (int i = 0; i < dataset.getRowKeys().size(); i++) {renderer.setSeriesPaint(i, LINE_COLORS[i]);}return chart;}/*** 柱状图** @param title* @param categoryAxisLabel* @param valueAxisLabel* @param dataset           数据集* @return org.jfree.chart.JFreeChart* @author Hou_fx* @date 2021.8.4 14:03*/public static JFreeChart barChart(String title, String categoryAxisLabel, String valueAxisLabel, DefaultCategoryDataset dataset){ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createBarChart(title,categoryAxisLabel,valueAxisLabel,dataset,PlotOrientation.VERTICAL,true,true,false);CategoryPlot plot = chart.getCategoryPlot();BarRenderer renderer = (BarRenderer) plot.getRenderer();// 纯色显示renderer.setBarPainter(new StandardBarPainter());// 柱子上显示小数字renderer.setDefaultItemLabelsVisible(true);renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());// 设置柱子间隔renderer.setItemMargin(0.0);// 设置柱子颜色for (int i = 0; i < dataset.getRowKeys().size(); i++) {renderer.setSeriesPaint(i, BAR_COLORS[i]);}return chart;}/*** 饼图** @param title* @param dataset* @return org.jfree.chart.JFreeChart* @author Hou_fx* @date 2021.8.4 14:04*/public static JFreeChart pieChart(String title, DefaultPieDataset dataset){ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createPieChart(title,dataset,true,true,false);PiePlot plot = (PiePlot) chart.getPlot();// 设置扇区颜色for (int i = 0; i < dataset.getKeys().size(); i++) {plot.setSectionPaint(dataset.getKey(i), PIE_COLORS[i]);}// 设置扇区的线条颜色plot.setDefaultSectionOutlinePaint(new Color(255, 255, 255));// 设置扇区的线条大小plot.setDefaultSectionOutlineStroke(new BasicStroke(3));// 设置标签颜色plot.setLabelLinkPaint(new Color(255,255,255, 0));// 设置标签背景色plot.setLabelBackgroundPaint(new Color(255, 255, 255,0));// 设置标签线条颜色plot.setLabelOutlinePaint(new Color(255, 255, 255, 0));// 设置标签阴影颜色plot.setLabelShadowPaint(new Color(255, 255, 255, 0));// 设置饼图阴影颜色plot.setShadowPaint(new Color(255, 255, 255, 0));// 添加标签数字百分比显示plot.setLabelGenerator(new StandardPieSectionLabelGenerator(("{0}{2}"), NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));return chart;}
}

itextpdf 添加折线图、饼图、柱状图相关推荐

  1. java + jfreechart + itextpdf创建折线图饼图并导出为pdf

    一.添加需要的maven依赖 <!--用于生成pdf--> <dependency><groupId>com.itextpdf</groupId>< ...

  2. python雷达和柱形图_Python Pygal常见数据图(折线图、柱状图、饼图、点图、仪表图和雷达图)详解...

    Pygal 同样支持各种不同的数据图,比如饼图.折线图等.Pygal 的设计很好,不管是创建哪种数据图,Pygal 的创建方式基本是一样的,都是先创建对应的数据图对象,然后添加数据,最后对数据图进行配 ...

  3. Vue中引入echarts的步骤,折线图、柱状图、饼图的常用配置项

    vue中引入echarts 一.安装echarts 二.引入echarts 1.全局引入 2.局部引入 三.vue中使用echarts 1.准备好函数 2.准备一个容器来放echarts 3.初始化容 ...

  4. excel分析图表制作,如何轻松创建折线图、柱状图、饼图

    ​在Excel中通过数据间的关系选择合适的图表,轻松创建折线图.柱状图.饼图使其表达的主题和内容更加简单清晰. 下面我们通过Smartbi大数据分析工具介绍excel分析图表制作方法,如何制作常用的图 ...

  5. 使用Telerik控件库制作WPF项目中的折线图、柱状图、饼图和甜甜圈图

    本博客是基于 .Net Framework 4.6.2 的WPF(MVVM)项目,Telerik版本为 2016.2.613.40.其他版本是否可用不详. 本文章所使用数据均为测试数据,无任何意义. ...

  6. Vue + Echarts(v5.版本)的简单组件封装(折线图、柱状图、散点图、饼/环形图、仪表盘、雷达图)

    项目中展示图表的地方很多,不想每次都写一长串的 options配置,就整合了一下常用的配置项,简单封装了一下,也能保证整个系统的图表风格统一,需要调整样式的时候也不用改很多地方 2022-11-07: ...

  7. Java使用Poi实现导出Word段落以及表格,XWPFParagraph和XWPFRun详解,生成目录,生成折线图、柱状图、饼状图

    导出段落 public void exportSummarizeWord(HttpServletResponse response, Integer id) {Summarize summarize ...

  8. python画柱状图和折线图-Python读取Excel表格,并同时画折线图和柱状图的方法

    今日给大家分享一个Python读取Excel表格,同时采用表格中的数值画图柱状图和折线图,这里只需要几行代码便可以实. 首先我们需要安装一个Excel操作的库xlrd,这个很简单,在安装Python后 ...

  9. python导入数据画折线图_Python读取Excel表格,并同时画折线图和柱状图的方法

    今日给大家分享一个Python读取Excel表格,同时采用表格中的数值画图柱状图和折线图,这里只需要几行代码便可以实. 首先我们需要安装一个Excel操作的库xlrd,这个很简单,在安装Python后 ...

最新文章

  1. UVA 10515 - Powers Et Al.(数论)
  2. (android之sqlite一)Sqlite介绍和sqlite工具介绍
  3. VC++下命名管道编程的原理及实现
  4. 引入spring-boot-starter-actuator,控制台没有mapper的映射信息打印问题
  5. 【算法】图论学习笔记与代码实现
  6. JAVA入门[22]—thymeleaf
  7. 事业单位考试题库计算机网络,2015年事业单位计算机基础知识试题及答案
  8. web安全day39:渗透测试方法论
  9. Android Builder模式
  10. html5仿ios底部菜单栏,仿苹果电脑任务栏菜单
  11. MathType的配置问题;将word中的公式转换为mathtype格式失败,缺少OMML2MML.XSL
  12. Ubuntu systemd配置文件/etc/systemd/system被删解决方案
  13. 【VBA】Speech.Speak 指定语音库
  14. 多线程?不用怕,大不了多学几遍 - 工具类
  15. 利用Fitbit,十秒入侵PC
  16. 嵌入式linux包含哪些内容
  17. Cesium 实景三维道路 注记 — 不迷茫、有安全感的三维注记系统
  18. 一个简单却富有生命力的故事——《谁动了我的奶酪》
  19. SpringBoot+Vue项目中实现登录验证码校验
  20. 一季度亏损,快手为何跌跌不休

热门文章

  1. 【第10篇】Python爬虫实战-手机归属地查询
  2. 【python】python制作暗黑2 建号器
  3. 共轭梯度法详细推导分析
  4. 联想Miix5安装Linux后WIFI设备无法识别问题
  5. 最强整理:Java开发究竟该如何学习,大厂面经合集
  6. 私有云:何去何从?解决方案有哪些
  7. ​LeetCode刷题实战584:寻找用户推荐人
  8. 广东省揭阳市谷歌卫星地图下载
  9. 带薪“看片”?继阿里后微信也公开招聘“鉴黄师”!
  10. 【java】HashMap底层实现原理及面试题