d3文本仅在添加到组时出现。为什么?

时间:2018-07-25 19:10:44

标签: d3.js

我在d3中正在处理的地图上附加了一些文本,这是我的代码:

    countriesGroup = svg
        .append("g")
        .attr("id", "map");

    //add background
    countriesGroup.append('rect')
        .attr("x", 0)
        .attr("y", 0)
        .attr("width", w)
        .attr("height", h);

    //Draw countries
    countries = countriesGroup.selectAll("path")
        .data(j.features)
        .enter()
        ...Draw country borders

    //Create Timer Text
    countriesGroup.append("text") <-----This worked
        .attr("transform", "translate(" + w/2 + "," + h/2 + ")")
        .style("fill", "#ff0000")
        .text("This is a test");

    //Create Timer Text
    svg.append("text") <-----This didn't
        .attr("transform", "translate(" + w/2 + "," + h/2 + ")")
        .style("fill", "#ff0000")
        .text("This is a test");

我最初尝试将Timer text直接附加到svg上,但这没有用。当我稍后尝试将其直接附加到countriesGroup组时,此方法可行。为什么只有在附加到组时才呈现文本,而直接附加到svg时却不能呈现?

0 个答案:

没有答案