给我一个vue 甘特图的示例和解析
时间: 2024-02-24 11:57:51 浏览: 17
好的,下面为您提供一个 Vue 甘特图的示例和解析。
示例代码如下:
```
<template>
<div class="gantt-chart">
<div class="gantt-header">
<div class="gantt-task-name">任务名称</div>
<div class="gantt-task-date">开始时间</div>
<div class="gantt-task-date">结束时间</div>
</div>
<div class="gantt-body">
<div v-for="task in tasks" :key="task.id" class="gantt-task" :style="{ width: task.width + '%', left: task.left + '%' }">
<div class="gantt-task-name">{{ task.name }}</div>
<div class="gantt-task-date">{{ task.start }}</div>
<div class="gantt-task-date">{{ task.end }}</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
tasks: [
{ id: 1, name: '任务1', start: '2021-01-01', end: '2021-01-10', width: 10, left: 0 },
{ id: 2, name: '任务2', start: '2021-01-05', end: '2021-01-15', width: 10, left: 10 },
{ id: 3, name: '任务3', start: '2021-01-10', end: '2021-01-20', width: 10, left: 20 },
{ id: 4, name: '任务4', start: '2021-01-15', end: '2021-01-25', width: 10, left: 30 },
{ id: 5, name: '任务5', start: '2021-01-20', end: '2021-01-30', width: 10, left: 40 },
{ id: 6, name: '任务6', start: '2021-01-25', end: '2021-02-04', width: 10, left: 50 },
{ id: 7, name: '任务7', start: '2021-01-28', end: '2021-02-07', width: 10, left: 60 },
{ id: 8, name: '任务8', start: '2021-02-01', end: '2021-02-10', width: 10, left: 70 },
{ id: 9, name: '任务9', start: '2021-02-05', end: '2021-02-15', width: 10, left: 80 },
{ id: 10, name: '任务10', start: '2021-02-10', end: '2021-02-20', width: 10, left: 90 }
]
}
}
}
</script>
<style scoped>
.gantt-chart {
position: relative;
width: 100%;
height: 300px;
border: 1px solid #ccc;
}
.gantt-header {
height: 30px;
background: #f5f5f5;
display: flex;
align-items: center;
}
.gantt-task-name {
flex: 2;
text-align: center;
}
.gantt-task-date {
flex: 1;
text-align: center;
}
.gantt-body {
position: absolute;
top: 30px;
bottom: 0;
left: 0;
right: 0;
display: flex;
overflow: hidden;
}
.gantt-task {
position: absolute;
top: 0;
bottom: 0;
background: #2196f3;
color: #fff;
padding: 5px;
box-sizing: border-box;
border-radius: 5px;
display: flex;
flex-direction: column;
justify-content: center;
}
</style>
```
代码解析:
1. 在 data 中定义了一个 tasks 数组,数组中包含了 10 个任务的信息,包括任务名称、开始时间、结束时间、宽度和左偏移量。
2. 在模板中,使用 v-for 遍历 tasks 数组,为每个任务创建一个 div 元素,使用 CSS 样式设置它的宽度和左偏移量。
3. 使用 CSS 样式设置任务 div 元素的位置、背景颜色、边框等样式。
4. 使用 CSS 样式设置任务名称和时间的样式。
5. 将任务名称和时间渲染到任务 div 元素中。
6. 整个甘特图的样式由外层的 gantt-chart 和 gantt-body 控制,其中 gantt-body 中包含了所有的任务 div 元素。
7. 为了方便展示,使用了固定的高度和宽度,实际使用时可以根据需要动态计算宽度和位置。
这是一个简单的 Vue 甘特图示例,您可以根据需要进行修改和扩展。