Why does my saved D3 selection have no effect in some cases?

依然范特西╮ 提交于 2019-12-01 12:43:59

This code:

gxaxis = svg.append("g").attr("class", "x axis")
                .attr("transform", "translate(0," + height + ")")
                .call(xAxis)
                .append("text")
                .attr("class", "label")
                .attr("x", width)
                .attr("y", -6)
                .style("text-anchor", "end");

Creates this DOM:

<svg>
    <...>
        <g class="x axis">
            <g class="tick">
                <line ...>
                <text ...>
            </g>
        </g>
    </...>
</svg>

And saves the text to the selection, because it was appended last.
If you want the axis, save the axis to the selection before you edit the text.

gxaxis = svg.append("g").attr("class", "x axis")
                .attr("transform", "translate(0," + height + ")")
                .call(xAxis);
gxaxis.selectAll("text")
                .attr("class", "label")
                .attr("x", width)
                .attr("y", -6)
                .style("text-anchor", "end");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!