jcommon-1.0.16.jar
jfreechart-1.0.13.jar
2.在web中配置 JfreeChart
jcPie com.sh.servlet.jcPie jcPie /servlet/jcPie DisplayChart org.jfree.chart.servlet.DisplayChart DisplayChart /DisplayChart index.jsp
3.编写 jcPie servlet
package com.sh.servlet; 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.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset; public class jcPie extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置 饼图默认的数据集 DefaultPieDataset dataset=new DefaultPieDataset(); dataset.setValue("古典名著", 0.3); dataset.setValue("青春校园", 0.1); dataset.setValue("儿童读物", 0.3); dataset.setValue("异国风情", 0.1); dataset.setValue("报刊杂志", 0.2); JFreeChart chart=ChartFactory.createPieChart3D("图书销量", dataset, true, true, false); PiePlot pieplot=(PiePlot) chart.getPlot(); //定义一个饼图对象 pieplot.setLabelFont(new Font("宋体",0,12)); TextTitle txtTitle=null; txtTitle=chart.getTitle(); Font font=new Font("宋体",Font.BOLD,16); txtTitle.setFont(font); chart.getLegend().setItemFont(font); //表示在web服务器 临时目录中生成一个图片 String filename=ServletUtilities.saveChartAsPNG(chart, 450, 300, null,request.getSession()); String graphURL=request.getContextPath()+"/DisplayChart?filename="+filename; System.out.println(graphURL); request.setAttribute("imgURL", graphURL); this.getServletContext().getRequestDispatcher("/pie.jsp").forward(request, response); } }
4.index.jsp 做请求 到jfpie servlet 然后转发到 pie.jsp
index.jsp
pie.jsp
5.访问 http://localhost:8083/JfreeChart/