Skip to content

Instantly share code, notes, and snippets.

@tpogden
Last active August 29, 2015 14:08
Show Gist options
  • Save tpogden/25e565c0a3305644d983 to your computer and use it in GitHub Desktop.
Save tpogden/25e565c0a3305644d983 to your computer and use it in GitHub Desktop.
Random Scatter

A set of 100 points are given a new uniform random distribution every 5 seconds.

<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>Scatter</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
body {
font-family: "Helvetica", sans-serif;
font-size: 10px;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
circle {
fill: rgba(189,54,19,1);
stroke: white;
}
</style>
<body>
<script>
(function() {
var num_points = 100;
delay = 5000;
var margin = {top: 20, right: 20, bottom: 40, left: 40},
width = 944 - margin.left - margin.right,
height = 480 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width])
.domain([0, 1]);
var y = d3.scale.linear()
.range([height, 0])
.domain([0, 1]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
// Draw SVG
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// X axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Y axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
var data_xy = d3.range(num_points)
.map(function() { return [Math.random(), Math.random()]; });
var circles = svg.selectAll(".circle")
.data(data_xy)
.enter().append("svg:circle")
.attr("cx", function (d) { return x(d[0]); } )
.attr("cy", function (d) { return y(d[1]); } )
.attr("r", 8);
setInterval(function() {
var data_xy = d3.range(num_points)
.map(function() { return [Math.random(), Math.random()]; });
circles.data(data_xy)
.transition()
.duration(500)
.attr("cx", function (d) { return x(d[0]); } )
.attr("cy", function (d) { return y(d[1]); } )
}, delay);
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment