ireport入門教程之餅圖製作過程,本文介紹ireport中餅圖的具體實現方法
工具/原料
ireport
方法/步驟
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可以查看預覽生成結果如下:
三.為餅圖添加百分比顯示
在餅圖屬性裡面:
填寫如下:
{0}{2}
說明:{0}為名稱 {1}為數量 {2}為百分比
在次預覽如圖:
四.PDF預覽中文問題
4.0.2中pdf預覽中文依然存在問題,默認對靜態文本將不會顯示中文,依舊悲劇.
可以採用原來的辦法,既加入itext-asian.jar到classpath中,然後修改 pdf font name 和pdf encoding的辦法來顯示中文,但這種做法ireport已經拋棄,這裡採用另一種做法.
下載truetype字體,我下載的是微軟雅黑字體:
選擇[工具]->[選項]->[ireport]下font卡牌欄,install font,然後選擇剛下載的字體文件,下一步如圖:
然後完成.這時候直接預覽pdf的話還是悲劇,還要做最後一步修改.
在static text的屬性裡面修改
Font name為雅黑字體.然後預覽:
設計好報表後,把*.jasper文件拷貝到工程中,可以開始編碼了,但是上一篇寫的只是設計器中預覽出現中文,而程序生成的PDF報表依舊不能顯示中文,這篇將會做個補充.
先填代碼吧:
Java代碼
1. package cn.com.gkhb.ireport.demo;
2.
3. import java.sql.Connection;
4. import java.sql.DriverManager;
5. import java.sql.SQLException;
6. import java.util.HashMap;
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.engine.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/pie_chart_demo.pdf";
28.
29. /**
30. * register database driver for mysql
31. */
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 HashMap();
43. try {
44. // Fill the report using an empty data source
45. JasperPrint print = JasperFillManager.fillReport(jasperfile, hm, connectionProvider());
46.
47. // Create a PDF exporter
48. JRExporter exporter = new JRPdfExporter();
49. // JRExporter exporter = new JRHtmlExporter();
50.
51. // Configure the exporter (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 file
56. exporter.exportReport();
57. } catch (JRException e) {
58. e.printStackTrace();
59. System.exit(1);
60. } catch (Exception e) {
61. e.printStackTrace();
62. System.exit(1);
63. }
64. }
65.
66. /**
67. * database connection provider
68. * @return
69. */
70. protected static Connection connectionProvider() {
71. Connection connection = null;
72. String url = "jdbc:mysql://localhost:3306/test";
73. String user = "root";
74. String password = "";
75. try {
76. connection = DriverManager.getConnection(url, user, password);
77. } catch (SQLException ex) {
78. ex.printStackTrace();
79. }
80. return connection;
81. }
82.
83. /*
84. * jpeg
85. JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters_header, dataSource);
86. out = response.getOutputStream();
87. JasperExportManager.exportReportToPdfStream(jasperPrint, out);
88.
89. if (printType==0) //if pdf stream
90. {
91. JasperExportManager.exportReportToPdfStream(jasperPrint, out);
92. } else { // if jpeg image stream
93. int pageIndex = 0;
94. BufferedImage pageImage = new BufferedImage(jasperPrint.getPageWidth() + 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());
98. 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安裝目錄下,然後進入:\ireport\fonts文件夾,用將文件夾下所有內容直接打包成zip,修改後綴為jar,加入到項目classpath就OK了,此時應該就能生成pdf中有中文顯示了
同為比較常用的java報表生成工具,FineReport對餅圖的解決方案和實現方法與ireport有所不同,具體實現思路和步驟如下,供ireport使用者借鑑:
複合餅圖和複合條餅圖
1. 問題描述
當餅圖中的多個數據點均小於餅圖的10%時,便很難辨別扇區,怎樣使小扇區在餅圖中更清晰可見,這時就可以使用第二繪圖區製作複合餅圖或者複合條餅圖來解決,如下圖效果:
2. 實現原理
複合餅圖和複合條餅圖將小扇區從主餅圖中分離出來,放在第二繪圖區,可設置為餅圖或堆積條形圖,百分比的大小還可手動設置。
3. 示例
3.1 打開報表
打開報表%FR_HOME%\WebReport\WEB-INF\reportlets\doc\Advanced\Chart\3Darea.cpt。
3.2 設置百分比數據模板
複合餅圖和複合條餅圖只支持百分比的系列分割方式,因此我們計算每個地區的應付金額所佔比例是多少,隱藏B列,在C1中輸入佔比,C2中輸入公式:=B2/SUM(B2[!0]),B2[!0]是B2擴展出來所以單元格,根據情況設置單元格樣式等操作。
3.3 複合餅圖
下面以製作複合餅圖為例,選中圖表,勾選圖表屬性表-樣式>系列中的第二繪圖區,將第二繪圖區小於百分比的數據改為10,默認為5,表示第二繪圖區包含所有小於該百分比的值為,如下圖:
注:第二繪圖區內容可選擇餅狀和條形,選擇餅狀即複合餅圖,選擇條形即複合條形圖。
根據情況可設置系列標籤樣式,勾選圖表屬性表-樣式>系列中的標籤,選中值,設置格式,百分比保留兩位小數。
3.4 修改圖表數據
修改圖例項(系列)值為C2單元格,複合餅圖則可設置完成。
3.5 預覽與保存
保存模板,點擊分頁預覽,即可看到效果。