初学者一个关于JFreeChart的问题
我在网上查了不少关于这方面的资料
各有说法,我其中一方法
将jfreechart-1.0.0.zip中的lib路径下的jar文件拷贝到tomcat的common/lib路径下
3. 修改tomcat的conf/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>
测试demo文件:http://localhost:8080/jfreechart.jsp,当你看到图片就说明jfreechart配置成功啦!
jfreechart.jsp代码:
<%@ page contentType= "text/html;charset=GBK "%>
<%@ page import= "org.jfree.data.general.DefaultPieDataset "%>
<%@ page import= "org.jfree.chart.JFreeChart "%>
<%@ page import= "org.jfree.chart.plot.PiePlot "%>
<%@ page import= "org.jfree.chart.ChartRenderingInfo "%>
<%@ page import= "org.jfree.chart.servlet.ServletUtilities "%>
<%@ page import= "org.jfree.chart.urls.StandardPieURLGenerator "%>
<%@ page import= "org.jfree.chart.entity.StandardEntityCollection "%>
<%@ page import= "org.jfree.chart.encoders.SunPNGEncoderAdapter "%>
<%
DefaultPieDataset data = new DefaultPieDataset();
data.setValue( "六月 ", 500);
data.setValue( "七月 ", 580);
data.setValue( "八月 ", 828);
PiePlot plot = new PiePlot(data);
JFreeChart chart = new JFreeChart( " ", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(java.awt.Color.white); //可选,设置图片背景色
chart.setTitle( "Welcome to Jfreechart ! "); //可选,设置图片标题
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
//500是图片长度,300是图片高度
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename= " + filename;
%>
<HTML>
<HEAD>
<TITLE> Welcome to Jfreechart ! </TITLE>
</HEAD>
<BODY>
<P ALIGN= "CENTER ">
<img src= " <%= graphURL %> " width=500 height=300 border=0 usemap= "# <%= filename %> ">
</P>