Skip to content

Instantly share code, notes, and snippets.

@chriskwan
Created January 9, 2014 23:43
Show Gist options
  • Select an option

  • Save chriskwan/8344280 to your computer and use it in GitHub Desktop.

Select an option

Save chriskwan/8344280 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Data-driven circles</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: gray;
}
svg {
background-color: white;
}
</style>
</head>
<body>
<script type="text/javascript">
//Width and height
var w = 2*500;
var h = 2*100;
var dataset = [
{
x: 5,
y: 20,
r: 10,
color: "#ffccdd"
},
{
x: 480,
y: 90,
r: 20,
color: "#3333dd"
},
{
x: 250,
y: 50,
r: 15,
color: "#ff8800"
},
{
x: 100,
y: 33,
r: 7,
color: "#eeee13"
},
{
x: 330,
y: 95,
r: 18,
color: "#beefff"
},
{
x: 410,
y: 12,
r: 19,
color: "#11f22e"
},
{
x: 475,
y: 44,
r: 25,
color: "#dd4433"
},
{
x: 25,
y: 67,
r: 12,
color: "#ccaabb"
},
{
x: 85,
y: 21,
r: 5,
color: "#88ff22"
},
{
x: 220,
y: 88,
r: 3,
color: "#ba12d7"
}
];
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 2)
.transition()
.duration(10000)
.attr("cx", function(d) {
console.log(d);
return 2*d.x;
})
.attr("cy", function(d) {
return 2*d.y;
})
.attr("r", function(d) {
return 2*d.r;
})
.attr("fill", function(d) {
return d.color;
});
svg.append("text")
.text("Lattice")
.attr("x", 2)
.attr("y", 2)
.attr("fill", "white")
.transition()
.duration(5000)
.attr("x", 2*270)
.attr("y", 2*70)
.style("font-family", "Helvetica")
.style("font-weight", "bold")
.style("font-size", "4em")
.transition()
.duration(3000)
.attr("fill", "black")
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment