Add ellipses to overflowing text in SVG?

前端 未结 6 1060
广开言路
广开言路 2020-11-30 03:20

I\'m using D3.js. I\'d like to find an SVG equivalent to this CSS class, which adds ellipses if text flows out of its containing div:



        
6条回答
  •  野性不改
    2020-11-30 03:45

    a wrapper function for overflowing text:

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

    usage:

    text.append('tspan').text(function(d) { return d.name; }).each(wrap);
    

提交回复
热议问题