首先我们来修改一下web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>/servlet/DisplayChart</url-pattern> </servlet-mapping> </web-app>
配置上DisplayChart就相当于完成了与Web的集成,JFreeChart现在就可以通过数据流将图片显示在页面上。
下面我们看一下生成JFreeChart的java代码:
package feng.five00.jfreechart;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
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.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
/**
* @author Five00
*
*/
public class PieImage {
public static PieDataset generatePieDataset(int allCount, ResultSet rs){
DefaultPieDataset dataset = new DefaultPieDataset();
try {
while (rs.next())
{
String TArea=rs.getString(1);
String TAreNum=rs.getString(2);
dataset.setValue(TArea, Integer.parseInt(TAreNum));
}
} catch (SQLException e) {
e.printStackTrace();
}
return dataset;
}
public static String createPie(PieDataset dataset,PrintWriter pw,HttpSession session){
String filename = null;
JFreeChart chart = ChartFactory.createPieChart3D("访问地址", dataset, true, true, false);
PiePlot plot = (PiePlot)chart.getPlot();
plot.setSectionOutlinesVisible(false);
plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
plot.setNoDataMessage("No data available");
plot.setForegroundAlpha(Float.parseFloat("0.4"));
plot.setCircular(false);
plot.setLabelGap(0.02);
plot.setToolTipGenerator(new StandardPieToolTipGenerator("{0}: ({1}M, {2})",
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}: ({1}M, {2})",
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
try {
filename = ServletUtilities.saveChartAsJPEG(chart, 500, 300, info, session);
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
}
这里我用到的数据集是通过JDBC的一个ResultSet对象传递过来的,不是重点,重点请看红字部分,在这里,通过ChartRenderingInfo获得了一个chart的info,然后通过ServletUtilities将chart传递给服务器并返回创建后的文件名,这个文件名是Servlet用到的,并不会产生到常见的文件夹中,具体在哪里我也不清楚,但是这个不是关键问题,关键是通过这个文件名就可以再经由Servlet来显示出图片。
下面来看一下显示图片的JSP,由于JSP文件太长,为了方便观看,这里只给出与显示图片有关的部分:
<jsp:useBean id="pie" scope="page" class="feng.five00.jfreechart.PieImage" />
<%String sql="Select Sum(TAddNum) From FAddress where admin='"+admin+"'";
Connection con = feng.getConn() ;
Statement stmt=con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int AllCount=0;
while (rs.next())
{
AllCount=rs.getInt(1);
};
%>
<%
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql="Select * From FAddress where admin='"+admin+"' Order By TAddNum DESC";
rs = stmt.executeQuery(sql) ;
{color:#ff0000}String filename = pie.createPie(pie.generatePieDataset(AllCount,rs),new PrintWriter(out),session);{color}
{color:#ff0000}String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;{color}
{color:#ff0000}%>{color}
{color:#ff0000}<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>">{color}
String filename = pie.createPie(pie.generatePieDataset(AllCount,rs),new PrintWriter(out),session);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>">
其中上面的红字代码就是如何将图片显示出来,看一下<imp src。。。>部分,应该就不难弄懂了。
这样,web项目中就能显示JFreeChart图表了.