网站建议:179001057@qq.com

jfreechart配置与应用

技术2022-05-12  3

(1)

    下载jfreechar获取jar包

(2)

    将下载的zip解压,获取文件夹中lib中的*.jar包

(3)

   将*.jar导入到你建的WEB工程中去,也就是lib目录下;

(4)

   更改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>

(5)

    以饼状图为例:

    在你的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"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%  DefaultPieDataset data = new DefaultPieDataset();  data.setValue("一月", 800);  data.setValue("二月", 1200);  data.setValue("三月", 3562);   data.setValue("四月", 20);  data.setValue("五月", 2569);  data.setValue("六月", 1052);     PiePlot plot = new PiePlot(data);  JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);    chart.setBackgroundPaint(java.awt.Color.white);  //可选,设置图片背景色  chart.setTitle("上半年工作完成状况"); //可选,设置图片标题  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></BODY></HTML>

 

(6)

     部署到Tomcat上,运行就可以了!

      


最新回复(0)