Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add ellipses to overflowing text in SVG?

Tags:

css

svg

d3.js

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:

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

This is my 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> 

It's generated as follows:

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

Currently the text is overflowing and overlapping the rect element.

Is there any way I can say "if text is more than a certain width, crop it and add ellipses"?

like image 964
Richard Avatar asked Apr 12 '13 15:04

Richard


People also ask

How do I add an ellipsis to a text-overflow?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.

What is overflow ellipsis?

The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user. It can be clipped, display an ellipsis (...), or display a custom string.

Why is text-overflow ellipsis not working?

text-overflow: ellipsis only works when the following is true: Parent element is not set to display: inline (span's default,) You must use display: block or display: inline-block. The element's width must be constrained in px (pixels) – it doesn't work with values specified using % (percent.)

How do I use text-overflow ellipsis in a div?

The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string. Syntax: text-overflow: clip|string|ellipsis|initial|inherit; Property Values: All the properties are described well with the example below.


2 Answers

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); 
like image 89
user2846569 Avatar answered Sep 27 '22 18:09

user2846569


I am not aware of an equivalent CSS class for SVG, but you can use foreignObject to embed HTML in SVG. This gives you access to this functionality and is more flexible in general (e.g. you can do automatic line breaking easily).

See here for a complete example.

like image 36
Lars Kotthoff Avatar answered Sep 27 '22 19:09

Lars Kotthoff