一、首先需要jfreechart的包:jfreechart-1.0.15.jar、jfreechart-1.0.15-experimental.jar、jfreechart-1.0.15-swt.jar、jcommon-1.0.18.jar
搜索“jfreechart-1.0.15.zip下载”,解压后在lib文件夹里可以找到
二、配置站点的web.xml文件,增加以下节点:
<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>
三、JSP文件代码:
bing1.jsp 一个基本的饼图
<%@ page contentType="text/html;charset=GBK"%>
<%@ page
import="org.jfree.chart.*,org.jfree.chart.plot.PiePlot,
org.jfree.data.general.DefaultPieDataset,
org.jfree.chart.servlet.ServletUtilities,
java.awt.*,org.jfree.chart.title.TextTitle"%>
<%
//设置数据集
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("初中高级程序员", 0.55);
dataset.setValue("项目经理", 0.1);
dataset.setValue("系统分析师", 0.1);
dataset.setValue("软件架构师", 0.1);
dataset.setValue("其他", 0.2);
//通过工厂类生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图",
dataset, true, false, false);
PiePlot pieplot = (PiePlot) chart.getPlot();
//没有数据的时候显示的内容
pieplot.setNoDataMessage("无数据显示");
pieplot.setCircular(false);
pieplot.setLabelGap(0.02D);
//标题文字乱码
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
//饼上的文字乱码
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.BOLD, 20));
//图例文字乱码
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, session);
String graphURL = request.getContextPath()
+ "/DisplayChart?filename=" + filename;
%>
<html>
<head>
<title>饼状图1(基本的饼图)</title>
</head>
<body>
<img src="<%=graphURL%>" width=500 height=300 border=0
usemap="#<%= filename %>">
</body>
</html>
bing2.jsp 一个显示百分比的饼图
<%@ page contentType="text/html;charset=GBK"%>
<%@ page
import="org.jfree.chart.*,org.jfree.chart.plot.PiePlot,
org.jfree.data.general.DefaultPieDataset,
org.jfree.chart.servlet.ServletUtilities,
java.awt.*,org.jfree.chart.title.TextTitle"%>
<%@ page
import="org.jfree.chart.labels.StandardPieSectionLabelGenerator"%>
<%@ page import="java.text.NumberFormat"%>
<%@page import="java.text.DecimalFormat"%>
<%
//设置数据集
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("初中高级程序员", 0.52);
dataset.setValue("项目经理", 0.1);
dataset.setValue("系统分析师", 0.1);
dataset.setValue("软件架构师", 0.1);
dataset.setValue("其他", 0.18);
//通过工厂类生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图",
dataset, true, false, false);
PiePlot pieplot = (PiePlot) chart.getPlot();
//一块突出的饼图,在网上搜了好久也没找到可行的实现方式,欢迎大侠指导 <?后期如果找到解决方法再来做修改?>
//pieplot.setExplodePercent("A",0.3D);//炸开的饼图,目前实现还有问题
//DecimalFormat:
//NumberFormat:
//StandardPieSectionLabelGenerator:
//setLabelGenerator():
DecimalFormat df = new DecimalFormat("0.00%");//获得一个DecimalFormat对象,主要是设置小数问题
NumberFormat nf = NumberFormat.getNumberInstance();//获得一个NumberFormat对象
StandardPieSectionLabelGenerator sp = new StandardPieSectionLabelGenerator(
"{0}{2}", nf, df);//获得StandardPieSectionLabelGenerator对象
pieplot.setLabelGenerator(sp);//设置饼图显示百分比
//没有数据的时候显示的内容
pieplot.setNoDataMessage("无数据显示");
pieplot.setCircular(false);
pieplot.setLabelGap(0.02D);
pieplot.setIgnoreNullValues(true);//设置不显示空值
pieplot.setIgnoreZeroValues(true);//设置不显示负值
//标题文字乱码 IT行业职业分布图
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
//饼上的文字乱码
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.PLAIN, 20));
//图例文字乱码 饼图下面的5个说明
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 20));
String filename = ServletUtilities.saveChartAsPNG(chart, 1000, 600,
null, session);
String graphURL = request.getContextPath()
+ "/DisplayChart?filename=" + filename;
%>
<html>
<head>
<title>饼状图2(加上百分比并突出显示某块)</title>
</head>
<body>
<img src="<%=graphURL%>" width=1000 height=600 border=0
usemap="#<%= filename %>">
</body>
</html>
bing2.jsp 一个水晶效果的拼图
<%@ page contentType="text/html;charset=GBK"%>
<%@ page
import="org.jfree.chart.*,org.jfree.chart.servlet.ServletUtilities,
org.jfree.util.Rotation,org.jfree.data.general.DefaultPieDataset,
org.jfree.chart.plot.PiePlot3D,org.jfree.chart.title.TextTitle,
java.awt.Font,org.jfree.chart.plot.PiePlot"%>
<%@ page
import="org.jfree.chart.labels.StandardPieSectionLabelGenerator"%>
<%@ page import="java.text.NumberFormat"%>
<%@page import="java.text.DecimalFormat"%>
<%
//设置数据集
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("初中高级程序员", 0.52);
dataset.setValue("项目经理", 0.1);
dataset.setValue("系统分析师", 0.1);
dataset.setValue("软件架构师", 0.1);
dataset.setValue("其他", 0.18);
//通过工厂类生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图",
dataset, true, true, false);
//获得3D的水晶图对象
PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot();
PiePlot pieplot = (PiePlot) chart.getPlot();
DecimalFormat df = new DecimalFormat("0.00%");//获得一个DecimalFormat对象,主要是设置小数问题
NumberFormat nf = NumberFormat.getNumberInstance();//获得一个NumberFormat对象
StandardPieSectionLabelGenerator sp = new StandardPieSectionLabelGenerator(
"{0}{2}", nf, df);//获得StandardPieSectionLabelGenerator对象
pieplot.setLabelGenerator(sp);//设置饼图显示百分比
//设置开始角度
pieplot3d.setStartAngle(150D);
//设置方向为“顺时针方向”
pieplot3d.setDirection(Rotation.CLOCKWISE);
//设置透明度,0.5F为半透明,1为不透明,0为全透明
pieplot3d.setForegroundAlpha(0.5F);
//没有数据的时候显示的内容
pieplot3d.setNoDataMessage("无数据显示");
//标题文字乱码 IT行业职业分布图
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
//饼上的文字乱码
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.PLAIN, 20));
//图例文字乱码 饼图下面的5个说明
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 20));
String filename = ServletUtilities.saveChartAsPNG(chart, 1000, 600,
null, session);
String graphURL = request.getContextPath()
+ "/DisplayChart?filename=" + filename;
%>
<html>
<head>
<title>饼状图3(水晶饼图)</title>
</head>
<body>
<img src="<%=graphURL%>" width=1000 height=600 border=0
usemap="#<%= filename %>">
</body>
</html>