echarts实现甘特图(项目进度/任务进度)
<template>
<!-- Echarts 图表 -->
<div ref="progressChart" class="progressChart"></div>
</template>
<script>
mounted() {
this.initChart()
},
initChart() {
const progressChart = echarts.init(this.$refs.progressChart)
const option = {
grid: {
containLabel: true,
show: false,
right: 80,
left: 40,
bottom: 40,
top: 20,
backgroundColor: '#fff'
},
legend: {
data: ['持续时间'],
align: 'auto',
top: 'bottom'
},
xAxis: {
type: 'time',
position: 'top',
axisTick: {
show: false
},
axisLine: {
show: false
},
splitLine: {
show: true
},
interval: 3600 * 24 * 1000 * 30
},
yAxis: {
axisTick: {
show: false
},
axisLine: {
show: false
},
data: [
'项目一',
'项目二',
'项目三',
'项目四',
'项目五',
'项目六',
'项目七'
].reverse()
},
series: [
{
name: '持续时间',
type: 'bar',
stack: 'duration',
label: {
normal: {
show: true,
color: '#000',
position: 'right',
formatter: function (params) {
return params.name
}
}
},
itemStyle: {
normal: {
color: '#ed7d31',
borderColor: '#fff',
borderWidth: 1
}
},
zlevel: -1,
data: [
new Date('2021-01-31'),
new Date('2021-02-25'),
new Date('2021-03-25'),
new Date('2021-04-01'),
new Date('2021-04-10'),
new Date('2021-05-25'),
new Date('2021-07-25')
].reverse()
},
{
name: '持续时间',
type: 'bar',
stack: 'duration',
itemStyle: {
normal: {
color: '#fff'
}
},
zlevel: -1,
z: 9,
data: [
new Date('2021-01-01'),
new Date('2021-01-31'),
new Date('2021-02-25'),
new Date('2021-03-25'),
new Date('2021-04-01'),
new Date('2021-04-10'),
new Date('2021-05-25')
].reverse()
}
]
}
progressChart.setOption(option)
window.addEventListener('resize', () => {
progressChart.resize()
})
},
</script>
<style lang="scss" scoped>
.progressChart {
width: 80%;
height: 400px;
}
</style>
|