Posted on 2012-02-28 19:12
viery 阅读(256)
评论(0) 编辑 收藏 所属分类:
Struts2
action
package org.vle.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.DefaultCategoryDataset;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TpupiaoAction extends ActionSupport {
private JFreeChart chart;
private List<String> toupiao;
public List<String> getToupiao() {
return toupiao;
}
public void setToupiao(List<String> toupiao) {
this.toupiao = toupiao;
}
public JFreeChart getChart() {
DefaultCategoryDataset set=this.getDataSet();
this.chart=ChartFactory.createBarChart("投票统计图", "项目", "票数",set, PlotOrientation.VERTICAL, true, false, false);
chart.setTitle(new TextTitle("投票统计图",new Font("宋体",Font.BOLD,28)));
CategoryPlot plot=(CategoryPlot) chart.getPlot();
CategoryAxis axis=plot.getDomainAxis();
axis.setLabelFont(new Font("黑体",Font.BOLD,18));
axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
axis.setTickLabelFont(new Font("宋体",Font.PLAIN,15));
NumberAxis number=(NumberAxis) plot.getRangeAxis();
number.setLabelFont(new Font("黑体",Font.BOLD,18));
return this.chart;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
private DefaultCategoryDataset getDataSet(){
DefaultCategoryDataset set=new DefaultCategoryDataset();
this.add(toupiao);
ActionContext context=ActionContext.getContext();
Map map=context.getApplication();
set.addValue((Integer)map.get("zu"), "", "足球");
set.addValue((Integer)map.get("lan"), "", "篮球");
set.addValue((Integer)map.get("pai"), "", "排球");
set.addValue((Integer)map.get("yu"), "", "羽毛球");
return set;
}
@SuppressWarnings({ "unchencked", "unchecked" })
private void add(List<String> list){
ActionContext context=ActionContext.getContext();
Map map=context.getApplication();
if(list!=null){
for(String s:list){
if(map.get(s)==null){//如果还没有得到投票
map.put(s, 1);
}else{
map.put(s, (Integer)map.get(s)+1);
}
}
}
}
}xml配置
<struts>
<constant name="struts.i18n.encoding" value="gbk"/>
<package name="toupiao" extends="jfreechart-default">
<action name="Toupiao" class="org.vle.action.TpupiaoAction">
<result name="success" type="chart">
<param name="width">800</param>
<param name="height">600</param>
</result>
</action>
</package>
</struts>
投票jsp
<body>
投票<br/>
<s:form action="Toupiao">
<s:checkbox name="toupiao" label="足球" fieldValue="zu"></s:checkbox>
<s:checkbox name="toupiao" label="蓝球" fieldValue="lan"></s:checkbox>
<s:checkbox name="toupiao" label="排球" fieldValue="pai"></s:checkbox>
<s:checkbox name="toupiao" label="羽毛球" fieldValue="yu"></s:checkbox>
<s:submit value="投票"></s:submit>
</s:form>
</body>
需要引入jfreechart的插件包