我使用 d3.layout.force
来可视化图形结构。
我想要节点拖动功能和平移功能。
(通过“平移”,我的意思是类似于谷歌地图的行为。)
如几个 d3 示例所示,可以通过 nodes.call(force.drag)
使节点可拖动。
平移可以通过d3.behavior.drag
实现。
不过,好像不能同时使用。
这是一个代码示例:
var width = 400, height = 300;
var svg = d3.select('body').append('svg').attr({width: width, height: height});
var nodes = [{}, {}, {}, {}];
var links = [
{source: 1, target: 0},
{source: 2, target: 0},
{source: 0, target: 3}
];
var nodeSel = svg.selectAll('circle')
.data(nodes).enter().append('circle')
.attr('r', 10).attr('fill', 'black');
var linkSel = svg.selectAll('line').data(links).enter().append('line')
.attr('stroke', 'black');
var force = d3.layout.force()
.size([width, height])
.nodes(nodes).links(links)
.linkDistance(80)
.charge(-300)
.on('tick', function() {
linkSel
.attr('x1', function(d) { return d.source.x; })
.attr('y1', function(d) { return d.source.y; })
.attr('x2', function(d) { return d.target.x; })
.attr('y2', function(d) { return d.target.y; });
nodeSel
.attr('cx', function(d) { return d.x; })
.attr('cy', function(d) { return d.y; });
});
nodeSel.call(force.drag);
/* This block of codes spoils force.drag */
/* ****************************************
var drag = d3.behavior.drag();
var viewBoxX = 0, viewBoxY = 0;
drag.on('dragstart', function() {
console.log('new dragstart is called');
}).on('drag', function() {
viewBoxX -= d3.event.dx;
viewBoxY -= d3.event.dy;
svg.attr('viewBox', [viewBoxX, viewBoxY, width, height].join(' '));
}).on('dragend', function() {
console.log('new dragend is called');
});
svg.call(drag);
**************************************** */
force.start();
(工作之一在 jsfiddle 上:http://jsfiddle.net/dersinces/hzL8T/1/。)
此代码仅启用节点拖动,而不启用平移。 激活底部的代码块(第 36 行)启用平移,但它使节点不可拖动。
如何在保持节点可拖动的同时实现图形平移?
最佳答案
这里是你如何做到的:Demo
想法是将具有可拖动行为的区域放在包含节点的区域下方,以便节点仍然可以接收鼠标事件。
// First append the area where the dragging will happen.
svg.append('rect')
.classed('bg', true)
.attr('stroke', 'transparent')
.attr('fill', 'transparent')
.attr('x', 0)
.attr('y', 0)
.attr('width', width)
.attr('height', height)
.call(drag);
// Then add the area which will contain the nodes.
var nodeArea = svg.append('g')
.classed('node-area', true);
关于javascript - 在 d3 力布局中保持节点可拖动的同时实现平移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20099299/