vue使用甘特图插件dhtmlx-gantt( 简单版)

4 篇文章 0 订阅

一. 文档地址

官方文档

使用html生成的图表,样式修改会容易的多

二. 引用方式

yarn install dhtmlx-gantt
import { gantt } from 'dhtmlx-gantt'; // 引入dhtmlx-gantt
<style>
@import 'dhtmlx-gantt/codebase/dhtmlxgantt.css';
</style>

三. 部分组成的使用

1. tasks(任务)

在这里插入图片描述
在这里插入图片描述

let gantttt ={
            data:[ 
            { id: 3, text:"Team", type:'milestone', start_date:"14-07-2022"},
            { id: 1, text: '1222',start_date:'25-04-2022', end_date:'01-07-2022', open:true },
            { id: 12323544, text: '44444', start_date: '27-04-2022', end_date: '01-06-2022' , duration: 2, progress: 0, person: 'Julia Garner',parent:1 ,open:true},
            { id: 1232354422, text: '5555', start_date: new Date('2022-04-27'), end_date: new Date('2022-05-01') , duration: 2, progress: 0, color: '#2F80ED', person: 'Julia Garner',parent:12323544 },
            { id: 1232354421, text: '22222', start_date: new Date('2022-05-02'), end_date: new Date('2022-05-21') , duration: 2, progress: 0, color: '#2F80ED',parent:12323544 },
            { id: 12323545, text: '333333333333333', start_date: new Date('2022-05-15'), end_date: new Date('2022-06-30'),time:'02/01-02/20' , duration: 2, progress: 0,,parent:1 ,open:true},
            { id: 12345453, text: '222222', start_date: new Date('2022-04-27'), end_date: new Date('2022-05-18'),time:'02/01-02/20' , duration: 3, progress: 0, color: '#ED263D',parent:12323545},
            ]
}

可自定义增加变量显示
id:任务标识,可用来标识父子关系、连接links等
start_date,end_date:项目开始截至时间 Date|string //(‘14-07-2022’)
text:文本,任务的显示文字
progress:项目的进度,用颜色深浅显示
parent:父子关系(id标识);子任务的parent为父任务的id
type:任务类型,有三种,object,task,milestone;
object:没有时间限制,长度为包含所有子任务的长度
task:普通任务
milestone:菱形块,可表示中转关系

2. links(连接关系)

在这里插入图片描述
在这里插入图片描述

links:[
          { id:1, source:1, target:3, type:"0"},                       
          { id:2, source:12323545, target:12345453, type:"1"},                     
          { id:3, source:12345453, target:12345437, type:"0"}   
 ]

3. 数据结构

let gantttt ={
     data:[....],
     links:[.....]
    } 

4. columns(指定列)

在这里插入图片描述

const ganttColumns = ref([
            { align: 'right', name: 'color', label: '', width: '15',
                template:function(task){
                    if(task.color){
                        return  "<div class='tttttt' style='background:"+ task.color+ "'>"+"</div>"}  
                    }
            },
            { align: 'left', name: 'text', label: '', tree: true, width:"*",min_width:120},
            { align: 'center', name: 'person', label: '负责人', width: '100' },
            { align: 'right', name: 'time', label: '时间节点', width: '80' },
        ]);
gantt.config.columns = ganttColumns.value;

name: task列定义的变量
label:列头显示的文本
tree:是否显示连接树关系
template:可以通过方法对变量样式自定义
在这里插入图片描述

5. scales(时间轴)

在这里插入图片描述
在这里插入图片描述
单位包含:年,月,周,日等;需要特殊标识可以使用方法

var weekScaleTemplate = function (date) {
    var dateToStr = gantt.date.date_to_str("%d");
    var endDate = gantt.date.add(gantt.date.add(date, 1, "week"), -1, "day");
    const weekNum = gantt.date.date_to_str('W%W  ');
    return weekNum(date) + dateToStr(date) + " - " + dateToStr(endDate);
};
gantt.config.scales = [
   { unit: 'month', step: 1, format: '%F %Y' },
    { unit: 'week', step: 1, format:weekScaleTemplate } ,
];

在这里插入图片描述
可以动态切换时间轴的单位

在这里插入图片描述

function setScaleConfig(level) {
   switch (level) {
       case "day":
           gantt.config.scales = [
               {unit: "day", step: 1, format: "%d %M"}
           ];
           gantt.config.scale_height = 27;
           break;
       case "week":
           var weekScaleTemplate = function (date) {
             var dateToStr = gantt.date.date_to_str("%d %M");
             var endDate = gantt.date.add(gantt.date.add(date, 1, "week"), -1, "day");
             return dateToStr(date) + " - " + dateToStr(endDate);
           };
           gantt.config.scales = [
             {unit: "week", step: 1, format: weekScaleTemplate},
           ];
           gantt.config.scale_height = 27;
           break;
       case "month":
           gantt.config.scales = [
               {unit: "month", step: 1, format: "%F, %Y"},
           ];
           gantt.config.scale_height = 27;
           break;
       case "year":
           gantt.config.scales = [
               {unit: "year", step: 1, format: "%Y"},
           ];
           gantt.config.scale_height = 27;
           break;
   }
 }

6. markers(标记)

在这里插入图片描述
在这里插入图片描述
根据时间设立就可以

四. 使用设置

let gantttt ={...};
const ganttColumns = ref([...])
let weekScaleTemplate = function (date) {...}
 onMounted(() => {
            // 清空之前的配置
            gantt.clearAll();
            // 默认配置
            gantt.plugins({
                marker: true,
            });
            markerId = gantt.addMarker({
                start_date: new Date(2022, 4, 26),
                css: 'marker',
                text: '',
            });
            //任务的点击方法
            gantt.attachEvent("onTaskClick", function(id,e) {
                console.log(id)
                return true;
            });
            gantt.config.work_time = true;
            gantt.i18n.setLocale('cn'); // 设置中文
            gantt.config.readonly = true; // 设置为只读
            gantt.config.bar_height = 24; //task高度
            //自适应甘特图的尺寸大小, 使得在不出现滚动条的情况下, 显示全部任务
            gantt.config.autosize = true;
            gantt.config.open_split_tasks = true;
            // 显示列配置,限制最大最小时间
            // gantt.config.start_date = new Date(2022, 3, 25);
            // gantt.config.end_date = new Date(2022, 5, 26);
            gantt.config.columns = ganttColumns.value;
            gantt.config.scales = [
                { unit: 'month', step: 1, format: '%F %Y' },
                { unit: 'week', step: 1, format:weekScaleTemplate } ,
            ];
            gantt.getMarker(markerId);
            // 初始化甘特图
            gantt.init(ganttRef.value);
            gantt.parse(gantttt)       
  });

五.demo

根据ui需要修改样式
在这里插入图片描述

  • 6
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
Vue使用甘特图插件dhtmlx-gantt的步骤如下: 1. 首先,你需要使用yarn或npm安装dhtmlx-gantt插件。可以通过以下命令之一来安装插件: ```bash yarn install dhtmlx-gantt ``` 2. 在你的Vue组件中,引入dhtmlx-gantt插件。你可以使用以下代码实现引入: ```javascript import { gantt } from 'dhtmlx-gantt'; ``` 3. 在样式中引入dhtmlx-gantt的CSS文件。你可以在style标签中添加以下代码: ```html <style> @import 'dhtmlx-gantt/codebase/dhtmlxgantt.css'; </style> ``` 4. 对于部分组件的使用,你可以使用以下代码定义任务、列和链接: ```javascript const ganttColumns = ref([ { align: 'right', name: 'color', label: '', width: '15', template: function(task) { if (task.color) { return "<div class='tttttt' style='background:" + task.color + "'></div>"; } } }, { align: 'left', name: 'text', label: '', tree: true, width: "*", min_width: 120 }, { align: 'center', name: 'person', label: '负责人', width: '100' }, { align: 'right', name: 'time', label: '时间节点', width: '80' }, ]); gantt.config.columns = ganttColumns.value; const ganttLinks = [ { id: 1, source: 1, target: 3, type: "0" }, { id: 2, source: 12323545, target: 12345453, type: "1" }, { id: 3, source: 12345453, target: 12345437, type: "0" } ]; let ganttData = { data: [...], links: ganttLinks }; ``` 5. 最后,你可以使用dhtmlx-gantt插件的各种配置项和API来创建甘特图。可以根据需要使用不同的配置项来定义任务列、时间轴等。详情可以参考dhtmlx-gantt的官方文档。 通过以上步骤,你就可以在Vue使用甘特图插件dhtmlx-gantt了。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值