g6/demos/interact-state.html

127 lines
3.1 KiB
HTML
Raw Normal View History

2019-03-18 14:25:18 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
2020-02-14 10:10:54 +08:00
<meta charset="UTF-8" />
<title>Set States</title>
<style>
.g6-tooltip {
border: 1px solid #e2e2e2;
border-radius: 4px;
font-size: 12px;
color: #545454;
background-color: rgba(255, 255, 255, 0.9);
padding: 10px 8px;
box-shadow: rgb(174, 174, 174) 0px 0px 10px;
2019-03-18 14:25:18 +08:00
}
</style>
</head>
2020-02-14 10:10:54 +08:00
<body>
<div id="mountNode"></div>
<script src="../build/g6.js"></script>
<script src="../build/minimap.js"></script>
<script src="./assets/d3-4.13.0.min.js"></script>
<script src="./assets/jquery-3.2.1.min.js"></script>
<script>
const minimap = new Minimap({
2020-02-14 10:10:54 +08:00
size: [200, 160],
});
const graph = new G6.Graph({
container: 'mountNode',
width: 1000,
height: 800,
autoPaint: false,
2020-02-14 10:10:54 +08:00
plugins: [minimap],
modes: {
2020-02-14 10:10:54 +08:00
default: [
'drag-canvas',
{
type: 'tooltip',
formatText: model => {
return model.name;
},
},
{
type: 'edge-tooltip',
formatText: (model, e) => {
const edge = e.item;
return (
'来源:' +
edge.getSource().getModel().name +
'<br/>去向:' +
edge.getTarget().getModel().name
);
},
},
{
type: 'activate-relations',
activeState: 'highlight',
inactiveState: 'dark',
},
],
},
defaultNode: {
size: [10, 10],
2019-10-10 20:01:23 +08:00
color: 'steelblue',
style: {
lineWidth: 2,
fill: '#fff',
2020-02-14 10:10:54 +08:00
opacity: 0.8,
},
2019-10-10 20:01:23 +08:00
},
defaultEdge: {
size: 1,
style: {
stroke: '#e2e2e2',
2020-02-14 10:10:54 +08:00
lineAppendWidth: 2,
},
2019-10-10 20:01:23 +08:00
},
nodeStateStyle: {
highlight: {
2020-02-14 10:10:54 +08:00
opacity: 1,
},
dark: {
2020-02-14 10:10:54 +08:00
opacity: 0.2,
},
},
2019-10-10 20:01:23 +08:00
edgeStateStyles: {
highlight: {
2020-02-14 10:10:54 +08:00
stroke: '#999',
},
},
});
$.getJSON('./assets/data/xiaomi.json', data => {
graph.data({
nodes: data.nodes,
edges: data.edges.map((edge, i) => {
edge.id = 'edge' + i;
2020-02-14 10:10:54 +08:00
return Object.assign({}, edge);
}),
});
2020-02-14 10:10:54 +08:00
const simulation = d3
.forceSimulation()
.force(
'link',
d3
.forceLink()
.id(function(d) {
return d.id;
})
.strength(0.5),
)
.force('charge', d3.forceManyBody())
.force('center', d3.forceCenter(500, 300));
simulation.nodes(data.nodes).on('tick', ticked);
simulation.force('link').links(data.edges);
2019-03-18 14:25:18 +08:00
graph.render();
function ticked() {
graph.refreshPositions();
graph.paint();
}
});
</script>
</body>
2020-02-14 10:10:54 +08:00
</html>