【资料链接】
内置 Combo | G6 (antgroup.com) 矩形
内置 Combo | G6 (antgroup.com) 圆形
Combo 复合布局 ComboCombined | G6 (antgroup.com)
【例子】
部分数据的console.log
注:以下代码中的node与edge数据是根据B站视频里提到的部分自行拼凑的,没有100%一样
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>04</title>
<!-- 引入 G6 -->
<script src="https://gw.alipayobjects.com/os/lib/antv/g6/4.6.11/dist/g6.min.js"></script>
</head>
<body>
<div id="container"></div>
<script>
const data = {
nodes: [
{
id: "node-0",
label: "node-0",
properties: {
city: "110000",
age: 40,
amount: 100,
wifi: "10001"
}
},
{
id: "node-1",
label: "node-1",
properties: {
city: "110000",
age: 22,
amount: 100,
wifi: "10001"
}
},
{
id: "node-2",
label: "node-2",
properties: {
city: "110000",
age: 20,
amount: 100,
wifi: "10001"
}
},
{
id: "node-3",
label: "node-3",
properties: {
city: "110000",
age: 30,
amount: 100,
wifi: "10001"
}
},
{
id: "node-4",
label: "node-4",
properties: {
city: "110000",
age: 48,
amount: 100,
wifi: "10001"
}
},
{
id: "node-5",
label: "node-5",
properties: {
city: "310000",
age: 32,
amount: 100,
wifi: "10001"
}
},
{
id: "node-6",
label: "node-6",
properties: {
city: "310000",
age: 88,
amount: 100,
wifi: "10001"
}
},
{
id: "node-7",
label: "node-7",
properties: {
city: "310000",
age: 46,
amount: 100,
wifi: "10001"
}
},
{
id: "node-8",
label: "node-8",
properties: {
city: "310000",
age: 78,
amount: 100,
wifi: "10001"
}
},
{
id: "node-9",
label: "node-9",
properties: {
city: "310000",
age: 32,
amount: 100,
wifi: "10001"
}
},
{
id: "node-10",
label: "node-10",
properties: {
city: "310000",
age: 14,
amount: 100,
wifi: "10001"
}
},
{
id: "node-11",
label: "node-11",
properties: {
city: "440300",
age: 23,
amount: 100,
wifi: "10001"
}
},
{
id: "node-12",
label: "node-12",
properties: {
city: "440300",
age: 65,
amount: 100,
wifi: "10001"
}
},
{
id: "node-13",
label: "node-13",
properties: {
city: "440300",
age: 74,
amount: 100,
wifi: "10001"
}
},
{
id: "node-14",
label: "node-14",
properties: {
city: "440300",
age: 60,
amount: 100,
wifi: "10001"
}
},
{
id: "node-15",
label: "node-15",
properties: {
city: "440300",
age: 75,
amount: 100,
wifi: "10001"
}
},
{
id: "node-16",
label: "node-16",
properties: {
city: "440300",
age: 55,
amount: 100,
wifi: "10001"
}
},
],
edges: [
{
id: "edge-0",
source: "node-0",
target: "node-1"
},
{
id: "edge-1",
source: "node-0",
target: "node-2"
},
{
id: "edge-2",
source: "node-0",
target: "node-3"
},
{
id: "edge-3",
source: "node-0",
target: "node-4"
},
{
id: "edge-4",
source: "node-1",
target: "node-2"
},
{
id: "edge-5",
source: "node-0",
target: "node-5"
},
{
id: "edge-6",
source: "node-5",
target: "node-6"
},
{
id: "edge-7",
source: "node-6",
target: "node-7"
},
{
id: "edge-8",
source: "node-7",
target: "node-8"
},
{
id: "edge-9",
source: "node-8",
target: "node-9"
},
{
id: "edge-10",
source: "node-9",
target: "node-10"
},
{
id: "edge-11",
source: "node-10",
target: "node-5"
},
{
id: "edge-12",
source: "node-6",
target: "node-9"
},
{
id: "edge-13",
source: "node-5",
target: "node-11"
},
{
id: "edge-14",
source: "node-0",
target: "node-11"
},
{
id: "edge-15",
source: "node-11",
target: "node-12"
},
{
id: "edge-16",
source: "node-12",
target: "node-13"
},
{
id: "edge-17",
source: "node-13",
target: "node-14"
},
{
id: "edge-18",
source: "node-14",
target: "node-15"
},
{
id: "edge-19",
source: "node-15",
target: "node-12"
},
{
id: "edge-20",
source: "node-15",
target: "node-13"
},
{
id: "edge-21",
source: "node-5",
target: "node-16"
},
],
combos: []
}
const cities = ['110000', '310000', '440300']
const childNodeCountMap = {}
// 为每个node节点设置comboId
data.nodes.forEach(node => {
if (node.properties.age < 25) {
node.comboId = `combo-${node.properties.city}-25`;
} else if (node.properties.age < 35) {
node.comboId = `combo-${node.properties.city}-35`;
} else {
node.comboId = `combo-${node.properties.city}`;
}
childNodeCountMap[node.comboId] = childNodeCountMap[node.comboId] || 0
childNodeCountMap[node.comboId]++;
})
// 增加data.combos数组内容
cities.forEach(city => {
data.combos.push({
id: `combo-${city}`,
label: `combo-${city}`
})
if (childNodeCountMap[`combo-${city}-25`]) {
data.combos.push({
id: `combo-${city}-25`,
label: `combo-${city}-25`,
parentId: `combo-${city}`
})
}
if (childNodeCountMap[`combo-${city}-35`]) {
data.combos.push({
id: `combo-${city}-35`,
label: `combo-${city}-35`,
parentId: `combo-${city}`
})
}
})
// 区分 combo 样式
const subjectColors = [
"#5F95FF",
"#7262FD",
"#F6BD16",
"#78D3F8",
"#F6903D",
"#008685",
"#F09BB4",
]
const colorSet = G6.Util.getColorSetsBySubjectColors(
subjectColors, // 主题色数组
'#fff', // 背景色
'default', // 主题
'#333' // 置灰时的颜色
)
// console.log('colorSet',colorSet);
// 关于颜色配置的关键部分
const comboColorMap = {}
data.combos.forEach((combo, i) => {
const color = comboColorMap[combo.parentId] || colorSet[i % colorSet.length];
comboColorMap[combo.id] = color
combo.style = {
stroke: color.mainStroke,
fill: color.mainFill,
opacity: 0.3
}
})
data.nodes.forEach(node => {
const color = comboColorMap[node.comboId]
node.style = {
stroke: color.mainStroke,
fill: color.mainFill
}
})
// console.log('comboColorMap',comboColorMap);
// 关于颜色配置的关键部分 END
const width = document.getElementById('container').scrollWidth
const height = document.getElementById('container').scrollHeight || 650
const graph = new G6.Graph({
container: 'container',
width,
height,
fitView: true,
layout: {
// type: 'force'
type: 'comboCombined',
comboPadding: 50,
center: [400, 300], // 设置画布的中心点
},
defaultNode: {
labelCfg: {
position: 'bottom'
}
},
modes: {
default: ['zoom-canvas', 'drag-canvas',
'collapse-expand-combo', // 展开收起combo
{
type: 'drag-node',
onlyChangeComboSize: true, // true node无法被拖出combo外,无法改变结构
}, {
type: 'drag-combo',
onlyChangeComboSize: true, // true combo无法被拖出combo外,无法改变结构
}
]
}
})
graph.data(data)
graph.render()
// 拓展:使用api控制展开收起combo
// graph.on('canvas:click', e => {
// graph.collapseExpandCombo('combo-440300')
// })
// 拓展:使用api控制解散combo,注:dblclick会触发click
graph.on('canvas:dblclick', e => {
// graph.uncombo('combo-310000')
graph.uncombo('combo-440300')
graph.layout() // 注:graph.uncombo('combo-310000') 重新layout的时候布局会往右边歪
})
</script>
</body>
</html>