Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript D3.js在鼠标上方更改文本,可能吗?_Javascript_D3.js_Label_Force Layout - Fatal编程技术网

Javascript D3.js在鼠标上方更改文本,可能吗?

Javascript D3.js在鼠标上方更改文本,可能吗?,javascript,d3.js,label,force-layout,Javascript,D3.js,Label,Force Layout,我有一个力定向图,我想改变鼠标上节点的文本。我试着将“full_name”:的数据放在数据数组中,然后在鼠标上调用它,就像调用蓝色矩形图像一样。但它似乎不工作,我没有得到任何错误。所以我不知道是什么问题 您可以在此处查看和编辑示例: 脚本如下所示: var data = {"nodes":[ {"name":"Action 1", "type":2, "slug": "", "entity":"employee" },

我有一个力定向图,我想改变鼠标上节点的文本。我试着将
“full_name”:
的数据放在数据数组中,然后在鼠标上调用它,就像调用蓝色矩形图像一样。但它似乎不工作,我没有得到任何错误。所以我不知道是什么问题

您可以在此处查看和编辑示例:

脚本如下所示:

    var data = {"nodes":[
                        {"name":"Action 1", "type":2, "slug": "", "entity":"employee" },
                        {"name":"Action 2", "type":3, "slug": "", "entity":"employee" },
                        {"name":"Action 4", "type":5, "slug": "", "value":265000, "entity":"employee"},
                        {"name":"Action 5", "type":6, "slug": "", "value":23000, "entity":"employee"},
                        {"name":"Action 3", "type":4, "slug": "", "value":115000, "entity":"employee"},
                        {"name":"YHO", "full_name":"Yahoo", "type":1, "slug": "www.yahoo.com", "entity":"company", "img_hrefD":"http://img4host.net/upload/30143226522090da3be7a.jpg", "img_hrefL":"http://img4host.net/upload/30145118522095467b7e3.jpg"},
                        {"name":"GGL", "full_name":"Google", "type":1, "slug": "www.google.com", "entity":"company", "img_hrefD":"http://img4host.net/upload/30143226522090da3be7a.jpg", "img_hrefL":"http://img4host.net/upload/30145118522095467b7e3.jpg" },
                        {"name":"BNG", "full_name":"Bing", "type":1, "slug": "www.bing.com", "entity":"company", "img_hrefD":"http://img4host.net/upload/30143226522090da3be7a.jpg", "img_hrefL":"http://img4host.net/upload/30145118522095467b7e3.jpg" },
                        {"name":"YDX", "full_name":"Yandex", "type":1, "slug": "", "entity":"company", "img_hrefD":"http://img4host.net/upload/30143226522090da3be7a.jpg", "img_hrefL":"http://img4host.net/upload/30145118522095467b7e3.jpg" }
                    ], 
            "links":[
                        {"source":0,"target":3,"value":10},
                        {"source":4,"target":3,"value":1},
                        {"source":1,"target":7,"value":10},
                        {"source":2,"target":4,"value":10},
                        {"source":4,"target":7,"value":1},
                        {"source":4,"target":5,"value":10},
                        {"source":4,"target":6,"value":10},
                        {"source":8,"target":4,"value":1}
                        ]
               }    



    var w = 560,
        h = 500,
        radius = d3.scale.log().domain([0, 312000]).range(["10", "50"]);

    var vis = d3.select("body").append("svg:svg")
        .attr("width", w)
        .attr("height", h);

        vis.append("defs").append("marker")
        .attr("id", "arrowhead")
        .attr("refX", 25 + 3) /*must be smarter way to calculate shift*/
        .attr("refY", 2)
        .attr("markerWidth", 6)
        .attr("markerHeight", 4)
        .attr("orient", "auto")
        .append("path")
            .attr("d", "M 0,0 V 4 L6,2 Z"); //this is actual shape for arrowhead

    //d3.json(data, function(json) {
        var force = self.force = d3.layout.force()
            .nodes(data.nodes)
            .links(data.links)
            .distance(150)
            .charge(-1000)
            .size([w, h])
            .start();



        var link = vis.selectAll("line.link")
            .data(data.links)
            .enter().append("svg:line")
            .attr("class", function (d) { return "link" + d.value +""; })
            .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; })
            .attr("marker-end", function(d) {
                                                if (d.value == 1) {return "url(#arrowhead)"}
                                                else    { return " " }
                                            ;});


        function openLink() {
            return function(d) {
                var url = "";
                if(d.slug != "") {
                    url = d.slug
                } //else if(d.type == 2) {
                    //url = "clients/" + d.slug
                //} else if(d.type == 3) {
                    //url = "agencies/" + d.slug
                //}
                window.open("//"+url)
            }
        }




        var node = vis.selectAll("g.node")
            .data(data.nodes)
          .enter().append("svg:g")
            .attr("class", "node")
            .call(force.drag);


        node.append("circle")
            .attr("class", function(d){ return "node type"+d.type})
            .attr("r", function(d) { return radius(d.value) || 10 })
            //.style("fill", function(d) { return fill(d.type); })


        node.append("svg:image")
            .attr("class", function(d){ return "circle img_"+d.name })
            .attr("xlink:href", function(d){ return d.img_hrefD})
            .attr("x", "-36px")
            .attr("y", "-36px")
            .attr("width", "70px")
            .attr("height", "70px")
            .on("click", openLink())
            .on("mouseover", function (d) { if(d.entity == "company")
                                                {
                    d3.select(this).attr("width", "90px")
                                    .attr("x", "-46px")
                                    .attr("y", "-36.5px")
                                   .attr("xlink:href", function(d){ return d.img_hrefL});

                                                }
                })
            .on("mouseout", function (d) { if(d.entity == "company")
                                            {
                    d3.select(this).attr("width", "70px")
                                    .attr("x", "-36px")
                                    .attr("y", "-36px")
                                   .attr("xlink:href", function(d){ return d.img_hrefD});
                                            }
                });    


        node.append("svg:text")
            .attr("class", function(d){ return "nodetext title_"+d.name })
            .attr("dx", 0)
            .attr("dy", ".35em")
            .attr("text-anchor", "middle")
            .style("fill", "white")
            .text(function(d) { return d.name })
            .on("mouseover", function (d) { if(d.entity == "company"){
                    d3.select(this).text(function(d) { return d.full_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; });

          node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
        });
    //});       
css:

加载所有内容后,是否可以更改文本


请帮助

每个节点都有一个与之关联的图像和文本元素。图像和文本元素的鼠标悬停事件相互干扰,因为它们占用相同的空间

我分叉了JSFIDLE,为节点创建了一个鼠标悬停侦听器,并将两个事件放在同一个函数中:

        node.on("mouseover", function (d) {
            d3.select(this).select('text')
                .text(function(d){
                    return d.full_name;
                })
            if (d.entity == "company") {
                d3.select(this).select('image')
                    .attr("width", "90px")
                    .attr("x", "-46px")
                    .attr("y", "-36.5px")
                    .attr("xlink:href", function (d) {
                        return d.img_hrefL
                    });               
            }
        });
(节点上有一个类似的侦听器,mouseout可以反转这些更改)

        node.on("mouseover", function (d) {
            d3.select(this).select('text')
                .text(function(d){
                    return d.full_name;
                })
            if (d.entity == "company") {
                d3.select(this).select('image')
                    .attr("width", "90px")
                    .attr("x", "-46px")
                    .attr("y", "-36.5px")
                    .attr("xlink:href", function (d) {
                        return d.img_hrefL
                    });               
            }
        });