饼状图入门实例

1、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.jfree.data.general.DefaultPieDataset"%>
<%@page import="org.jfree.chart.plot.PiePlot"%>
<%@page import="org.jfree.chart.plot.Plot"%>
<%@page import="org.jfree.chart.JFreeChart"%>
<%@page import="org.jfree.chart.ChartRenderingInfo"%>
<%@page import="org.jfree.chart.entity.StandardEntityCollection"%>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%
	DefaultPieDataset dataSet = new DefaultPieDataset();
	//设置数据
	dataSet.setValue("java",17.509);
	dataSet.setValue("c",17.279);
	dataSet.setValue("php",17.509);
	dataSet.setValue("C++",9.908);
	dataSet.setValue("(Visual) Basic",6.574);
	dataSet.setValue("其他",7.509);
	PiePlot plot = new PiePlot(dataSet);
	//创建图表
	JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT,plot,true);
	chart.setTitle("2010年 3月编程语言排行榜");
	
	ChartRenderingInfo rinfo = new ChartRenderingInfo(new StandardEntityCollection());
	//生成的图表图片
	String filename = ServletUtilities.saveChartAsPNG(chart,400,300,rinfo,session);
	//图表图片的URL
	String graphUrl = request.getContextPath()+"/servlet/DisplayChart?filename="+filename;
	
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>饼状图实例——2010年 3月编程语言排行榜</title>
  </head>
  <body>
  	<p align="center">
  		<img alt="2010年 3月编程语言排行榜" src="<%=graphUrl %>" width="400" height="300">
  	</p>
  </body>
</html>


2、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>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


3、效果图

饼状图入门实例

你可能感兴趣的:(java,编程,jsp,servlet,jfreechart)