把图表数据显示的jsp中.
首先我们建一个Servlet:
package
com;
![](https://i-blog.csdnimg.cn/blog_migrate/9723ec7779ef47d51f1f48b9337ffca0.gif)
import
java.awt.Color;
import
java.io.File;
import
java.io.IOException;
![](https://i-blog.csdnimg.cn/blog_migrate/9723ec7779ef47d51f1f48b9337ffca0.gif)
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
![](https://i-blog.csdnimg.cn/blog_migrate/9723ec7779ef47d51f1f48b9337ffca0.gif)
import
org.jfree.chart.ChartUtilities;
import
org.jfree.chart.JFreeChart;
import
org.jfree.chart.labels.StandardPieToolTipGenerator;
import
org.jfree.chart.plot.PiePlot3D;
import
org.jfree.chart.urls.StandardPieURLGenerator;
import
org.jfree.data.general.DefaultPieDataset;
![](https://i-blog.csdnimg.cn/blog_migrate/9723ec7779ef47d51f1f48b9337ffca0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/04e85f05536ccd79982bbca17251f63e.gif)
/** */
/**
*
* @author zdw
*
*/
@SuppressWarnings(
"
serial
"
)
public
class
ChartServlet
extends
HttpServlet
![](https://i-blog.csdnimg.cn/blog_migrate/04e85f05536ccd79982bbca17251f63e.gif)
{
public ChartServlet()
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
super();
}
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
public void init() throws ServletException
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
}
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
private void createPieChart(String path) throws IOException
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
// 饼图数据源
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("中国", 44.4);
dataset.setValue("美国", 25.5);
dataset.setValue("日本", 18.5);
dataset.setValue("刚果", 4.6);
dataset.setValue("英国", 5.5);
dataset.setValue("其他", 0.5);
// 生成3D饼图
PiePlot3D plot = new PiePlot3D(dataset);
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
// 设置图片链接
plot.setURLGenerator(new StandardPieURLGenerator(
"http://www.blogjava.net/supercrsky"));
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
JFreeChart chart = new JFreeChart("人口统计表",
JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(Color.gray);
ChartUtilities.saveChartAsJPEG(new File(path), chart, 800, 600);
}
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
System.out.println("get begin");
// 得到当前网站在服务上的绝对路径
String path = request.getSession().getServletContext().getRealPath("/");
// 保存到根目录
createPieChart(path + "/chart.jpg");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
System.out.println("post begin");
this.doGet(request, response);
}
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
public void destroy()
![](https://i-blog.csdnimg.cn/blog_migrate/a5ca97a1042a494993b61012f0c7f4e7.gif)
{
super.destroy();
}
![](https://i-blog.csdnimg.cn/blog_migrate/7a3782e39bce8b40e9b05a5e4fd452d8.gif)
}
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
>
ChartServlet
</
servlet-name
>
<
servlet-class
>
com.ChartServlet
</
servlet-class
>
</
servlet
>
![](https://i-blog.csdnimg.cn/blog_migrate/9723ec7779ef47d51f1f48b9337ffca0.gif)
<
servlet-mapping
>
<
servlet-name
>
ChartServlet
</
servlet-name
>
<
url-pattern
>
/servlet/ChartServlet
</
url-pattern
>
</
servlet-mapping
>
<
welcome-file-list
>
<
welcome-file
>
index.jsp
</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
index.jsp:
<%
@ page language="java" pageEncoding="gbk"
%>
<
html
>
<
head
>
<
title
>
人口统计表
</
title
>
</
head
>
<
body
>
<
hr
/>
<
img
src
="/jfreechart/chart.jpg"
/>
</
body
>
</
html
>
在浏览器中输入:
http://localhost:8888/jfreechart/servlet/ChartServlet
就可以看生成的饼图效果了.
源码可以
点此下载