1.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>
2.javabean
package com.logcd.jchart;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
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.StandardPieToolTipGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.general.Dataset;
import org.jfree.data.general.DefaultPieDataset;
public class MapPieChart {
/**
* 数据集
*
* @return
*/
public static DefaultPieDataset getDataset() {
//DatasetUtilities.createPieDatasetForColumn(arg0, arg1)
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("苹果", 100);
pieDataset.setValue("梨子", 200);
pieDataset.setValue("葡萄", 300);
pieDataset.setValue("香蕉", 400);
pieDataset.setValue("荔枝", 500);
return pieDataset;
}
public static String createPieChart(Dataset dataset,String title, int width, int height,
HttpSession session, PrintWriter pw,String useMap,boolean is3D) {
String filename = "";
PiePlot plot = null;
if(is3D){
plot = new PiePlot3D((DefaultPieDataset)dataset);// 3D饼图
}else{
plot = new PiePlot((DefaultPieDataset)dataset);
}
plot.setURLGenerator(new StandardPieURLGenerator("view.jsp","fruit","pieIdex"));// 设定热区超链接
JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT,
plot, true);
chart.setBackgroundPaint(java.awt.Color.white);// 可选,设置图片背景色
chart.setTitle(title);// 可选,设置图片标题
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
plot.setNoDataMessage("无数据显示");
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
try {
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info,
session);
ChartUtilities.writeImageMap(pw, useMap, info, false);// 输出MAP信息
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
}
3.在jsp中调用
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="com.logcd.jchart.MapPieChart"%>
<%@ page import="java.io.PrintWriter"%>
<HTML>
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=GBK">
</HEAD>
<BODY>
<%
String filename = MapPieChart.createPieChart(MapPieChart
.getDataset(), "水果产量图",480, 350, session, new PrintWriter(out),
"imgMap", false);
String file = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + filename;
String filename1 = MapPieChart.createPieChart(MapPieChart
.getDataset(), "水果产量图",480, 350, session, new PrintWriter(out),
"imgMap1", true);
String file1 = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + filename1;
%>
<P ALIGN="CENTER">
<img src="<%=file%>" border=0 usemap="#imgMap">
</P>
<P ALIGN="CENTER">
<img src="<%=file1%>" border=0 usemap="#imgMap1">
</P>
</BODY>
</HTML>