简体   繁体   中英

Truncate text in d3

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM