JFreeChart最佳实践:甘特图

开发 后端
本文将介绍作者通过Java最佳图形解决方案JFreeChart实现甘特图的详细过程。

最近由于项目需要,需要在网页中生成甘特图,在网上浏览资料之后,***通过JFreeChart实现了在jsp页面画甘特图。

在此就与大家分享一下我的收获。

首先把重要的代码粘出来供大家参考,项目源码和数据库信息表,以及参考的资料和一些资料的网址我就打包放在下面,大家可以下载后学习参考。

ProgressControl.java//完成从数据库读数据,生成甘特图。

  1. package com.angrybirds.DAO;  
  2.  
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. //import java.util.Date;  
  6. import java.sql.*;  
  7. import java.util.ArrayList;  
  8. import java.util.ListIterator;  
  9.  
  10. import javax.servlet.http.HttpSession;  
  11.  
  12. import org.jfree.chart.ChartFactory;  
  13. import org.jfree.chart.ChartRenderingInfo;  
  14. import org.jfree.chart.ChartUtilities;  
  15. import org.jfree.chart.JFreeChart;  
  16. import org.jfree.chart.entity.StandardEntityCollection;  
  17. import org.jfree.chart.servlet.ServletUtilities;  
  18. import org.jfree.data.gantt.Task;  
  19. import org.jfree.data.gantt.TaskSeries;  
  20. import org.jfree.data.gantt.TaskSeriesCollection;  
  21.  
  22. public class ProgressControl {  
  23.       
  24.     public static String createPlanGanttChart(String missionId,String missionName,HttpSession session,PrintWriter pw){  
  25.           
  26.         String fileName = null;  
  27.         Connection conn = null;  
  28.         Statement stmt = null;  
  29.         ResultSet rs = null;  
  30.         String sql = null;  
  31.         String sql2 = null;  
  32.           
  33.         ArrayList nameList = new ArrayList();  
  34.         ArrayList startList = new ArrayList();  
  35.         ArrayList endList = new ArrayList();  
  36.           
  37.         conn = DB.getConnection();  
  38.         stmt = DB.getStatement(conn);  
  39.           
  40.         sql = "select * from SQLJ.Pro_control"// where Task_id = '" + missionId + "'";  
  41.           
  42.         //System.out.println(sql);  
  43.           
  44.         rs = DB.getResultSet(stmt, sql);  
  45.           
  46.         try {  
  47.             while(rs.next()){  
  48.                   
  49.                 nameList.add(rs.getString("Task_name"));  
  50.                 startList.add(rs.getDate("Plan_starttime"));  
  51.                 endList.add(rs.getDate("Plan_endtime"));                  
  52.                             }  
  53.         } catch (SQLException e) {  
  54.             // TODO Auto-generated catch block  
  55.             e.printStackTrace();  
  56.         }  
  57.           
  58.         ListIterator iterN = nameList.listIterator();  
  59.         ListIterator iterS = startList.listIterator();  
  60.         ListIterator iterE = endList.listIterator();  
  61.           
  62.         TaskSeriesCollection tsc = new TaskSeriesCollection();  
  63.         TaskSeries tsPlan = new TaskSeries("Plan");  
  64.               
  65.         while(iterN.hasNext()){  
  66.               
  67.             String submissionName = (String)iterN.next();  
  68.             java.util.Date sDate = (java.util.Date)iterS.next();  
  69.             java.util.Date eDate = (java.util.Date)iterE.next();  
  70.                           
  71.             if(sDate.before(eDate)){  
  72.             Task task = new Task(submissionName,sDate,eDate);  
  73.             tsPlan.add(task);  
  74.             }  
  75.             else{}  
  76.         }  
  77.             tsc.add(tsPlan);//tsc.add(tsPlan);  
  78.                   
  79.             JFreeChart chart = ChartFactory.createGanttChart(missionName + "GanttProject""Project""Time", tsc, truetruetrue);  
  80.             ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());  
  81.                 try {  
  82.                     fileName = ServletUtilities.saveChartAsPNG(chart, 50080+65*nameList.size(), info,session);  
  83.                 } catch (IOException e) {  
  84.                     // TODO Auto-generated catch block  
  85.                     e.printStackTrace();  
  86.                 }  
  87.           
  88.                 try {  
  89.                     ChartUtilities.writeImageMap(pw, fileName, info,true);  
  90.                 } catch (IOException e) {  
  91.                     // TODO Auto-generated catch block  
  92.                     e.printStackTrace();  
  93.                 }  
  94.           
  95.                 try {  
  96.                     rs.close();  
  97.                     stmt.close();  
  98.                     conn.close();  
  99.                 } catch (SQLException e) {  
  100.                     // TODO Auto-generated catch block  
  101.                     e.printStackTrace();  
  102.                 }         
  103.             pw.flush();  
  104.         //System.out.println(fileName);  
  105.         return fileName;  
  106.     }  
  107.     public static String createRealGanttChart(String missionId,String missionName,HttpSession session,PrintWriter pw){  
  108.         String fileName = null;  
  109.         Connection conn = null;  
  110.         Statement stmt = null;  
  111.         ResultSet rs = null;  
  112.         String sql = null;  
  113.         String sql2 = null;  
  114.           
  115.         ArrayList nameList = new ArrayList();  
  116.         ArrayList startList = new ArrayList();  
  117.         ArrayList endList = new ArrayList();  
  118.           
  119.         conn = DB.getConnection();  
  120.         stmt = DB.getStatement(conn);  
  121.           
  122.         sql = "select * from SQLJ.Pro_control"// where Task_id = '" + missionId + "'";  
  123.         //System.out.println(sql);  
  124.         rs = DB.getResultSet(stmt, sql);  
  125.         try {  
  126.             while(rs.next()){  
  127.                 nameList.add(rs.getString("Task_name"));  
  128.                                 startList.add(rs.getDate("Reality_starttime"));  
  129.                 endList.add(rs.getDate("Reality_endtime"));  
  130.                             }  
  131.         } catch (SQLException e) {  
  132.             // TODO Auto-generated catch block  
  133.             e.printStackTrace();  
  134.         }  
  135.         //rs = null;  
  136.         //System.out.println("over:1");  
  137.             ListIterator iterN = nameList.listIterator();  
  138.         ListIterator iterS = startList.listIterator();  
  139.         ListIterator iterE = endList.listIterator();  
  140.         TaskSeriesCollection tsc = new TaskSeriesCollection();  
  141.         //TaskSeries tsPlan = new TaskSeries("Plan");  
  142.         TaskSeries tsReal = new TaskSeries("Real");  
  143.         while(iterN.hasNext()){  
  144.               
  145.             String submissionName = (String)iterN.next();  
  146.             java.util.Date sDate = (java.util.Date)iterS.next();  
  147.             java.util.Date eDate = (java.util.Date)iterE.next();  
  148.                           
  149.             if(sDate.before(eDate)){  
  150.               
  151.             Task task = new Task(submissionName,sDate,eDate);  
  152.             tsReal.add(task);  
  153.             }  
  154.             else{}               
  155.         }  
  156.             tsc.add(tsReal);//tsc.add(tsPlan);  
  157.             JFreeChart chart = ChartFactory.createGanttChart(missionName + "GanttProject""Project""Time", tsc, truetruetrue);  
  158.             ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());  
  159.                 try {  
  160.                     fileName = ServletUtilities.saveChartAsPNG(chart, 50080+65*nameList.size(), info,session);  
  161.                 } catch (IOException e) {  
  162.                     // TODO Auto-generated catch block  
  163.                     e.printStackTrace();  
  164.                 }  
  165.                 try {  
  166.                     ChartUtilities.writeImageMap(pw, fileName, info,true);  
  167.                 } catch (IOException e) {  
  168.                     // TODO Auto-generated catch block  
  169.                     e.printStackTrace();  
  170.                 }  
  171.                 try {  
  172.                     rs.close();  
  173.                     stmt.close();  
  174.                     conn.close();  
  175.                 } catch (SQLException e) {  
  176.                     // TODO Auto-generated catch block  
  177.                     e.printStackTrace();  
  178.                 }         
  179.             pw.flush();  
  180.         //System.out.println(fileName);  
  181.         return fileName;  
  182.     }  

我把计划任务和实际任务分开画的甘特图,所以有两个甘特图的jsp页面

PlanGantt.jsp

  1. <%@ page language="java" import="java.util.*,java.io.*" %> 
  2. <%@ page import="com.angrybirds.DAO.ProgressControl"%> 
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %> 
  7.  
  8.  
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  10. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> 
  11. <head> 
  12. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  13. <title>AngryBirds</title> 
  14. <link href="css.css" rel="stylesheet" type="text/css" /> 
  15.     <meta http-equiv="pragma" content="no-cache"> 
  16.     <meta http-equiv="cache-control" content="no-cache"> 
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  19.     <meta http-equiv="description" content="This is my page"> 
  20. </head> 
  21. <body> 
  22. <div class="size"> 
  23.   <div class="header"> 
  24.     <div class="header01"> 
  25.       <div class="logo"> <img src="image/img_15.bmp" alt="" /> </div> 
  26.     </div> 
  27.     <div class="cmainimg"> <img alt="" src="image/img_73.jpg" /> </div> 
  28.     <div class="mnav"> <img class="mnimgl" src="image/img_37.jpg" alt="" /> <img class="mnimgr" src="image/img_50.jpg" alt="" /> 
  29.       <div class="mnm"> 
  30.         <ul> 
  31.           
  32.         </ul> 
  33.       </div> 
  34.     </div> 
  35.   </div> 
  36.   <div class="content"> 
  37.     <div class="submenu" style="left: 0px; top: 0px"> <img class="snimgl" src="image/img_96.jpg" alt="" /> <img class="snimgr" src="image/img_107.jpg" alt="" /> 
  38.       <div class="subtext"> You are here:Home </div> 
  39.       <div class="subimag" style="width: 41px; height: 51px"> 
  40.         <ul> 
  41.           <li>1</li> 
  42.           <li>2</li> 
  43.           <li>3</li> 
  44.         </ul> 
  45.       </div> 
  46.     </div> 
  47.     <div class="mcontent"> 
  48.        <!--***************************************************************************×ó/--> 
  49.  
  50.       <!--***************************************************************************ÓÒ/--> 
  51.       <div align="center"  class="mcmid3"> 
  52.  <%   
  53.         String missionId = "1";  
  54.         String filename = ProgressControl.createPlanGanttChart(missionId,"AllProject",session,new PrintWriter(out));  
  55.         String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;  
  56.     %> 
  57.       
  58.     <img src="<%=graphURL %>" width="500" height="300" border="0" usemap="#<%=filename %>"> 
  59.       </div> 
  60.       <!--***************************************************************************/--> 
  61.     </div> 
  62.   </div> 
  63.   <div class="footer"> <img class="mnimgl" src="image/img_275.jpg" alt="" /> <img class="mnimgr" src="image/img_291.jpg" alt="" /> <a href="http://www.88web.org/">HOME</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">ABOUT US</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">GALLERY</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">FAQ</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">SERVICES</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">CONTACT</a> <br/> 
  64.     <font color="#333333">Copyright 2009 Your Website | <a style="color: #333333" href="http://www.88web.org/css-templates/">CSS Template</a> by <a style="color: #333333" href="http://www.88web.org/">AngryBirds</a></font></div> 
  65. </div> 
  66. </body> 
  67. </html> 

RealGantt.jsp

  1. <%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GBK"%> 
  2. <%@ page import="com.angrybirds.DAO.ProgressControl"%> 
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %> 
  7.  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  9. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> 
  10. <head> 
  11. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  12. <title>AngryBirds</title> 
  13. <link href="css.css" rel="stylesheet" type="text/css" /> 
  14.     <meta http-equiv="pragma" content="no-cache"> 
  15.     <meta http-equiv="cache-control" content="no-cache"> 
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  18.     <meta http-equiv="description" content="This is my page"> 
  19. </head> 
  20. <body> 
  21. <div class="size"> 
  22.   <div class="header"> 
  23.     <div class="header01"> 
  24.       <div class="logo"> <img src="image/img_15.bmp" alt="" /> </div> 
  25.     </div> 
  26.     <div class="cmainimg"> <img alt="" src="image/img_73.jpg" /> </div> 
  27.     <div class="mnav"> <img class="mnimgl" src="image/img_37.jpg" alt="" /> <img class="mnimgr" src="image/img_50.jpg" alt="" /> 
  28.       <div class="mnm"> 
  29.         <ul> 
  30.             
  31.         </ul> 
  32.       </div> 
  33.     </div> 
  34.   </div> 
  35.   <div class="content"> 
  36.     <div class="submenu" style="left: 0px; top: 0px"> <img class="snimgl" src="image/img_96.jpg" alt="" /> <img class="snimgr" src="image/img_107.jpg" alt="" /> 
  37.       <div class="subtext"> You are here:Home </div> 
  38.       <div class="subimag" style="width: 41px; height: 51px"> 
  39.         <ul> 
  40.           <li>1</li> 
  41.           <li>2</li> 
  42.           <li>3</li> 
  43.         </ul> 
  44.       </div> 
  45.     </div> 
  46.     <div class="mcontent"> 
  47.        <!--***************************************************************************左/--> 
  48.  
  49.      
  50.       <!--***************************************************************************右/--> 
  51.       <div align="center" class="mcmid3"> 
  52.     <%   
  53.         String missionId = "1";  
  54.         String filename = ProgressControl.createRealGanttChart(missionId,"AllProject",session,new PrintWriter(out));  
  55.         String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;  
  56.     %> 
  57.       
  58.     <img src="<%=graphURL %>" width="500" height="300" border="0" usemap="#<%=filename %>"> 
  59.       </div> 
  60.       <!--***************************************************************************/--> 
  61.     </div> 
  62.   </div> 
  63.   <div class="footer"> <img class="mnimgl" src="image/img_275.jpg" alt="" /> <img class="mnimgr" src="image/img_291.jpg" alt="" /> <a href="http://www.88web.org/">HOME</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">ABOUT US</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">GALLERY</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">FAQ</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">SERVICES</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://www.88web.org/">CONTACT</a> <br/> 
  64.     <font color="#333333">Copyright 2009 Your Website | <a style="color: #333333" href="http://www.88web.org/css-templates/">CSS Template</a> by <a style="color: #333333" href="http://www.88web.org/">AngryBirds</a></font></div> 
  65. </div> 
  66. </body> 
  67. </html> 

此图为效果图:(由于我用的是DB2所以时间显示有点问题,如果要想甘特图有更好的显示效果可以参考API文档中相关类和方法

地址:http://www.jfree.org/jfreechart/api/javadoc/index.html

原文链接:http://769672133.iteye.com/blog/1015897

【编辑推荐】

  1. JFreeChart***实践:时序图
  2. JFreeChart***实践:散点图
  3. JFreeChart***实践:折线图
  4. JFreeChart***实践:柱状图
  5. JFreeChart***实践:3D饼图

 

责任编辑:林师授 来源: 静妙仙人的博客
相关推荐

2011-12-21 13:35:39

JavaJFreeChart

2011-12-21 13:44:33

JavaJFreeChart

2011-12-21 14:15:08

JavaJFreeChart

2011-12-21 13:25:33

JavaJFreeChart

2011-12-21 12:58:41

JavaJFreeChart

2011-12-21 14:34:33

JavaJFreeChart

2011-12-21 12:46:43

2011-12-20 12:53:43

JavaJFreeChart

2023-07-21 01:12:30

Reactfalse​变量

2011-08-18 11:05:21

jQuery

2012-08-09 09:10:56

代码审查代码

2014-08-19 10:06:53

IAP

2024-08-21 08:02:47

2014-06-09 15:50:08

2023-09-11 08:50:03

Maven工具关系管理

2015-09-23 09:08:38

java反射

2023-09-13 08:00:00

JavaScript循环语句

2010-02-04 11:55:27

ibmdwDB2

2010-07-06 09:07:09

2013-06-13 09:21:31

RESTful APIRESTfulAPI
点赞
收藏

51CTO技术栈公众号