## jfreechart配合struts2简单配置及使用(饼图) ##
第一步:在web项目的lib目录下导入如下jar包
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
freemarker-2.3.22.jar
javassist-3.11.0.GA.jar
jcommon-1.0.23.jar
jfreechart-1.0.19.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
ognl-3.0.6.jar
struts2-core-2.3.24.1.jar
struts2-dojo-plugin-2.3.24.1.jar
struts2-jfreechart-plugin-2.3.24.1.jar
xwork-core-2.3.24.1.jar
第二步:配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>jfreechart</display-name>
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2 end -->
<!-- jfreechart 报表 -->
<servlet>
<servlet-name>jfreechart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jfreechart</servlet-name>
<url-pattern>/chart</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第三步配置在src目录下配置struts文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8" />
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.action.extension" value="action,," />
<constant name="struts.ui.theme" value="simple" />
<!-- 设置struts2不拦截/chart -->
<constant name="struts.action.excludePattern" value="/chart" />
<package name="default" extends="struts-default,jfreechart-default" namespace="/">
<action name="test" class="com.struts.action.Test">
<result name="success" type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
<action name="demo" class="com.struts.action.Demo">
<result name="success" type="chart">
<param name="width">800</param>
<param name="height">500</param>
</result>
</action>
</package>
</struts>
第四步:编写饼图测试jsp
<%@page import="org.jfree.chart.plot.PiePlot"%>
<%@page import="org.jfree.chart.title.LegendTitle"%>
<%@page import="java.awt.Font"%>
<%@page import="org.jfree.chart.title.TextTitle"%>
<%@ page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page import="org.jfree.chart.JFreeChart"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>JFreeChart</title>
<style>
body{
padding:50px;
text-align:center;
}
</style>
</head>
<body>
<%
DefaultPieDataset dpd = new DefaultPieDataset();
dpd.setValue("管理人员", 25);
dpd.setValue("市场人员", 125);
dpd.setValue("设计人员", 145);
dpd.setValue("开发人员", 45);
dpd.setValue("其他人员", 10);
String i = "某公司组织结构图";
i = new String(i.getBytes("utf-8"),"utf-8");
JFreeChart chart = ChartFactory.createPieChart(i,dpd, true, false, false);
Font f = new Font("宋体",Font.BOLD,20);
Font tf = new Font("宋体",Font.BOLD,40);
//图表标题中文
TextTitle textTitle = chart.getTitle();
textTitle.setFont(tf);
//图表图例中文
LegendTitle legend = chart.getLegend();
if (legend!=null) {
legend.setItemFont(f);
}
//饼图区域中文
PiePlot plot = (PiePlot)chart.getPlot();
plot.setLabelFont(f);
String fn = ServletUtilities.saveChartAsPNG(chart,800,600,session);
//ServletUtilities是面向web开发的工具类,返回一个字符串文件名,文件名自动生成,生成好的图片会自动放在服务器(tomcat)的临时文件下(temp)
String url = request.getContextPath() + "/chart?filename=" + fn;
//根据文件名去临时目录下寻找该图片,这里的/DisplayChart路径要与配置文件里用户自定义的<url-pattern>一致
%>
<img src="<%= url %>" alt="">
</body>
</html>
附加: 在src目录下建立com.struts.action包,建立两个类Dmeo,Test
(struts官方jfreechart案例)
(1)Demo
package com.struts.action;
import java.awt.Font;
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.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import com.opensymphony.xwork2.ActionSupport;
public class Demo extends ActionSupport {
private JFreeChart chart;
public String execute() throws Exception {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(100, "", "足球");//更新成最新值
dataset.setValue(20, "", "篮球");
dataset.setValue(5, "", "排球");
dataset.setValue(60, "", "羽毛球");
chart = ChartFactory.createBarChart("兴趣统计结果", "项目", "结果",dataset, PlotOrientation.VERTICAL, false, false, false);
chart.setTitle(new TextTitle("兴趣统计结果",new Font("黑体",Font.BOLD,22)));
CategoryPlot plot = (CategoryPlot)chart.getPlot();
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,22));
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//设置角度
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setLabelFont(new Font("宋体",Font.BOLD,22));
LegendTitle legend = chart.getLegend();
if (legend!=null) {
legend.setItemFont(new Font("宋体", Font.BOLD, 20));
}
CategoryAxis domainAxis = plot.getDomainAxis();//(柱状图的x轴)
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体
domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴上的标题的字体
return "success";
}
public JFreeChart getChart() {
return chart;
}
}
(2)Test
package com.struts.action;
import java.util.Random;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class Test {
private JFreeChart chart;
public String execute() throws Exception {
// chart creation logic...
XYSeries dataSeries = new XYSeries(new Integer(1)); // pass a key for
// this serie
for (int i = 0; i <= 100; i++) {
dataSeries.add(i, new Random().nextInt());
}
XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);
ValueAxis xAxis = new NumberAxis("Raw Marks");
ValueAxis yAxis = new NumberAxis("Moderated Marks");
// set my chart variable
chart = new JFreeChart("Moderation Function", JFreeChart.DEFAULT_TITLE_FONT, new XYPlot(xyDataset, xAxis, yAxis, new StandardXYItemRenderer(StandardXYItemRenderer.LINES)), false);
chart.setBackgroundPaint(java.awt.Color.white);
return "success";
}
public JFreeChart getChart() {
return chart;
}
}
第五步:编写jsp页面测试
<%@ page language="java" pageEncoding="UTF-8"%>
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>JFreeChart</title>
<style>
body{
padding:50px;
text-align:center;
}
</style>
</head>
<body>
<img src="demo">
<img src="test">
</body>
</html>
第六步:运行项目输入http://localhost:8080/项目名/test即可成功看到jfreechart效果