D3强制布局中的一个节点上有两个标签

佐尔兹

是否可以添加两个同时显示不同文本的标签,一个在节点的中间,一个在节点的左侧?

我尝试添加第二个.text属性,但是没有显示我的第一个属性……我不知道该怎么做。

我的代码如下所示:

var data = {"nodes":[
                        {"name":"YHO", "full_name":"Yahoo", "type":1, "slug": "www.yahoo.com", "entity":"company", "img_hrefD":"", "img_hrefL":""},
                        {"name":"GGL", "full_name":"Google", "type":2, "slug": "www.google.com", "entity":"company", "img_hrefD":"", "img_hrefL":""},
                        {"name":"BNG", "full_name":"Bing", "type":2, "slug": "www.bing.com", "entity":"company", "img_hrefD":"", "img_hrefL":""},
                        {"name":"YDX", "full_name":"Yandex", "type":2, "slug": "www.yandex.com", "entity":"company", "img_hrefD":"", "img_hrefL":""},

                        {"name":"Desc1", "type":4, "slug": "", "entity":"description"},
                        {"name":"Desc2", "type":4, "slug": "", "entity":"description"},
                        {"name":"Desc4", "type":4, "slug": "", "entity":"description"},

                        {"name":"CEO", "prefix":"Mr.", "fst_name":"Jim", "snd_name":"Bean", "type":3, "slug": "", "entity":"employee"},
                        {"name":"ATT", "prefix":"Ms.", "fst_name":"Jenna", "snd_name":"Jameson", "type":3, "slug": "", "entity":"employee"},
                        {"name":"CTO", "prefix":"Mr.", "fst_name":"Lucky", "snd_name":"Luke", "type":3, "slug": "", "entity":"employee"},
                        {"name":"CDO", "prefix":"Ms.", "fst_name":"Pamela", "snd_name":"Anderson", "type":3, "slug": "", "entity":"employee"},
                        {"name":"CEO", "prefix":"Mr.", "fst_name":"Nacho", "snd_name":"Vidal", "type":3, "slug": "", "entity":"employee"},
                    ], 
            "links":[
                        {"source":0,"target":4,"value":1,"distance":5},
                        {"source":0,"target":5,"value":1,"distance":5},
                        {"source":0,"target":6,"value":1,"distance":5},

                        {"source":1,"target":4,"value":1,"distance":5},
                        {"source":2,"target":5,"value":1,"distance":5},
                        {"source":3,"target":6,"value":1,"distance":5},

                        {"source":7,"target":3,"value":10,"distance":6},
                        {"source":8,"target":3,"value":10,"distance":6},
                        {"source":9,"target":1,"value":10,"distance":6},
                        {"source":10,"target":1,"value":10,"distance":6},
                        {"source":11,"target":2,"value":10,"distance":6},
                        ]
               }    



    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", 22 + 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)
            .linkDistance(function(d) { return (d.distance*10); })
            //.friction(0.5)
            .charge(-250)
            .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){if(d.entity == "description"){ return 6 } else { return 18 }})
            //.on("mouseover", expandNode);
            //.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")
            .style("font-size","10px")
            .attr("text-anchor", "middle")
            .style("fill", "white")
            .text(function(d) { if (d.entity != "description"){return d.name} });


        node.on("mouseover", function (d) {
            if (d.entity == "company"){   
                d3.select(this).select('text')
                    .transition()
                    .duration(300)
                    .text(function(d){
                            return d.full_name;
                        })
                    .style("font-size","15px")

            }
            else if(d.entity == "employee"){
                d3.select(this).select('text')
                    .transition()
                    .duration(300)
                    .text(function(d){return d.prefix + ' ' + d.fst_name + ' ' + d.snd_name})
                    .style("font-size","8px")   

            }
            else {
                d3.select(this).select('text')
                    .transition()
                    .duration(300)
                    .style("font-size","15px")
            }

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

            if (d.entity == "company") {

                d3.select(this).select('circle')
                                .transition()
                                .duration(300)
                                .attr("r",28)

            }
            else if (d.entity == "employee"){
                d3.select(this).select('circle')
                                .transition()
                                .duration(300)
                                .attr("r",32)
            }
        })


         node.on("mouseout", function (d) {
            if (d.entity == "company") {
                d3.select(this).select('text')
                    .transition()
                    .duration(300)
                    .text(function(d){return d.name;})
                    .style("font-size","10px")
                }
            else if(d.entity == "employee"){
                d3.select(this).select('text')
                    .transition()
                    .duration(300)
                    .text(function(d){return d.name;})
                    .style("font-size","10px")  

            }
            else {
                d3.select(this).select('text')
                    .transition()
                    .duration(300)
                    .style("font-size","10px")
            }


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

            if (d.entity == "company" || d.entity == "employee") {

                d3.select(this).select('circle')
                                .transition()
                                .duration(300)
                                .attr("r",18)
            }

        });

        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:

@charset "utf-8";
/* CSS Document */

.link10 { stroke: #ccc; stroke-width: 2px; stroke-dasharray: 3, 3; }
.link1 { stroke: #000; stroke-width: 2px;}
.nodetext { pointer-events: none;}

.node.type1 {
  fill:#690011;
}
.node.type2 {
  fill:#BF0426;
}
.node.type3 {
  fill:#E5B96F;
}
.node.type4 {
  fill:#FFFFFF;
  stroke:#1695A3; 
  stroke-width: 3px;
}

.node.type5 {
    fill:#1BC9E0;
}

.node.type6 {
    fill:#E01B98;
}

image.circle {
    cursor:pointer;
}

.fadein{
display:none;
font-size:20px;
}

.rectD{
background-color:#000000;
width:70px;
height:30px
}

.rectL{
background-color:#000000;
width:90px;
height:30px
}

您可以在这里看到工作示例:http : //jsfiddle.net/dzorz/6pHkn/

您可以自由编辑解决方案

谢谢

拉尔斯·科特霍夫(Lars Kotthoff)

您可以通过.append("text")再次调用很容易地做到这一点,例如:

node.append("svg:text")
.attr("class", function(d){ return "nodetext title_"+d.name })
.attr("dx", "-4em")
.attr("dy", ".35em")
.style("font-size","10px")
.attr("text-anchor", "right")
.style("fill", "black")
.text(function(d) { return d.prefix; });

在这里更新了jsfiddle 如果需要区分两种类型的标签(例如,以后选择它们),最简单的方法是添加不同的类,以使您能够做到这一点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

D3强制布局:分别定位第一个和最后一个节点

来自分类Dev

一个NSManagedObjectContext上有两个NSArrayControllers?

来自分类Dev

从两个具有相同标签结构的xml文件的差异中创建一个节点

来自分类Dev

布局资源文件中的两个线性布局,但只有一个显示

来自分类Dev

D3强制布局仅显示一个链接

来自分类Dev

在一个布局中合并两个foreach

来自分类Dev

D3:在力布局中的两个不同g元素之间移动圆

来自分类Dev

如何在d3力布局中实现两个方向的平行边?

来自分类Dev

是否可以在一个布局中制作两个具有相同ID的按钮?

来自分类Dev

在D3强制布局节点标签中插入换行符

来自分类Dev

如何使D3的强制布局中的标签和节点可单击以导航到URL?

来自分类Dev

一页上有两个sidenav只能使用一个

来自分类Dev

一个内存地址上有两个整数变量?

来自分类Dev

angularjs在一个元素上有两个指令

来自分类Dev

在同一个表上有两个联接的竞争性WHERE

来自分类Dev

一个NIC(网卡)上有两个IP

来自分类Dev

一个提交按钮上有两个_POST

来自分类Dev

一个网络上有两个ISP-需要帮助

来自分类Dev

一个Apache安装上有两个站点

来自分类Dev

两个SATA端口上有一个磁盘,可提供完整的性能

来自分类Dev

一个文件夹在 Ubuntu 18.04 上有两个不同的位置

来自分类Dev

一页上有两个Javascript,另一个则阻止了另一个

来自分类Dev

一页上有两个表单时不生成表单标签

来自分类Dev

如何替换d3强制布局中的节点?

来自分类Dev

识别NetworkX DiGraph中源节点具有两个邻居之后的第一个节点

来自分类常见问题

一个<a>标签的两个目标,并在两个不同的div中显示两个结果

来自分类Dev

在Hobbelt的“组/捆绑节点” D3强制布局示例中向节点添加标签吗?

来自分类Dev

在Hobbelt的“组/捆绑节点” D3强制布局示例中向节点添加标签吗?

来自分类Dev

d3:可能只有两个“打开”?

Related 相关文章

  1. 1

    D3强制布局:分别定位第一个和最后一个节点

  2. 2

    一个NSManagedObjectContext上有两个NSArrayControllers?

  3. 3

    从两个具有相同标签结构的xml文件的差异中创建一个节点

  4. 4

    布局资源文件中的两个线性布局,但只有一个显示

  5. 5

    D3强制布局仅显示一个链接

  6. 6

    在一个布局中合并两个foreach

  7. 7

    D3:在力布局中的两个不同g元素之间移动圆

  8. 8

    如何在d3力布局中实现两个方向的平行边?

  9. 9

    是否可以在一个布局中制作两个具有相同ID的按钮?

  10. 10

    在D3强制布局节点标签中插入换行符

  11. 11

    如何使D3的强制布局中的标签和节点可单击以导航到URL?

  12. 12

    一页上有两个sidenav只能使用一个

  13. 13

    一个内存地址上有两个整数变量?

  14. 14

    angularjs在一个元素上有两个指令

  15. 15

    在同一个表上有两个联接的竞争性WHERE

  16. 16

    一个NIC(网卡)上有两个IP

  17. 17

    一个提交按钮上有两个_POST

  18. 18

    一个网络上有两个ISP-需要帮助

  19. 19

    一个Apache安装上有两个站点

  20. 20

    两个SATA端口上有一个磁盘,可提供完整的性能

  21. 21

    一个文件夹在 Ubuntu 18.04 上有两个不同的位置

  22. 22

    一页上有两个Javascript,另一个则阻止了另一个

  23. 23

    一页上有两个表单时不生成表单标签

  24. 24

    如何替换d3强制布局中的节点?

  25. 25

    识别NetworkX DiGraph中源节点具有两个邻居之后的第一个节点

  26. 26

    一个<a>标签的两个目标,并在两个不同的div中显示两个结果

  27. 27

    在Hobbelt的“组/捆绑节点” D3强制布局示例中向节点添加标签吗?

  28. 28

    在Hobbelt的“组/捆绑节点” D3强制布局示例中向节点添加标签吗?

  29. 29

    d3:可能只有两个“打开”?

热门标签

归档