我用的是struts2.1.6,和jfreechart1.0.12
大家可以去
http://struts.apache.org/download.cgi#struts216 (下 载struts2相关jar文件)
http://sourceforge.net/projects/jfreechart/files/ (下 载jfreechart和jcommon和相关jar文件)
好了,当你都下载完了之后,里面的jar不是必须的,我们只需要导入一部分即可
- struts2-core-2.1.6.jar
- freemarker-2.3.13.jar
- xwork-2.1.2.jar
- ognl-2.6.11.jar
- commons-fileupload-1.2.1.jar
- commons-logging-1.0.4.jar
- struts2-jfreechart-plugin-2.1.6.jar
- jcommon-1.0.15.jar
- jfreechart-1.0.12.jar
前6个是struts2所需要的jar文件,如果大家用的不是struts2.1.6的话,commons-fileupload- 1.2.1.jar可以不用导入
我们要实现的效果就是,在一个页面上面用一个img标签显示jfreechart生成的图片
1.jsp页面代码非常简单
- < body >
- < img alt = "jfreechart" src = "jfreechart.action" />
- </ body >
<body>
<img alt="jfreechart" src="jfreechart.action"/>
</body>
就是一个img标签,src连接一个action
2.我们就写jfreechart代码,在这里我也是写一个非常简单的例子,就画一个饼图
- public class JfreeChartTest {
-
- public static JFreeChart createChart() throws IOException {
-
- DefaultPieDataset dpd = new DefaultPieDataset();
- dpd.setValue("管理人员" , 25 );
- dpd.setValue("市场人员" , 25 );
- dpd.setValue("开发人员" , 45 );
- dpd.setValue("其它人员" , 10 );
-
- JFreeChart chart = ChartFactory.createPieChart3D("某公司人员组织结构图" , dpd,
- true , true , false );
- utils.setFont(chart);
- return chart;
- }
- }
-
-
-
-
-
-
-
- class utils {
- public static void setFont(JFreeChart chart) {
- Font font = new Font( "宋体" , Font.ITALIC, 12 );
- PiePlot plot = (PiePlot) chart.getPlot();
- chart.getTitle().setFont(font);
- plot.setLabelFont(font);
- chart.getLegend().setItemFont(font);
- }
- }
public class JfreeChartTest {
public static JFreeChart createChart() throws IOException {
// 数据集
DefaultPieDataset dpd = new DefaultPieDataset();
dpd.setValue("管理人员", 25);
dpd.setValue("市场人员", 25);
dpd.setValue("开发人员", 45);
dpd.setValue("其它人员", 10);
// 创建PieChart对象
JFreeChart chart = ChartFactory.createPieChart3D("某公司人员组织结构图", dpd,
true, true, false);
utils.setFont(chart);
return chart;
}
}
/**
* 设置字体
*
* @author zyong
*
*/
class utils {
public static void setFont(JFreeChart chart) {
Font font = new Font("宋体", Font.ITALIC, 12);
PiePlot plot = (PiePlot) chart.getPlot();
chart.getTitle().setFont(font);
plot.setLabelFont(font);
chart.getLegend().setItemFont(font);
}
}
3.编写action
- @SuppressWarnings ( "serial" )
- public class JfreeCharAction extends ActionSupport {
-
-
-
-
-
-
-
-
-
- private JFreeChart chart;
-
- public JFreeChart getChart() {
- return chart;
- }
-
- public void setChart(JFreeChart chart) {
- this .chart = chart;
- }
-
- @Override
- public String execute() throws Exception {
-
- this .chart = JfreeChartTest.createChart();
- return SUCCESS;
- }
- }
@SuppressWarnings("serial")
public class JfreeCharAction extends ActionSupport {
/**
* 定义JFreeChart对象 大家请注意在这里JFreeChart对象名只能为chart
* 不能是别的
* 关于这点
* 大家可以上struts2网站上面查看一下
*
* http://struts.apache.org/2.x/docs/jfreechart-plugin.html
*/
private JFreeChart chart;
public JFreeChart getChart() {
return chart;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
@Override
public String execute() throws Exception {
// 调用方法
this.chart = JfreeChartTest.createChart();
return SUCCESS;
}
}
4.编写struts.xml
- < struts >
- <!--
- 关 于extends继承jfreechart-default这点请大家注意
- 因为在 struts-default这个包里并没有result-type为chart的
- chart 定 义在前面我们导入的struts2-jfreechart-plugin-2.1.6.jar
- 下 面的struts-plugin.xml文件中
- -->
- < package name = "jfreechart" extends = "jfreechart-default" >
- < action name = "jfreechart" class = "jfreechartAction" >
- < result name = "success" type = "chart" >
- < param name = "width" > 600 </ param >
- < param name = "height" > 400 </ param >
- </ result >
- </ action >
- </ package >
- </ struts >
<struts>
<!--
关于extends继承jfreechart-default这点请大家注意
因为在struts-default这个包里并没有result-type为chart的
chart 定义在前面我们导入的struts2-jfreechart-plugin-2.1.6.jar
下面的struts-plugin.xml文件中
-->
<package name="jfreechart" extends="jfreechart-default">
<action name="jfreechart" class="jfreechartAction">
<result name="success" type="chart">
<param name="width">600</param>
<param name="height">400</param>
</result>
</action>
</package>
</struts>
5.大家把上面的工作都做好了之后,就可以开始服务器,运行一下就可以看见自己的成果
原文地址:http://wind-sing.javaeye.com/blog/443980