<template>
<div ref="ganttChart" style="height: 400px;"></div>
</template>
<script>
import * as d3 from 'd3';
import { onMounted, ref } from 'vue';
export default {
name: 'GanttChart',
setup() {
const ganttChart = ref(null);
onMounted(() => {
// Sample data for the Gantt chart
const data = [
{ task: "Task 1", start: new Date(2023, 0, 1), end: new Date(2023, 0, 7) },
{ task: "Task 2", start: new Date(2023, 0, 5), end: new Date(2023, 0, 12) },
{ task: "Task 3", start: new Date(2023, 0, 8), end: new Date(2023, 0, 15) }
];
const margin = { top: 20, right: 30, bottom: 30, left: 40 };
const width = 800 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
const svg = d3.select(ganttChart.value)
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const x = d3.scaleTime()
.domain(d3.extent(data, d => d.start))
.range([0, width]);
const y = d3.scaleBand()
.domain(data.map(d => d.task))
.range([0, height])
.padding(0.1);
svg.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x));
svg.append("g")
.call(d3.axisLeft(y));
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", d => x(d.start))
.attr("y", d => y(d.task))
.attr("width", d => x(d.end) - x(d.start))
.attr("height", y.bandwidth())
.attr("fill", "steelblue");
});
return {
ganttChart
};
}
};
</script>
<style scoped>
.bar {
fill: steelblue;
}
</style>
模板部分 (<template>):
div 元素作为甘特图的容器,并通过 ref 绑定到 ganttChart 变量。脚本部分 (<script>):
ganttChart 引用,用于在挂载时获取 DOM 元素。onMounted 生命周期钩子中初始化甘特图:样式部分 (<style scoped>):
steelblue。这个示例展示了如何使用 Vue 3 和 D3.js 创建一个简单的甘特图。你可以根据需要调整数据和样式。
上一篇:vue 虚拟列表
下一篇:vue video player
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站