Struts2学习笔记19:
Struts2与JFreeChart的整合
第二十三讲
学习内容:
Struts2与JFreeChart的整合实例 |
下图是对struts2和JFreeChart整合的流程图解
|
详细步骤:
1.修改Struts2的lib目录
struts2-jfreechart-plugin-2.0.11.2.jar 中的struts-plugin.xml文件,使package继承于 struts-default
2.新建项目jfreechart
3.导入相关的包
4.index.jsp页面中,在body标签中写入如下代码:
- <h1 style="color: red">选择你所喜欢的运动项目</h1>
- <s:form action="interest" >
- <s:checkbox name="sport" fieldValue="football" label="足球"></s:checkbox>
- <s:checkbox name="sport" fieldValue="basketball" label="篮球"></s:checkbox>
- <s:checkbox name="sport" fieldValue="volleyball" label="排球"></s:checkbox>
- <s:checkbox name="sport" fieldValue="pingpong" label="乒乓球"></s:checkbox>
- <s:checkbox name="sport" fieldValue="badminton" label="羽毛球"></s:checkbox>
- <s:submit></s:submit>
- </s:form>
5.建立struts.xml文件,代码:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="struts2" extends="jfreechart-default">
- <action name="interest" class="action.InterestAction">
- <result name="success" type="chart">
- <param name="height">600</param>
- <param name="width">800</param>
- </result>
- </action>
- </package>
- </struts>
6.建立 action.InterestAction.java文件,代码:
|
- package action;
- import java.awt.Font;
- import java.util.List;
- import java.util.Map;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.JFreeChart;
- import org.jfree.chart.axis.CategoryAxis;
- import org.jfree.chart.axis.CategoryLabelPositions;
- import org.jfree.chart.axis.NumberAxis;
- import org.jfree.chart.plot.CategoryPlot;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.chart.title.TextTitle;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.category.DefaultCategoryDataset;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class InterestAction extends ActionSupport {
- private static final long serialVersionUID = 180794008736027209L;
- private JFreeChart chart;
- private List<String> sport;
- public List<String> getSport() {
- return sport;
- }
- public void setSport(List<String> sport) {
- this.sport = sport;
- }
- @Override
- public String execute() throws Exception {
- return SUCCESS;
- }
- //设置chart
- public JFreeChart getChart() {
- chart = ChartFactory.createBarChart("运动项目", "运动项目", "选择人数", this
- .createCategory(), PlotOrientation.VERTICAL, false, false,
- false);
- chart.setTitle( new TextTitle("喜欢的运动项目", new Font("隶书" , Font.BOLD , 30)));
- CategoryPlot plot = (CategoryPlot) chart.getPlot();
- CategoryAxis axis = plot.getDomainAxis();
- axis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
- //设置项目名的倾斜角度
- axis.setCategoryLabelPositions( CategoryLabelPositions.UP_45);
- axis.setTickLabelFont(new Font("隶书" , Font.BOLD , 15));
- //设置项目名的字体类型
- NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
- numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
- return chart;
- }
- @SuppressWarnings("unchecked")
- //获取ServletContext的上下文,将选择的数据暂时性的保存于Context中
- private void insertResult(List<String> list) {
- ActionContext context = ActionContext.getContext();
- Map m = context.getApplication();
- for (String src : list) {
- if (null == m.get(src)) {
- m.put(src, 1);
- } else {
- m.put(src, (Integer) m.get(src) + 1);
- }
- }
- }
- @SuppressWarnings("unchecked")
- //为JFreechart创建数据
- private CategoryDataset createCategory() {
- DefaultCategoryDataset dcd = new DefaultCategoryDataset();
- this.insertResult(this.getSport());
- ActionContext context = ActionContext.getContext();
- Map m = context.getApplication();
- dcd.setValue((Integer) m.get("football"), "足球", "足球");
- dcd.setValue((Integer) m.get("basketball"), "篮球", "篮球");
- dcd.setValue((Integer) m.get("volleyball"), "排球", "排球");
- dcd.setValue((Integer) m.get("pingpong"), "乒乓球", "乒乓球");
- dcd.setValue((Integer) m.get("badminton"), "羽毛球", "羽毛球");
- return dcd;
- }
- }
7.在web.xml中添加filter
|