javascript - Changing size of rect to fit inside text - Stack Overflow

I have some text I append to an svg object with D3.js using append('text').My code looks like

I have some text I append to an svg object with D3.js using append('text').

My code looks like this:

var countries = svg.append("svg:g")
        .attr("id", "countries");

var stateTexts = svg.append('rect')
            .attr('x', xstateText)
            .attr('y', ystateText)
            .attr('width', 'auto')
            .attr('height', 'auto')

var stateText = svg.append('text')  
            .attr('x', xstateText)
            .attr('y', ystateText)  
            .style("font-family", "Arial")
            .style("font-size", "14px")
            .style("font-weight", 'bold');

What I'd like is to put that text "inside" a rect which changes size based on the length of the text I append. The rect would have a stroke of 1px so as to give the appearance of a box.

How can I acplish this? Obviously, width and height can't be set to auto (css properties). I need something else there that can work native to D3.

Edit: Confused by the downvote..

I have some text I append to an svg object with D3.js using append('text').

My code looks like this:

var countries = svg.append("svg:g")
        .attr("id", "countries");

var stateTexts = svg.append('rect')
            .attr('x', xstateText)
            .attr('y', ystateText)
            .attr('width', 'auto')
            .attr('height', 'auto')

var stateText = svg.append('text')  
            .attr('x', xstateText)
            .attr('y', ystateText)  
            .style("font-family", "Arial")
            .style("font-size", "14px")
            .style("font-weight", 'bold');

What I'd like is to put that text "inside" a rect which changes size based on the length of the text I append. The rect would have a stroke of 1px so as to give the appearance of a box.

How can I acplish this? Obviously, width and height can't be set to auto (css properties). I need something else there that can work native to D3.

Edit: Confused by the downvote..

Share Improve this question edited Aug 10, 2014 at 0:45 Union find asked Aug 10, 2014 at 0:40 Union findUnion find 8,18017 gold badges68 silver badges117 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You can't do this automatically in SVG -- the dimensions of the text have to be puted and the rectangle added accordingly. Fortunately, this is not too difficult. The basic idea is illustrated in this function:

function mkBox(g, text) {
  var dim = text.node().getBBox();
  g.insert("rect", "text")
    .attr("x", dim.x)
    .attr("y", dim.y)
    .attr("width", dim.width)
    .attr("height", dim.height);
}

Given a container and a text element, pute the dimensions of the text element (the text must be set for this to work correctly) and add a rect to the container with those dimensions. If you want to get a bit fancier, you could add another argument that allows you to specify padding so that the text and the border are not immediately next to each other.

Complete demo here.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744036426a4547505.html

相关推荐

  • javascript - Changing size of rect to fit inside text - Stack Overflow

    I have some text I append to an svg object with D3.js using append('text').My code looks like

    9天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信