JfreeChart的简单应用

1.在web project 中加入jar
jcommon-1.0.16.jar
jfreechart-1.0.13.jar

2.在web中配置 JfreeChart
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
    <servlet-name>jcPie</servlet-name>
    <servlet-class>com.sh.servlet.jcPie</servlet-class>
</servlet>

  <servlet-mapping>
    <servlet-name>jcPie</servlet-name>
    <url-pattern>/servlet/jcPie</url-pattern>
  </servlet-mapping>

<!--配置   JfreeChart-->
<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>/DisplayChart</url-pattern>
</servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



3.编写 jcPie  servlet
package com.sh.servlet;
import java.awt.Font;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;


public class jcPie extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置 饼图默认的数据集 
		DefaultPieDataset dataset=new DefaultPieDataset();
		dataset.setValue("古典名著", 0.3);
		dataset.setValue("青春校园", 0.1);
		dataset.setValue("儿童读物", 0.3);
		dataset.setValue("异国风情", 0.1);
		dataset.setValue("报刊杂志", 0.2);
		
		JFreeChart chart=ChartFactory.createPieChart3D("图书销量", dataset, true, true, false);
		
		PiePlot pieplot=(PiePlot) chart.getPlot();
		//定义一个饼图对象
		pieplot.setLabelFont(new Font("宋体",0,12));
		
		TextTitle txtTitle=null;
		txtTitle=chart.getTitle();
		Font font=new Font("宋体",Font.BOLD,16);
		txtTitle.setFont(font);
		chart.getLegend().setItemFont(font);
		
		//表示在web服务器 临时目录中生成一个图片
		String filename=ServletUtilities.saveChartAsPNG(chart, 450, 300, null,request.getSession());
		String graphURL=request.getContextPath()+"/DisplayChart?filename="+filename;
		System.out.println(graphURL);
		request.setAttribute("imgURL", graphURL);
		this.getServletContext().getRequestDispatcher("/pie.jsp").forward(request, response);
		
	}
}


4.index.jsp  做请求 到jfpie servlet   然后转发到 pie.jsp
  index.jsp
<body onload="window.location.href='${pageContext.request.contextPath}/servlet/jcPie'">
 </body>


pie.jsp
 <body>
   	<img alt="pie" src="${imgURL}">
  </body>


5.访问 http://localhost:8083/JfreeChart/

你可能感兴趣的:(jfreechart)