Reputation: 630
I have a D3 text added inside a rectangle , where the value of text field is updated programtically. Now I need to expand the width based on the text value length but making sure to keep the default width as min width. I have tried getting the new value.length and updating the d3 text width like below.
var length = //length of the updated text value
var elm = d3.selectAll("[id=s1]");
elm.attr('width', function(d) { return length; });
This works fine but when i delete the text D3 text box also shrinks all the way. I need to keep a default width and then just increase if the value is longer than that. Any idea how to do this?
Upvotes: 1
Views: 450
Reputation: 102194
You can use a ternary operator to check for a minimum value:
elm.attr('width', function() {
return length < minimumValue ? minimumValue : length;
});
Where minimumValue
is, of course, your minimum value.
Upvotes: 1