d3/examples/hello-world/hello-sort.html

66 строки
1.2 KiB
HTML

<!DOCTYPE html>
<meta charset="utf-8">
<title>Hello, sort!</title>
<style>
body {
font: 14px Helvetica Neue;
}
circle {
fill: steelblue;
fill-opacity: .8;
stroke: #fff;
}
div {
position: fixed;
top: 10px;
left: 10px;
}
</style>
Press any key:<p>
<input id="sort" type="checkbox" checked>
<label for="sort">Ascending</label>
<script src="../../d3.js"></script>
<script>
var svg = d3.select("body").append("svg")
.attr("viewBox", "0 0 1000 1000")
.attr("width", 960)
.attr("height", 500);
var sort = d3.select("#sort")
.on("change", sort);
d3.select(window)
.on("keypress", transform);
transform();
function transform() {
var circle = svg.selectAll("circle")
.data(d3.range(400).map(Math.random));
circle.enter().append("circle")
.attr("cx", function() { return 100 + Math.random() * 800; })
.attr("cy", function() { return 100 + Math.random() * 800; })
.attr("r", function(d) { return 50 * d; });
circle.transition()
.duration(750)
.attr("r", function(d) { return 50 * d; });
sort();
}
function sort() {
d3.selectAll("circle")
.sort(sort.property("checked")
? function(a, b) { return b - a; }
: function(a, b) { return a - b; });
}
</script>