d3.js - 截断指定节点的节点文本 d.type

d3.js - Truncate node text for specified node d.type

我想根据节点 d.type(或 d.data.type 取决于 d3.js 版本)为指定节点截断节点 text/label。

所以对于一般截断我使用了:

if (d.name.length > 60)
    return d.name.substring(0, 30) + '...';
else
    return d.name;

但是如何针对特定的 d.type(或 d.data.type)节点执行此操作?

例如,if d.data.type == 'unit1' 然后截断标签

fiddle

为什么不延长您的病情?

if (d.name.length > 60 && d.data.type == 'unit1')
    return d.name.substring(0, 30) + '...';
else
    return d.name;

但是您的数据没有每个节点的 type,只有叶节点。也许这是你的问题。