Truncate text in d3

橙三吉。 提交于 2019-12-01 15:26:40

问题


I want to truncate text that is over a predefined limit in d3.

I'm not sure how to do it.

Here's what I have now:

  node.append("text")
  .attr("dx", 20)
  .attr("dy", ".20em")
  .text(function(d) { if(d.rating > 25) return d.name; }));

Text is only displayed if the rating > 25. How can truncate the text of those names?


回答1:


To truncate text use substring

Try this code:

DEMO

 .text(function (d) {
     if(d.name.length > 5)
         return d.name.substring(0,5)+'...';
     else
         return d.name;                       
 });


来源:https://stackoverflow.com/questions/21770344/truncate-text-in-d3

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