- 浏览: 144184 次
- 性别:
- 来自: 南昌
文章分类
- 全部博客 (123)
- Hibernate (10)
- Struts2 (6)
- Spring (5)
- Maven (11)
- Eclipse (5)
- Mysql (14)
- Html (3)
- IIS (4)
- Svn (2)
- Jquery (3)
- Apahce (1)
- Jsp (4)
- 其它 (10)
- ssh2 (2)
- java (14)
- js (8)
- Flex (4)
- barcode4j (1)
- powerdesiger (1)
- ibaits (2)
- googlemap (1)
- 淘宝客 (1)
- linux (7)
- idea (1)
- Query全屏滚动插件FullPage.js中文帮助文档API (1)
- 编写 (0)
最新评论
-
a276664990:
这玩意怎么使用?
googleMap.js -
xm_koma:
有没有将JFreeChart生成的图表导出到Excel中的例子 ...
在Web上用iText和JFreeChart将图形报表导出到PDF -
chong2230:
多谢 借用了
js复制当前链接 -
mikkjl:
非常感谢 按照你的做法 成功解决了问题 呵呵
struts2配置多个配置文件 -
yadsun:
推荐你用ActivePort,一个小工具300k不到,最重要的 ...
端口查看
最近项目中需要一个将生成的图形报表导出到PDF文件的功能。在图形上面本打算采用Flash进行展示,可是没有找到怎样直接将Flash导入PDF的方法,应该只有先生成图片才可以吧?(知道怎样做的大侠可以给个方法,先谢了。)于是改用JFreeChart来画图。生成PDF方面工具也很多,iText还比较成熟,于是选用它来生成PDF。
一开始本想图个省事,直接将网页转换成PDF文件。但是了n次后发现,这样的做法会导致图表严重变形,根本没法看,没办法,看来只能利用iText的API一点一点的做了。还好,iText的确够强大。就目前来看,完全可以处理项目的需求。下面就是我用iText结合JFreeChart在Web中将图表转换为PDF的方法。
本项目采用Struts2,分两个方法来处理,一个叫exportDeviceStat,调用它导出PDF,另一个叫getJFreeChart,调用它生成图形。下面直接上代码了:
/**
Java代码
1. * 将图表导出到PDF
2. * @return
3. */
4.public String exportDeviceStat() {
5. HttpServletResponse response = ServletActionContext.getResponse();
6.
7. String fileTitle = year + "年" + month + "月各办公区设备运行情况统计";
8.
9. response.setContentType("application/pdf;charset=utf-8");
10. response.addHeader("Content-Disposition", "attachment;filename=" + encodeFilename(fileTitle) + ".pdf");
11.
12. //获得办公区列表
13. List<Location> locationList = SystemCache.getLocationListFromCache();
14.
15. if (null != locationList && locationList.size() > 0) {
16.
17. //创建Document对象
18. Document document = new Document();
19.
20. try {
21. //创建PDF对象实例,直接输出到网页上
22. PdfWriter.getInstance(document, response.getOutputStream());
23.
24. //设置文档字体
25. BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
26. com.lowagie.text.Font fontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL, Color.BLACK);
27.
28. // 添加PDF文档的一些信息
29. document.addTitle(fileTitle);
30. document.addAuthor("设备运行情况统计");
31. document.addSubject("");
32. document.addKeywords("");
33. document.addCreator("设备运行情况统计");
34.
35. HeaderFooter footer = new HeaderFooter(new Phrase(" 第", fontChinese), new Phrase("页 ", fontChinese));
36. footer.setBorder(Rectangle.NO_BORDER);
37. footer.setAlignment(Element.ALIGN_RIGHT);
38. document.setFooter(footer);
39.
40. // 打开文档,将要写入内容
41. document.open();
42.
43. // 文档标题标题
44. document.add(new Paragraph(fileTitle, fontChinese));
45.
46. // 换行
47. document.add(Chunk.NEWLINE);
48.
49. // 插入表格
50. com.lowagie.text.Font f8 = new com.lowagie.text.Font(bfChinese, 8, com.lowagie.text.Font.BOLD);
51. com.lowagie.text.Font f12 = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.BOLD);
52.
53. //生成每个办公区的统计报表
54. for (Location location : locationList) {
55. //生成一个2列的表格
56. PdfPTable table = new PdfPTable(2);
57.
58. // 宽度定位100%
59. table.setWidthPercentage(100);
60. table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
61.
62. // 创建第一行
63. PdfPCell cell = new PdfPCell();
64. cell.setBorderColor(new Color(255, 255, 255));
65. cell.setBackgroundColor(new Color(24, 198, 248));
66. cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
67. cell.setColspan(2);
68. cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
69.
70. //第一行中显示的文字
71. cell.setPhrase(new Paragraph(location.getLocationName() + "办公区设备运行情况统计", f12));
72. table.addCell(cell);
73.
74. //插入统计图
75. PdfPCell newcell = new PdfPCell();
76.
77. newcell.setBorderColor(new Color(255, 255, 255));
78. //设备故障率统计,JFreeChart生成图表并返回
79. Image img = Image.getInstance("http://localhost:8080/DeviceStat/getJFreeChart.html?method=getDeviceNormalCount&locationId=" + location.getLocationId() + "&year=" + year + "&month=" + month);
80. img.setAbsolutePosition(0, 0);
81. newcell.setImage(img);
82. table.addCell(newcell);
83.
84. //故障类型统计,JFreeChart生成图表并返回
85. img = Image.getInstance("http://localhost:8080/DeviceStat/getJFreeChart.html?method=getDeviceAbnormalCount&locationId=" + location.getLocationId() + "&year=" + year + "&month=" + month);
86. img.setAbsolutePosition(0, 0);
87. newcell.setImage(img);
88. table.addCell(newcell);
89.
90.
91. document.add(table);
92. }
93.
94. } catch (DocumentException de) {
95. System.err.println(de.getMessage());
96. } catch (IOException ioe) {
97. System.err.println(ioe.getMessage());
98. } finally {
99. // 关闭打开的文档
100. document.close();
101. }
102. }
103. return NONE;
104.}
* 将图表导出到PDF
* @return
*/
public String exportDeviceStat() {
HttpServletResponse response = ServletActionContext.getResponse();
String fileTitle = year + "年" + month + "月各办公区设备运行情况统计";
response.setContentType("application/pdf;charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + encodeFilename(fileTitle) + ".pdf");
//获得办公区列表
List<Location> locationList = SystemCache.getLocationListFromCache();
if (null != locationList && locationList.size() > 0) {
//创建Document对象
Document document = new Document();
try {
//创建PDF对象实例,直接输出到网页上
PdfWriter.getInstance(document, response.getOutputStream());
//设置文档字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
com.lowagie.text.Font fontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL, Color.BLACK);
// 添加PDF文档的一些信息
document.addTitle(fileTitle);
document.addAuthor("设备运行情况统计");
document.addSubject("");
document.addKeywords("");
document.addCreator("设备运行情况统计");
HeaderFooter footer = new HeaderFooter(new Phrase(" 第", fontChinese), new Phrase("页 ", fontChinese));
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_RIGHT);
document.setFooter(footer);
// 打开文档,将要写入内容
document.open();
// 文档标题标题
document.add(new Paragraph(fileTitle, fontChinese));
// 换行
document.add(Chunk.NEWLINE);
// 插入表格
com.lowagie.text.Font f8 = new com.lowagie.text.Font(bfChinese, 8, com.lowagie.text.Font.BOLD);
com.lowagie.text.Font f12 = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.BOLD);
//生成每个办公区的统计报表
for (Location location : locationList) {
//生成一个2列的表格
PdfPTable table = new PdfPTable(2);
// 宽度定位100%
table.setWidthPercentage(100);
table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
// 创建第一行
PdfPCell cell = new PdfPCell();
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(24, 198, 248));
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setColspan(2);
cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
//第一行中显示的文字
cell.setPhrase(new Paragraph(location.getLocationName() + "办公区设备运行情况统计", f12));
table.addCell(cell);
//插入统计图
PdfPCell newcell = new PdfPCell();
newcell.setBorderColor(new Color(255, 255, 255));
//设备故障率统计,JFreeChart生成图表并返回
Image img = Image.getInstance("http://localhost:8080/DeviceStat/getJFreeChart.html?method=getDeviceNormalCount&locationId=" + location.getLocationId() + "&year=" + year + "&month=" + month);
img.setAbsolutePosition(0, 0);
newcell.setImage(img);
table.addCell(newcell);
//故障类型统计,JFreeChart生成图表并返回
img = Image.getInstance("http://localhost:8080/DeviceStat/getJFreeChart.html?method=getDeviceAbnormalCount&locationId=" + location.getLocationId() + "&year=" + year + "&month=" + month);
img.setAbsolutePosition(0, 0);
newcell.setImage(img);
table.addCell(newcell);
document.add(table);
}
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
} finally {
// 关闭打开的文档
document.close();
}
}
return NONE;
}
/**
Java代码
1. * 用JFreeChart生成统计图
2. * @return
3. */
4.public String getJFreeChart() {
5. HttpServletResponse response = ServletActionContext.getResponse();
6.
7. response.setContentType("image/jpeg;charset=utf-8");
8.
9. //获得办公区信息
10. Location location = SystemCache.getLocationByIdFromCache(locationId);
11.
12. if (null != location) {
13. DefaultPieDataset data = getDataSet(method, locationId, year, month);
14. JFreeChart chart = ChartFactory.createPieChart("getDeviceNormalCount".equals(method) ? "办公区设备运行情况统计" : "故障类型分布", data, true, false, false);
15.
16. chart.setBorderPaint(new Color(255,255,255));
17. chart.setBorderVisible(false);
18. chart.getTitle().setFont(new Font("宋体", Font.BOLD,12));
19. chart.getLegend().setItemFont(new Font("宋体", Font.BOLD,12));
20. PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
21. piePlot.setBackgroundPaint(new Color(255,255,255));
22. piePlot.setOutlineVisible(false); //无边框
23. piePlot.setNoDataMessage("暂无统计数据"); //设置无数据时的显示内容
24. piePlot.setLabelFont(new Font("宋体",Font.BOLD,12)); //饼图旁边的中文
25. //按百分比显示
26. piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(
27. "{0}:{2}", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")
28. ));
29.
30. try {
31. //输出图标
32. ChartUtilities.writeChartAsJPEG(response.getOutputStream(),
33. 1,chart,400,300,null);
34.
35. response.flushBuffer();
36. } catch (IOException e) {
37. // TODO Auto-generated catch block
38. e.printStackTrace();
39. } finally {
40.
41. }
42. }
43.
44. return NONE;
45.}
46.
47./**
48. * 获得JFreeChart需要的数据
49. * @param method
50. * @param locationId
51. * @param year
52. * @param month
53. * @return
54. */
55.private static DefaultPieDataset getDataSet(String method, Integer locationId, Integer year, Integer month) {
56. DefaultPieDataset dataset = new DefaultPieDataset();
57.
58. if ("getDeviceNormalCount".equals(method)) {
59. Integer normalCount = SystemCache.getDeviceStatNormalCountFromCache(locationId, year, month);
60. Integer abNormalCount = SystemCache.getDeviceStatAbnormalCountFromCache(locationId, year, month);
61.
62. if (null != normalCount && 0 != normalCount) {
63. dataset.setValue("设备正常率", normalCount);
64. }
65. if (null != abNormalCount && 0 != abNormalCount) {
66. dataset.setValue("设备故障率", abNormalCount);
67. }
68. } else {
69. List<DeviceBreakdownTypeCount> countList = SystemCache.getDeviceBreakdownTypeCountFromCache(locationId, year, month);
70. if (null != countList && countList.size() > 0) {
71. for (DeviceBreakdownTypeCount count : countList) {
72. dataset.setValue(Constant.DEVICE_BREAKDOWN_TYPE_DESC[count.getBreakdownType()], count.getBreakdownTypeCount());
73. }
74. }
75. }
76.
77. return dataset;
78.}
* 用JFreeChart生成统计图
* @return
*/
public String getJFreeChart() {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("image/jpeg;charset=utf-8");
//获得办公区信息
Location location = SystemCache.getLocationByIdFromCache(locationId);
if (null != location) {
DefaultPieDataset data = getDataSet(method, locationId, year, month);
JFreeChart chart = ChartFactory.createPieChart("getDeviceNormalCount".equals(method) ? "办公区设备运行情况统计" : "故障类型分布", data, true, false, false);
chart.setBorderPaint(new Color(255,255,255));
chart.setBorderVisible(false);
chart.getTitle().setFont(new Font("宋体", Font.BOLD,12));
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD,12));
PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
piePlot.setBackgroundPaint(new Color(255,255,255));
piePlot.setOutlineVisible(false); //无边框
piePlot.setNoDataMessage("暂无统计数据"); //设置无数据时的显示内容
piePlot.setLabelFont(new Font("宋体",Font.BOLD,12)); //饼图旁边的中文
//按百分比显示
piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}:{2}", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")
));
try {
//输出图标
ChartUtilities.writeChartAsJPEG(response.getOutputStream(),
1,chart,400,300,null);
response.flushBuffer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
return NONE;
}
/**
* 获得JFreeChart需要的数据
* @param method
* @param locationId
* @param year
* @param month
* @return
*/
private static DefaultPieDataset getDataSet(String method, Integer locationId, Integer year, Integer month) {
DefaultPieDataset dataset = new DefaultPieDataset();
if ("getDeviceNormalCount".equals(method)) {
Integer normalCount = SystemCache.getDeviceStatNormalCountFromCache(locationId, year, month);
Integer abNormalCount = SystemCache.getDeviceStatAbnormalCountFromCache(locationId, year, month);
if (null != normalCount && 0 != normalCount) {
dataset.setValue("设备正常率", normalCount);
}
if (null != abNormalCount && 0 != abNormalCount) {
dataset.setValue("设备故障率", abNormalCount);
}
} else {
List<DeviceBreakdownTypeCount> countList = SystemCache.getDeviceBreakdownTypeCountFromCache(locationId, year, month);
if (null != countList && countList.size() > 0) {
for (DeviceBreakdownTypeCount count : countList) {
dataset.setValue(Constant.DEVICE_BREAKDOWN_TYPE_DESC[count.getBreakdownType()], count.getBreakdownTypeCount());
}
}
}
return dataset;
}
最后运行的结果:
一开始本想图个省事,直接将网页转换成PDF文件。但是了n次后发现,这样的做法会导致图表严重变形,根本没法看,没办法,看来只能利用iText的API一点一点的做了。还好,iText的确够强大。就目前来看,完全可以处理项目的需求。下面就是我用iText结合JFreeChart在Web中将图表转换为PDF的方法。
本项目采用Struts2,分两个方法来处理,一个叫exportDeviceStat,调用它导出PDF,另一个叫getJFreeChart,调用它生成图形。下面直接上代码了:
/**
Java代码
1. * 将图表导出到PDF
2. * @return
3. */
4.public String exportDeviceStat() {
5. HttpServletResponse response = ServletActionContext.getResponse();
6.
7. String fileTitle = year + "年" + month + "月各办公区设备运行情况统计";
8.
9. response.setContentType("application/pdf;charset=utf-8");
10. response.addHeader("Content-Disposition", "attachment;filename=" + encodeFilename(fileTitle) + ".pdf");
11.
12. //获得办公区列表
13. List<Location> locationList = SystemCache.getLocationListFromCache();
14.
15. if (null != locationList && locationList.size() > 0) {
16.
17. //创建Document对象
18. Document document = new Document();
19.
20. try {
21. //创建PDF对象实例,直接输出到网页上
22. PdfWriter.getInstance(document, response.getOutputStream());
23.
24. //设置文档字体
25. BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
26. com.lowagie.text.Font fontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL, Color.BLACK);
27.
28. // 添加PDF文档的一些信息
29. document.addTitle(fileTitle);
30. document.addAuthor("设备运行情况统计");
31. document.addSubject("");
32. document.addKeywords("");
33. document.addCreator("设备运行情况统计");
34.
35. HeaderFooter footer = new HeaderFooter(new Phrase(" 第", fontChinese), new Phrase("页 ", fontChinese));
36. footer.setBorder(Rectangle.NO_BORDER);
37. footer.setAlignment(Element.ALIGN_RIGHT);
38. document.setFooter(footer);
39.
40. // 打开文档,将要写入内容
41. document.open();
42.
43. // 文档标题标题
44. document.add(new Paragraph(fileTitle, fontChinese));
45.
46. // 换行
47. document.add(Chunk.NEWLINE);
48.
49. // 插入表格
50. com.lowagie.text.Font f8 = new com.lowagie.text.Font(bfChinese, 8, com.lowagie.text.Font.BOLD);
51. com.lowagie.text.Font f12 = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.BOLD);
52.
53. //生成每个办公区的统计报表
54. for (Location location : locationList) {
55. //生成一个2列的表格
56. PdfPTable table = new PdfPTable(2);
57.
58. // 宽度定位100%
59. table.setWidthPercentage(100);
60. table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
61.
62. // 创建第一行
63. PdfPCell cell = new PdfPCell();
64. cell.setBorderColor(new Color(255, 255, 255));
65. cell.setBackgroundColor(new Color(24, 198, 248));
66. cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
67. cell.setColspan(2);
68. cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
69.
70. //第一行中显示的文字
71. cell.setPhrase(new Paragraph(location.getLocationName() + "办公区设备运行情况统计", f12));
72. table.addCell(cell);
73.
74. //插入统计图
75. PdfPCell newcell = new PdfPCell();
76.
77. newcell.setBorderColor(new Color(255, 255, 255));
78. //设备故障率统计,JFreeChart生成图表并返回
79. Image img = Image.getInstance("http://localhost:8080/DeviceStat/getJFreeChart.html?method=getDeviceNormalCount&locationId=" + location.getLocationId() + "&year=" + year + "&month=" + month);
80. img.setAbsolutePosition(0, 0);
81. newcell.setImage(img);
82. table.addCell(newcell);
83.
84. //故障类型统计,JFreeChart生成图表并返回
85. img = Image.getInstance("http://localhost:8080/DeviceStat/getJFreeChart.html?method=getDeviceAbnormalCount&locationId=" + location.getLocationId() + "&year=" + year + "&month=" + month);
86. img.setAbsolutePosition(0, 0);
87. newcell.setImage(img);
88. table.addCell(newcell);
89.
90.
91. document.add(table);
92. }
93.
94. } catch (DocumentException de) {
95. System.err.println(de.getMessage());
96. } catch (IOException ioe) {
97. System.err.println(ioe.getMessage());
98. } finally {
99. // 关闭打开的文档
100. document.close();
101. }
102. }
103. return NONE;
104.}
* 将图表导出到PDF
* @return
*/
public String exportDeviceStat() {
HttpServletResponse response = ServletActionContext.getResponse();
String fileTitle = year + "年" + month + "月各办公区设备运行情况统计";
response.setContentType("application/pdf;charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + encodeFilename(fileTitle) + ".pdf");
//获得办公区列表
List<Location> locationList = SystemCache.getLocationListFromCache();
if (null != locationList && locationList.size() > 0) {
//创建Document对象
Document document = new Document();
try {
//创建PDF对象实例,直接输出到网页上
PdfWriter.getInstance(document, response.getOutputStream());
//设置文档字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
com.lowagie.text.Font fontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL, Color.BLACK);
// 添加PDF文档的一些信息
document.addTitle(fileTitle);
document.addAuthor("设备运行情况统计");
document.addSubject("");
document.addKeywords("");
document.addCreator("设备运行情况统计");
HeaderFooter footer = new HeaderFooter(new Phrase(" 第", fontChinese), new Phrase("页 ", fontChinese));
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_RIGHT);
document.setFooter(footer);
// 打开文档,将要写入内容
document.open();
// 文档标题标题
document.add(new Paragraph(fileTitle, fontChinese));
// 换行
document.add(Chunk.NEWLINE);
// 插入表格
com.lowagie.text.Font f8 = new com.lowagie.text.Font(bfChinese, 8, com.lowagie.text.Font.BOLD);
com.lowagie.text.Font f12 = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.BOLD);
//生成每个办公区的统计报表
for (Location location : locationList) {
//生成一个2列的表格
PdfPTable table = new PdfPTable(2);
// 宽度定位100%
table.setWidthPercentage(100);
table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
// 创建第一行
PdfPCell cell = new PdfPCell();
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(24, 198, 248));
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setColspan(2);
cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
//第一行中显示的文字
cell.setPhrase(new Paragraph(location.getLocationName() + "办公区设备运行情况统计", f12));
table.addCell(cell);
//插入统计图
PdfPCell newcell = new PdfPCell();
newcell.setBorderColor(new Color(255, 255, 255));
//设备故障率统计,JFreeChart生成图表并返回
Image img = Image.getInstance("http://localhost:8080/DeviceStat/getJFreeChart.html?method=getDeviceNormalCount&locationId=" + location.getLocationId() + "&year=" + year + "&month=" + month);
img.setAbsolutePosition(0, 0);
newcell.setImage(img);
table.addCell(newcell);
//故障类型统计,JFreeChart生成图表并返回
img = Image.getInstance("http://localhost:8080/DeviceStat/getJFreeChart.html?method=getDeviceAbnormalCount&locationId=" + location.getLocationId() + "&year=" + year + "&month=" + month);
img.setAbsolutePosition(0, 0);
newcell.setImage(img);
table.addCell(newcell);
document.add(table);
}
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
} finally {
// 关闭打开的文档
document.close();
}
}
return NONE;
}
/**
Java代码
1. * 用JFreeChart生成统计图
2. * @return
3. */
4.public String getJFreeChart() {
5. HttpServletResponse response = ServletActionContext.getResponse();
6.
7. response.setContentType("image/jpeg;charset=utf-8");
8.
9. //获得办公区信息
10. Location location = SystemCache.getLocationByIdFromCache(locationId);
11.
12. if (null != location) {
13. DefaultPieDataset data = getDataSet(method, locationId, year, month);
14. JFreeChart chart = ChartFactory.createPieChart("getDeviceNormalCount".equals(method) ? "办公区设备运行情况统计" : "故障类型分布", data, true, false, false);
15.
16. chart.setBorderPaint(new Color(255,255,255));
17. chart.setBorderVisible(false);
18. chart.getTitle().setFont(new Font("宋体", Font.BOLD,12));
19. chart.getLegend().setItemFont(new Font("宋体", Font.BOLD,12));
20. PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
21. piePlot.setBackgroundPaint(new Color(255,255,255));
22. piePlot.setOutlineVisible(false); //无边框
23. piePlot.setNoDataMessage("暂无统计数据"); //设置无数据时的显示内容
24. piePlot.setLabelFont(new Font("宋体",Font.BOLD,12)); //饼图旁边的中文
25. //按百分比显示
26. piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(
27. "{0}:{2}", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")
28. ));
29.
30. try {
31. //输出图标
32. ChartUtilities.writeChartAsJPEG(response.getOutputStream(),
33. 1,chart,400,300,null);
34.
35. response.flushBuffer();
36. } catch (IOException e) {
37. // TODO Auto-generated catch block
38. e.printStackTrace();
39. } finally {
40.
41. }
42. }
43.
44. return NONE;
45.}
46.
47./**
48. * 获得JFreeChart需要的数据
49. * @param method
50. * @param locationId
51. * @param year
52. * @param month
53. * @return
54. */
55.private static DefaultPieDataset getDataSet(String method, Integer locationId, Integer year, Integer month) {
56. DefaultPieDataset dataset = new DefaultPieDataset();
57.
58. if ("getDeviceNormalCount".equals(method)) {
59. Integer normalCount = SystemCache.getDeviceStatNormalCountFromCache(locationId, year, month);
60. Integer abNormalCount = SystemCache.getDeviceStatAbnormalCountFromCache(locationId, year, month);
61.
62. if (null != normalCount && 0 != normalCount) {
63. dataset.setValue("设备正常率", normalCount);
64. }
65. if (null != abNormalCount && 0 != abNormalCount) {
66. dataset.setValue("设备故障率", abNormalCount);
67. }
68. } else {
69. List<DeviceBreakdownTypeCount> countList = SystemCache.getDeviceBreakdownTypeCountFromCache(locationId, year, month);
70. if (null != countList && countList.size() > 0) {
71. for (DeviceBreakdownTypeCount count : countList) {
72. dataset.setValue(Constant.DEVICE_BREAKDOWN_TYPE_DESC[count.getBreakdownType()], count.getBreakdownTypeCount());
73. }
74. }
75. }
76.
77. return dataset;
78.}
* 用JFreeChart生成统计图
* @return
*/
public String getJFreeChart() {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("image/jpeg;charset=utf-8");
//获得办公区信息
Location location = SystemCache.getLocationByIdFromCache(locationId);
if (null != location) {
DefaultPieDataset data = getDataSet(method, locationId, year, month);
JFreeChart chart = ChartFactory.createPieChart("getDeviceNormalCount".equals(method) ? "办公区设备运行情况统计" : "故障类型分布", data, true, false, false);
chart.setBorderPaint(new Color(255,255,255));
chart.setBorderVisible(false);
chart.getTitle().setFont(new Font("宋体", Font.BOLD,12));
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD,12));
PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
piePlot.setBackgroundPaint(new Color(255,255,255));
piePlot.setOutlineVisible(false); //无边框
piePlot.setNoDataMessage("暂无统计数据"); //设置无数据时的显示内容
piePlot.setLabelFont(new Font("宋体",Font.BOLD,12)); //饼图旁边的中文
//按百分比显示
piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}:{2}", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")
));
try {
//输出图标
ChartUtilities.writeChartAsJPEG(response.getOutputStream(),
1,chart,400,300,null);
response.flushBuffer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
return NONE;
}
/**
* 获得JFreeChart需要的数据
* @param method
* @param locationId
* @param year
* @param month
* @return
*/
private static DefaultPieDataset getDataSet(String method, Integer locationId, Integer year, Integer month) {
DefaultPieDataset dataset = new DefaultPieDataset();
if ("getDeviceNormalCount".equals(method)) {
Integer normalCount = SystemCache.getDeviceStatNormalCountFromCache(locationId, year, month);
Integer abNormalCount = SystemCache.getDeviceStatAbnormalCountFromCache(locationId, year, month);
if (null != normalCount && 0 != normalCount) {
dataset.setValue("设备正常率", normalCount);
}
if (null != abNormalCount && 0 != abNormalCount) {
dataset.setValue("设备故障率", abNormalCount);
}
} else {
List<DeviceBreakdownTypeCount> countList = SystemCache.getDeviceBreakdownTypeCountFromCache(locationId, year, month);
if (null != countList && countList.size() > 0) {
for (DeviceBreakdownTypeCount count : countList) {
dataset.setValue(Constant.DEVICE_BREAKDOWN_TYPE_DESC[count.getBreakdownType()], count.getBreakdownTypeCount());
}
}
}
return dataset;
}
最后运行的结果:
发表评论
-
linux安装jdk
2012-11-16 09:43 740第一步:查看Linux自带的JDK是否已安装 (卸载centO ... -
build.xml
2011-04-07 17:56 862<?xml version="1.0" ... -
java编码
2010-10-21 22:40 699Unicode字 符在某个平台上不存在,将会输出一个'?'。 ... -
j2ee书籍
2010-08-12 08:50 700J2EE核心模式.pdf http://www.itstud ... -
jstl为空
2010-08-11 09:51 1278JSTL与EL表达式(为空判断) 一、循环遍历集合 1、在j ... -
java 操作excel
2010-07-28 10:12 735//filePathNew为一个File对象的实例 Writ ... -
java工具包截取字符长度
2009-07-14 10:36 883package cn.jxsme.util.tool; /* ... -
java工具2简单加密
2009-07-14 10:35 832package cn.jxsme.util.tool; /* ... -
java工具日期部分
2009-07-14 10:34 735package cn.jxsme.util.tool; im ... -
过滤HTML部分
2009-07-14 10:34 896package cn.jxsme.util.tool; im ... -
生成文件部分
2009-07-14 10:33 635package cn.jxsme.util.tool; im ... -
用户验证部分
2009-07-14 10:33 654package cn.jxsme.util.tool; /* ... -
MD5加密码部分
2009-07-14 10:32 736package cn.jxsme.util.tool; /** ...
相关推荐
通过itext导出jfreechart到word,pdf和html,并且解决了中文乱码和报表字体模糊的问题!
包含 itext-asian-5.2.0.jar itextpdf-5.5.5.jar
velocity+itext的整合导出pdf
NULL 博文链接:https://keep-going.iteye.com/blog/1907664
程序使用Struts2(SSH即Struts2+Hibernate+Spring)+POI+MySQL实现Excel动态数据导入和导出,程序源码文件附带运行说明
adobe pdf编辑器,解压安装即可使用,配合java利用itextpdf根据模板导出pdf文章中只做模板的方法;
利用itext操作pdf从数据库导出大量数据 压缩包含需要的jar包、案例源码
itext 导出pdf java demo
itext导出PDFitext导出PDF
JAVA基于ITEXT实现的PDF报表系统,程序下载只是作为参考和学习,不是可以直接使用的。
c# winform Itext 实现PDF导出简单demo,适合初学者学习
java 使用IText导出PDF(包含图片、表格、文字等等Demo和依赖包),最全IText使用Demo,下载即可用
使用ITEXT导出EXCEL工具类使用ITEXT导出EXCEL工具类使用ITEXT导出EXCEL工具类使用ITEXT导出EXCEL工具类使用ITEXT导出EXCEL工具类
itext根据模板导出pdf,内容包括文字和图片,有任何问题,留言即可
博文链接:https://yongsky.iteye.com/blog/99406
使用IText生成PDF和WORD文档 包括源码和IText所需jar包
逻辑思路 1、先将excel转为pdf (TestForExcel2PDF测试类) 2、然后再pdf上加水印(TestWaterPrint、TestwaterMark都为加水印测试类)
itext java 导出pdf工具 itext java 导出pdf工具 itext java 导出pdf工具
flex实战报表:jFreeChart,cewolf,iText.doc