d3强制有向图删除文本光标 [英] d3 force directed graph remove text cursor
本文介绍了d3强制有向图删除文本光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将文本添加到d3强制定向图形布局的节点中时,将鼠标悬停在节点上时,鼠标指针将变为文本光标.有什么办法可以避免这种情况,并始终将其保留为常规指针?
When I add text to a node in a d3 force directed graph layout, the mouse pointer changes to a text cursor when I hover over the node. Is there a way to avoid this and always have it remain the regular pointer?
正常指针:
文本光标:
这是一个小提琴,其中包含用于生成这些图像的代码以及该代码:
Here's a fiddle with the code used to produce these images, along with the code:
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var drawGraph = function(graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var gnodes = svg.selectAll('g.gnode')
.data(graph.nodes)
.enter()
.append('g')
.classed('gnode', true)
.call(force.drag);
var node = gnodes.append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); });
node.append("title")
.text(function(d) { return d.name; });
var labels = gnodes.append("text")
.text(function(d) { return d.name; })
.attr('text-anchor', 'middle')
.attr('font-size', 8.0)
.attr('font-weight', 'bold')
.attr('y', 2.5)
.attr('fill', d3.rgb(50,50,50))
.attr('class', 'node-label')
.append("svg:title")
.text(function(d) { return d.name; });
force.on("tick", function() {
link.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; });
gnodes.attr("transform", function(d) {
return 'translate(' + [d.x, d.y] + ')';
});
});
};
推荐答案
将以下CSS添加到文本元素:
Add the following CSS to your text elements:
pointer-events: none;
这将防止文本元素发生任何鼠标事件,即,光标不会改变,但您也将无法选择文本.
This will prevent any mouse events for the text elements, i.e. the cursor won't change but you also won't be able to select the text.
完整演示此处.
这篇关于d3强制有向图删除文本光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文