这是我几年前做过的一个真实的小项目,结构和设计上可能不怎么样,仅供刚接触jfreechart的朋友参考。
显示图表的jsp:
test.jsp
<%@ page language="java" contentType="image/png;charset=GB2312"
import="org.jfree.chart.ChartFactory"
import="org.jfree.chart.JFreeChart"
import="org.jfree.chart.plot.PlotOrientation"
import="org.jfree.data.category.DefaultCategoryDataset"
import="org.jfree.chart.servlet.ServletUtilities"
import="org.jfree.chart.ChartRenderingInfo"
import="org.jfree.chart.entity.StandardEntityCollection"
import="testsystem.dao.chart.DatasetFromBuglist"
%>
<%
int width=600;
int height=375;
JFreeChart chart = new DatasetDAO ().get3DChartFromBuglist();
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
// 设置图表的格式及文件名
String fileName = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
// 设置图表浏览路径
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;
%>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<P ALIGN="CENTER">
<img src="<%=graphURL %>" border="1" alt="3d bar chart">
</P>
</BODY>
</HTML>
从数据库中检索要显示的数据:
public class DatasetDAO {
public JFreeChart get3DChartFromBuglist() {
return createChart(readData());
}
/**
* get data from oracle database
*/
public JDBCCategoryDataset readData() {
JDBCCategoryDataset data = null;
String dbUrl = "jdbc:oracle:thin:@localhost:1521:oradb";
Connection conn;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("loading oracle driver...!");
conn = DriverManager.getConnection(dbUrl,"test","test");
System.out.println("get Connection!");
data = new JDBCCategoryDataset(conn);
//SQL查询
String sqlStr = “…”;
data.executeQuery(sqlStr);
System.out.println("execute query!");
} catch(ClassNotFoundException cnfe) {
System.out.println("oracle driver can not be found!");
System.out.println(cnfe.getMessage());
} catch(SQLException sqle) {
System.out.println("SQLException:");
System.out.println(sqle.getMessage());
} catch(Exception e) {
System.out.println("Other Exception:");
System.out.println(e.getMessage());
}finally{
conn.close();
}
return data;
}
public static JFreeChart createChart(CategoryDataset categorydataset) {
JFreeChart jfreechart = ChartFactory.createBarChart3D(
"各模块bug数量对比图",
"当前版本中",
"数量",
categorydataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
CategoryPlot categoryplot = jfreechart.getCategoryPlot();
categoryplot.setForegroundAlpha(1.0F);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
CategoryLabelPositions categorylabelpositions = categoryaxis.getCategoryLabelPositions();
CategoryLabelPosition categorylabelposition = new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, 0.0D, CategoryLabelWidthType.RANGE, 0.3F);
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.replaceLeftPosition(categorylabelpositions, categorylabelposition));
//set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) categoryplot.getRangeAxis();
rangeAxis.setRange(0,20);
NumberTickUnit numberTickUnit = new NumberTickUnit(1, new DecimalFormat("0"));
rangeAxis.setTickUnit(numberTickUnit);
return jfreechart;
}
}