下载jfreechart框架包,百度上可以查到,开源的。这个是csdn上的
http://download.csdn.net/source/3108268
<!--[if !supportLists]-->二.<!--[endif]-->配置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>
<!--[if !supportLists]-->三.<!--[endif]-->编写类
代码如下:
package com.qm;
import java.awt.Color;
import java.awt.Font;
import java.io.PrintWriter;
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.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
public class AA {
//在此方法内填入数据,key-value.value为数字。
private static PieDataset createDataset()
{
DefaultPieDataset dpd=new DefaultPieDataset();
dpd.setValue("A", 1);
dpd.setValue("B", 2);
dpd.setValue("C", 3);
dpd.setValue("d", 4);
dpd.setValue("E", 5);
return dpd;
}
private static JFreeChart createChart(PieDataset dpd)
{
JFreeChart jfc=ChartFactory.createPieChart3D("",dpd,true,true,false); //这个有3D效果
//JFreeChart jfc=ChartFactory.createPieChart("",dpd,true,true,false); //这个没有有3D效果
jfc.setBackgroundPaint(Color.white); //背景色
jfc.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体
PiePlot plot= (PiePlot) jfc.getPlot();//获取图表区域对象
plot.setLabelFont(new Font("宋体",Font.BOLD,10));
jfc.getLegend().setItemFont(new Font("宋体",Font.BOLD,10));
return jfc;
}
public static String generateBarChart(HttpSession session, PrintWriter pw)
{
String filename=null;
PieDataset cd=createDataset();
JFreeChart chart=createChart(cd);
ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
try {
filename=ServletUtilities.saveChartAsPNG(chart, 800, 400, info,session);//设置生成的图片的大小
ChartUtilities.writeImageMap(pw, filename, info, false);
} catch (Exception e) {
e.printStackTrace();
}
pw.flush();
return filename;
}
}
<!--[if !supportLists]-->四. <!--[endif]-->在jsp中使用图表
代码如下:
<%@page import="com.qm.AA"%>
<%@page import="java.io.PrintWriter"%>
<%
String filename=AA.generateBarChart(request.getSession(),new PrintWriter(out));
String graphURl = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
<img alt="分析图" src="<%=graphURl %>" width="800" height="400">
%>