Created
January 23, 2013 19:32
-
-
Save andrewxhill/4611969 to your computer and use it in GitHub Desktop.
Regional breakdowns by month (CartoDB + D3)
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> | |
<meta charset="utf-8"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://libs.cartocdn.com/cartodb.js/v2/cartodb.js"></script> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
svg { | |
padding: 10px 0 0 10px; | |
} | |
.arc { | |
} | |
</style> | |
<body> | |
<script> | |
var radius = 48, | |
padding = 10; | |
var first_layer = 'unemploy_annual'; | |
var sql = new cartodb.SQL({ user: 'wdbassetmap', format: 'json', dp: 5}); | |
// var color = d3.scale.ordinal() | |
// .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); | |
var color = d3.scale.linear() | |
.domain([0, 0.03, 0.05, 0.07, 0.10, 0.14, 0.25]) | |
.range(["916D80","B64880","#C83680", "#DA2480", "#EC1280", "#FF0080", "red"]) | |
.interpolate(d3.interpolateLab); | |
var arc = d3.svg.arc() | |
.outerRadius(radius) | |
.innerRadius(radius - 24); | |
var pie = d3.layout.pie() | |
.sort(null) | |
.value(function(d) { | |
//return d.val; | |
return 40 | |
}); | |
sql.execute("SELECT name, jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec FROM {{table_name}}", {table_name: first_layer}) | |
.done(function(collection) { | |
var data = collection.rows; | |
// color.domain(d3.keys(data[0]).filter(function(key) { | |
// return key !== "name"; })); | |
var months = []; | |
d3.keys(data[0]).forEach(function(v){ | |
if (v !== "name") { | |
months.push(v); | |
} | |
}); | |
data.forEach(function(d) { | |
d.ages = d3.values(months).map(function(name) { | |
//console.log(d, name) | |
return {name: name, val: +d[name]}; | |
}); | |
}); | |
var legend = d3.select("body").append("svg") | |
.attr("class", "legend") | |
.attr("width", radius * 2) | |
.attr("height", radius * 2) | |
.selectAll("g") | |
.data(color.domain().slice().reverse()) | |
.enter().append("g") | |
.attr("transform", function(d, i) { return "translate(0," + i * 14 + ")"; }); | |
legend.append("rect") | |
.attr("width", 12) | |
.attr("height", 12) | |
.style("fill", color); | |
legend.append("text") | |
.attr("x", 18) | |
.attr("y", 6) | |
.attr("dy", ".35em") | |
.text(function(d) { return d; }); | |
var svg = d3.select("body").selectAll(".pie") | |
.data(data) | |
.enter().append("svg") | |
.attr("class", "pie") | |
.attr("width", radius * 2) | |
.attr("height", radius * 2) | |
.append("g") | |
.attr("transform", "translate(" + radius + "," + radius + ")"); | |
svg.selectAll(".arc") | |
.data(function(d) { return pie(d.ages); }) | |
.enter().append("path") | |
.attr("class", "arc") | |
.attr("d", arc) | |
.style("stroke", "#fff") | |
.style("fill", function(d) { | |
return color(d.data.val); }) | |
.on("mouseover", function() { | |
d3.select(this) | |
.style('stroke', d3.select(this).style("fill")); | |
}) | |
.on("mouseout", function() { | |
d3.select(this) | |
.style('stroke', "#fff") | |
}) | |
.append("svg:title") | |
.text(function(d) { | |
var label = d.data.name + ": " + d.data.val; | |
return label.charAt(0).toUpperCase() + label.slice(1); | |
}) | |
svg.append("text") | |
.attr("dy", ".284em") | |
.style("text-anchor", "middle") | |
.text(function(d) { return d.name; }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment