ITeye博客

《研磨struts2》第十八章 结合JFreeChart 之 18.3 Struts2结合JFreeChart

研磨struts2 · 2012-06-28 12:12 · 研磨struts2 · 企业架构 · Markdown

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代码:

查看复制到剪贴板打印

  1. public class PieAction  extends ActionSupport {
  2. private static final Font font = new Font("simsun",Font.ITALIC,22);
  3. private static DefaultPieDataset getDataset(){
  4. DefaultPieDataset dataset = new DefaultPieDataset();
  5. dataset.setValue("本科生", 50);
  6. dataset.setValue("研究生", 30);
  7. dataset.setValue("博士", 20);
  8. return dataset;
  9. }
  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代码:

查看复制到剪贴板打印

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

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

3:运行测试一下

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

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

18.3.3 Struts2结合JFreeChart来画柱状图

1:实现Action

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

示例代码如下:

 

java代码:

查看复制到剪贴板打印

  1. public class BarAction extends ActionSupport {
  2. private static final Font font = new Font("simsun",Font.ITALIC,22);
  3. private static DefaultCategoryDataset getDataset(){
  4. DefaultCategoryDataset  dataset = new DefaultCategoryDataset();
  5. dataset.setValue(50,"本科生","本科生");
  6. dataset.setValue(30,"研究生","研究生");
  7. dataset.setValue(20,"博士","博士");
  8. return dataset;
  9. }
  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代码:

查看复制到剪贴板打印

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

3:运行测试一下

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

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

 

 

私塾在线网站原创《研磨struts2》系列

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

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。