我正在使用D3.js. 我想找到一个等同于这个CSS类的SVG,如果文本流出其包含的div,它会添加省略号:
.ai-ellipsis {
display: block;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-o-text-overflow: ellipsis;
-moz-binding: url(<q>assets/xml/ellipsis.xml#ellipsis</q>);
}
Run Code Online (Sandbox Code Playgroud)
这是我的SVG:
<g class="bar" transform="translate(0,39)">
<text class="label" x="-3" y="6.5" dy=".35em" text-anchor="start">Construction</text>
<rect height="13" width="123"></rect>
</g>
Run Code Online (Sandbox Code Playgroud)
它的生成如下:
barEnter.append("text").attr("class", "label")
.attr("x", -3).attr("y", function() { return y.rangeBand() / 2})
.attr("dy", ".35em").attr("text-anchor", "start")
.text(function(d) {
return d.Name;
});
Run Code Online (Sandbox Code Playgroud)
目前,文本溢出并与rect元素重叠.
有什么方法可以说"如果文字超过一定宽度,裁剪它并添加省略号"?
use*_*569 60
溢出文本的包装函数:
function wrap() {
var self = d3.select(this),
textLength = self.node().getComputedTextLength(),
text = self.text();
while (textLength > (width - 2 * padding) && text.length > 0) {
text = text.slice(0, -1);
self.text(text + '...');
textLength = self.node().getComputedTextLength();
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
text.append('tspan').text(function(d) { return d.name; }).each(wrap);
Run Code Online (Sandbox Code Playgroud)
此函数不依赖于 d3:
function textEllipsis(el, text, width) {
if (typeof el.getSubStringLength !== "undefined") {
el.textContent = text;
var len = text.length;
while (el.getSubStringLength(0, len--) > width) {
el.textContent = text.slice(0, len) + "...";
}
} else if (typeof el.getComputedTextLength !== "undefined") {
while (el.getComputedTextLength() > width) {
text = text.slice(0,-1);
el.textContent = text + "...";
}
} else {
// the last fallback
while (el.getBBox().width > width) {
text = text.slice(0,-1);
// we need to update the textContent to update the boundary width
el.textContent = text + "...";
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26636 次 |
最近记录: |