jfreechart例子
+------+-------------+| id | client_name |+------+-------------+| 1 | 一级经销商 || 2 | 二级经销商 || 3 | 三级经销商 |+------+-------------+3 rows in set (0.00 sec)package com;import java.io.IOException;import java.text.DecimalFormat;import java.text.NumberFormat;import java.util.Iterator;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServlet;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.plot.PiePlot3D;import org.jfree.data.general.DefaultPieDataset;public class ChartDemoServlet extends HttpServlet {private static final long serialVersionUID = 1L;public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException {res.setContentType("image/jpeg");DefaultPieDataset data = getDataSet();req.setCharacterEncoding("UTF-8");res.setCharacterEncoding("UTF-8");JFreeChart chart = ChartFactory.createPieChart3D("I LOVE YOU", data,true, false, false);PiePlot3D plot = (PiePlot3D) chart.getPlot();// 图片中显示百分比:默认方式// plot.setLabelGenerator(new// StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));ChartUtilities.writeChartAsJPEG(res.getOutputStream(),1.0f, chart, 400, 300, null);}/** * * 获取一个演示用的简单数据集对象 * * @return */private static DefaultPieDataset getDataSet() {DefaultPieDataset dataset = new DefaultPieDataset();Map map = StateReportManger.getIntense().getClientLevelCount(); // 调用getClientLevelCount()得到所有的数据for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {// 遍历,用到Map的.// entrySet()方法Map.Entry entry = (Map.Entry) iter.next();dataset.setValue((String) entry.getKey(),Double.parseDouble(String.valueOf(entry.getValue())));// 设置数据}return dataset;}}在浏览器上输入,http://localhost:8080/JFreeChartTest/ChartDemoServlet