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

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.

主站蜘蛛池模板: 清水河县| 肥乡县| 始兴县| 镇康县| 思茅市| 枞阳县| 搜索| 西贡区| 夹江县| 登封市| 高雄县| 海丰县| 广宗县| 宜兴市| 通许县| 昭通市| 齐齐哈尔市| 桐庐县| 武宁县| 玉田县| 乐陵市| 九龙县| 台北县| 双城市| 临沂市| 西峡县| 水城县| 岳普湖县| 长春市| 紫云| 垣曲县| 手游| 汽车| 怀仁县| 清河县| 肃北| 从江县| 蓝田县| 葵青区| 盐津县| 方城县|