工作中经常遇到图形报表的问题.今天总结一下.jfreechart是一款免费开源的图形报表开发.但他的DEMO源代码和文档是收费的。所以今天把用jad反编译的代码也传上了上来与大家共享.
jfreechart可以实现各种报表,可以在应用程序中实现,也可以以web形式实现.所以代码都是通用的.demo中的代码是桌面应用程序的,所以此处不示范.web形式的代码如下.
在你的WEB工程里,导入以下"试验包"中的jar文件,直接复制代码就行了.我的环境是
tomcat6.0.28
jdk6.0.21
附件中还包含了jfreechart的其他几个官方下载的相关资料,读者们自己研究.有问题可以给我留言,我会尽快给大家回复.希望对大家能有帮助.^-^
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
import="org.jfree.chart.*"
import="org.jfree.chart.labels.*"
import="org.jfree.chart.plot.*"
import="org.jfree.chart.renderer.category.*"
import="org.jfree.data.category.*"
import="org.jfree.data.general.*"
import="java.io.*"
import="org.jfree.chart.axis.*"
%>
<!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>
<%
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
String s = "First";
String s1 = "Second";
String s2 = "Third";
String s3 = "Category 1";
String s4 = "Category 2";
String s5 = "Category 3";
String s6 = "Category 4";
String s7 = "Category 5";
defaultcategorydataset.addValue(1.0D, s, s3);
defaultcategorydataset.addValue(4D, s, s4);
defaultcategorydataset.addValue(3D, s, s5);
defaultcategorydataset.addValue(5D, s, s6);
defaultcategorydataset.addValue(5D, s, s7);
defaultcategorydataset.addValue(5D, s1, s3);
defaultcategorydataset.addValue(7D, s1, s4);
defaultcategorydataset.addValue(6D, s1, s5);
defaultcategorydataset.addValue(8D, s1, s6);
defaultcategorydataset.addValue(4D, s1, s7);
defaultcategorydataset.addValue(4D, s2, s3);
defaultcategorydataset.addValue(3D, s2, s4);
defaultcategorydataset.addValue(2D, s2, s5);
defaultcategorydataset.addValue(3D, s2, s6);
defaultcategorydataset.addValue(6D, s2, s7);
JFreeChart jfreechart = ChartFactory.createBarChart("Bar Chart Demo 8", "Category", "Value", defaultcategorydataset, PlotOrientation.VERTICAL, false, true, false);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setUpperMargin(0.14999999999999999D);
CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
categoryitemrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
categoryitemrenderer.setSeriesItemLabelsVisible(0, Boolean.TRUE);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
OutputStream os = response.getOutputStream();
ChartUtilities.writeChartAsPNG(os,jfreechart, 600, 500, true, 10);
os.flush();
os.close();
os=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
%>
</body>
</html>