==============================================
PieChart.java
==============================================
package cn.jxust.jfreechart;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
public class PieChart {
public static String generatePieChart(HttpSession session, PrintWriter pw,
int w, int h) {
/******** 基础数据 **********/
String filename = null;
double[] value = { 11, 9, 7 };
String[] keys = { "等级A", "等级B", "等级C" };
String chartTitle = "等级分布图";
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < value.length; i++) {
dataset.setValue(keys[i], value[i]);
}
JFreeChart chart = ChartFactory.createPieChart(chartTitle, dataset,
true, true, false);
chart.setTextAntiAlias(false);
// 图片背景色
chart.setBackgroundPaint(Color.white);
// 设置图标题的字体重新设置title
TextTitle title = new TextTitle(chartTitle);
title.setFont(new Font("隶书", Font.BOLD, 25));
chart.setTitle(title);
// 图例显示格式
chart.getLegend().setItemFont(new Font("SansSerif", Font.BOLD, 12));
PiePlot plot = (PiePlot) chart.getPlot();
// 图片中显示百分比:默认方式
// 指定饼图轮廓线的颜色
plot.setBaseSectionOutlinePaint(Color.BLACK);
plot.setBaseSectionPaint(Color.BLACK);
// 设置无数据时的信息
plot.setNoDataMessage("无对应的数据,请重新查询。");
// 设置无数据时的信息显示颜色
plot.setNoDataMessagePaint(Color.red);
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0} : {1} ({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
/*
* // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
* plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
* "{0}={1}({2})"));
*/
// 图片说明显示格式
plot.setLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));
// 指定图片的透明度(0.0-1.0)
plot.setForegroundAlpha(0.65f);
// 指定显示的饼图上圆形(true)还椭圆形(false)
plot.setCircular(true, true);
// 设置第一个 饼块section 的开始位置,默认是12点钟方向
plot.setStartAngle(65);
// 设置分饼颜色
// plot.setSectionPaint(keys[0], new Color(244, 194, 144));
// plot.setSectionPaint(keys[1], new Color(144, 233, 144));
// plot.setSectionPaint(keys[2], new Color(144, 253, 244));
try {
/*------得到chart的保存路径----*/
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,
session);
/*------使用printWriter将文件写出----*/
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return filename;
}
}
==============================================
BarChart.jsp
==============================================
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.io.PrintWriter"%>
<jsp:directive.page import="cn.jxust.jfreechart.*" />
<html>
<head>
<title></title>
<%
//饼状图
String fileNamePie = PieChart.generatePieChart(session,
new PrintWriter(out), 580, 250);
String graphURLPie = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + fileNamePie;
%>
</head>
<body bgcolor="#ffffff">
<table align="center" width="580" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td>
<img src=" <%=graphURLPie%>" width=580 height=250 border=0>
</td>
</tr>
</table>
</body>
</html>
============================================
web.xml
============================================
<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>/servlet/DisplayChart </url-pattern>
</servlet-mapping>