g6/packages/site/examples/tool/fisheye/demo/fisheyeStyle.js

102 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-08-31 10:59:52 +08:00
import { Graph as BaseGraph, Extensions, extend } from '@antv/g6';
2023-02-02 10:31:36 +08:00
2023-08-31 10:59:52 +08:00
const Graph = extend(BaseGraph, {
plugins: {
fisheye: Extensions.Fisheye,
},
});
let fisheye = new Extensions.Fisheye({
2023-02-02 10:31:36 +08:00
r: 200,
showLabel: true,
delegateStyle: {
fill: '#f00',
lineDash: [5, 5],
stroke: '#666',
},
});
2023-08-31 10:59:52 +08:00
const colors = ['#8FE9FF', '#87EAEF', '#FFC9E3', '#A7C2FF', '#FFA1E3', '#FFE269', '#BFCFEE', '#FFA0C5', '#D5FF86'];
2023-02-02 10:31:36 +08:00
const graphDiv = document.getElementById('container');
const buttonContainer = document.createElement('div');
buttonContainer.style.display = 'inline-block';
buttonContainer.style.height = '35px';
buttonContainer.style.width = '220px';
// clear the fisheye effect button
const clearButton = document.createElement('input');
clearButton.type = 'button';
clearButton.value = 'Clear';
clearButton.style.height = '25px';
clearButton.style.width = '100px';
buttonContainer.appendChild(clearButton);
// enable/disable the fisheye lens button
const switchButton = document.createElement('input');
switchButton.type = 'button';
switchButton.value = 'Disable';
switchButton.style.height = '25px';
switchButton.style.width = '100px';
switchButton.style.marginLeft = '10px';
buttonContainer.appendChild(switchButton);
graphDiv.parentNode.appendChild(buttonContainer);
const container = document.getElementById('container');
const width = container.scrollWidth;
const height = container.scrollHeight || 500;
2023-08-31 10:59:52 +08:00
const graph = new Graph({
2023-02-02 10:31:36 +08:00
container: 'container',
width,
height,
2023-08-31 18:16:13 +08:00
transforms: ['transform-v4-data'],
2023-02-02 10:31:36 +08:00
plugins: [fisheye],
2023-08-31 10:59:52 +08:00
node: (model) => {
return {
id: model.id,
data: {
...model.data,
keyShape: {
fill: colors[Math.floor(Math.random() * 9)],
r: Math.random() * 30 + 10,
},
labelShape: {
visibility: 'hidden',
text: model.id,
},
},
};
},
2023-02-02 10:31:36 +08:00
});
clearButton.addEventListener('click', (e) => {
fisheye.clear();
});
switchButton.addEventListener('click', (e) => {
if (switchButton.value === 'Disable') {
switchButton.value = 'Enable';
2023-08-31 10:59:52 +08:00
graph.removePlugins(fisheye);
2023-02-02 10:31:36 +08:00
} else {
switchButton.value = 'Disable';
2023-08-31 10:59:52 +08:00
fisheye = new Extensions.Fisheye({
2023-02-02 10:31:36 +08:00
r: 200,
showLabel: true,
});
2023-08-31 10:59:52 +08:00
graph.addPlugins(fisheye);
2023-02-02 10:31:36 +08:00
}
});
fetch('https://gw.alipayobjects.com/os/bmw-prod/afe8b2a6-f691-4070-aa73-46fc07fd1171.json')
.then((res) => res.json())
.then((data) => {
2023-08-31 10:59:52 +08:00
graph.read(data);
2023-02-02 10:31:36 +08:00
});
if (typeof window !== 'undefined')
window.onresize = () => {
2023-08-31 10:59:52 +08:00
if (!graph || graph.destroyed) return;
2023-02-02 10:31:36 +08:00
if (!container || !container.scrollWidth || !container.scrollHeight) return;
2023-08-31 10:59:52 +08:00
graph.setSize([container.scrollWidth, container.scrollHeight]);
2023-02-02 10:31:36 +08:00
};