Skip to content

Instantly share code, notes, and snippets.

@steveodom
Created November 9, 2011 12:04
Show Gist options
  • Save steveodom/1351255 to your computer and use it in GitHub Desktop.
Save steveodom/1351255 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Box Chart</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.4.5"></script>
<style type="text/css">
body {
text-align: center;
}
svg {
font: 10px sans-serif;
}
</style>
</head>
<body>
<div id='chart'></div>
<script type="text/javascript">
// data
degrees = [{key: "Damon",
values: {"PhD": 6, "BBA": 3, "jd": 2}},
{key: "Steve",
values: {"MBA": 6, "BBA": 3, "CS": 2}}
]
advanced = ["grad", "jd", "PhD", "Other"];
graduate = ["MFA", "MBA", "MS", "Masters"]
bachelor = ["BA", "BBA", "CS", "Bachelors"]
index = [advanced, graduate, bachelor]
viewer_colors = ["#B5C4A3", "#A7C485", "#99C468", "#87C442", "#6D9E35", "#537828" ]
subject_colors = ["#C2DAF2", "#9DC8F2", "#79B6F2", "#56A4F2", "#488ACC", "#3A70A6" ]
colors = [subject_colors, viewer_colors]
// Define the margin, radius, and color scale. Colors are assigned lazily, so
// if you want deterministic behavior, define a domain for the color scale.
var m = [19, 20, 20, 19], // top right bottom left margin
w = 200 - m[1] - m[3], // width
h = 250 - m[0] - m[2], // height
z = 50; // cell size
p = 0; //padding
l = 50; //label start or width;
// Insert an svg element (with margin) for each airport in our dataset. A
// child g element translates the origin to the pie center.
var svg = d3.select("#chart").selectAll("div")
.data([1])
.enter().append("div") // http://code.google.com/p/chromium/issues/detail?id=98951
.style("display", "inline-block")
.style("width", "200px")
.style("height", "200px")
.append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.attr("class", "RdYlGn")
.append("svg:g")
.attr("transform", "translate(0, -30)");
svg.append("svg:text")
.attr("transform", "translate(" + l + ","+ (z + (z/2))+")")
.attr("text-anchor", "end")
.text("Advanced");
svg.append("svg:text")
.attr("transform", "translate(" + l + ","+ ((z * 2) + (z/2))+")")
.attr("text-anchor", "end")
.text("Graduate");
svg.append("svg:text")
.attr("transform", "translate(" + l + ","+ ((z * 3) + (z/2))+")")
.attr("text-anchor", "end")
.text("Bachelors");
xpad = 5;
ypad = 12
svg.append("svg:text")
.attr("transform", "translate(" + (xpad + z + (z/2)) + ","+(z * 4 + ypad)+")")
.attr("text-anchor", "middle")
.text("arts");
svg.append("svg:text")
.attr("transform", "translate(" + ((xpad + (z * 2) + (z/2))) + "," +(z * 4 + ypad)+ ")")
.attr("text-anchor", "middle")
.text("business");
svg.append("svg:text")
.attr("transform", "translate(" + (xpad + (z * 3) + (z/2)) + ","+(z * 4 + ypad)+")")
.attr("text-anchor", "middle")
.text("science");
svg.append("svg:text")
.attr("transform", "translate(" + ((xpad + (z * 4) + (z/2))) + "," +(z * 4 + ypad)+ ")")
.attr("text-anchor", "middle")
.text("other");
for (person=0;person<=1;person++) {
for (row=0;row<=2;row++) {
svg.selectAll("rect.degree")
.data([0, 1, 2, 3])
.enter().append("svg:path")
.attr("d", trianglePath(z))
.attr("fill", function(d) {
return colorizer(person, row, d)
})
.attr("width", z)
.attr("height", z)
.attr("transform", function(d, i) { return trianglePos(d, i, z, person, row);})
.attr("stroke-width", 2)
.attr("stroke", "#FFFFFF")
.attr("class", function(d) {return "cell_" + d + "_" + row})
.on("mouseover", function(d, i) { highlight(d3.select(this), "on")})
.on("mouseout", function(d, i) { highlight(d3.select(this), "off")});
}
}
circle = svg.append("svg:g")
.attr("transform", "translate(50, 50)")
.append("svg:circle")
.attr("cx", "50")
.attr("cy", "50")
.attr("r", "40")
.attr("stroke-width", 2)
.attr("stroke", "red")
.attr("fill", "transparent")
circle.append("svg:path")
.attr("d", "M 50 0L 50 50")
.attr("stroke-width", 2)
.attr("stroke", "red")
function highlight(el, status) {
klass = "." + el.attr("class");
d3.select("#chart").selectAll(klass)
.each(function() {
cell = d3.select(this)
color = cell.attr("fill")
if (status == "on") {
cell.attr("data-color", color)
darker = d3.rgb(color).darker([1.1])
cell.attr("fill", darker)
} else {
orig = cell.attr("data-color")
cell.attr("fill", orig)
}
})
return true
}
function hide_pointer() {
}
function colorizer(person, row, column) {
degree = index[row][column]
exist = degrees[person].values[degree]
if (exist) {
if (exist > 5) {exist = 5}
return colors[person][exist]
} else {
return "#f1f1f1"
}
}
function trianglePath(z) {
return "M 0 " + z + " L " + z + " " + z + " L " + z + " 0 z"
}
function trianglePos(c, i, z, person, row) {
x = 5 + l + (z * i);
if (person == 0) {
y = (z * (row+1)) + (p * row)
str = "translate(" + x + "," + y + ")";
} else {
y = (z * (row+2)) + (p * row)
str = "translate(" + (x + z) + "," + y + ")rotate(180)";
}
return str;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment