Posted on 2010-07-06 17:33
幻海蓝梦 阅读(960)
评论(0) 编辑 收藏 所属分类:
Java-eclipse,svn 、
网管--拓扑图
转自:
http://hi.baidu.com/zh_m_zhou/blog/item/2772d017d9f39059f3de3218.html
作者:pocky
JFreeChart是利用已知数据生成饼图、柱状图(普通柱状图以及堆栈柱状图)、线图、区域图、分布图、混合图、甘特图以及一些仪表盘的一个开源项
目。本demo的就是关于如何结合数据生成一个与之对应的饼图。
1. 环境:
windowsXP,
eclipse3.3,
tomcat6.0,
jfreechart-1.0.12
2,下载。到以下地址下载jfreechart-1.0.12的zip包
http://www.jfree.org/jfreechart/index.html
3.
在eclipse下新建一个web工程,解压刚下载的zip包,将其中的jcommon-1.0.15.jar和jfreechart-
1.0.12.jar复制到web工程的lib目录下
4. 新建一个servlet,方法如下:
在web.xml中添加:
<servlet>
<servlet-name>IndexServlet</servlet-name>
<servlet-class>com.demo.IndexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>IndexServlet</servlet-name>
<url-pattern>/index.do</url-pattern>
</servlet-mapping>
新建类,内容如下:
package com.demo;
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.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
public class IndexServlet extends HttpServlet{
/**
* 获取一个演示用的简单数据集对象
* @return
*/
private static DefaultPieDataset getDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("苹果",100);
dataset.setValue("梨子",200);
dataset.setValue("葡萄",300);
dataset.setValue("香蕉",400);
dataset.setValue("荔枝",500);
return dataset;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse
resp)
throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse
resp)
throws ServletException, IOException {
resp.setCharacterEncoding("gb2312");
resp.setContentType("image/jpeg");
DefaultPieDataset data=getDataSet();
JFreeChart chart=ChartFactory.createPieChart3D("水果产量图", data,
true, false, false);
Font font = new Font("SimSun", 10, 20);
TextTitle txtTitle = null;
txtTitle = chart.getTitle();
txtTitle.setFont(font);
PiePlot pieplot = (PiePlot)chart.getPlot();
pieplot.setLabelFont(font);
chart.getLegend().setItemFont(font);
try {
ChartUtilities.writeChartAsJPEG(resp.getOutputStream(),
chart, 600, 400);
System.out.print("OK");
//this.getServletContext().getRequestDispatcher("/index2.jsp").forward(req,
resp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.将工程部署到tomcat下,启动tomcat,访问http://server/appName/index.do。至此,一个简单的demo完
成。
参考文档:
JFREECHART DEMO1
http://blog.sina.com.cn/s/blog_4ce6c3f80100bydr.html
JFREECHART DEMO2
http://hi.baidu.com/javafanc/blog/item/7da708b544fc16c936d3cade.html
JFREECHART 乱码
http://topic.csdn.net/u/20071207/11/41ef3b10-043b-496e-8dd6-a501b4c2932c.html