|
|
|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
body { |
|
font: 10px sans-serif; |
|
} |
|
|
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: #000; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.bar { |
|
fill: steelblue; |
|
} |
|
|
|
.x.axis path { |
|
display: none; |
|
} |
|
|
|
</style> |
|
<body> |
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> |
|
<script> |
|
|
|
var sort_select = 'state'; |
|
var margin = {top: 20, right: 40, bottom: 80, left: 40}, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom; |
|
|
|
var x0 = d3.scale.ordinal() |
|
.rangeRoundBands([0, width], .1); |
|
|
|
var x1 = d3.scale.ordinal(); |
|
|
|
var y = {}; |
|
|
|
y['area'] = d3.scale.linear() |
|
.range([height, 0]); |
|
|
|
y['pop2010'] = d3.scale.linear() |
|
.range([height, 0]); |
|
|
|
|
|
var color = d3.scale.ordinal() |
|
.range(["#8a89a6", "#d0743c", "#ff8c00"]); |
|
|
|
var xAxis = d3.svg.axis() |
|
.scale(x0) |
|
.orient("bottom"); |
|
|
|
var yAxis = d3.svg.axis() |
|
.scale(y['pop2010']) |
|
.orient("left") |
|
.tickFormat(d3.format(".2s")); |
|
|
|
var yAxis_right = d3.svg.axis() |
|
.scale(y['area']) |
|
.orient("right") |
|
.tickFormat(d3.format(".2s")); |
|
|
|
var svg = d3.select("body").append("svg") |
|
.attr("width", width + margin.left + margin.right) |
|
.attr("height", height + margin.top + margin.bottom) |
|
.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")") |
|
; |
|
|
|
var xi = 85; |
|
var fsz = 12; |
|
var yi = 10; |
|
var dyi = 15; |
|
var fl = '#111111'; |
|
|
|
var info_display_1 = svg.append('text') |
|
.attr('id', 'info_display_1') |
|
.attr('x', xi) |
|
.attr('y', yi + 0*dyi) |
|
.style('font-size', fsz + "px") |
|
.style('opacity', 1) |
|
.style('fill', fl) |
|
.style('background-color', '#000') |
|
; |
|
|
|
var info_display_2 = svg.append('text') |
|
.attr('id', 'info_display_2') |
|
.attr('x', xi) |
|
.attr('y', yi + 1*dyi) |
|
.style('font-size', fsz + "px") |
|
.style('opacity', 1) |
|
.style('fill', fl) |
|
.style('background-color', '#000') |
|
; |
|
|
|
var info_display_3 = svg.append('text') |
|
.attr('id', 'info_display_3') |
|
.attr('x', xi) |
|
.attr('y', yi + 2*dyi) |
|
.style('font-size', fsz + "px") |
|
.style('opacity', 1) |
|
.style('fill', fl) |
|
.style('background-color', '#000') |
|
; |
|
|
|
var info_display_4 = svg.append('text') |
|
.attr('id', 'info_display_4') |
|
.attr('x', xi) |
|
.attr('y', yi + 3*dyi) |
|
.style('font-size', fsz + "px") |
|
.style('opacity', 1) |
|
.style('fill', fl) |
|
.style('background-color', '#000') |
|
; |
|
|
|
var title = svg.append('text') |
|
.text("US States: 2010 Census") |
|
.attr('id', 'info_title') |
|
.attr('x', 350) |
|
.attr('y', 0) |
|
.style('font-size', "18px") |
|
.style('opacity', 1) |
|
.style('fill', fl) |
|
.style('background-color', '#000') |
|
; |
|
|
|
|
|
function sort_data(data, key) { |
|
if (key == sort_select) { |
|
sort_select = key; |
|
return data.reverse(); |
|
} |
|
|
|
sort_select = key; |
|
sdata = _.sortBy(data, key); |
|
|
|
if (key=='state') |
|
return sdata; |
|
return sdata.reverse(); |
|
}; |
|
|
|
|
|
d3.csv("state_area_pop.csv", function(error, data) { |
|
|
|
data.forEach(function(d) { |
|
d.area = parseFloat(d.area); |
|
d.pop2010 = parseInt(d.pop2010); |
|
}); |
|
|
|
var cols = ['area', 'pop2010'] |
|
|
|
data.forEach(function(d) { |
|
d.values = cols.map(function(name) { |
|
return {'name': name, 'value': +d[name]}; |
|
}); |
|
}); |
|
|
|
data.forEach(function(d) { |
|
d.density = d.pop2010/d.area; |
|
}); |
|
|
|
sdata = sort_data(data, 'state'); |
|
console.log('sdata', sdata); |
|
|
|
x0.domain(data.map(function(d, i) { return i; })); |
|
x1.domain(cols).rangeRoundBands([0, x0.rangeBand()]); |
|
|
|
y['pop2010'].domain([0, 38000000]); |
|
y['area'].domain([0, 680000]); |
|
|
|
var xa = svg.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(xAxis) |
|
.selectAll("text") |
|
.text(function(d, i) { |
|
return sdata[i].state.replace('_', ' ').replace('_', ' '); |
|
}) |
|
.style("text-anchor", "end") |
|
.attr("dx", "-.8em") |
|
.attr("dy", ".15em") |
|
.attr("transform", "rotate(-65)" ) |
|
.attr('font-size', '8px') |
|
.attr('class', 'xaxis_label') |
|
; |
|
|
|
|
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.call(yAxis) |
|
.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("y", 6) |
|
.attr("dy", ".71em") |
|
.style("text-anchor", "end") |
|
.text('Population') |
|
; |
|
|
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.call(yAxis_right) |
|
.attr("transform", "translate(" + width + " ,0)") |
|
.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("y", -16) |
|
.attr("y", -16) |
|
.attr("dy", ".71em") |
|
.style("text-anchor", "end") |
|
.text('Area (sq. miles)') |
|
|
|
; |
|
|
|
|
|
function updatePlot(sdata, key) { |
|
console.log('updating the plot'); |
|
old_keys = {} |
|
sdata.forEach(function(d, i) { |
|
old_keys[d.state] = i; |
|
}); |
|
|
|
keys = {}; |
|
sdata = sort_data(sdata, key); |
|
sdata.forEach(function(d, i) { |
|
keys[d.state] = i; |
|
}); |
|
|
|
state.transition() |
|
.attr("transform", function(d, i) { |
|
return "translate(" + x0(i) + ",0)"; |
|
}) |
|
; |
|
|
|
var k = 'area'; |
|
rect[k].transition().duration(2000) |
|
.attr("x", function(d, i) { |
|
return x0(keys[d.state]) + x1(k); |
|
}) |
|
; |
|
|
|
var k = 'pop2010'; |
|
rect[k].transition().duration(2000) |
|
.attr("x", function(d, i) { |
|
return x0(keys[d.state]) + x1(k); |
|
}) |
|
; |
|
|
|
xa.transition(2000) |
|
.text(function(d, i) { |
|
return sdata[i].state.replace('_', ' ').replace('_', ' '); |
|
}); |
|
|
|
} |
|
|
|
var state = svg.selectAll(".state") |
|
.data(sdata) |
|
.enter().append("g") |
|
.attr("class", "g") |
|
.attr("transform", function(d, i) { return "translate(" + x0(i) + ",0)"; }); |
|
|
|
function info_text(datum) { |
|
var fmt = d3.format('.1f'); |
|
d3.selectAll('#info_display_1') |
|
.text('state: ' + datum.state.replace('_', ' ').replace('_', ' ') ) |
|
.transition() |
|
.duration(100) |
|
.style('opacity', 1); |
|
d3.selectAll('#info_display_2') |
|
.text('population: ' + datum.pop2010) |
|
.transition() |
|
.duration(100) |
|
|
|
.style('opacity', 1); |
|
d3.selectAll('#info_display_3') |
|
.text('area (sq.mi): ' + datum.area) |
|
.transition() |
|
.duration(100) |
|
|
|
.style('opacity', 1); |
|
d3.selectAll('#info_display_4') |
|
.text('density (per sq.mi): ' + fmt(datum.density)) |
|
.transition() |
|
.duration(100) |
|
|
|
.style('opacity', 1); |
|
}; |
|
|
|
function info_text_off() { |
|
d3.selectAll('#info_display_1') |
|
.transition() |
|
.duration(100) |
|
.style('opacity', 0) |
|
; |
|
d3.selectAll('#info_display_2') |
|
.transition() |
|
.duration(100) |
|
.style('opacity', 0) |
|
; |
|
d3.selectAll('#info_display_3') |
|
.transition() |
|
.duration(100) |
|
.style('opacity', 0) |
|
; |
|
d3.selectAll('#info_display_4') |
|
.transition() |
|
.duration(100) |
|
.style('opacity', 0) |
|
; |
|
}; |
|
|
|
var rect = {}; |
|
|
|
var k = 'area'; |
|
rect[k] = svg.selectAll(".rect_area") |
|
.data(sdata) |
|
.enter().append("rect") |
|
.attr('class', 'rect_area') |
|
.attr("width", x1.rangeBand()) |
|
.attr("x", function(d, i) { |
|
return x0(i) + x1(k); }) |
|
.attr("y", function(d, i) { |
|
return y[k](d[k]); }) |
|
.attr("height", function(d) { |
|
return height - y[k](d[k]); }) |
|
.style("fill", function(d) { return color(k); }) |
|
.on('mouseover', function(d, i) { |
|
info_text(d); |
|
;}) |
|
.on('mouseout', function(d, i) { |
|
info_text_off(); |
|
}) |
|
; |
|
|
|
var k = 'pop2010'; |
|
rect[k] = svg.selectAll(".rect_pop2010") |
|
.data(sdata) |
|
.enter().append("rect") |
|
.attr('class', 'rect_pop2010') |
|
.attr("width", x1.rangeBand()) |
|
.attr("x", function(d, i) { |
|
return x0(i) + x1(k); }) |
|
.attr("y", function(d, i) { |
|
return y[k](d[k]); }) |
|
.attr("height", function(d) { |
|
return height - y[k](d[k]); }) |
|
.style("fill", function(d) { return color(k); }) |
|
.on('mouseover', function(d, i) { |
|
info_text(d); |
|
;}) |
|
.on('mouseout', function(d, i) { |
|
info_text_off(); |
|
}) |
|
; |
|
|
|
|
|
var sorteys = [{ 'k': 'name', 'v': 'state'}, |
|
{ 'k': 'pop.', 'v': 'pop2010'}, |
|
{ 'k': 'area', 'v': 'area'}, |
|
{ 'k':'density', 'v':'density'} |
|
]; |
|
|
|
var sort_options = svg.selectAll('.sort_options') |
|
.data(sorteys) |
|
.enter().append('g') |
|
.attr('class', 'sort_options') |
|
.attr('transform', function(d, i) { return "translate(0," + i * 20 + ")"; }) |
|
; |
|
|
|
sort_options.append('text') |
|
.text(function(d) {return d.k;}) |
|
.style('text-align', 'right') |
|
.attr("x", width - 60) |
|
.attr('y', function(d, i) { |
|
return 80; //parseInt(i + 70); |
|
}) |
|
.attr("width", 40) |
|
.attr("height", 12) |
|
.style("fill", '000') |
|
.style('cursor', 'pointer') |
|
.on('click', function(d, i) { |
|
updatePlot(sdata, d.v); |
|
}) |
|
; |
|
|
|
svg.append('text') |
|
.text('Sort By') |
|
.attr('x', width - 60) |
|
.attr('y', 60); |
|
|
|
var legend = svg.selectAll(".legend") |
|
.data(cols.slice().reverse()) |
|
.enter().append("g") |
|
.attr("class", "legend") |
|
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); |
|
|
|
legend.append("rect") |
|
.attr("x", width - 40) |
|
.attr("width", 18) |
|
.attr("height", 18) |
|
.style("fill", color); |
|
|
|
legend.append("text") |
|
.attr("x", width - 46) |
|
.attr("y", 9) |
|
.attr("dy", ".35em") |
|
.style("text-anchor", "end") |
|
.text(function(d) { return d; }); |
|
|
|
}); |
|
|
|
</script> |
|
|