1.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
jcommon-1.0.16.jar
jfreechart-1.0.13.jar
ognl-3.0.5.jar
struts2-core-2.3.4.jar
struts2-jfreechart-plugin-2.3.4.1.jar
xwork-core-2.3.4.jar
2.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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
<servlet>
<servlet-name>jcPie</servlet-name>
<servlet-class>com.sh.servlet.jcPie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jcPie</servlet-name>
<url-pattern>/servlet/jcPie</url-pattern>
</servlet-mapping>
-->
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.PieAction
package com.sh.action;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import com.opensymphony.xwork2.ActionSupport;
public class PieAction extends ActionSupport {
private JFreeChart chart;
public JFreeChart getChart() {
//设置 饼图默认的数据集
DefaultPieDataset dataset=new DefaultPieDataset();
dataset.setValue("古典名著", 0.3);
dataset.setValue("青春校园", 0.1);
dataset.setValue("儿童读物", 0.3);
dataset.setValue("异国风情", 0.1);
dataset.setValue("报刊杂志", 0.2);
chart=ChartFactory.createPieChart3D("图书销量", dataset, true, true, false);
PiePlot pieplot=(PiePlot) chart.getPlot();
//定义一个饼图对象
pieplot.setLabelFont(new Font("宋体",0,12));
TextTitle txtTitle=null;
txtTitle=chart.getTitle();
Font font=new Font("宋体",Font.BOLD,16);
txtTitle.setFont(font);
chart.getLegend().setItemFont(font);
return chart;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
}
4.struts.xml
<?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"/>
<package name="default" extends="jfreechart-default">
<action name="pieAction" class="com.sh.action.PieAction">
<result name="success" type="chart">
<!-- 如果不设置这个 大小 默认为 200*150 -->
<param name="width">450</param>
<param name="height">300</param>
</result>
</action>
</package>
</struts>
5.访问 http://localhost:8083/JfreeChart/pieAction.action