Fisheye Distortion插件的奇怪行为

时间:2014-11-04 18:58:16

标签: javascript graphics d3.js fisheye

您好我想在 d3.js 中使用Fisheye Distortion plugin作为我的强制导向图,但是当我想要应用此插件时,图形的行为很奇怪。我是 d3.js 的新手,不擅长计算机图形学。

complete sample in jsfiddle

var fisheye = d3.fisheye.circular()
                        .radius(200)
                        .distortion(2);

    // graph - variable which represents whole graph                    
    graph.svg.on("mousemove", function() {
    fisheye.focus(d3.mouse(this));

    d3.select("svg").selectAll("circle").each(function(d) { d.fisheye = fisheye(d); })
                                .attr("cx", function(d) { return d.fisheye.x; })
                                .attr("cy", function(d) { return d.fisheye.y; })
                                .attr("r", function(d) { return d.fisheye.z * 4.5; });


    d3.select("svg").selectAll("line").attr("x1", function(d) { return d.source.fisheye.x; })
                                .attr("y1", function(d) { return d.source.fisheye.y; })
                                .attr("x2", function(d) { return d.target.fisheye.x; })
                                .attr("y2", function(d) { return d.target.fisheye.y; });   
                    });

奇怪的行为我的意思是图形的节点在鼠标悬停操作后消失(隐藏)。

enter image description here

1 个答案:

答案 0 :(得分:3)

问题在于您使用代码将cxcy添加到圈子中,但您的圈子实际上被nodeElements括在transform内了

因此,将鱼眼代码更改为以下内容可以解决问题:

graph.svg.on("mousemove", function() {
    fisheye.focus(d3.mouse(this));

    // Change transform on the .node
    d3.select("svg").selectAll(".node")
    .each(function(d) { d.fisheye = fisheye({ x: graph.x(d.x), y: graph.y(d.y) }); console.log(d.fisheye, d); })
    .attr("transform", function (d) { return "translate(" + d.fisheye.x + "," + d.fisheye.y + ")"; })
    // Now change the 'r'adius on the circles within
    // One can also scale the font of the text inside nodeElements here
    .select("circle")
    .attr("r", function(d) { return 15 * graph.nodeSizeFactor * d.fisheye.z; });


    d3.select("svg").selectAll("line")
    .attr("x1", function(d) { return d.source.fisheye.x; })
    .attr("y1", function(d) { return d.source.fisheye.y; })
    .attr("x2", function(d) { return d.target.fisheye.x; })
    .attr("y2", function(d) { return d.target.fisheye.y; });   
});

请注意,我还为graph.x属性应用了适当的比例graph.ytransform,为圆圈的半径应用了15 * graph.nodeSizeFactor(而非{{1} })。

工作演示:http://jsfiddle.net/90u4sjzm/23/