Skip to content

Instantly share code, notes, and snippets.

@lhoworko
Last active August 29, 2015 14:13
Show Gist options
  • Save lhoworko/ef36fdd4e7315978925b to your computer and use it in GitHub Desktop.
Save lhoworko/ef36fdd4e7315978925b to your computer and use it in GitHub Desktop.
D3 Scatter Plot Transition
<!doctype html>
<head>
<meta charset="utf-8">
<title>D3 Scatter Plot</title>
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 600,
height = 400,
data = [];
var margin = {
"left": 30,
"top": 10,
"right": 10,
"bottom": 30
}
var chart_width = width - margin.left - margin.right,
chart_height = height - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([0, 1])
.range([0, chart_width]);
var y = d3.scale.linear()
.domain([0, 1])
.range([chart_height, 0]);
var xAxis = d3.svg.axis()
.scale(x);
var yAxis = d3.svg.axis()
.orient('left')
.scale(y);
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var chartGroup = svg.append("g")
.attr("transform", "translate(" + margin.left + ","
+ margin.top + ")");
chartGroup.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + chart_height + ")")
.call(xAxis);
chartGroup.append("g")
.attr("class", "y axis")
.call(yAxis);
function update() {
for (var i = 0; i < 100; i++) {
data[i] = [Math.random(), Math.random()];
}
chartGroup.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("r", 5);
chartGroup.selectAll("circle")
.transition()
.duration(800)
.attr("transform", function (d) {
return "translate(" + x(d[0]) + "," + y(d[1]) + ")";
});
setTimeout(update, 1000);
}
update();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment