Built with blockbuilder.org
Created
October 28, 2016 22:03
-
-
Save jcnesci/b7560055d02a61d803cf9b7d20b0aacf to your computer and use it in GitHub Desktop.
Broken - Object Constancy Example
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
license: mit |
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
<!-- from https://bost.ocks.org/mike/constancy/ --> | |
<!DOCTYPE html> | |
<html class="ocks-org do-not-copy"> | |
<meta charset="utf-8"> | |
<title>Object Constancy</title> | |
<style> | |
svg { | |
font: 10px sans-serif; | |
} | |
.bar rect { | |
fill: steelblue; | |
} | |
.bar:hover rect { | |
fill: brown; | |
} | |
.value { | |
fill: white; | |
} | |
.axis { | |
shape-rendering: crispEdges; | |
} | |
.axis path { | |
stroke: none; | |
} | |
.x.axis line { | |
stroke: #fff; | |
stroke-opacity: .8; | |
} | |
.y.axis path { | |
stroke: black; | |
} | |
</style> | |
<p id="chart"> | |
<p id="menu"><b>Top States by Age Bracket, 2008</b><br>Age: <select></select> | |
<script src="//d3js.org/d3.v2.min.js" charset="utf-8"></script> | |
<script> | |
var margin = {top: 20, right: 40, bottom: 10, left: 40}, | |
width = 900, | |
height = 250 - margin.top - margin.bottom; | |
var format = d3.format(".1%"), | |
states, | |
age; | |
var x = d3.scale.linear() | |
.range([0, width]); | |
var y = d3.scale.ordinal() | |
.rangeRoundBands([0, height], .1); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("top") | |
.tickSize(-height - margin.bottom) | |
.tickFormat(format); | |
var svg = d3.select("#chart").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.style("margin-left", -margin.left + "px") | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
svg.append("g") | |
.attr("class", "x axis"); | |
svg.append("g") | |
.attr("class", "y axis") | |
.append("line") | |
.attr("class", "domain") | |
.attr("y2", height); | |
var menu = d3.select("#menu select") | |
.on("change", change); | |
d3.csv("states-age.csv", function(data) { | |
states = data; | |
var ages = d3.keys(states[0]).filter(function(key) { | |
return key != "State" && key != "Total"; | |
}); | |
states.forEach(function(state) { | |
ages.forEach(function(age) { | |
state[age] = state[age] / state.Total; | |
}); | |
}); | |
menu.selectAll("option") | |
.data(ages) | |
.enter().append("option") | |
.text(function(d) { return d; }); | |
menu.property("value", "18 to 24 Years"); | |
redraw(); | |
}); | |
var altKey; | |
d3.select(window) | |
.on("keydown", function() { altKey = d3.event.altKey; }) | |
.on("keyup", function() { altKey = false; }); | |
function change() { | |
clearTimeout(timeout); | |
d3.transition() | |
.duration(altKey ? 7500 : 750) | |
.each(redraw); | |
} | |
function redraw() { | |
var age1 = menu.property("value"), | |
top = states.sort(function(a, b) { return b[age1] - a[age1]; }).slice(0, 10); | |
y.domain(top.map(function(d) { return d.State; })); | |
var bar = svg.selectAll(".bar") | |
.data(top, function(d) { return d.State; }); | |
var barEnter = bar.enter().insert("g", ".axis") | |
.attr("class", "bar") | |
.attr("transform", function(d) { return "translate(0," + (y(d.State) + height) + ")"; }) | |
.style("fill-opacity", 0); | |
barEnter.append("rect") | |
.attr("width", age && function(d) { return x(d[age]); }) | |
.attr("height", y.rangeBand()); | |
barEnter.append("text") | |
.attr("class", "label") | |
.attr("x", -3) | |
.attr("y", y.rangeBand() / 2) | |
.attr("dy", ".35em") | |
.attr("text-anchor", "end") | |
.text(function(d) { return d.State; }); | |
barEnter.append("text") | |
.attr("class", "value") | |
.attr("x", age && function(d) { return x(d[age]) - 3; }) | |
.attr("y", y.rangeBand() / 2) | |
.attr("dy", ".35em") | |
.attr("text-anchor", "end"); | |
x.domain([0, top[0][age = age1]]); | |
var barUpdate = d3.transition(bar) | |
.attr("transform", function(d) { return "translate(0," + (d.y0 = y(d.State)) + ")"; }) | |
.style("fill-opacity", 1); | |
barUpdate.select("rect") | |
.attr("width", function(d) { return x(d[age]); }); | |
barUpdate.select(".value") | |
.attr("x", function(d) { return x(d[age]) - 3; }) | |
.text(function(d) { return format(d[age]); }); | |
var barExit = d3.transition(bar.exit()) | |
.attr("transform", function(d) { return "translate(0," + (d.y0 + height) + ")"; }) | |
.style("fill-opacity", 0) | |
.remove(); | |
barExit.select("rect") | |
.attr("width", function(d) { return x(d[age]); }); | |
barExit.select(".value") | |
.attr("x", function(d) { return x(d[age]) - 3; }) | |
.text(function(d) { return format(d[age]); }); | |
d3.transition(svg).select(".x.axis") | |
.call(xAxis); | |
} | |
var timeout = setTimeout(function() { | |
menu.property("value", "65 Years and Over").node().focus(); | |
change(); | |
}, 5000); | |
</script> | |
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
State | Under 5 Years | 5 to 13 Years | 14 to 17 Years | 18 to 24 Years | 25 to 44 Years | 45 to 64 Years | 65 Years and Over | |
---|---|---|---|---|---|---|---|---|
AL | 310504 | 552339 | 259034 | 450818 | 1231572 | 1215966 | 641667 | |
AK | 52083 | 85640 | 42153 | 74257 | 198724 | 183159 | 50277 | |
AZ | 515910 | 828669 | 362642 | 601943 | 1804762 | 1523681 | 862573 | |
AR | 202070 | 343207 | 157204 | 264160 | 754420 | 727124 | 407205 | |
CA | 2704659 | 4499890 | 2159981 | 3853788 | 10604510 | 8819342 | 4114496 | |
CO | 358280 | 587154 | 261701 | 466194 | 1464939 | 1290094 | 511094 | |
CT | 211637 | 403658 | 196918 | 325110 | 916955 | 968967 | 478007 | |
DE | 59319 | 99496 | 47414 | 84464 | 230183 | 230528 | 121688 | |
DC | 36352 | 50439 | 25225 | 75569 | 193557 | 140043 | 70648 | |
FL | 1140516 | 1938695 | 925060 | 1607297 | 4782119 | 4746856 | 3187797 | |
GA | 740521 | 1250460 | 557860 | 919876 | 2846985 | 2389018 | 981024 | |
HI | 87207 | 134025 | 64011 | 124834 | 356237 | 331817 | 190067 | |
ID | 121746 | 201192 | 89702 | 147606 | 406247 | 375173 | 182150 | |
IL | 894368 | 1558919 | 725973 | 1311479 | 3596343 | 3239173 | 1575308 | |
IN | 443089 | 780199 | 361393 | 605863 | 1724528 | 1647881 | 813839 | |
IA | 201321 | 345409 | 165883 | 306398 | 750505 | 788485 | 444554 | |
KS | 202529 | 342134 | 155822 | 293114 | 728166 | 713663 | 366706 | |
KY | 284601 | 493536 | 229927 | 381394 | 1179637 | 1134283 | 565867 | |
LA | 310716 | 542341 | 254916 | 471275 | 1162463 | 1128771 | 540314 | |
ME | 71459 | 133656 | 69752 | 112682 | 331809 | 397911 | 199187 | |
MD | 371787 | 651923 | 316873 | 543470 | 1556225 | 1513754 | 679565 | |
MA | 383568 | 701752 | 341713 | 665879 | 1782449 | 1751508 | 871098 | |
MI | 625526 | 1179503 | 585169 | 974480 | 2628322 | 2706100 | 1304322 | |
MN | 358471 | 606802 | 289371 | 507289 | 1416063 | 1391878 | 650519 | |
MS | 220813 | 371502 | 174405 | 305964 | 764203 | 730133 | 371598 | |
MO | 399450 | 690476 | 331543 | 560463 | 1569626 | 1554812 | 805235 | |
MT | 61114 | 106088 | 53156 | 95232 | 236297 | 278241 | 137312 | |
NE | 132092 | 215265 | 99638 | 186657 | 457177 | 451756 | 240847 | |
NV | 199175 | 325650 | 142976 | 212379 | 769913 | 653357 | 296717 | |
NH | 75297 | 144235 | 73826 | 119114 | 345109 | 388250 | 169978 | |
NJ | 557421 | 1011656 | 478505 | 769321 | 2379649 | 2335168 | 1150941 | |
NM | 148323 | 241326 | 112801 | 203097 | 517154 | 501604 | 260051 | |
NY | 1208495 | 2141490 | 1058031 | 1999120 | 5355235 | 5120254 | 2607672 | |
NC | 652823 | 1097890 | 492964 | 883397 | 2575603 | 2380685 | 1139052 | |
ND | 41896 | 67358 | 33794 | 82629 | 154913 | 166615 | 94276 | |
OH | 743750 | 1340492 | 646135 | 1081734 | 3019147 | 3083815 | 1570837 | |
OK | 266547 | 438926 | 200562 | 369916 | 957085 | 918688 | 490637 | |
OR | 243483 | 424167 | 199925 | 338162 | 1044056 | 1036269 | 503998 | |
PA | 737462 | 1345341 | 679201 | 1203944 | 3157759 | 3414001 | 1910571 | |
RI | 60934 | 111408 | 56198 | 114502 | 277779 | 282321 | 147646 | |
SC | 303024 | 517803 | 245400 | 438147 | 1193112 | 1186019 | 596295 | |
SD | 58566 | 94438 | 45305 | 82869 | 196738 | 210178 | 116100 | |
TN | 416334 | 725948 | 336312 | 550612 | 1719433 | 1646623 | 819626 | |
TX | 2027307 | 3277946 | 1420518 | 2454721 | 7017731 | 5656528 | 2472223 | |
UT | 268916 | 413034 | 167685 | 329585 | 772024 | 538978 | 246202 | |
VT | 32635 | 62538 | 33757 | 61679 | 155419 | 188593 | 86649 | |
VA | 522672 | 887525 | 413004 | 768475 | 2203286 | 2033550 | 940577 | |
WA | 433119 | 750274 | 357782 | 610378 | 1850983 | 1762811 | 783877 | |
WV | 105435 | 189649 | 91074 | 157989 | 470749 | 514505 | 285067 | |
WI | 362277 | 640286 | 311849 | 553914 | 1487457 | 1522038 | 750146 | |
WY | 38253 | 60890 | 29314 | 53980 | 137338 | 147279 | 65614 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment