topical media & game development
mobile-data-circle-12.htm / htm
<script src="http://d3js.org/d3.v3.min.js"></script>
<link rel="stylesheet" type="text/css" href="mobile-data-circle-style-d3.css">
<script type="text/javascript">
data
var data = [32, 57, 112],
dataEnter = data.concat(293),
dataExit = data.slice(0, 2),
w = 360,
h = 180,
x = d3.scale.ordinal().domain([57, 32, 112]).rangePoints([0, w], 1),
y = d3.scale.ordinal().domain(data).rangePoints([0, h], 2);
</script>
<p>All that’s left to do, then, is to remove the exiting elements:</p>
<div class="chart" id="chart-12">
<pre class="code">circle.exit().remove();
</pre>
<button>run</button>
<!--
<svg width="360" height="180"><g class="data" transform="translate(180,45)"><circle class="little" r="12"></circle><text dy=".35em" text-anchor="middle">32</text></g><g class="data" transform="translate(60,90)"><circle class="little" r="12"></circle><text dy=".35em" text-anchor="middle">57</text></g><g class="data" transform="translate(300,135)"><circle class="little" r="12" style="fill: #f08080; stroke: #ff0000;"></circle><text dy=".35em" text-anchor="middle">112</text></g></svg>
-->
</div>
<script type="text/javascript">
script
(function() {
var svg = d3.select("#chart-12").append("svg")
.attr("width", w)
.attr("height", h);
select
var g = svg.selectAll(".data")
.data(data)
.enter().append("g")
.attr("class", "data")
.attr("transform", function(d) { return "translate(" + x(d) + "," + y(d) + ")"; });
append
g.append("circle")
.attr("class", "little")
.attr("r", 12);
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
filter
g = g.filter(function(d, i) { return i == 2; });
g.select("circle")
.style("fill", "lightcoral")
.style("stroke", "red");
button
d3.select("#chart-12 button").on("click", function() {
g.select("circle").attr("r", 12);
g.select("text").style("opacity", 1);
var t = g.transition().duration(750);
t.select("circle").attr("r", 1e-6);
t.select("text").style("opacity", 1e-6);
});
})();
</script>
<p>The enter, update and exit selections are computed by the <a href="https://github.com/mbostock/d3/wiki/Selections#data">data</a> operator, and don’t change when you append or remove elementst least until you call <a href="https://github.com/mbostock/d3/wiki/Selections#selectAll">selectAll</a> again. So, if you keep variables around that point to selections (such as <code>circle</code>, above), you’ll probably want to reselect after adding or removing elements.</p>
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2780434-1";
urchinTracker();
</script>