struts2与jfeechart整合

本文介绍了一个使用Struts框架整合JFreeChart库实现投票功能的例子。通过该示例,用户可以进行投票选择,并实时展示投票结果的柱状图。文章详细展示了vote.jsp页面代码、后端Action类实现及struts.xml配置。
下面是一个struts整合JFreeChart的投票程序
[color=red]注意:将struts-jfeechart-plugin.jar加进去[/color]
vote.jsp内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html><head><title></title><head>
<body>
投票项目: <br><s:form action="viewResult" namespace="/vote" method="get">
<s:checkboxlist list="#{'football':'足球','volleyball':'排球','basketball':'篮球','badminton':'羽毛球'}" name="interests" label="请选择" labelposition="top" listKey="key" listValue="value"></s:checkboxlist>
<br>
<s:submit/>
</s:form>
</body>
</html>

action代码:
package cn.hfut.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.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class JFreeViewAction extends ActionSupport{


private List<String> interests;
private JFreeChart chart;
public JFreeChart getChart() {//注意:此处的chart名称不可改变
chart = getJFreeChart();
return chart;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}

@Override
public String execute() throws Exception {

// System.out.println(interests);
return SUCCESS;
}
@SuppressWarnings("unchecked")
private void insertData(){
for(String interest : interests){
Map context = ActionContext.getContext().getApplication();
if(context.get(interest)!=null){
context.put(interest, (Integer)context.get(interest)+1);
}else{
context.put(interest,1);
}
}
}
@SuppressWarnings("unchecked")
private JFreeChart getJFreeChart(){
insertData();
DefaultCategoryDataset data = new DefaultCategoryDataset();
Map context = ActionContext.getContext().getApplication();
/* for(String interest : interests){
System.out.println(context.get(interest));
data.addValue((Integer)context.get(interest), interest , interest);
}*/
data.addValue((Integer)context.get("football"),"足球","足球");
data.addValue((Integer)context.get("volleyball"),"排球","排球");
data.addValue((Integer)context.get("basketball"),"篮球","篮球");
data.addValue((Integer)context.get("badminton"),"羽毛球","羽毛球");
// System.out.println(data);
JFreeChart chart = ChartFactory.createBarChart3D("兴趣统计表", null, "结果", data, PlotOrientation.VERTICAL, true, false, false);
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("����隶书",Font.BOLD+Font.ITALIC,20));
chart.setTitle(new TextTitle("兴趣统计表",new Font("宋体",Font.BOLD,20)));
CategoryPlot plot = (CategoryPlot)chart.getPlot();
CategoryAxis xAxis = plot.getDomainAxis();
xAxis.setLabel("标签");
xAxis.setLabelFont(new Font("宋体",Font.BOLD,22));
xAxis.setTickLabelFont(new Font("宋体",Font.BOLD+Font.ITALIC,18));
xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
ValueAxis yAxis = plot.getRangeAxis();
yAxis.setLabelFont(new Font("宋体",Font.BOLD+Font.ITALIC,22));
return chart;
}
public List<String> getInterests() {
return interests;
}
public void setInterests(List<String> interests) {
this.interests = interests;
}
}

struts.xml配置如下:
<package name="vote" extends="jfreechart-default"
namespace="/vote">
<action name="viewResult"
class="cn.hfut.action.JFreeViewAction">
<result name="success" type="chart">
<param name="height">600</param>
<param name="width">800</param>
</result>
</action>

</package>

运行结果:唯一柱状调查结果表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值