vue中使用ecarts的y轴为name,x轴为时间的甘特图的实际需求示例代码
时间: 2024-02-20 13:59:40 浏览: 23
以下是一个使用Echarts绘制甘特图的示例代码,其中y轴为name,x轴为时间:
```html
<template>
<div id="chart"></div>
</template>
<script>
import echarts from 'echarts'
export default {
mounted() {
this.drawChart()
},
methods: {
drawChart() {
const chart = echarts.init(document.getElementById('chart'))
const option = {
tooltip: {
formatter: '{b}: {c}'
},
grid: {
top: 20,
bottom: 20,
left: 50,
right: 50
},
xAxis: {
type: 'time',
splitLine: {
show: false
}
},
yAxis: {
type: 'category',
data: ['任务1', '任务2', '任务3', '任务4']
},
series: [{
type: 'bar',
data: [{
name: '任务1',
value: [
new Date('2022-06-01'),
new Date('2022-06-10')
]
}, {
name: '任务2',
value: [
new Date('2022-06-05'),
new Date('2022-06-20')
]
}, {
name: '任务3',
value: [
new Date('2022-06-15'),
new Date('2022-06-25')
]
}, {
name: '任务4',
value: [
new Date('2022-06-10'),
new Date('2022-06-30')
]
}],
barWidth: 20
}]
}
chart.setOption(option)
}
}
}
</script>
<style>
#chart {
width: 100%;
height: 300px;
}
</style>
```
在这个示例中,我们使用了Echarts中的`xAxis`和`yAxis`配置项来设置x轴和y轴的类型和数据。其中,`yAxis`的`type`为`'category'`,表示该轴使用类目型数据,而`data`则是一个数组,包含了所有的name值。`xAxis`的`type`为`'time'`,表示该轴使用时间型数据。
在甘特图的数据中,我们使用了一个包含了四个对象的数组,每个对象都包含了一个name和一个value。其中,name表示该任务的名称,而value则是一个数组,包含了任务开始时间和结束时间。这里我们使用了`new Date()`函数来创建时间对象。
最后,在`series`中,我们使用了`type: 'bar'`来表示绘制柱状图,`barWidth`用来设置柱状图的宽度。