当前位置: 首页 > article >正文

vue使用甘特图dhtmlxgantt + gantt.addTaskLayer

效果图:

甘特图
  • 官网地址

gantt安装与使用

vue版---部分功能收费
  1. 安装gantt  或  引入文件

    npm install dhtmlx-gantt -save
    
    或
    
    import gantt from "/public/static/dhtmlxgantt/dhtmlxgantt.js";
    import "/public/static/dhtmlxgantt/locale/locale_cn.js";
  2. 引入---组件

<template>
#节点高度要给,gantt不根据内容撑开
 <div ref="gantt" class="gantt-container" style="min-height: calc(85vh - 100px); width: 100%; overflow: hidden"></div>
</template>
<script>
 import gantt from "/public/static/dhtmlxgantt/dhtmlxgantt";
 import "/public/static/dhtmlxgantt/locale/locale_cn.js";
</script>

 借鉴

  1. css文件地址 examples/dhtmlx_gantt/dhtmlxgantt.css · 残星落影/博客 - 码云 - 开源中国 (gitee.com)
  2. js文件地址 examples/dhtmlx_gantt/dhtmlx · 残星落影/博客 - 码云 - 开源中国 (gitee.com)

定义渲染数据 

let tasks = ref({
  data: [
    {
      id: 1,
      text: "计划#1",
      start_date: "2023-09-28",
      end_date: "2023-10-28",
      sj_start_date: "2023-09-29",
      sj_end_date: "2023-10-25",
      open: true,
    },
    {
      id: 2,
      text: "已完成",
      start_date: "2023-09-28",
      end_date: "2023-10-28",
      sj_start_date: "2023-09-29",
      sj_end_date: "2023-10-25",
      parent: 1,
    },
    {
      id: 3,
      text: "计划#2",
      start_date: "2023-03-10",
      end_date: "2023-5-20",
      sj_start_date: "2023-03-10",
      sj_end_date: "2023-5-18",
      open: true,
    },
    {
      id: 4,
      text: "已完成",
      start_date: "2023-03-10",
      end_date: "2023-5-20",
      sj_start_date: "2023-03-10",
      sj_end_date: "2023-5-18",
      parent: 3,
    },
  ],
});

 完整代码

<script>
onMounted(() => {
  gantt.config.autosize = true;
  // 只读模式
  gantt.config.readonly = true;
  //是否显示左侧树表格
  gantt.config.show_grid = true;
  //表格列设置
  gantt.config.columns = [
    {
      name: "id",
      label: "编号",
      align: "center",
      tree: false,
      width: "50",
    },
    {
      name: "text",
      label: "计划名称",
      tree: true,
      width: "240",
    },
    {
      name: "start_date",
      label: "计划开始",
      align: "center",
      tree: false,
      width: "100",
    },
    {
      name: "end_date",
      label: "计划完成",
      align: "center",
      tree: false,
      width: "100",
    },
    {
      name: "sj_start_date",
      label: "实际开始",
      align: "center",
      tree: false,
      width: "100",
    },
    {
      name: "sj_end_date",
      label: "实际完成",
      align: "center",
      tree: false,
      width: "100",
    },
  ];
  gantt.config.subscales = [
    {
      unit: "day",
      step: 1,
      format: "%d日",
    },
  ];
  gantt.attachEvent("onGanttReady", function () {
    gantt.templates.tooltip_text = function (start: any, end: any, task: any) {
      return (
        task.toolTipsTxt +
        "<br/>" +
        "阶段:" +
        task.text +
        "<br/>" +
        gantt.templates.tooltip_date_format(start)
      );
    };
  });
  //任务条显示内容
  gantt.templates.task_text = function (start: any, end: any, task: any) {
    return (
      "<div style='text-align:center;color:#fff'>" +
      task.text +
      "(" +
      task.duration +
      "天)" +
      "</div>"
    );
  };
  //任务条上的文字大小 以及取消border自带样式
  gantt.templates.task_class = function (start: any, end: any, item: any) {
    return item.$level == 0 ? "firstLevelTask" : "secondLevelTask";
  };
  gantt.templates.timeline_cell_class = function (item: any, date: any) {
    if (date.getDay() == 0 || date.getDay() == 6) {
      return "weekend";
    }
  };
  gantt.config.layout = {
    //拖拽布局
    css: "gantt_container",
    rows: [
      {
        cols: [
          {
            view: "grid",
            id: "grid",
            scrollX: "scrollHor",
            scrollY: "scrollVer",
          },
          { resizer: true, width: 1 },
          {
            view: "timeline",
            id: "timeline",
            scrollX: "scrollHor",
            scrollY: "scrollVer",
          },
          { view: "scrollbar", scroll: "y", id: "scrollVer" },
        ],
      },
      { view: "scrollbar", scroll: "x", id: "scrollHor", height: 20 },
    ],
  };
  //时间轴图表中,任务条形图的高度
  //   gantt.config.task_height = 28;
  gantt.config.task_height = 16;
  gantt.config.row_height = 40;
  //时间轴图表中,甘特图的高度
  //   gantt.config.row_height = 36;
  //时间轴图表中,如果不设置,只有行边框,区分上下的任务,设置之后带有列的边框,整个时间轴变成格子状。
  gantt.config.show_task_cells = true;
  //当task的长度改变时,自动调整图表坐标轴区间用于适配task的长度
  gantt.config.fit_tasks = true;
  gantt.config.min_column_width = 40;
  gantt.config.auto_types = true;
  gantt.config.xml_date = "%Y-%m-%d";
  gantt.config.scale_unit = "month";
  gantt.config.step = 1;
  gantt.config.date_scale = "%Y年%M";
  gantt.config.start_on_monday = true;
  gantt.config.scale_height = 90;
  gantt.config.autoscroll = true;
  gantt.config.readonly = true;
  // gantt.i18n.setLocale("cn");
  gantt.attachEvent("onTaskLoading", function (task: any) {
    task.sj_start_date = gantt.date.parseDate(task.sj_start_date, "xml_date");
    task.sj_end_date = gantt.date.parseDate(task.sj_end_date, "xml_date");
    return true;
  });
  // 初始化
  gantt.init(proxy.$refs.gantt);
  // 数据解析
  gantt.parse(tasks.value);
  addTaskLayer();
});

// 渲染方法
function addTaskLayer() {
  gantt.addTaskLayer({
    renderer: {
      render: function draw_planned(task: any) {
        // console.log(task);
        if (task.sj_start_date && task.sj_end_date) {
          var sizes = gantt.getTaskPosition(
            task,
            task.sj_start_date,
            task.sj_end_date
          );
          // console.log(sizes);
          var el = document.createElement("div");
          el.className = "baseline";
          el.style.left = sizes.left + "px";
          el.style.width = sizes.width + "px";
          el.style.top = sizes.top + gantt.config.task_height + 17 + "px";
          // console.log(el);
          return el;
        }
        return false;
      },
      getRectangle: function (task: any, view: any) {
        if (task.start_date && task.end_date) {
          return gantt.getTaskPosition(task, task.start_date, task.end_date);
        }
        return null;
      },
    },
  });
}

// 切换日月周季
function setScaleConfig(value: any) {
  dayIndex.value = value;
  if (value == "1") {
    gantt.templates.timeline_cell_class = function (item: any, date: any) {
      if (date.getDay() == 0 || date.getDay() == 6) {
        return "weekend";
      }
    };
  } else {
    gantt.templates.timeline_cell_class = function (item: any, date: any) {
      if (date.getDay() == 0 || date.getDay() == 6) {
        return "";
      }
    };
  }
  switch (value) {
    case "1": //日
      gantt.config.xml_date = "%Y-%m-%d";
      gantt.config.scale_unit = "month";
      gantt.config.step = 1;
      gantt.config.date_scale = "%Y年%M";
      gantt.config.subscales = [
        {
          unit: "day",
          step: 1,
          date: "%d日",
        },
      ];
      gantt.render();
      break;
    case "2": //周
      gantt.config.scale_unit = "week";
      gantt.config.step = 1;
      gantt.config.date_scale = "%Y年%M";
      gantt.templates.date_scale = null;
      gantt.config.subscales = [
        {
          unit: "week",
          step: 1,
          date: "第%W周",
        },
        {
          unit: "day",
          step: 1,
          date: "%d日",
        },
      ];
      gantt.render();
      break;
    case "3": //月
      gantt.config.scale_unit = "month";
      gantt.config.step = 1;
      gantt.config.date_scale = "%Y年";
      gantt.templates.date_scale = null;
      gantt.config.subscales = [
        {
          unit: "month",
          step: 1,
          date: "%M",
        },
      ];
      gantt.render();
      break;
    case "4": //季
      gantt.config.scale_unit = "year";
      gantt.config.step = 1;
      gantt.config.date_scale = "%Y年";
      gantt.config.subscales = [
        {
          unit: "month",
          step: 1,
          date: "%M",
        },
        {
          unit: "quarter",
          step: 1,
          format: function (date: any) {
            var dateToStr = gantt.date.date_to_str("%M");
            var endDate = gantt.date.add(
              gantt.date.add(date, 3, "month"),
              -1,
              "day"
            );
            return dateToStr(date) + " - " + dateToStr(endDate);
          },
        },
      ];
      gantt.render();
      break;
  }
}
</script>

css样式

<style lang="scss" scoped>
@import "/public/static/dhtmlxgantt/dhtmlxgantt.css";

:deep(.gantt_task_line, .gantt_line_wrapper) {
  margin-top: -9px;
}

:deep(.gantt_task_line) {
  background-color: #3b97fe;
  border: #3b97fe;
  height: 15px !important;
  border-radius: 100px;
  border-radius: 100px;
}

:deep(.gantt_task_line) {
  margin-bottom: 10px !important;
}

:deep(.gantt_task_progress) {
  background: #ffd180;
  border-top-right-radius: 100px;
  border-radius: 100px;
}
:deep(.baseline) {
  position: absolute;
  border-radius: 100px;
  margin-top: -12px;
  height: 15px;
  background: #67dd23;
}
:deep(.gantt_grid_scale .gantt_grid_head_cell) {
  color: black !important;
}
:deep(.gantt_task .gantt_task_scale .gantt_scale_cell) {
  color: black !important;
}
:deep(.gantt_layout_cell) {
  ::-webkit-scrollbar {
    height: 8px;
  }
  ::-webkit-scrollbar-track {
    border-radius: 8px;
    background-color: #f5f5f5;
  }

  ::-webkit-scrollbar-thumb {
    border-radius: 8px;
    background-color: #b9b9b9;
  }
}

// 周末 区分
:deep(.weekend) {
  background: #f4f7f4;
}
:deep(.gantt_selected .weekend) {
  background: #f7eb91;
}

http://www.kler.cn/a/163333.html

相关文章:

  • ODOO学习笔记(3):Odoo和Django的区别是什么?
  • go T 泛型
  • uniapp分享功能
  • IDEA 如何手动创建spring boot工程
  • KubeVirt入门介绍
  • 原生 JavaScript基本内容和常用特性详解
  • 基于高通MSM8953平台android9.0的GPIO驱动开发
  • Hbase JAVA API 增删改查操作
  • 【电子取证篇】汽车取证数据提取与汽车取证实例浅析(附标准下载)
  • imazing正在查找最新的apple mobile device组件
  • SpringBoot AOP切面实现对自定义注解的属性动态修改
  • 无重复字符的最长子串(LeetCode 3)
  • 记录一下Mac配置SpringBoot开发环境
  • “华为杯”研究生数学建模竞赛2016年-【华为杯】A题:无人机在抢险救灾中的优化运用(附获奖论文及MATLAB代码实现)
  • perf与火焰图-性能分析工具
  • IntelliJ IDEA使用Eval Reset
  • Unity使用打成图集的Sprite作为模型贴图使用的问题
  • ubuntu server 20.04 备份和恢复 系统 LTS
  • 小红书用户采集工具:掌握策略,轻松吸引潜在客户
  • 【flink番外篇】1、flink的23种常用算子介绍及详细示例(1)- map、flatmap和filter
  • 【链表Linked List】力扣-82 删除链表中的重复元素II
  • velocity-engine-core是什么?Velocity模板引擎的使用
  • pr抖音素材42个手机竖屏抖音视频转场特效PR剪辑模板
  • 浅谈5G基站节能及数字化管理解决方案的设计与应用-安科瑞 蒋静
  • 智慧城市是什么?为什么要建智慧城市?
  • 8_企业架构缓存中间件分布式memcached