Library that uses HTML, CSS and SVG to bring data to life.
Open http://jsfiddle.net/
Click on External Resources on the left and add this url:
http://d3js.org/d3.v2.js
Add this in the HTML box
<div id="frame"></div>
Add this in the CSS box
#frame {
background-color : #eee;
}
Add this in JavaScript box
var box = d3.select("#frame")
.append("svg")
.attr("width", 900)
.attr("height", 600);
We created a variable box where we get d3 to select the #frame and change its width and height
Add this new code in the javascript box
var box = d3.select("#frame")
.append("svg")
.attr("width", 900)
.attr("height", 600);
box.append("circle") // make a circle
.style("stroke", "white") // make the circle border white
.attr("r", 40) // radius 40. You can make it bigger
.attr("cx", 150) // position at x
.attr("cy", 150); // position at y
Click Run!
Live Preview here
Modify the lines you just wrote:
box.append("circle")
.style("stroke", "white")
.attr("r", 40)
.attr("cx", 150)
.attr("cy", 150) // no semicolon here
.on("mouseover", bounce); // new line
We are telling it to bounce when we move our mouse over it.
box.append("circle")
.style("stroke", "white")
.attr("r", 40)
.attr("cx", 150)
.attr("cy", 150) // no semicolon here
.on("mouseover", bounce); // new line
function bounce()
{
d3.select(this) // we select our circle
.transition() // start changing the circle
.attr("r", 100) // change radius to 100
.duration(1000) // for 1 second
.ease("elastic"); // like an elastic band
}
Click Run!
Live Preview here
We add a new function dance right after the function bounce
function dance()
{
d3.select(this) // select the circle
.transition() // start animating
.attr("r", 50) // change radius to 50
.duration(1000) // for 1 second
.ease("elastic") // like an elastic band
.each("end", bounce); // and then call bounce function
}
And change this line to call dance function
.on("mouseover", dance); // dance instaed of bounce
Live Preview here
Lets create a new function marker
function marker()
{
var arrow = d3.mouse(this);
box.append("circle") // add new circle
.attr("cx", arrow[0]) // new x coordinate
.attr("cy", arrow[1]) // new y coordinate
.style("stroke", "gray") // make circle border gray
.transition() // start animating
.duration(1000) // for 1 second
.attr("r", 80) // change radius to 80
.style("stroke-opacity", 0.001) // start fading previous circles
.remove(); // then remove older circles
}
Change this
box.append("circle")
.style("stroke", "white")
.attr("r", 100)
.attr("cx", 150)
.attr("cy", 150)
.on("mouseover", dance);
To this
/*
box.append("circle")
.style("stroke", "white")
.attr("r", 100)
.attr("cx", 150)
.attr("cy", 150)
.on("mouseover", dance);
*/
var box = d3.select("#frame")
.append("svg")
.attr("width", 900)
.attr("height", 600) // no semicolon
.on("mousemove", marker); // add this new line
Click Run!
Live Preview here
Change the background-color of the frame to black and hit run!
#frame {
background-color : black;
}
Live Preview here
var i = 0; // new variable
var color = d3.scale.category20c(); // new variable
function marker() {
var arrow = d3.mouse(this);
box.append("circle")
.attr("cx", arrow[0])
.attr("cy", arrow[1])
.attr("r", 1e-6) // new line
.style("stroke", color(++i)) // changed
.transition()
.duration(1000)
.attr("r", 80)
.style("stroke-opacity", 0.001)
.remove();
}
Live Preview here
Convert the JSFiddle page to HTML, CSS and Javascript document