【转】FusionCharts兑现流程甘特图
程序员文章站
2024-02-26 11:06:22
...
package com.ist.pmp.gantt; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import org.springframework.context.ApplicationContext; import com.ibatis.sqlmap.client.SqlMapClient; import com.ist.common.jcs.CatchManager; import com.ist.pmp.dao.T06_GanttDAO; public class FusionChartDataFile { private Document document; private ApplicationContext context; private SqlMapClient sqlMap; private CatchManager cm; private String projectkey; private File path; private Map timeArea; private int rowNum; public FusionChartDataFile(ApplicationContext context,SqlMapClient sqlMap,CatchManager cm,String projectkey,File path) throws SQLException, IOException { this.context=context; this.sqlMap=sqlMap; this.cm=cm; this.projectkey=projectkey; this.path=path; getTimeArea(); bulidFusionChartDateFile(); } private void bulidFusionChartDateFile() throws SQLException, IOException{ //1创建根 Element rootElement=getRootElement(); addCategoriesnode1(rootElement); addCategoriesnode2(rootElement); addCategoriesnode3(rootElement); addProcesses(rootElement); addLegend(rootElement); addStyles(rootElement); /*项目用不到,所以这里不实现 //阶段联结线 addConnectors(rootElement); //里程碑星形图案 addMilestones(rootElement);*/ //创建文件 DateFileCreate(); } private Element addLegend(Element rootElement) { Map<String,String> attributesMap=new LinkedHashMap<String, String>(); Element legendNode=addChildElement(rootElement, "legend", attributesMap); attributesMap.put("label", "计划日期"); attributesMap.put("color", "4567aa"); addChildElement(legendNode, "item", attributesMap); attributesMap.clear(); attributesMap.put("label", "实际日期"); attributesMap.put("color", "999999"); addChildElement(legendNode, "item", attributesMap); attributesMap.clear(); attributesMap.put("label", "延迟"); attributesMap.put("color", "FF5E5E"); addChildElement(legendNode, "item", attributesMap); return legendNode; } private Element addStyles(Element rootElement) { Map<String,String> attributesMap=new LinkedHashMap<String, String>(); Element stylesNode=addChildElement(rootElement, "styles", attributesMap); Element definitionNode=addChildElement(rootElement, "definition", attributesMap); Element applicationNode=addChildElement(rootElement, "application", attributesMap); attributesMap.put("type", "Font"); attributesMap.put("name", "legendFont"); attributesMap.put("size", "14"); addChildElement(definitionNode, "style", attributesMap); attributesMap.clear(); attributesMap.put("toObject", "legend"); attributesMap.put("styles", "legendFont"); addChildElement(applicationNode, "apply", attributesMap); return stylesNode; } //添加项目各阶段 private void addProcesses(Element rootElement) throws SQLException { //添加processes节点 Map<String,String> attributesMap=new LinkedHashMap<String, String>(); attributesMap.put("headerText", "阶段"); attributesMap.put("fontColor", "000000"); attributesMap.put("fontSize", "12"); attributesMap.put("isAnimated", "1"); attributesMap.put("bgColor", "4567aa"); attributesMap.put("headerVAlign", "bottom"); attributesMap.put("headerAlign", "left"); attributesMap.put("headerbgColor", "4567aa"); attributesMap.put("headerFontColor", "ffffff"); attributesMap.put("headerFontSize", "16"); attributesMap.put("align", "left"); attributesMap.put("isBold", "0"); attributesMap.put("bgAlpha", "25"); Element processes=addChildElement(rootElement, "processes", attributesMap); //添加dataTable节点 attributesMap.clear(); attributesMap.put("showProcessName", "1"); attributesMap.put("nameAlign", "left"); attributesMap.put("fontColor", "000000"); attributesMap.put("fontSize", "10"); attributesMap.put("vAlign", "right"); attributesMap.put("align", "center"); attributesMap.put("headerVAlign", "bottom"); attributesMap.put("headerAlign", "left"); attributesMap.put("headerbgColor", "4567aa"); attributesMap.put("headerFontColor", "ffffff"); attributesMap.put("headerFontSize", "16"); Element dataTable=addChildElement(rootElement, "dataTable", attributesMap); //添加dataTable下的dataColumn子节点 attributesMap.clear(); attributesMap.put("bgColor","ffffff"); attributesMap.put("headerText","计划开始"); Element dataColumnPlanStarttime=addChildElement(dataTable, "dataColumn", attributesMap); attributesMap.clear(); attributesMap.put("bgColor","ffffff"); attributesMap.put("headerText","计划结束"); Element dataColumnPlanEndtime=addChildElement(dataTable, "dataColumn", attributesMap); attributesMap.clear(); attributesMap.put("bgColor","ffffff"); attributesMap.put("headerText","实际开始"); Element dataColumnActualStarttime=addChildElement(dataTable, "dataColumn", attributesMap); attributesMap.clear(); attributesMap.put("bgColor","ffffff"); attributesMap.put("headerText","实际结束"); Element dataColumnActualEndtime=addChildElement(dataTable, "dataColumn", attributesMap); attributesMap.clear(); attributesMap.put("bgColor","ffffff"); attributesMap.put("headerText","计划工时"); Element dataColumnPlanWorktime=addChildElement(dataTable, "dataColumn", attributesMap); attributesMap.clear(); attributesMap.put("bgColor","ffffff"); attributesMap.put("headerText","实际工时"); Element dataColumnActualWorktime=addChildElement(dataTable, "dataColumn", attributesMap); //添加tasks节点 attributesMap.clear(); Element tasks=addChildElement(rootElement, "tasks", attributesMap); addProcessDetail(processes, dataColumnPlanStarttime,dataColumnPlanEndtime,dataColumnActualStarttime,dataColumnActualEndtime,dataColumnPlanWorktime,dataColumnActualWorktime, tasks); } private void addProcessDetail(Element processesNode, Element planStarttimeNode,Element planEndtimeNode, Element actualStarttimeNode,Element actualEndtimeNode, Element planWorktimeNode,Element actualWorktimeNode, Element tasksNode) throws SQLException { T06_GanttDAO t06_GanttDAO = (T06_GanttDAO) context.getBean("t06_ganttDAO"); List stageList=(List)t06_GanttDAO.getProjectStage(sqlMap, projectkey); this.rowNum=stageList.size(); Iterator iter=stageList.iterator(); Map projectTypeMap=cm.getMapFromCache("t21worktimeType"); Map stageTemp; Map<String,String> attributesMap=new LinkedHashMap<String, String>(); int processId=1; while(iter.hasNext()){ //添加process子节点 stageTemp=(Map)iter.next(); attributesMap.clear(); attributesMap.put("label", (String) projectTypeMap.get(stageTemp.get("STAGE"))); attributesMap.put("id", String.valueOf(processId)); addChildElement(processesNode, "process", attributesMap); //添加dataColumn下的text计划开始子节点 attributesMap.clear(); attributesMap.put("label",(String) stageTemp.get("PLANSTARTDAY")); addChildElement(planStarttimeNode, "text", attributesMap); //添加dataColumn下的text计划结不子节点 attributesMap.clear(); attributesMap.put("label",(String) stageTemp.get("PLANENDDAY")); addChildElement(planEndtimeNode, "text", attributesMap); //添加dataColumn下的text实际开始子节点 attributesMap.clear(); attributesMap.put("label",(String) stageTemp.get("ACTUALSTARTDAY")); addChildElement(actualStarttimeNode, "text", attributesMap); //添加dataColumn下的text实际结束子节点 attributesMap.clear(); attributesMap.put("label",(String) stageTemp.get("ACTUALENDDAY")); addChildElement(actualEndtimeNode, "text", attributesMap); //添加dataColumn下的text计划工时子节点 attributesMap.clear(); attributesMap.put("label",String.valueOf(stageTemp.get("PLANWORKTIME"))); addChildElement(planWorktimeNode, "text", attributesMap); //添加dataColumn下的text实际工时子节点 attributesMap.clear(); attributesMap.put("label",String.valueOf(stageTemp.get("ACTUALWORKTIME"))); addChildElement(actualWorktimeNode, "text", attributesMap); //添加tasks下的task结点 attributesMap.clear(); attributesMap.put("label", "计划日期"); attributesMap.put("processId", String.valueOf(processId)); attributesMap.put("start", (String) stageTemp.get("PLANSTARTDAY")); attributesMap.put("end", (String) stageTemp.get("PLANENDDAY")); attributesMap.put("id", String.valueOf(processId)+"-1"); attributesMap.put("color", "4567aa"); attributesMap.put("height", "32%"); attributesMap.put("topPadding", "12%"); addChildElement(tasksNode, "task", attributesMap); attributesMap.clear(); if(null!=stageTemp.get("ACTUALSTARTDAY") && !"".equals(stageTemp.get("ACTUALSTARTDAY"))){ attributesMap.put("label", "实际日期"); attributesMap.put("processId", String.valueOf(processId)); attributesMap.put("start", (String) stageTemp.get("ACTUALSTARTDAY")); attributesMap.put("end", (String) stageTemp.get("ACTUALENDDAY")); attributesMap.put("id", String.valueOf(processId)); attributesMap.put("color", "EEEEEE"); attributesMap.put("alpha", "100"); attributesMap.put("topPadding", "56%"); attributesMap.put("height", "32%"); attributesMap.put("isAnimated", "1"); double planworktime=Double.parseDouble((String.valueOf(stageTemp.get("PLANWORKTIME")))); double actualworktime=Double.parseDouble((String.valueOf(stageTemp.get("ACTUALWORKTIME")))); //实际时间减计划时间,算超时百分比 int rate=100; if(actualworktime>=planworktime){ rate=(int) ((actualworktime-planworktime)/planworktime*100); if(rate<=100){ rate=100-rate; }else{ rate=0; } }else{ rate=100; } attributesMap.put("percentComplete", String.valueOf(rate)); addChildElement(tasksNode, "task", attributesMap); } processId++; } } //创建文件 private void DateFileCreate() throws IOException { OutputFormat formatter=OutputFormat.createPrettyPrint(); formatter.setIndentSize(4); formatter.setEncoding("UTF-8"); XMLWriter writer=new XMLWriter(new FileOutputStream(path),formatter); writer.write(document); writer.flush(); writer.close(); } //创建根 private Element getRootElement(){ document = DocumentHelper.createDocument(); Element chart=document.addElement("chart"); Map<String,String> rootAttributesMap=new LinkedHashMap<String, String>(); rootAttributesMap.put("manageResize","1"); rootAttributesMap.put("dateFormat","yyyy-MM-dd"); rootAttributesMap.put("outputDateFormat","yyyy年MM月dd日"); rootAttributesMap.put("ganttWidthPercent","70"); rootAttributesMap.put("canvasBorderColor","999999"); rootAttributesMap.put("canvasBorderThickness","0"); rootAttributesMap.put("gridBorderColor","4567aa"); rootAttributesMap.put("gridBorderAlpha","20"); rootAttributesMap.put("ganttPaneDuration","3"); rootAttributesMap.put("ganttPaneDurationUnit","m"); for(String key:rootAttributesMap.keySet()){ chart.addAttribute(key,rootAttributesMap.get(key)); } return chart; } //2创建第一个categories结点 private Element addCategoriesnode1(Element rootElement) throws SQLException{ Map<String,String> attributesMap=new LinkedHashMap<String, String>(); attributesMap.put("bgColor", "ffffff"); Element categories=addChildElement(rootElement,"categories",attributesMap); attributesMap.clear(); String start=(String)timeArea.get("MINDATE"); attributesMap.put("start",getFormattedDate(start,"firstday")); String end=(String)timeArea.get("MAXDATE"); attributesMap.put("end", getFormattedDate(end,"lastday")); attributesMap.put("label", cm.getMapFromCache("t06_project_plan").get(projectkey)+"进度表"); attributesMap.put("fontColor", "1589da"); attributesMap.put("align", "left"); attributesMap.put("fontSize", "16"); Element category=addChildElement(categories, "category", attributesMap); return category; } //3创建第二个categories结点 private Element addCategoriesnode2(Element rootElement) throws SQLException{ Map<String,String> attributesMap=new LinkedHashMap<String, String>(); attributesMap.clear(); attributesMap.put("bgColor", "4567aa"); attributesMap.put("fontColor", "ff0000"); Element categories=addChildElement(rootElement, "categories", attributesMap); attributesMap.clear(); String start=(String)timeArea.get("MINDATE"); attributesMap.put("start",getFormattedDate(start,"firstday")); String end=(String)timeArea.get("MAXDATE"); attributesMap.put("end",getFormattedDate(end,"lastday")); attributesMap.put("label", "时间轴"); attributesMap.put("alpha", ""); attributesMap.put("font", "Verdana"); attributesMap.put("align", "left"); attributesMap.put("fontColor", "ffffff"); attributesMap.put("fontSize", "16"); Element category=addChildElement(categories, "category", attributesMap); return category; } //4创建第三个categories结点 private Element addCategoriesnode3(Element rootElement) throws SQLException{ Map<String,String> attributesMap=new LinkedHashMap<String, String>(); attributesMap.put("bgColor", "ffffff"); attributesMap.put("fontColor", "1288dd"); attributesMap.put("fontSize", "10"); attributesMap.put("align", "center"); Element categories=addChildElement(rootElement, "categories", attributesMap); this.addTimeInterval(categories); Element category=null; return category; } private void addTimeInterval(Element categories){ String start=(String)timeArea.get("MINDATE"); String[] startParam=start.split("-"); Calendar startCal=new GregorianCalendar(); startCal.set(Calendar.YEAR, Integer.parseInt(startParam[0])); startCal.set(Calendar.MONTH, Integer.parseInt(startParam[1])-1); startCal.set(Calendar.DATE, 1); String end=(String)timeArea.get("MAXDATE"); String[] endParam=end.split("-"); Calendar endCal=new GregorianCalendar(); endCal.set(Calendar.YEAR, Integer.parseInt(endParam[0])); endCal.set(Calendar.MONTH, Integer.parseInt(endParam[1])-1); endCal.set(Calendar.DATE, 1); System.out.println("开始年月"+formatFullDate(startCal)); System.out.println("结束年月"+formatFullDate(endCal)); Map<String,String> attributesMap=new LinkedHashMap<String, String>(); while(startCal.before(endCal)){ startCal.set(Calendar.DATE, 1); System.out.println("添加start节点"+formatFullDate(startCal)); attributesMap.put("start", formatFullDate(startCal)); startCal.set(Calendar.DATE,startCal.getActualMaximum(Calendar.DATE)); System.out.println("添加end节点"+formatFullDate(startCal)); attributesMap.put("end", formatFullDate(startCal)); attributesMap.put("label",formatShortDate(startCal)); Element category=addChildElement(categories, "category", attributesMap); startCal.add(Calendar.MONTH, 1); System.out.println("下移一个月后"+formatFullDate(startCal)); attributesMap.clear(); } startCal.set(Calendar.DATE, 1); attributesMap.put("start", formatFullDate(startCal)); startCal.set(Calendar.DATE,startCal.getActualMaximum(Calendar.DATE)); attributesMap.put("end", formatFullDate(startCal)); attributesMap.put("label",formatShortDate(startCal)); Element category=addChildElement(categories, "category", attributesMap); } private Element addChildElement(Element parentElement,String childElementName,Map<String,String> childElementAttributesMap){ Element childElement=parentElement.addElement(childElementName); for(String key:childElementAttributesMap.keySet()){ childElement.addAttribute(key,childElementAttributesMap.get(key)); } return childElement; } private String getFormattedDate(String dateStr,String balance){ String[] dateParam=dateStr.split("-"); Calendar cal=new GregorianCalendar(); cal.set(Calendar.YEAR, Integer.parseInt(dateParam[0])); cal.set(Calendar.MONTH, Integer.parseInt(dateParam[1])-1); if(balance.equals("firstday")){ cal.set(Calendar.DATE, 1); }else if(balance.equals("lastday")){ cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE)); } SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(new Date(cal.getTimeInMillis())); } //得到项目时间区间 private void getTimeArea() throws SQLException{ T06_GanttDAO t06_GanttDAO = (T06_GanttDAO) context.getBean("t06_ganttDAO"); timeArea=(Map)t06_GanttDAO.getMinMaxDate(sqlMap, projectkey); } private String formatFullDate(Calendar cal){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(new Date(cal.getTimeInMillis())); } private String formatShortDate(Calendar cal){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM"); return sdf.format(new Date(cal.getTimeInMillis())); } /*待实现*/ private Element addMilestones(Element rootElement) { return null; } /*待实现*/ private Element addConnectors(Element rootElement) { return null; } /*用于jsp页面定义fusionChart的高度*/ public double getFusionChartHeigth() { return 165+rowNum*25; } } JSP页面 <%@ page contentType="text/html; charset=GBK"%> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%> <html> <head> <%@ include file="../../bmp/platform/common/style_include.jsp"%> <link href="<%=request.getContextPath()%>/skin/<%=style%>/css/awp_base.css" rel="stylesheet" type="text/css"> <SCRIPT LANGUAGE="JavaScript" src="<%=request.getContextPath()%>/js/awp_base.js"></SCRIPT> <link rel="stylesheet" href="<%=request.getContextPath()%>/css/css_all.css" type="text/css"> <script language="JavaScript" src="<%=request.getContextPath()%>/js/fusionchart/FusionCharts.js"></script> <script language="JavaScript" src="<%=request.getContextPath()%>/js/fusionchart/jquery-1.6.2.min.js"></script> <script language="javascript"> jQuery.noConflict(); jQuery(function() { var winWidth = 0; var winHeight = 0; if(window.innerWidth){ winWidth = window.innerWidth; }else if ((document.body) && (document.body.clientWidth)){ winWidth = document.body.clientWidth; } //获取窗口高度 if(window.innerHeight){ winHeight = window.innerHeight; }else if((document.body) && (document.body.clientHeight)){ winHeight = document.body.clientHeight; } //通过深入Document内部对body进行检测,获取窗口大小 if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) { winHeight = document.documentElement.clientHeight; winWidth = document.documentElement.clientWidth; } var currentRenderer = 'flash'; var chartObj; FusionCharts.setCurrentRenderer(currentRenderer); chartObj = new FusionCharts({ swfUrl : "<%=request.getContextPath()%>/charts/Gantt.swf", width : winWidth+210, height : '<c:out value="${requestScope.fusionChartHeight}"></c:out>', id : 'sampleChart', dataSource : "<%=request.getContextPath()%>/projectGannt.xml", dataFormat : FusionChartsDataFormats.XMLURL, renderAt : 'chart1div' }).render(); }); </script> </head> <body> <div id='content'> <div class='awp_title'> <table> <tr> <td> 甘特图 </td> <td align="right" class="td2"> <input type="button" name="btsearch2" value="查 找" onclick="btnDisplayHidden(this, 'searchtable')" /> <input type="button" name="new" id="new" value="申 请" /> <input type="button" name="delete" id="delete" value="删 除" /> </td> </tr> </table> </div> </div> <div id="chart1div"> </div> </BODY> </html> ibatis映射文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap namespace="T06_Gantt"> <!-- 得到项目计划和工时记录的最小的最大日期 --> <statement id="getMinMaxTime" parameterClass="java.lang.String" resultClass="java.util.HashMap"> select min(datearea) mindate,max(datearea) maxdate from ( select t1.workday datearea from t21_worktime t1 where t1.isproject='1' and t1.projectid=#projectkey# union select t2.plbegindate datearea from t06_work_schedule t2 where t2.projectkey=#projectkey# union select t3.plendate datearea from t06_work_schedule t3 where t3.projectkey=#projectkey#) </statement> <!-- 获取项目计划阶段的信息 --> <statement id="getProjectStage" parameterClass="java.lang.String" resultClass="java.util.HashMap"> select t1.ptype stage, t1.plbegindate planstartday, t1.plendate planendday, min(t2.workday) actualstartday, max(t2.workday) actualendday, t1.plwtcounts planworktime, nvl(sum(t2.workcounts + t2.directorcounts + t2.managerconts),0) actualworktime from t06_work_schedule t1 left join t21_worktime t2 on t1.ptype = t2.type and t1.projectkey = t2.projectid and t2.isproject = '1' and t1.projectkey = #projectkey# group by t1.ptype, t1.plbegindate, t1.plendate, t1.plwtcounts order by t1.ptype </statement> </sqlMap>