`
mywebcode
  • 浏览: 1004587 次
文章分类
社区版块
存档分类
最新评论

JFreeChart 与 Struts2 整合小例子

 
阅读更多

JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<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" labelposition="top" label="陈学港">
</s:checkboxlist>
-->
<s:submit></s:submit>
</s:form>
</body>
</html>

Action类(ViewResultAction)

package org.cxg.struts.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.CategoryLabelPosition;
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 JFreeChart chart;
private List<String> interest;

@SuppressWarnings("unchecked")
private void increaseResult(List<String> list){
ActionContext context = ActionContext.getContext();
Map map = context.getApplication();
for(String string : list){
System.out.println("8888"+string);
if(null == map.get(string))
{

map.put(string, 1);
}
else
{
map.put(string, (Integer)map.get(string) + 1);
}
}
}

private CategoryDataset getDataset(){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

this.increaseResult(this.interest);

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;

}

@Override
public String execute() throws Exception {

return super.SUCCESS;

}


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


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



public JFreeChart getChart() {

chart = ChartFactory.createBarChart("兴趣统计结果", "项目", "结果", this.getDataset(), PlotOrientation.VERTICAL, false, false, false);

chart.setTitle(new TextTitle("兴趣统计结果", new Font("黑体",Font.BOLD,22)));

CategoryPlot plot = (CategoryPlot)chart.getPlot();

//设置横轴s
CategoryAxis categoryAxis = plot.getDomainAxis();

categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,12));

categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

//设总走
NumberAxis numberAxis = new NumberAxis();
numberAxis.setLabelFont(new Font("宋体",Font.BOLD,12));


return chart;
}


public void setChart(JFreeChart chart) {
this.chart = chart;
}


}

web.xml配置

<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>

Struts.xml配置(注意:struts必须继承jfreechart-default)

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

基本思路:
在Action中定义private JFreeChart chart; private List<String> interest;两个属性,并写出get,set方法.(注意JFreeChart对象的属性值能为chart)
从JSP页面中提交数据到Action中,在Action中通过List<String> interest 属性接受值,并将值通过increaseResult()方法放入到Applicatoin属性范围中,然后在getDataset()方法中取出Applicatoin属性范围的值并生成树状图数据集,当execute()方法返回结果,然后Struts会调用Action中的chart属性的getChart()方法,当调用getChart()方法时,会产生一个JFreeChart对象,返回到页面会生成树状图图标

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics