GanttChart.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import moment from 'moment';
  2. // import './g.js';
  3. /**
  4. * 数据定义区域
  5. *
  6. */
  7. /**
  8. * 甘特图
  9. * @param {} options
  10. */
  11. export function GanttChart(options) {
  12. // 任务列表
  13. this.tasks = [];
  14. // AntVG Canvas
  15. this.gCanvas = null;
  16. // 视口宽度 800,可视区域
  17. this.viewWidth = options['viewWidth'] || 800;
  18. // 物理画布宽度 800
  19. this.cWidth = options['cWidth'] || 2400;
  20. this.cHeight = options['cHeight'] || 600;
  21. // 画布偏移位置
  22. this.startPos = 0;
  23. // 是否拖动中
  24. this.draging = false;
  25. // 开始拖动事件
  26. this.startEvent = null;
  27. // 结束拖动事件
  28. this.endEvent = null;
  29. // 拖动过程事件
  30. this.dragingEvent = null;
  31. // 拖动偏移量
  32. this.offsetDis = options['viewWidth'] || 800;
  33. // 拖动定时器
  34. this.dragTimer = null;
  35. // 每天的间隔宽度
  36. this.dayStep = 40;
  37. // 分组标题高度
  38. this.groupTitleHeight = 30;
  39. // 任务矩形高度
  40. this.taskRowHeight = 16;
  41. // 总天数
  42. this.daysCount = options['daysCount'] || 60;
  43. // 每像素代表的小时数
  44. this.timePerPix = this.cWidth/this.daysCount/24/3600
  45. // 当前视图开始时间,向前推N天
  46. this.startAt = moment().subtract(this.daysCount / 3, 'days');
  47. this.endAt = moment(this.startAt).add(this.daysCount, 'days');
  48. this.graphDiv = document.getElementById('graphDiv');
  49. // 图形容器组
  50. this.graphGroup = null;
  51. // 上一次拖动的事件
  52. this.lastDragEv = null;
  53. }
  54. /**
  55. * 设置数据
  56. * @param {*} _tasks
  57. */
  58. GanttChart.prototype.changeTasks = function(_tasks){
  59. this.tasks = _tasks
  60. }
  61. /**
  62. * 打开关闭分组
  63. */
  64. GanttChart.prototype.toggle = function(no) {
  65. if (tasks[no].open) {
  66. tasks[no].open = false;
  67. } else {
  68. tasks[no].open = true;
  69. }
  70. processData();
  71. drawTasks();
  72. }
  73. /**
  74. * 预处理数据
  75. */
  76. GanttChart.prototype.processData = function() {
  77. for (let i = 0; i < this.tasks.length; i++) {
  78. let currentTopTask = this.tasks[i];
  79. let lastTopTask = null;
  80. currentTopTask.renderOptions = {};
  81. if (i != 0) {
  82. // 非0个,要补上前面的数据
  83. lastTopTask = tasks[i - 1];
  84. currentTopTask.renderOptions.startY = lastTopTask.renderOptions.endY + 20;
  85. } else {
  86. // 第一个
  87. currentTopTask.renderOptions.startY = 120;
  88. }
  89. if (currentTopTask.open) {
  90. currentTopTask.renderOptions.endY =
  91. currentTopTask.renderOptions.startY + this.groupTitleHeight + currentTopTask.dataList.length * this.taskRowHeight;
  92. } else {
  93. currentTopTask.renderOptions.endY = currentTopTask.renderOptions.startY + this.groupTitleHeight;
  94. }
  95. }
  96. }
  97. /**
  98. * 强制清空图像,重绘
  99. */
  100. GanttChart.prototype.forceRefreshGraph = function() {
  101. this.tasks.forEach((topTask) => {
  102. topTask.gGroup = null;
  103. });
  104. this.gCanvas.destroy();
  105. this.initDrawingReady();
  106. }
  107. /**
  108. * 准备绘制,用于初始化和强制刷新
  109. */
  110. GanttChart.prototype.initDrawingReady = function() {
  111. this.initCanvas();
  112. this.initDragHandler();
  113. this.drawTimeZone();
  114. this.processData();
  115. this.drawTasks();
  116. this.graphGroup = null
  117. }
  118. /**
  119. * 翻页
  120. */
  121. GanttChart.prototype.pageTo = function(dir = 'next') {
  122. if (dir == 'next') {
  123. // 向后翻页`
  124. this.startAt = this.startAt.add(this.daysCount, 'days');
  125. this.offsetDis = 0
  126. } else {
  127. // 向前翻页
  128. this.startAt = this.startAt.subtract(this.daysCount, 'days');
  129. this.offsetDis = 1800 //2*viewWidth
  130. }
  131. this.endAt = moment(this.startAt).add(this.daysCount, 'days');
  132. console.log('已翻页', this.startAt.format('YYYY-MM-DD'),this.endAt.format('YYYY-MM-DD'));
  133. // offsetDis = viewWidth;
  134. this.forceRefreshGraph();
  135. }
  136. // 上次点击时间,用于滚动时误触停止
  137. let lastClickAt = null;
  138. /**
  139. * 执行拖动
  140. * 改变graphDiv 滚动距离
  141. * 到达边界距离后,刷新页面
  142. */
  143. GanttChart.prototype.doDrag = function(sEvent, eEvent) {
  144. if (sEvent == null) {
  145. sEvent = this.startEvent;
  146. }
  147. let sPos = sEvent.clientX;
  148. let cPos = eEvent.clientX;
  149. // 滚动距离
  150. let dis = cPos - sPos;
  151. let tempDis = this.offsetDis
  152. // console.log('offsetDis before:', this.offsetDis, dis)
  153. this.offsetDis = this.offsetDis - dis / 2;
  154. // console.log('draging...',tempDis, this.offsetDis, dis)
  155. if (this.offsetDis <= -20) {
  156. // 向前滑动,向前翻页
  157. console.log('此处应该向前翻页', this.startAt.format('YYYY-MM-DD'),this.endAt.format('YYYY-MM-DD'));
  158. this.offsetDis = this.viewWidth;
  159. this.pageTo('prev');
  160. }
  161. if (this.offsetDis - 20 >= 1800 ){ //cWidth - viewWidth) {
  162. // 向后滑动,向后翻页
  163. console.log('此处应该向后翻页', this.startAt.format('YYYY-MM-DD'),this.endAt.format('YYYY-MM-DD'));
  164. this.offsetDis = this.viewWidth;
  165. this.pageTo('next');
  166. }
  167. this.graphDiv.scrollLeft = this.offsetDis;
  168. }
  169. /**
  170. * 初始化抓取拖动事件
  171. */
  172. GanttChart.prototype.initDragHandler = function() {
  173. this.graphDiv.scrollLeft = this.offsetDis;
  174. let _canvas = document.getElementsByTagName('canvas')[0];
  175. _canvas.addEventListener('mousedown', (ev) => {
  176. this.draging = true;
  177. this.startEvent = ev;
  178. this.dragingEvent = null;
  179. this.endEvent = null;
  180. this.lastClickAt = new Date();
  181. this.lastClickAt = ev;
  182. this.lastDragEv = ev;
  183. });
  184. _canvas.addEventListener('mouseup', (ev) => {
  185. this.draging = false;
  186. this.endEvent = ev;
  187. });
  188. _canvas.addEventListener('mousemove', (ev) => {
  189. if (this.draging) {
  190. if (new Date() - this.lastClickAt < 20) {
  191. return false;
  192. }
  193. this.dragingEvent = ev;
  194. this.doDrag(this.lastDragEv, ev);
  195. this.lastDragEv = ev;
  196. }
  197. });
  198. }
  199. /**
  200. * 初始化画布
  201. *
  202. */
  203. GanttChart.prototype.initCanvas = function() {
  204. this.gCanvas = new G.Canvas({
  205. container: 'ganttContainer',
  206. width: 2400,
  207. height: 800,
  208. });
  209. }
  210. /**
  211. * 绘制时间区域
  212. */
  213. GanttChart.prototype.drawTimeZone = function() {
  214. console.log('时间段', this.startAt.format('YYYY-MM-DD'),this.endAt.format('YYYY-MM-DD'));
  215. let start = moment(this.startAt);
  216. let timeGroup = this.gCanvas.addGroup();
  217. timeGroup._tname = 'TimeGroup';
  218. // 绘制第一级
  219. timeGroup.addShape('text', {
  220. attrs: {
  221. x: 20,
  222. y: 20,
  223. fontSize: 12,
  224. text: start.format('YYYY-MM'),
  225. lineDash: [10, 10],
  226. fill: 'red',
  227. },
  228. });
  229. timeGroup.addShape('text', {
  230. attrs: {
  231. x: 20 + this.viewWidth,
  232. y: 20,
  233. fontSize: 12,
  234. text: start.add(this.daysCount / 3, 'days').format('YYYY-MM'),
  235. lineDash: [10, 10],
  236. fill: 'red',
  237. },
  238. });
  239. timeGroup.addShape('text', {
  240. attrs: {
  241. x: 20 + this.viewWidth * 2,
  242. y: 20,
  243. fontSize: 12,
  244. text: start.add(this.daysCount / 3, 'days').format('YYYY-MM'),
  245. lineDash: [10, 10],
  246. fill: 'red',
  247. },
  248. });
  249. let startSecond = moment(this.startAt);
  250. // 绘制第二级
  251. for (let i = 0; i < this.daysCount; i++) {
  252. let timeText = startSecond.add(1, 'days').format('MM-DD');
  253. timeGroup.addShape('text', {
  254. attrs: {
  255. x: 40 * i,
  256. y: 40,
  257. fontSize: 10,
  258. text: timeText,
  259. lineDash: [10, 10],
  260. fill: 'red',
  261. },
  262. });
  263. }
  264. }
  265. /**
  266. * 处理点击
  267. */
  268. GanttChart.prototype.handleClick = function(task, flag, ev) {
  269. let detailDiv = document.getElementById('detailDiv')
  270. if(flag == 'enter'){
  271. detailDiv.style.display = 'block'
  272. detailDiv.style.left = ev.clientX+'px';
  273. detailDiv.style.top = ev.clientY+'px';
  274. document.getElementById('detailTaskName').textContent = task._pdata.description
  275. document.getElementById('detailTaskStatus').textContent = task._pdata.status
  276. document.getElementById('detailTaskStartDate').textContent = task._pdata.startDate
  277. document.getElementById('detailTaskEndDate').textContent = task._pdata.endDate
  278. console.log('show:', task);
  279. }else{
  280. detailDiv.style.display = 'none'
  281. console.log('hide:', task);
  282. }
  283. }
  284. /**
  285. * 根据任务状态区分颜色
  286. *
  287. */
  288. GanttChart.prototype.statusColor =function(task) {
  289. switch (task.status) {
  290. case '按时完成':
  291. return 'aqua';
  292. break;
  293. case '计划批准':
  294. return '#ff9800';
  295. break;
  296. case '已完成':
  297. return '#19b720';
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. /**
  304. * 判断任务是否在视图内
  305. *
  306. */
  307. GanttChart.prototype.isInView = function(task) {
  308. let isLessThanEndAt = (task.endDate <= this.startAt.format('YYYY-MM-DD'))
  309. let isGreaterThanStartAt = task.startDate >= this.endAt.format('YYYY-MM-DD')
  310. return !(isLessThanEndAt || isGreaterThanStartAt)
  311. }
  312. /**
  313. * 分组绘制任务块
  314. *
  315. */
  316. GanttChart.prototype.drawTasks = function() {
  317. if (this.graphGroup) {
  318. this.graphGroup.clear();
  319. } else {
  320. this.graphGroup = this.gCanvas.addGroup();
  321. this.graphGroup._tname = 'graphGroup';
  322. }
  323. this.tasks.forEach((topTask, topIndex) => {
  324. if (topTask.open) {
  325. let taskGroup = null;
  326. taskGroup = this.graphGroup.addGroup();
  327. taskGroup._tname = 'TaskGroup_' + topTask.id;
  328. topTask.gGroup = taskGroup;
  329. topTask.dataList.forEach((taskP, index) => {
  330. // 在视图中才显示
  331. let taskPGroup = taskGroup.addGroup();
  332. taskGroup.addGroup(taskPGroup);
  333. taskP.tasks.forEach((_taskItem, _index) => {
  334. let _isInView = this.isInView(_taskItem)
  335. if(_isInView){
  336. let pos = this.calRectPos(_taskItem);
  337. // console.log('render rect:', _taskItem, pos, topTask.renderOptions.startY + index * taskRowHeight);
  338. let rectEl = taskPGroup.addShape('rect', {
  339. attrs: {
  340. x: pos.x,
  341. y: topTask.renderOptions.startY + (index* this.taskRowHeight),
  342. width: pos.width,
  343. height: this.taskRowHeight,
  344. fill: this.statusColor(_taskItem),
  345. stroke: 'black',
  346. radius: [2, 4],
  347. },
  348. });
  349. rectEl._pdata = _taskItem;
  350. rectEl.on('mouseover', (ev) => {
  351. this.handleClick(ev.target, 'enter', ev);
  352. });
  353. rectEl.on('mouseleave', (ev) => {
  354. this.handleClick(ev.target, 'leave', ev);
  355. });
  356. }
  357. });
  358. });
  359. taskGroup.show();
  360. } else {
  361. if (topTask.gGroup) {
  362. // topTask.gGroup.hide()
  363. topTask.gGroup = null;
  364. }
  365. }
  366. });
  367. }
  368. /**
  369. * 根据 Task 计算矩形位置
  370. *
  371. */
  372. GanttChart.prototype.calRectPos = function(taskItem) {
  373. let duraStartAt = new Date(taskItem.startDate) - new Date(this.startAt.format('YYYY-MM-DD'));
  374. let secondsStartAt = duraStartAt/1000
  375. let duraEndAt = new Date(taskItem.endDate) - new Date(this.startAt.format('YYYY-MM-DD'));
  376. let secondsEndAt = duraEndAt/1000
  377. return {
  378. x: secondsStartAt * this.timePerPix,
  379. y: 0,
  380. width: secondsEndAt * this.timePerPix,
  381. height: 0,
  382. };
  383. }
  384. /**
  385. * 主函数
  386. *
  387. */
  388. GanttChart.prototype.main = function() {
  389. this.initDrawingReady();
  390. }