官术网_书友最值得收藏!

Removing elements which aren't needed anymore

The final step we need to take is to remove rectangles that aren't needed anymore. If in the first call to update we add five rectangles, and in the next call only three are needed, we're stuck with two leftover ones. D3 also has an elegant mechanism to deal with that:

rectangles.exit().remove();

The call to exit() will select the elements for which no data is available. We can then do anything we want with those rectangles. In this case, we just remove them by calling remove(), but we could also change their opacity to make them look transparent, or animate them to slowly disappear.

For instance, if we replace the previous line of code with this:

rectangles.exit().attr("class", "remove");

Then set the CSS for the remove class to this:

.remove { 
fill: red;
opacity: 0.2;
}

In that case, we'd see the following:

In the preceding screenshot, we've reused two existing rectangles, and instead of removing the five we don't need, we change their style to the remove class, which renders them semi-transparent red.

主站蜘蛛池模板: 尼玛县| 怀远县| 乐安县| 鱼台县| 嫩江县| 博客| 读书| 兴国县| 乐亭县| 古浪县| 桐乡市| 台中市| 鄂州市| 绥中县| 建水县| 鄂尔多斯市| 北京市| 白河县| 开化县| 大竹县| 尉犁县| 调兵山市| 内黄县| 麻江县| 鲁甸县| 合作市| 鄂托克前旗| 濉溪县| 汾西县| 镇赉县| 乐陵市| 资溪县| 文水县| 普安县| 海门市| 延庆县| 瑞昌市| 阳曲县| 河西区| 怀化市| 太原市|