饼状图入门实例

本文展示了一个使用JFreeChart库生成的2010年3月编程语言排名饼状图。该图表展示了当时流行的编程语言如Java、C、PHP等的市场份额占比,并提供了实现这一饼状图的JSP代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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、效果图

[img]http://dl.iteye.com/upload/attachment/215141/3e94b2d7-2bd8-3013-8994-386f7dbef836.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值