|
|
|
|
@ -212,105 +212,129 @@ export default { |
|
|
|
|
this.isSubOpen = false; |
|
|
|
|
this.isComponentOpen = false; |
|
|
|
|
}, |
|
|
|
|
getTextWidth(text, fontSize = 14) { |
|
|
|
|
getTextWidth(text, fontSize = 12) { |
|
|
|
|
// 中文字符宽度≈fontSize,英文字符≈fontSize/2,加额外内边距 |
|
|
|
|
const cnChar = text.replace(/[a-zA-Z0-9]/g, '').length; |
|
|
|
|
const enChar = text.length - cnChar; |
|
|
|
|
return cnChar * fontSize + enChar * (fontSize / 2) + 20; // +20 内边距 |
|
|
|
|
return cnChar * fontSize + enChar * (fontSize / 2) + 10; // +20 内边距 |
|
|
|
|
}, |
|
|
|
|
// 图表 |
|
|
|
|
createBarChart(value) { |
|
|
|
|
const mapBoxEchart = this.$echarts.init(this.$refs.lineChart); |
|
|
|
|
const option = { |
|
|
|
|
series: [ |
|
|
|
|
{ |
|
|
|
|
roam: true, |
|
|
|
|
type: 'tree', |
|
|
|
|
data: value || [], |
|
|
|
|
top: '20%', |
|
|
|
|
left: '7%', |
|
|
|
|
bottom: '20%', |
|
|
|
|
right: '20%', |
|
|
|
|
expandAndCollapse: false, |
|
|
|
|
animationDuration: 550, |
|
|
|
|
animationDurationUpdate: 750, |
|
|
|
|
orient: 'vertical', |
|
|
|
|
symbol: 'roundRect', |
|
|
|
|
itemStyle: { |
|
|
|
|
color: '#284c89', |
|
|
|
|
borderWidth: 2, |
|
|
|
|
borderColor: '#fff', |
|
|
|
|
borderRadius: 4, // 圆角半径,值越大圆角越明显 |
|
|
|
|
paddingLeft: 10, |
|
|
|
|
}, |
|
|
|
|
// 动态计算节点大小:宽度=最长文本宽度,高度=行数*行高+内边距 |
|
|
|
|
symbolSize: (val, params) => { |
|
|
|
|
// 容错:params为空/无name时给默认值 |
|
|
|
|
const nodeName = params?.name || '未知节点'; |
|
|
|
|
const childCount = params?.children?.length || 0; |
|
|
|
|
const childText = `${childCount}个子件`; |
|
|
|
|
const nodeQuota = nodeData?.quota !== undefined ? nodeData.quota : ''; |
|
|
|
|
// 销毁旧的 ECharts 实例 |
|
|
|
|
if (this.mapBoxEchart) { |
|
|
|
|
this.mapBoxEchart.dispose(); |
|
|
|
|
this.mapBoxEchart = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 计算文本宽度 |
|
|
|
|
const textWidth = Math.max(nodeName.length * 12, childText.length * 12) + 30; |
|
|
|
|
// 动态计算行数 |
|
|
|
|
let lineCount = 2; // 默认 2 行(partCode + name) |
|
|
|
|
if (nodeQuota !== '' && nodeQuota !== null) { |
|
|
|
|
lineCount = 3; // 有 quota 时 3 行 |
|
|
|
|
} |
|
|
|
|
// 计算高度:每行 20px + 上下内边距 20px |
|
|
|
|
const height = lineCount * 20 + 10; |
|
|
|
|
return [Math.max(textWidth, 100), height]; // 高度设为60px(适配两行文本) |
|
|
|
|
}, |
|
|
|
|
this.$nextTick(() => { |
|
|
|
|
if (!this.$refs.lineChart) return; |
|
|
|
|
|
|
|
|
|
const mapBoxEchart = this.$echarts.init(this.$refs.lineChart); |
|
|
|
|
this.mapBoxEchart = mapBoxEchart; |
|
|
|
|
|
|
|
|
|
const option = { |
|
|
|
|
tooltip: { |
|
|
|
|
trigger: 'item', |
|
|
|
|
triggerOn: 'mousemove', |
|
|
|
|
}, |
|
|
|
|
series: [ |
|
|
|
|
{ |
|
|
|
|
roam: true, |
|
|
|
|
type: 'tree', |
|
|
|
|
data: value || [], |
|
|
|
|
top: '10%', |
|
|
|
|
left: '5%', |
|
|
|
|
bottom: '10%', |
|
|
|
|
right: '10%', |
|
|
|
|
expandAndCollapse: false, // 允许展开收起 |
|
|
|
|
initialTreeDepth: 2, // 初始展开层级 |
|
|
|
|
animationDuration: 550, |
|
|
|
|
animationDurationUpdate: 750, |
|
|
|
|
orient: 'vertical', |
|
|
|
|
symbol: 'roundRect', |
|
|
|
|
itemStyle: { |
|
|
|
|
color: '#284c89', |
|
|
|
|
borderWidth: 2, |
|
|
|
|
borderColor: '#fff', |
|
|
|
|
borderRadius: 2, |
|
|
|
|
}, |
|
|
|
|
// 节点间距配置(关键) |
|
|
|
|
// nodePadding: [30, 100], // [上下间距,左右间距] |
|
|
|
|
// layerPadding: 80, // 层级间距 |
|
|
|
|
// 动态计算节点大小 |
|
|
|
|
symbolSize: (val, params) => { |
|
|
|
|
const nodeData = params?.data || params; |
|
|
|
|
const nodeName = nodeData?.name || '未知节点'; |
|
|
|
|
const nodePartCode = nodeData?.partCode || ''; |
|
|
|
|
const nodeQuota = nodeData?.quota !== undefined ? nodeData.quota : ''; |
|
|
|
|
|
|
|
|
|
// 计算文本宽度 |
|
|
|
|
const nameWidth = this.getTextWidth(nodeName); |
|
|
|
|
const codeWidth = this.getTextWidth(nodePartCode); |
|
|
|
|
const quotaWidth = nodeQuota ? this.getTextWidth(`${nodeQuota}个`) : 0; |
|
|
|
|
|
|
|
|
|
const maxWidth = Math.max(nameWidth, codeWidth, quotaWidth, 60); |
|
|
|
|
|
|
|
|
|
// 动态计算行数 |
|
|
|
|
let lineCount = 2; |
|
|
|
|
if (nodeQuota !== '' && nodeQuota !== null) { |
|
|
|
|
lineCount = 3; |
|
|
|
|
} |
|
|
|
|
const height = lineCount * 20 + 10; |
|
|
|
|
|
|
|
|
|
label: { |
|
|
|
|
position: 'inside', |
|
|
|
|
show: true, |
|
|
|
|
align: 'center', |
|
|
|
|
verticalAlign: 'middle', |
|
|
|
|
fontSize: 12, |
|
|
|
|
lineHeight: 20, |
|
|
|
|
color: '#fff', |
|
|
|
|
formatter: params => { |
|
|
|
|
const childText = params.data.quota >= 0 ? `${params.data.quota}个` : ''; |
|
|
|
|
// 去掉模板字符串缩进,用\n换行,避免多余空白 |
|
|
|
|
return `${params.data.partCode}\n${params.name}${ |
|
|
|
|
childText ? '\n' + childText : '' |
|
|
|
|
}`; |
|
|
|
|
return [Math.max(maxWidth, 60), height]; |
|
|
|
|
}, |
|
|
|
|
label: { |
|
|
|
|
position: 'inside', |
|
|
|
|
show: true, |
|
|
|
|
align: 'center', |
|
|
|
|
verticalAlign: 'middle', |
|
|
|
|
fontSize: 12, |
|
|
|
|
lineHeight: 18, |
|
|
|
|
color: '#fff', |
|
|
|
|
formatter: params => { |
|
|
|
|
const nodeData = params.data || params; |
|
|
|
|
const quotaText = nodeData.quota >= 0 ? `${nodeData.quota}个` : ''; |
|
|
|
|
let result = `${nodeData.partCode}\n${nodeData.name}`; |
|
|
|
|
if (quotaText) { |
|
|
|
|
result += '\n' + quotaText; |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
// 连线样式 |
|
|
|
|
lineStyle: { |
|
|
|
|
color: '#ccc', |
|
|
|
|
width: 1.5, |
|
|
|
|
curveness: 0.5, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
], |
|
|
|
|
}; |
|
|
|
|
// 使用制定的配置项和数据显示图表 |
|
|
|
|
mapBoxEchart.setOption(option, true); |
|
|
|
|
// // 窗口大小改变时,重新渲染图表 |
|
|
|
|
window.addEventListener( |
|
|
|
|
'resize', |
|
|
|
|
function () { |
|
|
|
|
mapBoxEchart.resize(); |
|
|
|
|
}, |
|
|
|
|
200 |
|
|
|
|
); |
|
|
|
|
// 4. 核心:监听点击事件,实现label跳转 |
|
|
|
|
mapBoxEchart.on('click', params => { |
|
|
|
|
// 过滤条件:仅点击节点(label/text)时触发,排除连接线等 |
|
|
|
|
if (params.componentType === 'series' && params.seriesType === 'tree' && params.data) { |
|
|
|
|
window.open(`${params.data.docLink}`, '_blank'); |
|
|
|
|
} |
|
|
|
|
], |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
mapBoxEchart.setOption(option, true); |
|
|
|
|
|
|
|
|
|
window.addEventListener('resize', () => { |
|
|
|
|
if (this.mapBoxEchart) { |
|
|
|
|
this.mapBoxEchart.resize(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
mapBoxEchart.on('dblclick', params => { |
|
|
|
|
if (params.componentType === 'series' && params.seriesType === 'tree' && params.data) { |
|
|
|
|
window.open(`${params.data.docLink}`, '_blank'); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
</script> |
|
|
|
|
<style lang="scss" scoped> |
|
|
|
|
.form-box { |
|
|
|
|
// height: 700px; |
|
|
|
|
height: calc(100vh - 60px - 40px - 10px - 90px - 31px); |
|
|
|
|
} |
|
|
|
|
.form-bom { |
|
|
|
|
height: calc(100vh - 60px - 40px - 10px - 90px - 31px); |
|
|
|
|
width: '100%'; |
|
|
|
|
min-width: 100%; |
|
|
|
|
background: #f0f2f5; |
|
|
|
|
overflow: visible; // 让 ECharts 自由扩展 |
|
|
|
|
} |
|
|
|
|
</style> |
|
|
|
|
|