Vue 2手写甘特图

网上找不到符合项目需求的,那就直接手写一个吧,可左右拉伸改变灰色块的宽度,爱拖到哪拖到哪

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
实现甘特图需要以下步骤: 1. 定义数据结构,包括任务名称、开始时间、结束时间等信息。 2. 根据数据结构,计算每个任务在甘特图中的位置和长度。 3. 使用 Vue 组件渲染甘特图,包括任务条、时间轴等。 4. 实现交互功能,如拖拽任务条、缩放时间轴等。 以下是一个简单的 Vue 甘特图组件实现示例: ```html <template> <div class="gantt-chart"> <div class="gantt-timeline"> <div v-for="date in dates" :key="date" class="gantt-date">{{ date }}</div> </div> <div v-for="(task, index) in tasks" :key="index" class="gantt-task" :style="taskStyle(task)"> {{ task.name }} </div> </div> </template> <script> export default { props: { tasks: { type: Array, required: true, }, startDate: { type: Date, required: true, }, endDate: { type: Date, required: true, }, width: { type: Number, default: 800, }, height: { type: Number, default: 400, }, }, computed: { dates() { const dates = []; let currentDate = new Date(this.startDate); while (currentDate <= this.endDate) { dates.push(currentDate.toLocaleDateString()); currentDate.setDate(currentDate.getDate() + 1); } return dates; }, }, methods: { taskStyle(task) { const start = new Date(task.start); const end = new Date(task.end); const totalDays = (this.endDate - this.startDate) / (24 * 60 * 60 * 1000); const left = ((start - this.startDate) / (24 * 60 * 60 * 1000)) / totalDays * this.width; const width = ((end - start) / (24 * 60 * 60 * 1000)) / totalDays * this.width; return { left: `${left}px`, width: `${width}px`, height: '30px', backgroundColor: '#007bff', color: '#fff', borderRadius: '4px', padding: '4px', position: 'absolute', }; }, }, }; </script> <style> .gantt-chart { position: relative; width: 100%; height: 100%; } .gantt-timeline { position: absolute; top: 0; left: 0; right: 0; height: 30px; border-bottom: 1px solid #ccc; } .gantt-date { position: absolute; top: 0; bottom: 0; transform: translateX(-50%); } .gantt-task { cursor: move; } </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值