struts2与jfeechart整合
下面是一个struts整合JFreeChart的投票程序
注意:将struts-jfeechart-plugin.jar加进去
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>
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;}@Overridepublic 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;}}
<package name="vote" extends="jfreechart-default"namespace="/vote"><action name="viewResult"type="chart"><param name="height">600</param><param name="width">800</param></result></action></package>