GanttChart.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. import moment from 'moment';
  2. /**
  3. * 甘特图
  4. * @param {} options
  5. */
  6. export function GanttChart(options) {
  7. // 任务列表
  8. this.tasks = options.tasks || [];
  9. // AntVG Canvas
  10. this.gCanvas = null;
  11. // 视口宽度 800,可视区域
  12. this.viewWidth = options['viewWidth'] || 800;
  13. // 物理画布宽度 800
  14. this.cWidth = options['cWidth'] || 2400;
  15. this.cHeight = options['cHeight'] || 600;
  16. // 画布偏移位置
  17. this.startPos = 0;
  18. // 是否拖动中
  19. this.draging = false;
  20. // 开始拖动事件
  21. this.startEvent = null;
  22. // 结束拖动事件
  23. this.endEvent = null;
  24. // 拖动过程事件
  25. this.dragingEvent = null;
  26. // 拖动偏移量
  27. this.offsetDis = (options['viewWidth']/2) || 800;
  28. // 拖动定时器
  29. this.dragTimer = null;
  30. // 每天的间隔宽度
  31. this.dayStep = 40;
  32. // 分组标题高度
  33. this.groupTitleHeight = 38;
  34. // 任务矩形高度
  35. this.taskRowHeight = 16;
  36. // 每行任务的纵向间距
  37. this.rowSpanDis = 22;
  38. // 总天数
  39. this.daysCount = options['daysCount'] || 60;
  40. // 任务图距离顶部高度
  41. this.graphTopDis = 40
  42. // 每像素代表的小时数
  43. this.timePerPix = this.cWidth/this.daysCount/24/3600
  44. // 当前视图开始时间,向前推N天
  45. this.startAt = moment().subtract(this.daysCount / 3, 'days');
  46. this.endAt = moment(this.startAt).add(this.daysCount, 'days');
  47. this.graphDiv = document.getElementById(options['chartParentContainer']);
  48. this.chartContainer = options['chartContainer']
  49. // 图形容器组
  50. this.graphGroup = null;
  51. // 上一次拖动的事件
  52. this.lastDragEv = null;
  53. // 当日刻度线
  54. this.todayTimeLineEl = null;
  55. }
  56. /**
  57. * 设置数据
  58. * @param {*} _tasks
  59. */
  60. GanttChart.prototype.changeTasks = function(_tasks){
  61. this.tasks = _tasks
  62. }
  63. /**
  64. * 打开关闭分组
  65. */
  66. GanttChart.prototype.toggle = function(index) {
  67. if (this.tasks[index].open) {
  68. this.tasks[index].open = false;
  69. } else {
  70. this.tasks[index].open = true;
  71. }
  72. this.processData();
  73. this.drawTasks();
  74. }
  75. /**
  76. * 预处理数据
  77. */
  78. GanttChart.prototype.processData = function() {
  79. for (let i = 0; i < this.tasks.length; i++) {
  80. let currentTopTask = this.tasks[i];
  81. let lastTopTask = null;
  82. currentTopTask.renderOptions = {};
  83. if (i != 0) {
  84. // 非0个,要补上前面的数据
  85. lastTopTask = this.tasks[i - 1];
  86. currentTopTask.renderOptions.startY = lastTopTask.renderOptions.endY + 20;
  87. } else {
  88. // 第一个
  89. currentTopTask.renderOptions.startY = this.graphTopDis;
  90. }
  91. if (currentTopTask.open) {
  92. currentTopTask.renderOptions.endY =
  93. currentTopTask.renderOptions.startY + this.rowSpanDis + this.groupTitleHeight + currentTopTask.dataList.length * this.taskRowHeight;
  94. } else {
  95. currentTopTask.renderOptions.endY = currentTopTask.renderOptions.startY + this.groupTitleHeight;
  96. }
  97. }
  98. }
  99. /**
  100. * 强制清空图像,重绘
  101. */
  102. GanttChart.prototype.forceRefreshGraph = function() {
  103. this.tasks.forEach((topTask) => {
  104. topTask.gGroup = null;
  105. });
  106. this.todayTimeLineEl = null
  107. this.gCanvas.destroy();
  108. this.initDrawingReady();
  109. }
  110. /**
  111. * 准备绘制,用于初始化和强制刷新
  112. */
  113. GanttChart.prototype.initDrawingReady = function() {
  114. this.initCanvas();
  115. setTimeout(() => {
  116. this.initDragHandler();
  117. this.drawTimeZone();
  118. this.processData();
  119. this.drawTasks();
  120. this.graphGroup = null
  121. }, 200);
  122. }
  123. /**
  124. * 翻页
  125. */
  126. GanttChart.prototype.pageTo = function(dir = 'next') {
  127. this.draging = false;
  128. this.endEvent = null;
  129. if (dir == 'next') {
  130. // 向后翻页`
  131. this.startAt = this.startAt.add(this.daysCount, 'days');
  132. this.offsetDis = 0
  133. } else {
  134. // 向前翻页
  135. this.startAt = this.startAt.subtract(this.daysCount, 'days');
  136. this.offsetDis = 2*this.viewWidth
  137. }
  138. this.endAt = moment(this.startAt).add(this.daysCount, 'days');
  139. console.log('已翻页', this.startAt.format('YYYY-MM-DD'),this.endAt.format('YYYY-MM-DD'), this.offsetDis);
  140. // offsetDis = viewWidth;
  141. this.forceRefreshGraph();
  142. }
  143. // 上次点击时间,用于滚动时误触停止
  144. let lastClickAt = null;
  145. /**
  146. * 执行拖动
  147. * 改变graphDiv 滚动距离
  148. * 到达边界距离后,刷新页面
  149. */
  150. GanttChart.prototype.doDrag = function(sEvent, eEvent) {
  151. if (sEvent == null) {
  152. sEvent = this.startEvent;
  153. }
  154. let sPos = sEvent.clientX;
  155. let cPos = eEvent.clientX;
  156. // 滚动距离
  157. let dis = cPos - sPos;
  158. let tempDis = this.offsetDis
  159. // console.log('offsetDis before:', this.offsetDis, dis)
  160. this.offsetDis = this.offsetDis - dis / 2;
  161. // console.log('draging...',tempDis, this.offsetDis, dis)
  162. if (this.offsetDis <= -20) {
  163. // 向前滑动,向前翻页
  164. console.log('此处应该向前翻页', this.startAt.format('YYYY-MM-DD'),this.endAt.format('YYYY-MM-DD'),this.offsetDis);
  165. this.offsetDis = this.viewWidth;
  166. this.pageTo('prev');
  167. }
  168. if ((this.offsetDis - 20) >= this.viewWidth*2.2 ){ //cWidth - viewWidth) {
  169. // 向后滑动,向后翻页
  170. console.log('此处应该向后翻页', this.startAt.format('YYYY-MM-DD'),this.endAt.format('YYYY-MM-DD'),this.offsetDis);
  171. this.offsetDis = this.viewWidth;
  172. this.pageTo('next');
  173. }
  174. this.graphDiv.scrollLeft = this.offsetDis;
  175. }
  176. /**
  177. * 初始化抓取拖动事件
  178. */
  179. GanttChart.prototype.initDragHandler = function() {
  180. this.graphDiv.scrollLeft = this.offsetDis;
  181. let _canvas = this._canvas
  182. _canvas.addEventListener('mousedown', (ev) => {
  183. this.draging = true;
  184. this.startEvent = ev;
  185. this.dragingEvent = null;
  186. this.endEvent = null;
  187. this.lastClickAt = new Date();
  188. this.lastClickAt = ev;
  189. this.lastDragEv = ev;
  190. });
  191. _canvas.addEventListener('mouseleave', (ev) => {
  192. console.log('leave...恢复')
  193. this.draging = false;
  194. this.endEvent = ev;
  195. });
  196. _canvas.addEventListener('mouseup', (ev) => {
  197. this.draging = false;
  198. this.endEvent = ev;
  199. });
  200. _canvas.addEventListener('mousemove', (ev) => {
  201. // console.log('this over', this)
  202. if (this.draging) {
  203. if (new Date() - this.lastClickAt < 20) {
  204. return false;
  205. }
  206. this.dragingEvent = ev;
  207. this.doDrag(this.lastDragEv, ev);
  208. this.lastDragEv = ev;
  209. }
  210. });
  211. }
  212. /**
  213. * 初始化画布
  214. *
  215. */
  216. GanttChart.prototype.initCanvas = function() {
  217. console.error('初始化画布...')
  218. this.gCanvas = new G.Canvas({
  219. container: this.chartContainer,
  220. width: this.cWidth,
  221. height: this.cHeight,
  222. });
  223. this._canvas = document.getElementsByTagName('canvas')[0];
  224. }
  225. /**
  226. * 绘制时间区域
  227. */
  228. GanttChart.prototype.drawTimeZone = function() {
  229. console.log('时间段', this.startAt.format('YYYY-MM-DD'),this.endAt.format('YYYY-MM-DD'));
  230. let start = moment(this.startAt);
  231. let timeGroup = this.gCanvas.addGroup();
  232. this.timeGroupEl = timeGroup
  233. timeGroup._tname = 'TimeGroup';
  234. let startSecond = moment(this.startAt);
  235. // 顶部底部边框
  236. timeGroup.addShape('rect',{
  237. attrs: {
  238. x: 0,
  239. y: 0,
  240. width: this.cWidth,
  241. height: 40,
  242. fill: '#dbcccc',
  243. radius: [2, 4],
  244. },
  245. })
  246. // 绘制第二级
  247. let nowAtStr = moment().format('YYYY-MM-DD')
  248. for (let i = 0; i < this.daysCount; i++) {
  249. let tempAt = startSecond.add(1, 'days')
  250. let timeText = tempAt.format('DD');
  251. if(timeText == '01'){
  252. // 第一天,顶部需要绘制年月
  253. timeGroup.addShape('text', {
  254. attrs: {
  255. x: this.dayStep * i,
  256. y: 20,
  257. fontSize: 12,
  258. text: tempAt.format('YYYY-MM'),
  259. lineDash: [10, 10],
  260. fill: '#8D9399',
  261. },
  262. });
  263. }
  264. let isToday = nowAtStr == tempAt.format('YYYY-MM-DD')
  265. if(isToday){
  266. //是今日,需要画线
  267. console.log('绘制 当日刻度线')
  268. this.todayTimeLineOffsetPos = this.dayStep * i
  269. timeGroup.addShape('rect',{
  270. attrs: {
  271. x: this.dayStep * i-10,
  272. y: 25,
  273. width: 30,
  274. height: 16,
  275. fill: '#0091FF',
  276. radius: [2, 4],
  277. },
  278. })
  279. }
  280. timeGroup.addShape('text', {
  281. attrs: {
  282. x: this.dayStep * i,
  283. y: 40,
  284. fontSize: 10,
  285. text: timeText,
  286. lineDash: [10, 10],
  287. fill: '#8D9399',
  288. },
  289. });
  290. timeGroup.addShape('rect', {
  291. attrs: {
  292. x: this.dayStep * i-10,
  293. y: 20,
  294. width: 1,
  295. height: this.cHeight,
  296. fill: '#deebeb',
  297. radius: [2, 4],
  298. },
  299. });
  300. }
  301. }
  302. /**
  303. * 处理点击
  304. */
  305. GanttChart.prototype.handleClick = function(task, flag, ev) {
  306. // let detailDiv = document.getElementById('detailDiv')
  307. if(flag == 'enter'){
  308. // detailDiv.style.display = 'block'
  309. // detailDiv.style.left = ev.clientX+'px';
  310. // detailDiv.style.top = ev.clientY+'px';
  311. // document.getElementById('detailTaskName').textContent = task._pdata.description
  312. // document.getElementById('detailTaskStatus').textContent = task._pdata.status
  313. // document.getElementById('detailTaskStartDate').textContent = task._pdata.startDate
  314. // document.getElementById('detailTaskEndDate').textContent = task._pdata.endDate
  315. console.log('show:', task);
  316. } else if (flag === 'leave'){
  317. // detailDiv.style.display = 'none'
  318. console.log('hide:', task);
  319. } else {
  320. this.callback(task);
  321. console.log('click:', task);
  322. }
  323. }
  324. /**
  325. * 根据任务状态区分颜色
  326. *
  327. */
  328. GanttChart.prototype.statusColor =function(task) {
  329. switch (task.status) {
  330. case '按时完成':
  331. return 'aqua';
  332. break;
  333. case '计划批准':
  334. return '#ff9800';
  335. break;
  336. case '已完成':
  337. return '#19b720';
  338. break;
  339. default:
  340. break;
  341. }
  342. }
  343. /**
  344. * 判断任务是否在视图内
  345. *
  346. */
  347. GanttChart.prototype.isInView = function(task) {
  348. let isLessThanEndAt = (task.endDate <= this.startAt.format('YYYY-MM-DD'))
  349. let isGreaterThanStartAt = task.startDate >= this.endAt.format('YYYY-MM-DD')
  350. return !(isLessThanEndAt || isGreaterThanStartAt)
  351. }
  352. /**
  353. * 分组绘制任务块
  354. *
  355. */
  356. GanttChart.prototype.drawTasks = function() {
  357. if (this.graphGroup) {
  358. this.graphGroup.clear();
  359. } else {
  360. this.graphGroup = this.gCanvas.addGroup();
  361. this.graphGroup._tname = 'graphGroup';
  362. }
  363. // 第一层循环,用于分组,例如,维保--xxxx
  364. this.tasks.forEach((topTask, topIndex) => {
  365. if (topTask.open) {
  366. let taskGroup = null;
  367. taskGroup = this.graphGroup.addGroup();
  368. taskGroup._tname = 'TaskGroup_' + topTask.id;
  369. topTask.gGroup = taskGroup;
  370. // 组名背景矩形
  371. // let TopGroupRectEl = taskGroup.addShape('rect', {
  372. // attrs: {
  373. // x: 0,
  374. // y: topTask.renderOptions.startY,
  375. // width: this.cWidth,
  376. // height: this.taskRowHeight,
  377. // fill: '#a6ed53',
  378. // radius: [2, 4],
  379. // },
  380. // });
  381. // 第二层循环,用于 区分具体多少任务,例如,维保-商管1/商管2...
  382. topTask.dataList.forEach((taskP, index) => {
  383. let taskPGroup = taskGroup.addGroup()
  384. taskGroup.addGroup(taskPGroup);
  385. // 任务背景矩形,主要用于Hover效果变更颜色
  386. if(true){
  387. let tempTaskContainerEl = taskPGroup.addShape('rect', {
  388. attrs: {
  389. x: 0,
  390. y: topTask.renderOptions.startY + ((index+1)* (this.taskRowHeight + this.rowSpanDis))-5,
  391. width: this.cWidth,
  392. height: this.taskRowHeight+10,
  393. fill: '#fff',
  394. // stroke: 'black',
  395. radius: [2, 4],
  396. },
  397. });
  398. tempTaskContainerEl.setZIndex(1)
  399. tempTaskContainerEl._pdata = taskP
  400. tempTaskContainerEl.on('mouseenter',(ev)=>{
  401. tempTaskContainerEl.attr({fill: '#F5F6F7'})
  402. tempTaskContainerEl._pdata.tasks.forEach(_tempTask=>{
  403. if(_tempTask._rectEl){
  404. _tempTask._rectEl.setZIndex(5)
  405. }
  406. })
  407. })
  408. tempTaskContainerEl.on('mouseleave',(ev)=>{
  409. tempTaskContainerEl.attr({fill: '#fff'})
  410. tempTaskContainerEl._pdata.tasks.forEach(_tempTask=>{
  411. if(_tempTask._rectEl){
  412. _tempTask._rectEl.setZIndex(5)
  413. }
  414. })
  415. })
  416. taskP._containerEl = tempTaskContainerEl
  417. }
  418. // 第三层循环,用户区分每个子任务的执行时间段,例如:维保-商管1-2020.05-2020.06 / 2020.08- 2020.09
  419. taskP.tasks.forEach((_taskItem, _index) => {
  420. let _isInView = this.isInView(_taskItem)
  421. // 在视图中才显示
  422. if(_isInView){
  423. let pos = this.calRectPos(_taskItem);
  424. // console.log('render rect:', _taskItem, pos, topTask.renderOptions.startY + index * taskRowHeight);
  425. let rectEl = taskPGroup.addShape('rect', {
  426. attrs: {
  427. x: pos.x,
  428. y: topTask.renderOptions.startY + (index* (this.taskRowHeight + this.rowSpanDis)),
  429. width: pos.width,
  430. height: this.taskRowHeight,
  431. fill: this.statusColor(_taskItem),
  432. stroke: 'black',
  433. radius: [2, 4],
  434. },
  435. });
  436. rectEl.setZIndex(5)
  437. rectEl._pdata = _taskItem;
  438. _taskItem._rectEl = rectEl
  439. rectEl.on('mouseover', (ev) => {
  440. this.handleClick(ev.target, 'enter', ev);
  441. });
  442. rectEl.on('mouseleave', (ev) => {
  443. this.handleClick(ev.target, 'leave', ev);
  444. });
  445. rectEl.on('click', (ev) => {
  446. this.handleClick(ev.target, 'click', ev);
  447. });
  448. }
  449. });
  450. });
  451. taskGroup.show();
  452. } else {
  453. if (topTask.gGroup) {
  454. // topTask.gGroup.hide()
  455. topTask.gGroup = null;
  456. }
  457. }
  458. });
  459. // 画当前线条 TODO,放前面不行
  460. let todayAt = new Date()
  461. if(this.startAt < todayAt && this.endAt > todayAt){
  462. this.todayTimeLineEl = this.gCanvas.addShape('rect',{
  463. attrs: {
  464. x: this.todayTimeLineOffsetPos,
  465. y: 40,
  466. width: 3,
  467. height: this.cHeight,
  468. fill: '#0091FF',
  469. radius: [2, 4],
  470. }
  471. })
  472. this.todayTimeLineEl.setZIndex(50)
  473. }
  474. }
  475. /**
  476. * 根据 Task 计算矩形位置
  477. *
  478. */
  479. GanttChart.prototype.calRectPos = function(taskItem) {
  480. let duraStartAt = new Date(taskItem.startDate) - new Date(this.startAt.format('YYYY-MM-DD'));
  481. let secondsStartAt = duraStartAt/1000
  482. let duraEndAt = new Date(taskItem.endDate) - new Date(taskItem.startDate);
  483. let secondsEndAt = duraEndAt/1000
  484. return {
  485. x: secondsStartAt * this.timePerPix,
  486. y: 0,
  487. width: secondsEndAt * this.timePerPix,
  488. height: 0,
  489. };
  490. }
  491. /**
  492. * 主函数
  493. *
  494. */
  495. GanttChart.prototype.main = function() {
  496. this.initDrawingReady();
  497. }