我正在玩vis.js因为我喜欢它的网络可视化模块。我想知道,因为我无法在文档中找到它,如果可以选择多个节点。
干杯,
的Riccardo
答案 0 :(得分:9)
更新! 文档的链接是http://visjs.org/docs/network/interaction.html
将multiselect属性设置为true。
将此部分添加到您的网络选项对象。
interaction: { multiselect: true}
答案 1 :(得分:3)
搜索" selectable"文件中的财产 http://visjs.org/docs/network/
如果为true,则可以通过单击选择网络中的节点。长 按可以用来选择多个节点。
答案 2 :(得分:1)
我刚刚发现,在vis.js中你可以选择长按它们的多个节点。
答案 3 :(得分:0)
如果您正在寻找一个矩形来选择节点,只需在visjs-network github上查看此线程即可:https://github.com/almende/vis/issues/3594
完整的演示代码:
const nodes = new vis.DataSet([
{ id: 1, label: 'Node 1' },
{ id: 2, label: 'Node 2' },
{ id: 3, label: 'Node 3' },
{ id: 4, label: 'Node 4' },
{ id: 5, label: 'Node 5' }
]);
const edges = new vis.DataSet([
{ from: 1, to: 3 },
{ from: 1, to: 2 },
{ from: 2, to: 4 },
{ from: 2, to: 5 }
]);
const options = {
layout: { randomSeed: 2 },
interaction:{
hover: true,
multiselect: true
}
};
// Everything is in there
const makeMeMultiSelect = (container, network, nodes) => {
const NO_CLICK = 0;
const RIGHT_CLICK = 3;
// Disable default right-click dropdown menu
container[0].oncontextmenu = () => false;
// State
let drag = false, DOMRect = {};
// Selector
const canvasify = (DOMx, DOMy) => {
const { x, y } = network.DOMtoCanvas({ x: DOMx, y: DOMy });
return [x, y];
};
const correctRange = (start, end) =>
start < end ? [start, end] : [end, start];
const selectFromDOMRect = () => {
const [sX, sY] = canvasify(DOMRect.startX, DOMRect.startY);
const [eX, eY] = canvasify(DOMRect.endX, DOMRect.endY);
const [startX, endX] = correctRange(sX, eX);
const [startY, endY] = correctRange(sY, eY);
network.selectNodes(nodes.get().reduce(
(selected, { id }) => {
const { x, y } = network.getPositions(id)[id];
return (startX <= x && x <= endX && startY <= y && y <= endY) ?
selected.concat(id) : selected;
}, []
));
}
// Listeners
container.on("mousedown", function({ which, pageX, pageY }) {
// When mousedown, save the initial rectangle state
if(which === RIGHT_CLICK) {
Object.assign(DOMRect, {
startX: pageX - this.offsetLeft,
startY: pageY - this.offsetTop,
endX: pageX - this.offsetLeft,
endY: pageY - this.offsetTop
});
drag = true;
}
});
container.on("mousemove", function({ which, pageX, pageY }) {
// Make selection rectangle disappear when accidently mouseupped outside 'container'
if(which === NO_CLICK && drag) {
drag = false;
network.redraw();
}
// When mousemove, update the rectangle state
else if(drag) {
Object.assign(DOMRect, {
endX: pageX - this.offsetLeft,
endY: pageY - this.offsetTop
});
network.redraw();
}
});
container.on("mouseup", function({ which }) {
// When mouseup, select the nodes in the rectangle
if(which === RIGHT_CLICK) {
drag = false;
network.redraw();
selectFromDOMRect();
}
});
// Drawer
network.on('afterDrawing', ctx => {
if(drag) {
const [startX, startY] = canvasify(DOMRect.startX, DOMRect.startY);
const [endX, endY] = canvasify(DOMRect.endX, DOMRect.endY);
ctx.setLineDash([5]);
ctx.strokeStyle = 'rgba(78, 146, 237, 0.75)';
ctx.strokeRect(startX, startY, endX - startX, endY - startY);
ctx.setLineDash([]);
ctx.fillStyle = 'rgba(151, 194, 252, 0.45)';
ctx.fillRect(startX, startY, endX - startX, endY - startY);
}
});
}; // end makeMeMultiSelect
$(document).ready(() => {
const container = $("#network");
const network = new vis.Network(container[0], { nodes, edges }, options);
makeMeMultiSelect(container, network, nodes);
});