初教程之饼图制作过程

上传人:lcm****20 文档编号:47517200 上传时间:2018-07-02 格式:PDF 页数:16 大小:793.60KB
返回 下载 相关 举报
初教程之饼图制作过程_第1页
第1页 / 共16页
初教程之饼图制作过程_第2页
第2页 / 共16页
初教程之饼图制作过程_第3页
第3页 / 共16页
初教程之饼图制作过程_第4页
第4页 / 共16页
初教程之饼图制作过程_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《初教程之饼图制作过程》由会员分享,可在线阅读,更多相关《初教程之饼图制作过程(16页珍藏版)》请在金锄头文库上搜索。

1、ireport 5.5 初级教程之饼图制作过程ireport 5.5 初级教程之饼图制作过程Ireport 所需环境:ireport 4.0.2, mysql 5.1目标:饼图,百分比,PDF 中文一.DB 如图:数据源配置略.创建 SQL 语句:select count(id) as id,department from piechart group by department;二.创建饼图一路下一步到 key and values 的时候填写如下:然后完成.拖动改变下大小后点击 preview 可以查看预览生成结果如下:三.为饼图添加百分比显示在饼图属性里面:填写如下:02说明:0为名称

2、1为数量 2为百分比在次预览如图:四.PDF 预览中文问题4.0.2 中 pdf 预览中文依然存在问题,默认对静态文本将不会显示中文,依旧悲剧.可以采用原来的办法,既加入 itext-asian.jar 到 classpath 中,然后修改 pdf font name 和 pdf encoding 的办法来显示中文,但这种做法 ireport 已经抛弃,这里采用另一种做法.下载 truetype 字体,我下载的是微软雅黑字体:选择工具-选项-ireport下 font 卡牌栏,install font,然后选择刚下载的字体文件,下一步如图:然后完成.这时候直接预览 pdf 的话还是悲剧,还要做

3、最后一步修改.在 static text 的属性里面修改Font name 为雅黑字体.然后预览:设计好报表后,把*.jasper 文件拷贝到工程中,可以开始编码了,但是上一篇写的只是设计器中预览出现中文,而程序生成的 PDF 报表依旧不能显示中文,这篇将会做个补充.先填代码吧:Java 代码1. package .gkhb.ireport.demo;2.3. import java.sql.Connection;4. import java.sql.DriverManager;5. import java.sql.SQLException;6. import java.util.HashMa

4、p;7.8. import net.sf.jasperreports.engine.JRException;9. import net.sf.jasperreports.engine.JRExporter;10.import net.sf.jasperreports.engine.JRExporterParameter;11.import net.sf.jasperreports.engine.JasperFillManager;12.import net.sf.jasperreports.engine.JasperPrint;13.import net.sf.jasperreports.en

5、gine.export.JRPdfExporter;14.15./*16. * 导入报表为 pdf 文件17. */18.public class ExportDemo 19./*20.* 报表模板位置21.*/22.private final static String jasperfile = “E:/myeclipse/IreportDemo/WebRoot/WEB-INF/jasperfiles/pieChart.jasper“;23.24./*25.* 目标文件位置26.*/27.private final static String desc = “G:/download/font

6、/pie_chart_demo.pdf“;28.29./*30.* register database driver for mysql31.*/32.static 33.try 34.Class.forName(“com.mysql.jdbc.Driver“);35. catch (ClassNotFoundException ex) 36.ex.printStackTrace();37.38.39.40.SuppressWarnings(“unchecked“)41.public static void main(String args) 42.HashMap hm = new HashM

7、ap();43.try 44./ Fill the report using an empty data source45.JasperPrint print = JasperFillManager.fillReport(jasperfile, hm, connectionProvider();46.47./ Create a PDF exporter48.JRExporter exporter = new JRPdfExporter();49./JRExporter exporter = new JRHtmlExporter();50.51./ Configure the exporter

8、(set output file name and print object)52.exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, desc);53.exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);54.55./ Export the PDF file56.exporter.exportReport();57. catch (JRException e) 58.e.printStackTrace();59.System.exit(1);60. c

9、atch (Exception e) 61.e.printStackTrace();62.System.exit(1);63.64.65.66./*67.* database connection provider68.* return69.*/70.protected static Connection connectionProvider() 71.Connection connection = null;72.String url = “jdbc:mysql:/localhost:3306/test“;73.String user = “root“;74.String password

10、= “;75.try 76.connection = DriverManager.getConnection(url, user, password);77. catch (SQLException ex) 78.ex.printStackTrace();79.80.return connection;81.82.83./*84.* jpeg85.JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters_header, dataSource);86.out = response.getOutp

11、utStream();87.JasperExportManager.exportReportToPdfStream(jasperPrint, out);88.89.if (printType=0) /if pdf stream90.91.JasperExportManager.exportReportToPdfStream(jasperPrint, out);92. else / if jpeg image stream93.int pageIndex = 0;94.BufferedImage pageImage = new BufferedImage(jasperPrint.getPageW

12、idth() + 1, jasperPrint.getPageHeight() + 1, BufferedImage.TYPE_INT_RGB);95.JRGraphics2DExporter exporter = new JRGraphics2DExporter();96.exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);97.exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage.getGraphics();9

13、8.exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(pageIndex);99.exporter.exportReport();100.ImageIO.write(pageImage, “jpeg“, out);101.102.out.close();103.*/104.这个时候如果需要 PDF 生成后显示中文,需要把安装的字体打包成 JAR 导入classpath 中去,步骤如下:进入 ireport 安装目录下,然后进入:ireportfonts 文件夹,用将文件夹下所有内容直接打包成 zip,修改后缀为

14、jar,加入到项目 classpath 就 OK 了,此时应该就能生成 pdf 中有中文显示了同为比较常用的 java 报表生成工具,FineReport 对饼图的解决方案和实现方法与 ireport 有所不同, 具体实现思路和步骤如下, 供 ireport 使用者借鉴:复合饼图和复合条饼图复合饼图和复合条饼图1. 问题描述当饼图中的多个数据点均小于饼图的 10%时,便很难辨别扇区,怎样使小扇区在饼图中更清晰可见, 这时就可以使用第二绘图区制作复合饼图或者复合条饼图来解决,如下图效果:复合饼图复合饼图复合条饼图复合条饼图2. 实现原理复合饼图和复合条饼图将小扇区从主饼图中分离出来,放在第二绘图

15、区,可设置为饼图或堆积条形图,百分比的大小还可手动设置。3. 示例3.1 打开报表打开报表%FR_HOME%WebReportWEB-INFreportletsdocAdvancedChart3Darea.cpt。3.2 设置百分比数据模板复合饼图和复合条饼图只支持百分比的系列分割方式,因此我们计算每个地区的应付金额所占比例是多少,隐藏 B 列,在 C1 中输入占比,C2 中输入公式:=B2/SUM(B2!0),B2!0是 B2 扩展出来所以单元格,根据情况设置单元格样式等操作。3.3 复合饼图下面以制作复合饼图为例,选中图表,勾选图表属性表-样式系列中的第二绘图区,将第二绘图区小于百分比的数据改为 10,默认为 5,表示第二绘图区包含所有小于该百分比的值为,如下图:注:第二绘图区内容可选择饼状和条形,选择饼状即复合饼图,选择条形即复合条形图。根据情况可设置系列标签样式,勾选图表属性表-样式系列中的标签,选中值,设置格式,百分比保留两位小数。3.4 修改图表数据修改图例项(系列)值为 C2 单元格,复合饼图则可设置完成。3.5 预览与保存保存模板,点击分页预览,即可看到效果。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号