代码之家  ›  专栏  ›  技术社区  ›  lapots

svg文本元素中的不可见文本

  •  0
  • lapots  · 技术社区  · 6 年前

    function d3manipulation() {
        var svg = d3.select("body").append("svg")
            .attr("width", 500)
            .attr("height", 500);
        var nodes = [
            { "name": "Michael" },
            { "name": "John" }
        ];
        svg
            .selectAll("text")
            .data(nodes)
            .enter()
            .append("text")
            .text(function(d) { return d.name; });
    }
    

    但在页面上什么也没显示。我也没有任何控制台错误。

    然而 inspector 表明事实上 text 包含数据的元素。

    <svg width="500" height="500">
        <text>Michael</text>
        <text>John</text>
    </svg>
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   rioV8    6 年前

    x y

    var svg = d3.select("body").append("svg")
            .attr("width", 500)
            .attr("height", 500);
        var nodes = [
            { "name": "Michael" },
            { "name": "John" }
        ];
        svg
            .selectAll("text")
            .data(nodes)
            .enter()
            .append("text")
            .text(function(d) { return d.name; })
            .attr('x', 20)
            .attr('y', (d, i) => 30 + 20 * i);
    <script src="https://d3js.org/d3.v5.min.js"></script>