topical media & game development
mobile-data-circle-05.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>Once data is bound, that data is accessible as an argument to our attribute and style functions. This means we visually encode data, or in other words, create a visualization! For example, here we set the <em>x</em>-position and radius using the data:</p>
<div class="chart" id="chart-6">
<pre class="code">circle.attr("cx", function(d) {
return d;
});
circle.attr("r", function(d) {
return Math.sqrt(d);
});
</pre>
<button>run</button>
<!--
<svg width="360" height="180"><g transform="translate(180,45)"><circle class="little" r="12"></circle><text dy=".35em" text-anchor="middle">32</text></g><g transform="translate(60,90)"><circle class="little" r="12"></circle><text dy=".35em" text-anchor="middle">57</text></g><g transform="translate(300,135)"><circle class="little" r="12"></circle><text dy=".35em" text-anchor="middle">112</text></g></svg>
-->
</div>
<script type="text/javascript">
script
(function() {
var svg = d3.select("#chart-6").append("svg")
.attr("width", w)
.attr("height", h);
select
var g = svg.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function(d) { return "translate(" + x(d) + "," + y(d) + ")"; });
g.append("circle")
.attr("class", "little")
.attr("r", 12);
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
button
d3.select("#chart-6 button").on("click", function() {
g
.attr("transform", function(d) { return "translate(" + x(d) + "," + y(d) + ")"; })
.select("circle")
.attr("r", 12);
transition
g.transition()
.duration(750)
.attr("transform", function(d) { return "translate(" + d + "," + y(d) + ")"; })
.select("circle")
.attr("r", Math.sqrt);
});
})();
</script>
(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>