下面我将详细讲解“Vue echarts画甘特图流程详细讲解”的完整攻略。
1. 甘特图简介
甘特图是一种常用于项目管理的图表类型,用于展示任务的时间轴,显示各个任务的开始时间、结束时间和持续时间。它能够清晰地展示每个任务的进展情况,帮助团队掌握项目进展,及时协调、调整工作计划和资源分配。
2. 准备工作
在使用 Vue 和 echarts 画甘特图之前,我们需要做一些准备工作:
- 安装 Vue 和 echarts:
npm install vue echarts --save
- 在 Vue 项目中引用 echarts:
import echarts from 'echarts'
Vue.use(echarts)
3. 画甘特图
3.1 图表配置项
在画甘特图之前,我们需要先配置图表的样式和数据。以下是一个示例的配置项:
option = {
tooltip: {
formatter: '{b}'
},
timeline: {
data: []
},
grid: {
left: 100,
right: 100,
top: 50,
bottom: 20,
containLabel: true
},
xAxis: {
type: 'value',
scale: true,
max: Date.now(),
splitLine: {
show: false
},
axisLabel: {
formatter: '{value}ms'
}
},
yAxis: {
type: 'category',
data: [],
axisLabel: {
interval: 0,
rotate: 30
},
splitLine: {
show: false
},
axisTick: {
width: 2
},
axisLine: {
onZero: false
}
},
series: []
};
3.2 数据处理
在配置好图表的样式和数据之后,我们需要对数据进行处理,使其符合 echarts 的数据格式。以下是一个示例数据:
data = [
{name:"task1",startTime:"2022-01-01",endTime:"2022-01-05"},
{name:"task2",startTime:"2022-01-06",endTime:"2022-01-10"},
{name:"task3",startTime:"2022-01-11",endTime:"2022-01-15"},
{name:"task4",startTime:"2022-01-16",endTime:"2022-01-20"},
];
这个数据的格式与 echarts 的数据格式有所差异,我们需要对它进行处理。以下是一个将上述数据处理成 echarts 的 series 数据的示例:
let gData=[];
data.forEach((d,i)=>{
gData.push({
value: [
d.startTime,
d.endTime + ' ' + d.name
],
itemStyle: {
normal: {
color: i % 2 === 0 ? '#FFEFD5' : '#FFF0F5'
}
}
})
})
option.series.push({
type: 'custom',
renderItem: (params, api) => {
const categoryIndex = api.value(3);
const endX = api.coord([api.value(2), categoryIndex])[0] + 1;
const rectShape = echarts.graphic.clipRectByRect({
x: api.coord([api.value(0), categoryIndex])[0],
y: api.coord([0, categoryIndex - 0.4])[1],
width: endX - api.coord([api.value(0), categoryIndex])[0],
height: api.size([0, 1])[1] * 0.8 //挑一个合适的高度
}, {
x: params.coordSys.x,
y: params.coordSys.y,
width: params.coordSys.width,
height: params.coordSys.height
});
return rectShape && {
type: 'rect',
transition: ['shape'],
shape: rectShape,
style: { fill: params.itemStyle.color }
};
},
data: gData
});
3.3 渲染图表
将以上配置项和数据处理代码添加到 Vue 的组件中,并通过 echarts 属性将其绑定到 echarts 的实例上,就可以渲染出甘特图了。以下是一个完整的示例代码:
<template>
<div ref="chart" style="width: 100%; height: 400px;"></div>
</template>
<script>
import echarts from 'echarts';
export default {
data() {
return {
option: {
tooltip: {
formatter: '{b}'
},
timeline: {
data: []
},
grid: {
left: 100,
right: 100,
top: 50,
bottom: 20,
containLabel: true
},
xAxis: {
type: 'value',
scale: true,
max: Date.now(),
splitLine: {
show: false
},
axisLabel: {
formatter: '{value}ms'
}
},
yAxis: {
type: 'category',
data: [],
axisLabel: {
interval: 0,
rotate: 30
},
splitLine: {
show: false
},
axisTick: {
width: 2
},
axisLine: {
onZero: false
}
},
series: []
}
}
},
mounted() {
const chart = echarts.init(this.$refs.chart);
const data = [
{name:"task1",startTime:"2022-01-01",endTime:"2022-01-05"},
{name:"task2",startTime:"2022-01-06",endTime:"2022-01-10"},
{name:"task3",startTime:"2022-01-11",endTime:"2022-01-15"},
{name:"task4",startTime:"2022-01-16",endTime:"2022-01-20"},
];
//数据处理
let gData=[];
data.forEach((d,i)=>{
gData.push({
value: [
d.startTime,
d.endTime + ' ' + d.name
],
itemStyle: {
normal: {
color: i % 2 === 0 ? '#FFEFD5' : '#FFF0F5'
}
}
})
})
//添加甘特图series
this.option.series.push({
type: 'custom',
renderItem: (params, api) => {
const categoryIndex = api.value(3);
const endX = api.coord([api.value(2), categoryIndex])[0] + 1;
const rectShape = echarts.graphic.clipRectByRect({
x: api.coord([api.value(0), categoryIndex])[0],
y: api.coord([0, categoryIndex - 0.4])[1],
width: endX - api.coord([api.value(0), categoryIndex])[0],
height: api.size([0, 1])[1] * 0.8 //挑一个合适的高度
}, {
x: params.coordSys.x,
y: params.coordSys.y,
width: params.coordSys.width,
height: params.coordSys.height
});
return rectShape && {
type: 'rect',
transition: ['shape'],
shape: rectShape,
style: { fill: params.itemStyle.color }
};
},
data: gData
});
//渲染图表
chart.setOption(this.option);
},
}
</script>
4. 示例展示
4.1 滑动条时间轴甘特图
以下是一个基于 echarts 时间轴的甘特图示例:
<template>
<div ref="chart" style="width: 100%; height: 400px;"></div>
</template>
<script>
import echarts from 'echarts';
export default {
data() {
return {
option: {
tooltip: {
formatter: '{b}'
},
timeline: {
data: [
'2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05',
'2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10',
'2022-01-11', '2022-01-12', '2022-01-13', '2022-01-14', '2022-01-15',
'2022-01-16', '2022-01-17', '2022-01-18', '2022-01-19', '2022-01-20'
],
label: {
formatter: (s) => {
return echarts.format.formatTime('dd', s);
}
},
currentIndex: 0,
autoPlay: true,
playInterval: 500
},
grid: {
left: 100,
right: 100,
top: 50,
bottom: 20,
containLabel: true
},
xAxis: {
type: 'value',
scale: true,
max: Date.now(),
splitLine: {
show: false
},
axisLabel: {
formatter: '{value}ms'
}
},
yAxis: {
type: 'category',
data: ['task1', 'task2', 'task3', 'task4'],
axisLabel: {
interval: 0,
rotate: 30
},
splitLine: {
show: false
},
axisTick: {
width: 2
},
axisLine: {
onZero: false
}
},
series: []
}
}
},
mounted() {
const chart = echarts.init(this.$refs.chart);
const data = [
{name:"task1",startTime:"2022-01-01",endTime:"2022-01-05"},
{name:"task2",startTime:"2022-01-06",endTime:"2022-01-10"},
{name:"task3",startTime:"2022-01-11",endTime:"2022-01-15"},
{name:"task4",startTime:"2022-01-16",endTime:"2022-01-20"},
];
let gData = [];
data.forEach((d, i) => {
gData.push({
value: [
d.startTime,
d.endTime + ' ' + d.name
]
})
});
this.option.series = [{
type: 'custom',
renderItem: (params, api) => {
const categoryIndex = api.value(1);
const endX = api.coord([api.value(1), categoryIndex])[0] + 1;
const rectShape = echarts.graphic.clipRectByRect({
x: api.coord([api.value(0), categoryIndex])[0],
y: api.coord([0, categoryIndex - 0.4])[1],
width: endX - api.coord([api.value(0), categoryIndex])[0],
height: api.size([0, 1])[1] * 0.8
}, {
x: params.coordSys.x,
y: params.coordSys.y,
width: params.coordSys.width,
height: params.coordSys.height
});
return rectShape && {
type: 'rect',
transition: ['shape'],
shape: rectShape,
style: { fill: '#4a94ff' }
};
},
data: gData
}];
chart.setOption(this.option);
//时间轴自动播放
setInterval(() => {
chart.dispatchAction({
type: 'timelineChange',
currentIndex: (this.option.timeline.currentIndex + 1) % this.option.timeline.data.length
});
}, this.option.timeline.playInterval);
}
}
</script>
4.2 线性时间轴甘特图
以下是一个基于 echarts 线性时间轴的甘特图示例:
<template>
<div ref="chart" style="width: 100%; height: 400px;"></div>
</template>
<script>
import echarts from 'echarts';
export default {
data() {
return {
option: {
tooltip: {
formatter: '{b}'
},
xAxis: {
type: 'time',
min: '2022-01-01',
max: '2022-01-20',
splitLine: {
show: false
}
},
yAxis: {
type: 'category',
data: ['task1', 'task2', 'task3', 'task4'],
axisLabel: {
interval: 0,
rotate: 30
},
splitLine: {
show: false
}
},
series: []
}
}
},
mounted() {
const chart = echarts.init(this.$refs.chart);
const data = [
{name:"task1",startTime:"2022-01-01",endTime:"2022-01-05"},
{name:"task2",startTime:"2022-01-06",endTime:"2022-01-10"},
{name:"task3",startTime:"2022-01-11",endTime:"2022-01-15"},
{name:"task4",startTime:"2022-01-16",endTime:"2022-01-20"},
];
let gData = [];
data.forEach((d, i) => {
gData.push({
value: [
d.startTime,
d.endTime + ' ' + d.name
]
})
});
this.option.series = [{
type: 'custom',
renderItem: (params, api) => {
const categoryIndex = api.value(1);
const endX = api.coord([api.value(1), categoryIndex])[0] + 1;
const rectShape = echarts.graphic.clipRectByRect({
x: api.coord([api.value(0), categoryIndex])[0],
y: api.coord([0, categoryIndex - 0.4])[1],
width: endX - api.coord([api.value(0), categoryIndex])[0],
height: api.size([0, 1])[1] * 0.8
}, {
x: params.coordSys.x,
y: params.coordSys.y,
width: params.coordSys.width,
height: params.coordSys.height
});
return rectShape && {
type: 'rect',
transition: ['shape'],
shape: rectShape,
style: { fill: '#4a94ff' }
};
},
data: gData
}];
chart.setOption(this.option);
}
}
</script>
以上是基于 Vue 和 echarts 画甘特图的详细讲解和示例。希望这个攻略对你有所帮助!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Vue echarts画甘特图流程详细讲解 - Python技术站