d3.js - Inserting a line break in D3 force layout node labels -


so, playing force directed graph, , i've made .text on node changes on mouse on text data.

my code looks this:

script:

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 smarter way 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 actual shape 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 + ")"; });         });     //}); 

you can see working example in jsfiddle: http://jsfiddle.net/dzorz/6phkn/

the part of code bothers me mouseover:

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

i wanna add line break between d.fst_name , d.snd_name , tried '\n' , '<\br>' , not doing wanted get...

which way in d3 add line break on text?

you can edit linked jsfiddle above...

any suggestion welcome

here answer without using html inside svg because reason wont work force stuff.

else if(d.entity == "employee"){                 var asdf = d3.select(this);                 asdf.select('text').remove();                  asdf.append("text")                             .text(function(d){return d.prefix + ' ' + d.fst_name })                             .attr("class","nodetext")                             .attr("dx", 0)                             .attr("dy", ".35em")                             .style("font-size","5px")                             .attr("text-anchor", "middle")                             .style("fill", "white")                             .transition()                             .duration(300)                             .style("font-size","12px");                  asdf.append("text").text(function(d){return d.snd_name })                             .attr("class","nodetext")                             .attr("transform","translate(0, 12)")                             .attr("dx", 0)                             .attr("dy", ".35em")                             .style("font-size","5px")                             .attr("text-anchor", "middle")                             .style("fill", "white")                             .transition()                             .duration(300)                             .style("font-size","12px");                                                      } 

jsfiddle example: http://jsfiddle.net/cuckovic/fwkt5/


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -