Created
August 12, 2015 20:53
-
-
Save krish85/77e322ffbf9e47e4cd56 to your computer and use it in GitHub Desktop.
Folding Rects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
</head> | |
<body> | |
<style> | |
</style> | |
<script> | |
var h = 300, w = 1200, padding = 150; | |
var data = ["Yo", "What", "The", "Hell", "You", "Want", "Man!!"]; | |
var mid = Math.floor(data.length/2); | |
var color = d3.scale.category10(); | |
var svg = d3.select("body").append("svg").attr("height", h).attr("width", w); | |
svg.selectAll("rect").data(data).enter().append("rect"); | |
var closeWings = function(){ | |
d3.selectAll("rect") | |
.attr("x", function(d,i){ | |
if(i == mid){ | |
return (i * padding) + padding + (i * 2) + 12; | |
} | |
else if(i < mid){ | |
return (mid * padding) + padding + (mid * 2) - (mid - i -2)*mid + (i * 2); | |
} | |
else {return (mid * padding) + padding + (mid * 2) + 135 + (mid+i)*mid +(i * 2);} | |
}) | |
.attr("y", padding) | |
.attr("width", function(d,i){ | |
if(i == mid){ return 150;} | |
else { return 3; } | |
}) | |
.attr("height",40) | |
.style("fill", function(d,i){ return color(i);}) | |
.on("mouseover", function(d){ | |
d3.selectAll("rect") | |
.attr("x", function(d,i){return (i * padding) + padding + (i * 2);}) | |
.attr("y", padding) | |
.attr("height",40) | |
.attr("width",150) | |
.style("fill", function(d,i){ return color(i);}); | |
svg.selectAll("text").data(data).enter().append("text").text(function(d){ return d;}) | |
.attr("x",function(d,i){return (i * padding) + padding+(padding/2)+ (i* 2);}) | |
.attr("y", padding + 30) | |
.attr("fill","white") | |
.attr("text-anchor", "middle") | |
.attr("text-size", "12px"); | |
}) | |
.on("mouseout", closeWings); | |
} | |
closeWings(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment