檔案配置
<?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">
<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>
</web-app>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset,org.jfree.chart.title.TextTitle,java.awt.Font,org.jfree.chart.plot.PiePlot,org.jfree.chart.JFreeChart,org.jfree.chart.ChartFactory,org.jfree.chart.ChartFrame" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("宣傳部",25);
dataset.setValue("外聯部",23);
dataset.setValue("財務部",25);
dataset.setValue("技術部",12);
JFreeChart chart = ChartFactory.createPieChart3D("某社團各部門",dataset,true,true,false);
//設定餅狀圖的 主標題字型
chart.setTitle(new TextTitle("某社團各部門",new Font("微軟雅黑",Font.BOLD,18)));
PiePlot plot = (PiePlot) chart.getPlot();
//設定餅狀圖體裡的的各個標籤字型
plot.setLabelFont(new Font("微軟雅黑",Font.BOLD,12));
//設定圖表下方的圖例說明字型
chart.getLegend().setItemFont(new Font("微軟雅黑",Font.BOLD,12));
String fileName = ServletUtilities.saveChartAsPNG(chart,800,600,session);
String url = request.getContextPath()+"/DisplayChart?filename="+fileName;
%>
<img src="<%=url %>" width="800" height="600">
</body>
</html>
運行結果:
在tomcat的temp目錄下會有產生的餅狀圖