g6/demos/tree-graph.html

161 lines
3.2 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
</style>
<body>
<select id="layout">
2019-03-14 19:40:04 +08:00
<option value="dendrogram">系统树</option>
<option value="compactBox">紧凑树</option>
2019-03-14 19:40:04 +08:00
<option value="mindmap">脑图布局</option>
</select>
<div id="mountNode"></div>
<script src="assets/hierarchy.js"></script>
<script src="../build/g6.js"></script>
<script>
let currentLayout = 'dendrogram';
const layouts = {
2019-04-29 16:52:31 +08:00
dendrogram: {
type: 'dendrogram',
direction: 'LR', // H / V / LR / RL / TB / BT
nodeSep: 50,
rankSep: 100,
radial: true
2019-04-29 16:52:31 +08:00
},
compactBox: {
type: 'compactBox',
direction: 'LR',
getId(d) { return d.id; },
getHeight() { return 16 },
getWidth() { return 16 },
getVGap() { return 50 },
2019-04-29 16:52:31 +08:00
getHGap() { return 100 }
2019-03-14 19:40:04 +08:00
},
mindmap: {
type: 'mindmap',
direction: 'H',
getHeight() { return 16; },
getWidth() { return 16; },
getVGap() { return 50; },
}
};
const graph = new G6.TreeGraph({
container: 'mountNode',
width: 500,
height: 500,
pixelRatio: 2,
2019-04-26 15:11:20 +08:00
renderer: 'svg',
modes: {
2019-04-26 15:11:20 +08:00
default: ['collapse-expand', 'drag-canvas']
},
2019-05-08 17:19:37 +08:00
fitView: true,
layout: layouts.dendrogram
});
graph.node(node => {
return {
2019-04-26 15:11:20 +08:00
size: 16,
2019-05-08 17:19:37 +08:00
anchorPoints: [[0,0.5], [1,0.5]],
style: {
fill: '#40a9ff',
stroke: '#096dd9'
2019-05-08 17:19:37 +08:00
},
label: node.id,
labelCfg: {
position: node.children && node.children.length > 0 ? 'left' : 'right'
}
2019-05-08 17:19:37 +08:00
}
});
let i = 0;
graph.edge(() => {
i++;
return {
shape: 'cubic-horizontal',
color: '#A3B1BF',
label: i
}
});
const data = {
isRoot: true,
id: 'Root',
style: {
fill: 'red'
},
children: [
{
id: 'SubTreeNode1',
raw: {},
children: [
{
id: 'SubTreeNode1.1'
},
{
id: 'SubTreeNode1.2',
children: [
{
id: 'SubTreeNode1.2.1'
},
{
id: 'SubTreeNode1.2.2'
},
{
id: 'SubTreeNode1.2.3'
}
]
}
]
},
{
id: 'SubTreeNode2',
children: [
{
id: 'SubTreeNode2.1'
}
]
}, {
2019-03-14 19:40:04 +08:00
id: 'SubTreeNode3',
children: [
{
id: 'SubTreeNode3.1'
},
{
id: 'SubTreeNode3.2'
},
{
id: 'SubTreeNode3.3'
}
]
2019-03-14 19:40:04 +08:00
}, {
id: 'SubTreeNode4'
}, {
id: 'SubTreeNode5'
}, {
id: 'SubTreeNode6'
}, {
id: 'SubTreeNode7',
}, {
id: 'SubTreeNode8'
}, {
id: 'SubTreeNode9'
}, {
id: 'SubTreeNode10'
}, {
id: 'SubTreeNode11'
}
]
};
graph.data(data);
graph.render();
document.getElementById('layout').addEventListener('change', (e) => {
const layout = e.target.value;
if (currentLayout !== layout) {
currentLayout = layout;
2019-03-14 19:40:04 +08:00
graph.changeLayout(layouts[currentLayout]);
}
});
</script>
</body>
</html>