javascript - d3js svg 文本和形状在同一行

标签 javascript css d3.js svg

我正在使用 SVG 在 D3.js 中绘制文本和形状,并希望绘制与文本内联且尺寸与文本相似的形状。我能想到的唯一方法是在每个 tspan 周围绘制一个矩形,然后在与 tspan 矩形相对的位置绘制形状。结果是:

这是一个矩形[]这是一个圆()

上面的括号代表 SVG 形状。当前代码如下。

js:

function setupSVG(){
    d3.select("div#chartId")
        .append("div")
        .classed("svg-container", true)  
        .append("svg")
        .attr("preserveAspectRatio", "xMinYMin meet")
        .attr("viewBox", "0 0 200 200")
        .attr("id", "svg_area_id")
}

function renderSVGText(){
    var svgArea = d3.select("svg#svg_area_id");

    svgArea.append("rect")     
        .attr("x", 100)         
        .attr("y", 0)          
        .attr("height", 10)    
        .attr("width", 10)    
        .attr("id", "shape");

    var group = svgArea.append("g")
        .attr("width", "100%")
        .attr("height", "100%")
        .style("stroke", "red") //I only want to draw rect stroke
        .style("fill", "none");

    var text = group.append("text")
        .attr("y", "0")
        .attr("font-size",52)
        .attr("dy", "1em")
        .style('fill', 'black')

    var tspan1 = text.append('tspan')
    tspan1.text("This is a square");
    var tspan2 = text.append('tspan')
    tspan2.text("and this is a triangle");

    var boundingRect = group.append("rect")
    //see http://phrogz.net/SVG/tspan_bounding_box.xhtml
    var bbox = tspan1.getBoundingClientRect();
    var pt = svg.createSVGPoint();
    pt.x = bbox.left;
    pt.y = bbox.top;
    var pt2 = pt.matrixTransform(xform);
    rect.setAttribute('x',pt2.x);
    rect.setAttribute('y',pt2.y);

    pt.x = bbox.right;
    pt.y = bbox.bottom;
    pt = pt.matrixTransform(xform);
    boundingRect.attr('width', pt.x-pt2.x);
    boundingRect.attr('height',pt.y-pt2.y);

    /* this draws a rect around all text
    var textSize = text.node().getBBox();
    boundingRect.attr("width", textSize.width)
        .attr("height", textSize.height);
    */

html:

<div class="svg-container" id="chartId"></div>

CSS:

.svg-container {
    display: inline-block;
    width: 512px;
    height: 512px;
    padding-top: 50px;
    padding-bottom: 100%; /* aspect ratio */
    vertical-align: top;
    overflow: hidden;
    border: 1px solid black;
}

关于如何做到这一点有什么想法吗?有比我正在关注的轨道更简单的方法吗?

最佳答案

我尝试使用 tspan.node().getComputedTextLength() 获取 tspan 维度,但这返回了一个错误,我推测是因为它在调用时没有被渲染。我只是使用 text.node().getBBox() 来获取每个文本 block 的尺寸:

function renderSVGText(){
    var svgArea = d3.select("svg#svg_area_id");

    var group = svgArea.append("g")
        .attr("width", "100%")
        .attr("height", "100%")
        .style("stroke", "red")
        .style("fill", "none");

    var text = group.append("text")
        .attr("y", "0")
        .attr("font-size",52)
        .attr("dy", "1em")
        .attr("id", "text_id")
        .style('fill', 'black');

    var tspan1 = text.append('tspan')
        .attr("id", "tspan1_id")
    tspan1.text("This is a square");

    var boundingRect = svgArea.append("rect")
        .style("stroke", "pink")
        .style("fill", "none");

    var textSize = text.node().getBBox();
    boundingRect.attr("width", textSize.width)
        .attr("height", textSize.height);

    svgArea.append("rect")
        .attr("x", textSize.width+10)
        .attr("y", 0)
        .attr("height", textSize.height)
        .attr("width", textSize.height)
        .attr("id", "shape");

    var text2 = group.append("text")
        .attr("x", textSize.width+textSize.height+20)
        .attr("y", "0")
        .attr("font-size",52)
        .attr("dy", "1em")
        .attr("id", "text2_id")
        .style('fill', 'black');

    var tspan2 = text2.append('tspan')
    tspan2.text("and this is a triangle");
}

关于javascript - d3js svg 文本和形状在同一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354393/

相关文章:

javascript - 将边框颜色变成背景颜色

html - CSS: make overflow-x: hidden 真的隐藏了吗?

javascript - D3 树/聚类图 - 更改节点文本颜色

javascript - SVG 线超出圆弧起始位置 - d3.js

从 Div onclick running button.click 调用的 JavaScript

javascript - 如何填补 ASP.NET Webform 中服务器端和客户端之间的空白?

css - 如何在属性选择器中使用/模拟类似正则表达式的反向引用?

javascript - 在函数内部延迟jquery函数?

javascript - 从 Greasemonkey 访问变量到页面,反之亦然

javascript - 堆积条形图过渡