---
title: "《研磨struts2》第十八章 结合JFreeChart 之 18.3 Struts2结合JFreeChart "
author: "研磨struts2"
date: "2012-06-28T12:12:07Z"
tags: []
categories: ["企业架构"]
slug: "struts2-jfreechart-18-3-struts2-jfreechart"
draft: false
lastmod: "2012-06-28T12:12:07Z"
id: 1569908
url: "https://rubbing-struts2.iteye.com/blog/1569908"
---

## 18.3 Struts2结合JFreeChart

### []()18.3.1构建结合环境

有了前面的基础，接下来可以来把Struts2与JFreeChart结合使用了。

先来构建结合开发的环境，很简单，只有一步工作，把struts2-jfreechart-plugin-2.1.8.1.jar拷入到web工程的lib下就可以了。

### []()18.3.2 Struts2结合JFreeChart来画饼图

**1：实现Action**

JFreeChart作为一种结果类型，可以作为某个Action的Result。接下来就来编写一个Action，结合着JFreeChart来实现画饼图。

编写这种Action和编写普通的Action大大的不同。**这种Action不需要execute方法，而是需要一个getChart方法，这个方法必须是公有的，且返回一个JFreeChart对象。**

示例代码如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4170.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4170.html)[打印](http://sishuok.com/forum/blogPost/list/4170.html)

01. public class PieAction  extends ActionSupport {
02. private static final Font font = new Font("simsun",Font.ITALIC,22);
03. private static DefaultPieDataset getDataset(){
04. DefaultPieDataset dataset = new DefaultPieDataset();
05. dataset.setValue("本科生", 50);
06. dataset.setValue("研究生", 30);
07. dataset.setValue("博士", 20);
08. return dataset;
09. }
10. public JFreeChart getChart(){
11. final DefaultPieDataset dataset = getDataset();
12. JFreeChart chart = ChartFactory.createPieChart("XX公司员工学历比例图",
13. dataset,
14. true,//是否有标题
15. false,//是否有悬浮提示
16. false//是否有连接
17. );
18. chart.setTitle(new TextTitle("XX公司员工学历比例图",font));
19. //重新设置图例的字体，保证汉字的显示
20. LegendTitle legend = chart.getLegend();
21. legend.setItemFont(font);
22. //重新设置统计表对象的字体，保证汉字的显示
23. PiePlot plot = (PiePlot) chart.getPlot();
24. plot.setBackgroundAlpha(0.9f);
25. plot.setLabelFont(font);
26. return chart;
27. }
28. }

观察上面的两个方法getDataset和getChart，其实现方式和前面在Java中使用JFreeChart来画饼图的实现是一模一样的。仍然是getDataset方法提供饼形图要显示的数据，getChart方法用来设置饼形图的相关信息(如图例的字体，标题的字体等等)。

**2：配置struts.xml**

接下来看看在struts.xml中，Struts2是如何跟JFreeChart结合的，示例代码如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4170.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4170.html)[打印](http://sishuok.com/forum/blogPost/list/4170.html)

1. &lt;package name="helloworld" extends="jfreechart-default"&gt;
2. &lt;action name="jfreechartAction" class="cn.javass.chart.PieAction"&gt;
3. &lt;result type="chart"&gt;
4. &lt;param name="width"&gt;400&lt;/param&gt;
5. &lt;param name="height"&gt;300&lt;/param&gt;
6. &lt;/result&gt;
7. &lt;/action&gt;
8. &lt;/package&gt;

在配置文件中，我们的包继承了jfreechart-default包；在我们的包中使用了jfreechart-default包中的chart这种&lt;result-type&gt;；然后通过给&lt;result&gt;元素设置&lt;param&gt;子元素来限定图形化报表的宽和高。

**3：运行测试一下**

       在IE浏览器中输入http://localhost:9080/helloworld/jfreechartAction.action，可以看到结果，如下图所示：

![](http://sishuok.com/forum/upload/2012/4/23/ba80a3bce649a61eda230957d77a8543__%E6%9C%AA%E5%91%BD%E5%90%8D.jpg)

图18.4 使用Struts2与JFreeChart结合画出的饼图

### []()18.3.3 Struts2结合JFreeChart来画柱状图

**1：实现Action**

跟上面Struts2结合JFreeChart来画饼图的过程类似，都是不需要execute方法的，都需要一个getChart方法，这个方法必须是公有的，且返回一个JFreeChart对象。

示例代码如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4170.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4170.html)[打印](http://sishuok.com/forum/blogPost/list/4170.html)

01. public class BarAction extends ActionSupport {
02. private static final Font font = new Font("simsun",Font.ITALIC,22);
03. private static DefaultCategoryDataset getDataset(){
04. DefaultCategoryDataset  dataset = new DefaultCategoryDataset();
05. dataset.setValue(50,"本科生","本科生");
06. dataset.setValue(30,"研究生","研究生");
07. dataset.setValue(20,"博士","博士");
08. return dataset;
09. }
10. public JFreeChart getChart(){
11. final DefaultCategoryDataset dataset = getDataset();
12. JFreeChart chart = ChartFactory.createBarChart3D("XX公司员工学历比例图","学历","人数",
13. dataset,
14. PlotOrientation.VERTICAL,
15. true,
16. false,
17. false
18. );
19. chart.setTitle(new TextTitle("XX公司员工学历比例图",font));
20. //重新设置图例的字体，保证汉字的显示
21. LegendTitle legend = chart.getLegend();
22. legend.setItemFont(font);
23. //设置X轴坐标上的文字的字体
24. chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font);
25. //设置X轴坐标标题的字体
26. chart.getCategoryPlot().getDomainAxis().setLabelFont(font);
27. //设置Y轴坐标上的文字的字体
28. chart.getCategoryPlot().getRangeAxis().setTickLabelFont(font);
29. //设置Y轴坐标标题的字体
30. chart.getCategoryPlot().getRangeAxis().setLabelFont(font);
31. return chart;
32. }
33. }

       观察上面的两个方法getDataset和getChart，其实现方式和前面在Java中使用JFreeChart来画柱状图的实现是一模一样的。仍然是getDataset方法提供饼形图要显示的数据，getChart方法用来设置饼形图的相关信息(如图例的字体，标题的字体等等)。

**2：配置struts.xml**

struts.xml中的配置和上面画饼图的方式也是一样的，示例代码如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4170.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4170.html)[打印](http://sishuok.com/forum/blogPost/list/4170.html)

1. &lt;package name="helloworld" extends="jfreechart-default"&gt;
2. &lt;action name="jfreechartAction" class="cn.javass.chart.BarAction"&gt;
3. &lt;result type="chart"&gt;
4. &lt;param name="width"&gt;400&lt;/param&gt;
5. &lt;param name="height"&gt;300&lt;/param&gt;
6. &lt;/result&gt;
7. &lt;/action&gt;
8. &lt;/package&gt;

**3：运行测试一下**

       在IE浏览器中输入http://localhost:9080/helloworld/jfreechartAction.action，可以看到结果，如下图所示：

![](http://sishuok.com/forum/upload/2012/4/23/9391bca907a4aa67ae0924525ac8dd29__%E6%9C%AA%E5%91%BD%E5%90%8D.jpg)

图18.5 使用Struts2与JFreeChart结合画出的柱状图

 

 

私塾在线网站原创《[研磨struts2](http://sishuok.com/forum/blogCategory/showByCategory.html?categories_id=81&user_id=5948)》系列

转自请注明出处：【[http://sishuok.com/forum/blogPost/list/0/4170.html](http://sishuok.com/forum/blogPost/list/0/4170.html)】

欢迎访问[http://sishuok.com](http://sishuok.com/)获取更多内容