日期:2014-05-18  浏览次数:20711 次

如何将JFreeChart生成的图片转换到web页面上?
最近工作要实现个图表分析功能
碰见了这个问题
求教诸位高人

------解决方案--------------------
去看看这个例子吧。
http://homepage.ntlworld.com/richard_c_atkinson/jfreechart/
http://homepage.ntlworld.com/richard_c_atkinson/jfreechart/jfreechart-sample.war
------解决方案--------------------
写一个 Servlet,在 doGet() 中生成 JFreeCart 的图表,再做以下设置

OutputStream out = response.getOutputStream();
response.setContentType( "image/jpeg ");
// out,是一个OutputStream;
// chart,是你生成的 JFreeChart 类的对象
// 后面两个是图片的大小
ChartUtilities.writeChartAsJPEG(out, chart, 400, 300);

或使用 ChartUtilities.writeChartAsPNG,要将 response.setContentType 设为 “image/png”。


配置一下 web.xml,加上这一 Servlet,在页面中将 <img> 的src属性设为该 Servlet 的 URL,就可以显示该图表了。
------解决方案--------------------
<%@ page contentType= "text/html;charset=UTF-8 "%>
<%@ page import= "org.jfree.data.general.DefaultPieDataset "%>
<%@ page import= "org.jfree.chart.* "%>
<%@ page import= "org.jfree.chart.plot.* "%>
<%@ page import= "org.jfree.chart.servlet.ServletUtilities "%>
<%@ page import= "org.jfree.chart.labels.StandardPieToolTipGenerator "%>
<%@ page import= "org.jfree.chart.urls.StandardPieURLGenerator "%>
<%@ page import= "org.jfree.chart.entity.StandardEntityCollection "%>
<%@ page import= "java.io.* "%>

<HTML>
<HEAD>
<META http-equiv=Content-Type content= "text/html; charset=GBK ">
<META NAME= "Author " CONTENT= "Alpha ">
<TITLE> test </TITLE>
</HEAD>
<BODY>
<%
DefaultPieDataset data = new DefaultPieDataset();

data.setValue( "test1 ",380);
data.setValue( "test2 ",8904);
data.setValue( "test3 ",6100);
data.setValue( "test4 ",1620);
data.setValue( "test5 ",3520);
data.setValue( "test6 ",180);

PiePlot3D plot = new PiePlot3D(data);

JFreeChart chart = new JFreeChart( " ",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(java.awt.Color.white);
chart.setTitle( "测试 ");
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(out);

String filename = ServletUtilities.saveChartAsJPEG(chart,500,300,info,session);
ChartUtilities.writeImageMap(w, "img01 ", info, false);

String graphURL = request.getContextPath() + "/servlet/MyChart?filename= " + filename;
%>

<P ALIGN= "CENTER ">
<img src= " <%= graphURL %> " width=500 height=300 border=0 usemap= "#img01 ">
</P>
<p align= "center "> 测试日期 2007-07-05, 仅供参考 </p>
</BODY>
</HTML>

参考这个小例子
------解决方案--------------------
请参考以下代码:

import java.io.FileOutputStream;
import java.io.I