小編給大家分享一下如何通過JFreeChart實現在jsp頁面畫甘特圖,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:域名與空間、網絡空間、營銷軟件、網站建設、永年網站維護、網站推廣。
首先把重要的代碼粘出來供大家參考,項目源碼和數據庫信息表,以及參考的資料和一些資料的網址我就打包放在下面,大家可以下載后學習參考。
ProgressControl.java//完成從數據庫讀數據,生成甘特圖。
package com.angrybirds.DAO; import java.io.IOException; import java.io.PrintWriter; //import java.util.Date; import java.sql.*; import java.util.ArrayList; import java.util.ListIterator; import javax.servlet.http.HttpSession; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.entity.StandardEntityCollection; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.data.gantt.Task; import org.jfree.data.gantt.TaskSeries; import org.jfree.data.gantt.TaskSeriesCollection; public class ProgressControl { public static String createPlanGanttChart(String missionId,String missionName,HttpSession session,PrintWriter pw){ String fileName = null; Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = null; String sql2 = null; ArrayList nameList = new ArrayList(); ArrayList startList = new ArrayList(); ArrayList endList = new ArrayList(); conn = DB.getConnection(); stmt = DB.getStatement(conn); sql = "select * from SQLJ.Pro_control"; // where Task_id = '" + missionId + "'"; //System.out.println(sql); rs = DB.getResultSet(stmt, sql); try { while(rs.next()){ nameList.add(rs.getString("Task_name")); startList.add(rs.getDate("Plan_starttime")); endList.add(rs.getDate("Plan_endtime")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } ListIterator iterN = nameList.listIterator(); ListIterator iterS = startList.listIterator(); ListIterator iterE = endList.listIterator(); TaskSeriesCollection tsc = new TaskSeriesCollection(); TaskSeries tsPlan = new TaskSeries("Plan"); while(iterN.hasNext()){ String submissionName = (String)iterN.next(); java.util.Date sDate = (java.util.Date)iterS.next(); java.util.Date eDate = (java.util.Date)iterE.next(); if(sDate.before(eDate)){ Task task = new Task(submissionName,sDate,eDate); tsPlan.add(task); } else{} } tsc.add(tsPlan);//tsc.add(tsPlan); JFreeChart chart = ChartFactory.createGanttChart(missionName + "GanttProject", "Project", "Time", tsc, true, true, true); ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); try { fileName = ServletUtilities.saveChartAsPNG(chart, 500, 80+65*nameList.size(), info,session); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { ChartUtilities.writeImageMap(pw, fileName, info,true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } pw.flush(); //System.out.println(fileName); return fileName; } public static String createRealGanttChart(String missionId,String missionName,HttpSession session,PrintWriter pw){ String fileName = null; Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = null; String sql2 = null; ArrayList nameList = new ArrayList(); ArrayList startList = new ArrayList(); ArrayList endList = new ArrayList(); conn = DB.getConnection(); stmt = DB.getStatement(conn); sql = "select * from SQLJ.Pro_control"; // where Task_id = '" + missionId + "'"; //System.out.println(sql); rs = DB.getResultSet(stmt, sql); try { while(rs.next()){ nameList.add(rs.getString("Task_name")); startList.add(rs.getDate("Reality_starttime")); endList.add(rs.getDate("Reality_endtime")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //rs = null; //System.out.println("over:1"); ListIterator iterN = nameList.listIterator(); ListIterator iterS = startList.listIterator(); ListIterator iterE = endList.listIterator(); TaskSeriesCollection tsc = new TaskSeriesCollection(); //TaskSeries tsPlan = new TaskSeries("Plan"); TaskSeries tsReal = new TaskSeries("Real"); while(iterN.hasNext()){ String submissionName = (String)iterN.next(); java.util.Date sDate = (java.util.Date)iterS.next(); java.util.Date eDate = (java.util.Date)iterE.next(); if(sDate.before(eDate)){ Task task = new Task(submissionName,sDate,eDate); tsReal.add(task); } else{} } tsc.add(tsReal);//tsc.add(tsPlan); JFreeChart chart = ChartFactory.createGanttChart(missionName + "GanttProject", "Project", "Time", tsc, true, true, true); ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); try { fileName = ServletUtilities.saveChartAsPNG(chart, 500, 80+65*nameList.size(), info,session); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { ChartUtilities.writeImageMap(pw, fileName, info,true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } pw.flush(); //System.out.println(fileName); return fileName; } }
我把計劃任務和實際任務分開畫的甘特圖,所以有兩個甘特圖的jsp頁面
PlanGantt.jsp
<%@ page language="java" import="java.util.*,java.io.*" %> <%@ page import="com.angrybirds.DAO.ProgressControl"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>AngryBirds
You are here:Home
- 1
- 2
- 3
<% String missionId = "1"; String filename = ProgressControl.createPlanGanttChart(missionId,"AllProject",session,new PrintWriter(out)); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; %> " width="500" height="300" border="0" usemap="#<%=filename %>">HOME | ABOUT US | GALLERY | FAQ | SERVICES | CONTACT
Copyright 2009 Your Website | CSS Template by AngryBirds
RealGantt.jsp
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GBK"%> <%@ page import="com.angrybirds.DAO.ProgressControl"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>AngryBirds
You are here:Home
- 1
- 2
- 3
<% String missionId = "1"; String filename = ProgressControl.createRealGanttChart(missionId,"AllProject",session,new PrintWriter(out)); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; %> " width="500" height="300" border="0" usemap="#<%=filename %>">
此圖為效果圖:(由于我用的是DB2所以時間顯示有點問題,如果要想甘特圖有更好的顯示效果可以參考API文檔中相關類和方法
地址:http://www.jfree.org/jfreechart/api/javadoc/index.html)
看完了這篇文章,相信你對“如何通過JFreeChart實現在jsp頁面畫甘特圖”有了一定的了解,如果想了解更多相關知識,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道,感謝各位的閱讀!