4

嗨,我想在d3.js中为我的力导向图使用Fisheye Distortion 插件,但是当我想应用这个插件时,图的行为很奇怪。我是d3.js的新手,不擅长计算机图形学。

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; });   
                    });

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

在此处输入图像描述

4

1 回答 1

3

问题是您正在使用代码来添加cx和添加cy圆圈,但您的圆圈实际上被封闭在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.ytransform15 * graph.nodeSizeFactor4.5

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

于 2014-11-13T16:26:32.367 回答