Inserting a line break in D3 force layout node labels

℡╲_俬逩灬. 提交于 2019-12-04 06:29:56

Here is answer without using HTML inside SVG because for some reason it wont work with this 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/

The above answer is beautiful, but, just for the sake of variety,here is also just a text wrap function written that will automatically do this for you. The original representation is for labels specifically on a chart, but you can probably manipulate it to do what you want. http://bl.ocks.org/mbostock/7555321

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!