Struts2结合JFreeChart制作统计图表实例教程



Struts2结合JFreeChart制作统计图表实例项目介绍。

项目结构示意图:http://blog.sina.com.cn/s/blog_586b6c050100c4d0.html

Struts2结合JFreeChart制作统计图表

struts.properties文件内容:

struts.locale=zh_CN
struts.i18n.encoding=GBK

struts.xml文件内容:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN” “http://struts.apache.org/dtds/struts-2.0.dtd”>
<struts>
<include file=”struts-default.xml” />
<package name=”struts2″ extends=”jfreechart-default”>
<action name=”vote”>
<result name=”success” type=”chart”>
<param name=”height”>600</param>
<param name=”width”>800</param>
</result>
</action>
</package>
</struts>

VoteAction.java文件内容:

package com.luo.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.StandardChartTheme;
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.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;


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

public class VoteAction extends ActionSupport {
private List<String> interest;
private JFreeChart chart;
public JFreeChart getChart() {
return chart;
}
public void setChart() {
StandardChartTheme standardChartTheme = new StandardChartTheme(“name”);
standardChartTheme.setLargeFont(new Font(“楷体”, Font.BOLD, 12));// 可以改变轴向的字体
standardChartTheme.setRegularFont(new Font(“宋体”, Font.BOLD, 12));// 可以改变图例的字体
standardChartTheme.setExtraLargeFont(new Font(“隶书”, Font.BOLD, 12));// 可以改变图标的标题字体
ChartFactory.setChartTheme(standardChartTheme);// 设置主题
JFreeChart chart1 = ChartFactory.createBarChart(“投票系统”, “投票类别”, “投票结果”,
this.createDataset(), PlotOrientation.VERTICAL, false, false,
false);
CategoryPlot plot = (CategoryPlot) chart1.getPlot();
CategoryAxis categoryAxis = plot.getDomainAxis(); // 取得横轴
categoryAxis.setLabelFont(new Font(“宋体”, Font.ITALIC, 22));
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();// 取得纵轴
numberAxis.setLabelFont(new Font(“宋体”, Font.ITALIC, 22));
this.chart = chart1;
}
public List<String> getInterest() {
return interest;
}
public void setInterest(List<String> interest) {
this.interest = interest;
}
@Override
public String execute() throws Exception {
setChart();
return SUCCESS;
}
@SuppressWarnings(“unchecked”)
public void increamentResult(List<String> voteList) {
ActionContext context = ActionContext.getContext();
Map map = context.getApplication();
for (String vote : voteList) {
if (null == map.get(vote)) {
map.put(vote, 1);
} else {
map.put(vote, ((Integer) map.get(vote)).intValue() + 1);
}
}
}
public CategoryDataset createDataset() {
DefaultCategoryDataset category = new DefaultCategoryDataset();
ActionContext context = ActionContext.getContext();
Map map = context.getApplication();
this.increamentResult(this.interest);
category.setValue((Integer) map.get(“football”), “”, “足球”);
category.setValue((Integer) map.get(“featherball”), “”, “羽毛球”);
category.setValue((Integer) map.get(“pingpong”), “”, “乒乓球”);
category.setValue((Integer) map.get(“basketball”), “”, “篮球”);
return category;
}
}

web.xml文件内容:

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″ xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

Struts2结合JFreeChart制作统计图表
<welcome-file-list>
<welcome-file>vote.jsp</welcome-file>
</welcome-file-list>
</web-app>

vote.jsp文件内容:

<%@ page language=”java” pageEncoding=”GBK”%>
<%@taglib prefix=”s” uri=”/struts-tags”%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<meta http-equiv=”pragma” content=”no-cache”>
<meta http-equiv=”cache-control” content=”no-cache”>
<meta http-equiv=”expires” content=”0″>
</head>

<body>
<s:form action=”vote”>
<s:checkbox name=”interest” fieldValue=”football” label=”足球”
labelposition=”left”></s:checkbox>
<s:checkbox name=”interest” fieldValue=”basketball” label=”篮球”
labelposition=”left”></s:checkbox>
<s:checkbox name=”interest” fieldValue=”featherball” label=”羽毛球”
labelposition=”left”></s:checkbox>
<s:checkbox name=”interest” fieldValue=”pingpong” label=”乒乓球”
labelposition=”left”></s:checkbox>
<s:submit label=”submit”></s:submit>
</s:form>
</body>
</html>
生成图表:

Struts2结合JFreeChart制作统计图表

以上程序调试通过!