Struts2整合JfreeChart

2009-09-09 19:48

action:

package com.cstp.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 ViewResultAction extends ActionSupport {
private static final long serialVersionUID = 1L;

private JFreeChart chart;//固定名字
private List<String> interest;

public JFreeChart getChart() {//拦截器调用getChart()方法获取图片对象
   chart = ChartFactory.createBarChart3D("兴趣统计结果", "项目", "结果", this
     .getDataset(), PlotOrientation.VERTICAL, true, true, false);
   Font font = new Font("宋体", Font.BOLD, 22);

   chart.setTitle(new TextTitle("兴趣统计结果", font));

   chart.getLegend().setItemFont(font);
   CategoryPlot plot = (CategoryPlot) chart.getPlot();

   CategoryAxis categoryAxis = plot.getDomainAxis();

   categoryAxis.setLabelFont(font);
   categoryAxis.setTickLabelFont(font);
   categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

   NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
   numberAxis.setLabelFont(font);
   // numberAxis.setTickLabelFont(new Font("宋体",Font.BOLD,22));//数字不会乱码,没必要
   return chart;
}

public List<String> getInterest() {
   return interest;
}

public void setInterest(List<String> interest) {
   this.interest = interest;
}

@Override
public String execute() throws Exception {
   return SUCCESS;
}

@SuppressWarnings("unchecked")
private void increaseResult(List<String> list) {
//应该写在业务类里
   ActionContext context = ActionContext.getContext();
   Map map = context.getApplication();
   for (String str : list) {
    if (null == map.get(str)) {//未勾选
     map.put(str, 1);
    } else {//选了的
     map.put(str, (Integer) map.get(str) + 1);
    }
   }
}

@SuppressWarnings("unchecked")
private CategoryDataset getDataset() {//获取数据集
   DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  
  this.increaseResult(this.getInterest());//算出投票结果
  
   ActionContext context = ActionContext.getContext();
   Map map = context.getApplication();
   dataset.setValue((Integer) map.get("football"), "足球", "足球");
   dataset.setValue((Integer) map.get("basketball"), "篮球", "篮球");
   dataset.setValue((Integer) map.get("volleyball"), "排球", "排球");
   dataset.setValue((Integer) map.get("badminton"), "羽毛球", "羽毛球");

   return dataset;
}

}

Struts.xml:

<package name="struts2_demo" extends="jfreechart-default"
   namespace="/">

新版本的插件struts2-jfreechart-plugin-2.1.6.jar里 的struts-plugin.xml里的

<package name="jfreechart-default" extends="struts-default"> 继承了struts-default 所以Struts.xml直接继承jfreechart-default 为什么不直接继承struts-default呢?因为struts-default里的result-type 没有chart 类型的。struts-plugin.xml里有。

<action name="viewResult" class="com.cstp.action.ViewResultAction">
    <result name="success" type="chart">
     <param name="height">600</param>
     <param name="width">800</param>
    </result>
   </action>

 

select.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1><font color="blue">请选择喜欢的运动项目</font></h1>

<s:form action="viewResult.action">
<s:checkbox name="interest" label="足球" fieldValue="football" labelposition="left"></s:checkbox>
<s:checkbox name="interest" label="篮球" fieldValue="basketball" labelposition="left"></s:checkbox>
<s:checkbox name="interest" label="排球" fieldValue="volleyball" labelposition="left"></s:checkbox>
<s:checkbox name="interest" label="羽毛球" fieldValue="badminton" labelposition="left"></s:checkbox>

<!--
<s:checkboxlist list="#{'computer' : '计算机' , 'math' : '数学'}" name="interest" label="浪曦" labelposition="top">
</s:checkboxlist>
-->

<s:submit value="提交"></s:submit>
</s:form>

</body>
</html>

效果图:

多次来回提交产生的效果(值保存在Application中):

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值