JFreeChart是一组功能强大、灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图、饼图、曲线图、甘特图等。它能够用在Swing和Web等中制作自定义的图表或报表,并且得到广泛的应用
应用步骤:
1:把jfreechart-1.0.12.jar、gnujaxp.jar和jcommon-1.0.15.jar这三个包加入到classpath中 或 web-info的lib文件夹下
2:在WebRoot\WEB-INF\web.xml文件中增加如下配置:
<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>
实例
1:柱状图3D
<%@ page contentType="text/html;charset=GBK"%>
<%@ page
import="org.jfree.chart.ChartFactory,org.jfree.chart.JFreeChart,org.jfree.chart.plot.PlotOrientation,org.jfree.chart.servlet.ServletUtilities,org.jfree.data.category.DefaultCategoryDataset"%>
<!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>柱状图3D</title>
</head>
<body>
<!--
JFreeChart chart = ChartFactory.createBarChart3D(
chartTitle, // 图表标题
xName, // x轴的显示标签
yName, // y轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平:HORIZONTAL、垂直:VERTICAL
true, // 是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工具
false // 是否生成URL链接
);
-->
<%
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "100", "100");
dataset.addValue(300, "300", "300");
dataset.addValue(600, "600", "600");
JFreeChart chart = ChartFactory.createBarChart3D("Test JFreeChart",
"name", "num", dataset, PlotOrientation.HORIZONTAL, false,
false, false);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, session);
String graphURL = request.getContextPath()
+ "/DisplayChart?filename=" + filename;
%>
<img src="<%=graphURL%>" height=300 border=0 usemap="#<%=filename%>">
</body>
</html>
2:饼状图
<body>
<%
DefaultPieDataset dataset = new DefaultPieDataset();
//dataset.addValue(100, "100", "100");
//dataset.addValue(300, "300", "300");
//dataset.addValue(600, "600", "600");
dataset.setValue("a", 0.46);
dataset.setValue("b", 0.02);
dataset.setValue("c, 0.01);
//通过工厂类生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D("我的一家人", dataset,
true, false, false);
PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
piePlot.setLabelFont(new Font("宋体", 0, 12));
chart.getTitle().setFont(new Font("黑体",Font.BOLD,20));//设置标题字体
chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10));
//没有数据的时候显示的内容
piePlot.setNoDataMessage("无数据显示");
piePlot.setCircular(false);
piePlot.setLabelGap(0.02D);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, session);
String graphURL = request.getContextPath()
+ "/DisplayChart?filename=" + filename;
%>
<img src="<%=graphURL%>" height=300 border=0 usemap="#<%=filename%> ">
</body>
其他转载的:
jfreechart中文乱码问题解决方案(转) 柱状图(CategoryPlot): CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象 CategoryAxis domainAxis=plot.getDomainAxis(); //水平底部列表 domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14)); //水平底部标题 domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //垂直标题 ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状 rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15)); chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15)); 饼图(PiePlot): JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图", dataset, true, false, false); chart.getTitle().setFont(new Font("黑体",Font.BOLD,20));//设置标题字体 PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象 piePlot.setLabelFont(new Font("黑体",Font.BOLD,10)); chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10)); 时序图(TimeSeries) XYPlot plot = (XYPlot) chart.getPlot(); //纵轴字体 plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15)); //横轴框里的标题字体 chart.getLegend().setItemFont(new Font("宋体", Font.ITALIC, 15)); //横轴列表字体 plot.getDomainAxis().setTickLabelFont(new Font("新宋体", 1, 15)); //横轴小标题字体 plot.getDomainAxis().setLabelFont(new Font("新宋体", 1, 12)); 折线图 chart.getTitle().setFont(new Font("宋体", Font.BOLD, 15)); chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15)); CategoryAxis domainAxis = plot.getDomainAxis(); /*------设置X轴坐标上的文字-----------*/ domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 11)); /*------设置X轴的标题文字------------*/ domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis(); /*------设置Y轴坐标上的文字-----------*/ numberaxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12)); /*------设置Y轴的标题文字------------*/ numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12))