在d3中截断文本 [英] Truncate text in d3

查看:162
本文介绍了在d3中截断文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想截断d3中超过预定义限制的文本。

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

我不知道该怎么做。



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

仅当评分> 25时才显示文本。如何截断这些名称的文本? p>

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

推荐答案

截断文本使用 substring

尝试以下代码:

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

这篇关于在d3中截断文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆