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

Vue 和 dhtmlx-gantt 实现图表构建动态多级甘特图效果 ,横坐标为动态刻度不是日期

注意事项:1、横坐标根据日期转换成时间刻度在( gantt.config.scales);2、获取时间刻度的最大值(findMaxRepairTime);3、甘特图多级列表需注意二级三级每个父子id需要唯一(convertData

  1. 安装依赖

    npm install dhtmlx-gantt --save

  2. 在当前页引入和配置 dhtmlx-gantt

    import gantt from "dhtmlx-gantt"; // 引入模块
    // import 'dhtmlx-gantt/codebase/dhtmlxgantt.css'
    import "dhtmlx-gantt/codebase/skins/dhtmlxgantt_terrace.css"; //皮肤
    import "dhtmlx-gantt/codebase/locale/locale_cn"; // 本地化
    // import "dhtmlx-gantt/codebase/ext/dhtmlxgantt_tooltip.js"; //任务条悬浮提示
    

     

  3. 创建甘特图容器

    <div ref="gantt" class="left-container" />initMount() {let convertedData = this.convertData(this.aircraftData);this.tasks.data = convertedData;let maxNum = this.findMaxRepairTime(this.aircraftData);gantt.config.start_date = new Date(2025, 0, 16, 0, 0, 0);gantt.config.end_date = new Date(2025,0,16,Math.floor(maxNum / 60),maxNum % 60,0);gantt.config.autosize = true;gantt.config.readonly = true;gantt.config.show_grid = true;gantt.config.columns = [{ name: "text", label: "名称", tree: true, width: "260" },];gantt.config.show_task_cells = true;// 设置时间刻度gantt.config.scales = [{unit: "minute",step: 10,format: function (date) {// 提取小时和分钟var hours = date.getHours();var minutes = date.getMinutes();// 将小时转换为分钟,并加上额外的分钟数var totalMinutes = hours * 60 + minutes + 10;return totalMinutes;},},];// 设置任务条上展示的内容,参数task会返回当前任务的数据gantt.templates.task_text = function (start, end, task) {return '<div class="gantt-task-content">' + task.text + "-" + task.time + "分钟" + '</div>';};// gantt.config.xml_date = '%Y-%m-%d %H:%i:%s';this.initData();// 初始化甘特图gantt.init(this.$refs.gantt);gantt.clearAll();//销毁// gantt.destructor()gantt.parse(this.tasks);},//初始化甘特图initData: function () {this.tasks.data.map(function (current, ind, arry) {var newObj = {};if (current.type) {if (current.type == 1) {newObj = Object.assign({}, current, { color: "#67c23a" });}//   } else if (current.type == 2) {//     newObj = Object.assign({}, current, { color: "#fec0dc" });//   } else if (current.type == 3) {//     newObj = Object.assign({}, current, { color: "#62ddd4" });//   } else if (current.type == 4) {//     newObj = Object.assign({}, current, { color: "#d1a6ff" });//   }} else {newObj = Object.assign({}, current, { color: "#f9d484" });}return newObj;});},

  4. 处理后端返回的数据

    //处理数组convertData(aircraftData) {let result = [];let nextUniqueId = 0; // 用于生成唯一的IDaircraftData.forEach((aircraft) => {let aircraftInfo = {id: aircraft.taskId,text: aircraft.planeNum,start_date: new Date(2025, 0, 16, 0, 0),end_date: new Date(2025,0,16,Math.floor(aircraft.repairTime / 60),aircraft.repairTime % 60,0),duration: aircraft.repairTime,open: true,time: aircraft.repairTime,};result.push(aircraftInfo);aircraft.lruList.forEach((sparePart) => {let sparePartId = `sparepart-${nextUniqueId++}`; // 生成唯一的IDlet sparePartInfo = {text: sparePart.lruName,start_date: new Date(2025, 0, 16, 0, 0),end_date: new Date(2025,0,16,Math.floor(sparePart.repairTime / 60),sparePart.repairTime % 60,0),id: sparePartId, // sparePart.lruId,  //随机数这个id与下面的sparePart.lruWorkList的parent一致duration: sparePart.repairTime,parent: sparePart.taskId,type: 1,open: true,time: sparePart.repairTime,};result.push(sparePartInfo);sparePart.lruWorkList.forEach((procedure) => {let procedureId = `procedure-${nextUniqueId++}`; // 生成唯一的IDlet procedureInfo = {text: procedure.taskName,start_date: new Date(2025, 0, 16, 0, 0),end_date: new Date(2025,0,16,Math.floor(procedure.workTime / 60),procedure.workTime % 60,0),id: procedureId, //procedure.lruWorkId, //这个 id要唯一duration: procedure.workTime,parent: sparePartId, // procedure.lruIdThree, //aircraft.lruList这个id和parent一致type: procedure.isCompleted,time: procedure.workTime,};result.push(procedureInfo);});});});return result;},//取最大的修理时间findMaxRepairTime(data) {let maxRepairTime = 0;data.forEach((aircraft) => {if (aircraft.repairTime > maxRepairTime) {maxRepairTime = aircraft.repairTime;}});return maxRepairTime;},
    

  5. 子组件监听调用

    watch: {newData: {handler(newVal) {if (newVal !== undefined && newVal !== null)
http://www.lryc.cn/news/534689.html

相关文章:

  • collabora online+nextcloud+mariadb在线文档协助
  • “可通过HTTP获取远端WWW服务信息”漏洞修复
  • 【AI时代】-开发环境准备 之 Conda 创建 Python 环境 (含pip常用命令、jupyter 安装及汉化、自定义文档位置等配置)
  • [LeetCode] day19 454. 四数相加 II
  • LeetCodehot 力扣热题100 验证二叉搜索树
  • 四次挥手详解
  • Deepseek-v3 / Dify api接入飞书机器人go程序
  • 2025.2.9 每日学习记录2:技术报告写了一半+一点点读后感
  • qml ToolBar详解
  • 机器学习在癌症分子亚型分类中的应用
  • Ansible自动化部署K8s集群一 Ansible的基础使用实战
  • ZooKeeper Watcher 机制详解:从注册到回调的全过程
  • flutter_tools/gradle Unsupported class file major version 65 问题解决
  • C++设计模式 - 模板模式
  • mysql查缺补漏
  • 跨越边界,大模型如何助推科技与社会的完美结合?
  • 哪吒闹海!SCI算法+分解组合+四模型原创对比首发!SGMD-FATA-Transformer-LSTM多变量时序预测
  • 前端【技术方案】浏览器兼容问题(含解决方案、CSS Hacks、条件注释、特性检测、Polyfill 等)
  • 荣耀手机Magic3系列、Magic4系列、Magic5系列、Magic6系列、Magic7系列详情对比以及最新二手价格预测
  • 后盾人JS -- 模块化开发
  • CNN卷积神经网络多变量多步预测,光伏功率预测(Matlab完整源码和数据)
  • 深入 JVM 虚拟机:字符串常量池演变与 intern() 方法工作原理解析
  • 单向/双向,单层/多层RNN输入输出维度问题
  • chromium-mojo
  • ZooKeeper 的典型应用场景:从概念到实践
  • 缓存组件<keep-alive>
  • YouBIP 项目
  • react概览webpack基础
  • DeepSeek 助力 Vue 开发:打造丝滑的步骤条
  • STM32的HAL库开发---高级定时器---互补输出带死区实验