前面我们讲了 JFreeChart 折线图 JFreeChart 柱形图
1.JFreeChart 3D饼图:
1.所需要的架包:架包下载地址:架包下载 因为是我上传的免费下载。下载后解压,所需的架包在lib文件夹中2.编写Servlet控制类:3.编写JSP显示页面:public static String getPieChart(HttpSession session) throws Exception { //创建数据集对象 //如果要从数据库中获取数据 //你只需把数据填写到此数据集中就OK了 DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("黑心矿难", 1000); dataset.setValue("醉酒驾驶", 800); dataset.setValue("城管强拆", 400); dataset.setValue("医疗事故", 100); dataset.setValue("其他", 29); JFreeChart chart=ChartFactory.createPieChart3D("非正常死亡人数分布图", dataset, true, true, true); // 副标题 chart.addSubtitle(new TextTitle("2016年度")); PiePlot pieplot=(PiePlot)chart.getPlot(); pieplot.setLabelFont(new Font("宋体",0,11)); // 设置饼图是圆的(true),还是椭圆的(false);默认为true pieplot.setCircular(true); // 没有数据的时候显示的内容 pieplot.setNoDataMessage("无数据显示"); StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1}.{2})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()); pieplot.setLabelGenerator(standarPieIG); PiePlot3D pieplot3d = (PiePlot3D)chart.getPlot(); //设置开始角度 pieplot3d.setStartAngle(120D); //设置方向为”顺时针方向“ pieplot3d.setDirection(Rotation.CLOCKWISE); //设置透明度,0.5F为半透明,1为不透明,0为全透明 pieplot3d.setForegroundAlpha(0.7F); //最后返回组成的饼图数值 String fileName=ServletUtilities.saveChartAsPNG(chart, 700, 500, null, session); return fileName; }
4 .配置web.xml核心文件:<%@page import="com.java1234.chart.pie.PieChart3"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% String fileName=PieChart3.getPieChart(session); System.out.println(fileName); %> <img src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0"/> </body> </html>
5.最后的显示效果:<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>/DisplayChart</url-pattern> </servlet-mapping>