经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
前端甘特图dhtmx-gantt
来源:cnblogs  作者:ygunoil  时间:2021/3/8 11:50:45  对本文有异议

一、背景

    公司业务需要,管理层做项目管理就会制定项目计划,为了更好的的做好项目计划就需要用到做计划常用的工具甘特图,而且做好计划管理对项目的风险管控也有很大的好处。

二、甘特图官网以及文档地址

   破解版:***********

 

 

三、甘特图六要素

    1.   tasks   甘特图任务相关,包含data(数据)、links(数据之间的关系)、collections等  ——> gantt.parse(tasks)  注:parse前一定要先清一下数据不然来回切换不同的甘特图会渲染出错 gantt.clearAll()
    2. config   甘特图配置相关,相当于插件的options,不过有些options依赖于plugin(有plugin配值config才生效)——> gantt.config.xxx = xxx
    3. plugins  甘特图插件相关,有些options依赖于plugin(有plugin配值config才生效)——> gantt.plugins(plugins)
    4. templates  甘特图自定义模板相关,比如自定义tooltip_text、 task_text|、task_class等等——> gantt.templates.xxx = xxx
    5. events   甘特图事件相关——> gantt.attachEvent(name, handle, settins)
    6. zoomConfig 甘特图(日周月年)视图相关——> gantt.ext.zoom.init(zoomConfig)
 

四、甘特图使用

    1.引入 
  1. 1.引入js
  2. import {gantt , Gantt} from "yys-pro-gantt";
  3. 2.导入css
  4. ajs中导入
  5. import "yys-pro-gantt/codebase/dhtmlxgantt.css";
  6. import "yys-pro-gantt/codebase/skins/dhtmlxgantt_terrace.css";
  7. bstyle中导入方式
  8. @import "~yys-pro-gantt/codebase/dhtmlxgantt.css";
  9. /*@import "~yys-pro-gantt/codebase/skins/dhtmlxgantt_terrace.css"; // 皮肤*/
  10. @import "./skins/dhtmlxgantt_yys.css"; // 自定义皮肤
  11. 不需要下面可以不引入
  12. // import "dhtmlx-gantt/codebase/locale/locale_cn" ; // 本地化
  13. // import "dhtmlx-gantt/codebase/ext/dhtmlxgantt_tooltip.js"; // 任务条悬浮提示
  14. // this.createScript("http://export.dhtmlx.com/gantt/api.js"); // 使用甘特图自带的导出功能必须引入
  15. createScript
  16. createScript(url: string) {
  17. const scriptElement = document.createElement("script");
  18. scriptElement.src = url;
  19. const headerElement = document.querySelector("head") as any;
  20. if (headerElement) {
  21. headerElement.appendChild(scriptElement);
  22. }
  23. }
 
2、甘特图初始化
 
  1. template
  2. <div ref="gantt"
  3. id="yys-gantt">
  4. </div>
  5. vuejs
  6. gantt() { // 获取到dom元素
  7. return this.$refs.gantt as any;
  8. }
  9. get ganttInstance() { // 获取到甘特图实例便能调用甘特图的所有方法
  10. return gantt || Gantt.getGanttInstance();
  11. }
  12. this.ganttInstance.init(this.gantt());
  13. this.ganttInstance.clearAll();
  14. this.ganttInstance.parse(this.tasks);

  

 
3.甘特图更新
 
  a. tasks变更甘特图更新
  1. @Watch("config", { immediate: true, deep: true })
  2. updateConfig(nConfig: any, oConfig: any) {
  3. if (JSON.stringify(nConfig) === JSON.stringify(oConfig)) { return; }
  4. this.$nextTick(() => {
  5. if (this.config) {
  6. Object.assign(this.ganttInstance.config, this.defaultConfig, this.config);
  7. }
  8. if (this.gantt()) {
  9. this.ganttInstance.init(this.gantt());
  10. gantt.clearAll();
  11. this.ganttInstance.parse(this.tasks);
  12. }
  13. })
  14. }

  

  b. config变更甘特图更新
 
  1. @Watch("config", { deep: true })
  2. updateConfig() {
  3. if (this.config) {
  4. Object.assign(this.ganttInstance.config, this.defaultConfig, this.config);
  5. }
  6. if (this.gantt()) {
  7. this.ganttInstance.init(this.gantt());
  8. gantt.clearAll();
  9. this.ganttInstance.parse(this.tasks);
  10. }
  11. }
 
4.甘特图销毁
  1. gantt.destructor()

  

五.那些踩过的坑

   1、data里面的部分属性的key是不能更改的,比如id,parent,start_date、end_date、progress、open
       links里面的全部属性的key是不能更改的id source target type
   2、data如果type是project,那么这条数据的开始时间会取其里面所有数据的最早开始时间,结束时间会取里面所有数据的最晚开始时间,如果这条数据里面的所有数据都是空数据,那么start_date会甘特图渲染的最早时间,end_date是start_date的后一天,这样数据就会不准确?
         解决方案: data里加个unscheduled属性来控制是否渲染这条数据,需要注意的是在所有涉及到日期的地方都要加,如tooltips_text 、columns、 拖拽等等
   3、 dhtmlx-gantt都是下划线分割,而且api中都是这样,但在layout中min_width、max_width不生效,要用minWidth、maxWidth替换才生效。
   4、排序后的数据默认从页面获取的row元素可能是不准确的,需要从dataStroe中获取。
   5、甘特图在不占全屏的情况下, order_branch = true,拖拽会有限制?
    解决方案:
 6、在左侧表格和列都能拖拽的情况下,会突然弹回到默认宽度?
    解决方案:监控config阻止掉更新;
  1. @Watch("config", { deep: true })
  2. updateConfig(nConfig: any, oConfig: any) {
  3. if (JSON.stringify(nConfig) === JSON.stringify(oConfig)) return;
  4. }
 
 7、默认情况下甘特图经常出现错误提示?
    解决方案:将show_errors设为false
8、link添加类型&&计划和里程碑规则
  1. link_class: (link: any) => {
  2. // console.log(link)
  3. const {type} = link;
  4. return `link-type-${type}`;
  5. },
  6.  
  7.  
  8.  
  9. target.forEach((linkId: any) => {
  10. const link = this.gantt.getLink(linkId);
  11. const {
  12. sourceTask,
  13. targetTask,
  14. type,
  15. } = this.getSourceTaskAndTargetTaskByLink(link);
  16. switch (type) {
  17. case LinkType.fs:
  18. if ( +targetTask.start_date < +sourceTask.end_date ) {
  19. fsLimit(task, sourceTask);
  20. }
  21. break;
  22. case LinkType.ss:
  23. if (+targetTask.start_date > +sourceTask.start_date) {
  24. limitLeft(task, targetTask);
  25. }
  26. break;
  27. case LinkType.ff:
  28. if (+targetTask.end_date > +sourceTask.end_date) {
  29. limitRight(task, targetTask);
  30. }
  31. break;
  32. case LinkType.sf:
  33. if (+targetTask.start_date > +sourceTask.end_date) {
  34. limitRight(task, targetTask);
  35. }
  36. break;
  37. default:
  38. break;
  39. }
  40.  
  41.  
  42. fsMoveLimit(task: any, limit: any) {
  43. const dur = task.end_date - task.start_date;
  44. if (task.type === GANTT_TYPE.计划 && limit.type === GANTT_TYPE.计划) {
  45. task.start_date = new Date(limit.end_date);
  46. task.end_date = new Date(limit.start_date + dur);
  47. }
  48. if (task.type === GANTT_TYPE.计划 && limit.type === GANTT_TYPE.里程碑) {
  49. task.start_date = new Date(limit.end_date);
  50. task.end_date = new Date(limit.start_date + dur);
  51. }
  52. if (task.type === GANTT_TYPE.里程碑 && limit.type === GANTT_TYPE.里程碑) {
  53. task.start_date = new Date(limit.start_date);
  54. }
  55. if (task.type === GANTT_TYPE.里程碑 && limit.type === GANTT_TYPE.计划) {
  56. task.start_date = new Date(limit.end_date);
  57. }
  58. }
  59. fsResizeLimit(task: any, limit: any) {
  60. const dur = task.end_date - task.start_date;
  61. if (task.type === GANTT_TYPE.计划 && limit.type === GANTT_TYPE.计划) {
  62. task.start_date = new Date(limit.end_date);
  63. }
  64. if (task.type === GANTT_TYPE.计划 && limit.type === GANTT_TYPE.里程碑) {
  65. task.start_date = new Date(limit.end_date);
  66. }
  67. }
  68.  
  69.  
  70.  
  71. .gantt_task_link.link-type-0 .gantt_line_wrapper:nth-of-type(2)::before{
  72. content: "fs";
  73. position: absolute;
  74. top: 10px;
  75. left: 15px;
  76. font-size: 16px;
  77. }
  78. .gantt_task_link.link-type-1 .gantt_line_wrapper:nth-of-type(2)::before{
  79. content: "ss";
  80. position: absolute;
  81. top: 10px;
  82. left: 15px;
  83. font-size: 16px;
  84. }
  85. .gantt_task_link.link-type-2 .gantt_line_wrapper:nth-of-type(2)::before{
  86. content: "ff";
  87. position: absolute;
  88. top: 10px;
  89. left: 15px;
  90. font-size: 16px;
  91. }
  92. .gantt_task_link.link-type-3 .gantt_line_wrapper:nth-of-type(2)::before{
  93. content: "sf";
  94. position: absolute;
  95. top: 10px;
  96. left: 15px;
  97. font-size: 16px;
  98. }

  

转载请注明出处:https://www.cnblogs.com/ygunoil

原文链接:http://www.cnblogs.com/ygunoil/p/14416981.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号