缺少产线时间冲突爆红问题
This commit is contained in:
181
klp-ui/src/views/wms/productionLine/GanttChartEcharts.vue
Normal file
181
klp-ui/src/views/wms/productionLine/GanttChartEcharts.vue
Normal file
@@ -0,0 +1,181 @@
|
||||
<template>
|
||||
<div ref="ganttChart" class="echarts-gantt-wrapper" style="width:100%;height:320px;"></div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import * as echarts from 'echarts';
|
||||
const colorList = [
|
||||
'#409EFF', '#67C23A', '#E6A23C', '#F56C6C', '#909399',
|
||||
'#13C2C2', '#B37FEB', '#FF85C0', '#36CBCB', '#FFC53D'
|
||||
];
|
||||
function getColor(lineId, orderId, idx) {
|
||||
if (!lineId) return colorList[idx % colorList.length];
|
||||
const base = Math.abs(Number(lineId)) % colorList.length;
|
||||
if (!orderId) return colorList[base];
|
||||
return colorList[(base + Math.abs(Number(orderId)) % colorList.length) % colorList.length];
|
||||
}
|
||||
export default {
|
||||
name: 'GanttChartEcharts',
|
||||
props: {
|
||||
tasks: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
chart: null
|
||||
};
|
||||
},
|
||||
watch: {
|
||||
tasks: {
|
||||
handler() {
|
||||
this.renderChart();
|
||||
},
|
||||
deep: true
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.renderChart();
|
||||
window.addEventListener('resize', this.resizeChart);
|
||||
},
|
||||
beforeDestroy() {
|
||||
if (this.chart) this.chart.dispose();
|
||||
window.removeEventListener('resize', this.resizeChart);
|
||||
},
|
||||
methods: {
|
||||
renderChart() {
|
||||
if (!this.$refs.ganttChart) return;
|
||||
if (this.chart) this.chart.dispose();
|
||||
this.chart = echarts.init(this.$refs.ganttChart);
|
||||
if (!this.tasks || this.tasks.length === 0) {
|
||||
this.chart.clear();
|
||||
return;
|
||||
}
|
||||
// 处理数据,兼容多种字段名,保证任务名唯一
|
||||
const taskData = this.tasks.map((item, idx) => {
|
||||
const name = (item.remark || item.taskName || item.productName || item.name || `任务${idx+1}`) + (item.productName ? `-${item.productName}` : '');
|
||||
const start = item.startDate || item.start_time || item.start || item.start_date;
|
||||
const end = item.endDate || item.end_time || item.end || item.end_date;
|
||||
return {
|
||||
name,
|
||||
value: [start, end],
|
||||
itemStyle: {
|
||||
color: getColor(item.lineId, item.orderId, idx),
|
||||
borderRadius: 6
|
||||
},
|
||||
lineId: item.lineId,
|
||||
orderId: item.orderId,
|
||||
productId: item.productId,
|
||||
quantity: item.quantity,
|
||||
startDate: start,
|
||||
endDate: end
|
||||
};
|
||||
});
|
||||
// Y轴任务名
|
||||
const yData = taskData.map(d => d.name);
|
||||
// X轴时间范围
|
||||
const minDate = Math.min(...taskData.map(d => new Date(d.value[0]).getTime()));
|
||||
const maxDate = Math.max(...taskData.map(d => new Date(d.value[1]).getTime()));
|
||||
// 自动调整时间轴范围,避免跨度过大导致任务条重叠
|
||||
const oneMonth = 30 * 24 * 3600 * 1000;
|
||||
let xMin = minDate - oneMonth;
|
||||
let xMax = maxDate + oneMonth;
|
||||
// 如果跨度大于一年,仍然只扩展一个月
|
||||
// 如果跨度小于一个月,最小跨度为两个月
|
||||
if (xMax - xMin < 2 * oneMonth) {
|
||||
xMax = xMin + 2 * oneMonth;
|
||||
}
|
||||
console.log(taskData);
|
||||
|
||||
// 配置
|
||||
const option = {
|
||||
tooltip: {
|
||||
confine: true,
|
||||
formatter: params => {
|
||||
const d = Array.isArray(params.data) ? params.data[3] : params.data;
|
||||
return `任务:${d.name}` +
|
||||
`<br/>开始:${d.startDate}` +
|
||||
`<br/>结束:${d.endDate}` +
|
||||
`<br/>日产能:${d.capacity != null ? d.capacity : d.capacity || ''}` +
|
||||
`<br/>总产能:${d.totalCapacity != null ? d.totalCapacity : d.total_capacity || ''}` +
|
||||
`<br/>目标生产:${d.planQuantity != null ? d.planQuantity : d.plan_quantity || ''}` +
|
||||
`<br/>天数:${d.days != null ? d.days : d.day || ''}` +
|
||||
`<br/>数量:${d.quantity != null ? d.quantity : ''}`;
|
||||
}
|
||||
},
|
||||
grid: { left: 120, right: 40, top: 30, bottom: 80 },
|
||||
xAxis: {
|
||||
type: 'time',
|
||||
min: xMin,
|
||||
max: xMax,
|
||||
axisLabel: {
|
||||
formatter: v => echarts.format.formatTime('yyyy-MM-dd', v),
|
||||
rotate: 45 // 关键:倾斜45度
|
||||
}
|
||||
},
|
||||
yAxis: {
|
||||
type: 'category',
|
||||
data: yData,
|
||||
axisTick: { show: false },
|
||||
axisLine: { show: false },
|
||||
axisLabel: { fontWeight: 'bold' }
|
||||
},
|
||||
series: [{
|
||||
type: 'custom',
|
||||
renderItem: (params, api) => {
|
||||
const categoryIndex = api.value(2);
|
||||
const start = api.coord([api.value(0), categoryIndex]);
|
||||
const end = api.coord([api.value(1), categoryIndex]);
|
||||
const barHeight = 18;
|
||||
const d = Array.isArray(params.data) ? params.data[3] : params.data;
|
||||
// 调试:输出d
|
||||
// console.log('renderItem d:', d);
|
||||
let fillColor = api.style().fill;
|
||||
if (d && d.itemStyle && d.itemStyle.color) {
|
||||
fillColor = d.itemStyle.color;
|
||||
} else if (d && d.name && d.name.indexOf('预览') !== -1) {
|
||||
// 兜底:只要是预览条,强制绿色或红色
|
||||
fillColor = '#67C23A';
|
||||
}
|
||||
return {
|
||||
type: 'rect',
|
||||
shape: {
|
||||
x: start[0],
|
||||
y: start[1] - barHeight / 2,
|
||||
width: end[0] - start[0],
|
||||
height: barHeight,
|
||||
r: 6
|
||||
},
|
||||
style: {
|
||||
...api.style(),
|
||||
fill: fillColor
|
||||
}
|
||||
};
|
||||
},
|
||||
encode: {
|
||||
x: [0, 1],
|
||||
y: 2
|
||||
},
|
||||
data: taskData.map((d, i) => [d.value[0], d.value[1], i, d]),
|
||||
itemStyle: {
|
||||
borderRadius: 6
|
||||
}
|
||||
}]
|
||||
};
|
||||
this.chart.setOption(option);
|
||||
},
|
||||
resizeChart() {
|
||||
if (this.chart) this.chart.resize();
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.echarts-gantt-wrapper {
|
||||
width: 100%;
|
||||
min-height: 220px;
|
||||
background: #fff;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user