Last active
May 13, 2016 04:19
-
-
Save willwhitney/4374345a38f0f3182f7045eba1bf9a69 to your computer and use it in GitHub Desktop.
Getting per-state per capita usage numbers from https://projects.propublica.org/checkup with jank javascript.
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
/* | |
To use me, go to one of the individual drug pages from propublica (e.g. https://projects.propublica.org/checkup/drugs/1726), | |
open the console (press ⌘-⌥-J on Mac), | |
paste in this script and hit Enter. | |
A new column should appear in your table. | |
This is very much hacked together in the console, so... no guarantees it will work next week. | |
*/ | |
states = [[1, "California", 39144818], | |
[2, "Texas", 27469114], | |
[3, "Florida", 20271272], | |
[4, "New York", 19795791], | |
[5, "Illinois", 12859995 ], | |
[6, "Pennsylvania", 12802503 ], | |
[7, "Ohio", 11613423 ], | |
[8,"Georgia", 10214860 ], | |
[9,"North Carolina", 10042802], | |
[10, "Michigan", 9922576 ], | |
[11, "New Jersey", 8958013], | |
[12, "Virginia", 8382993 ], | |
[13, "Washington", 7170351 ], | |
[14, "Arizona", 6828065 ], | |
[15, "Massachusetts", 6794422 ], | |
[16, "Indiana", 6619680 ], | |
[17, "Tennessee", 6600299], | |
[18, "Missouri", 6083672], | |
[19, "Maryland", 6006401], | |
[20, "Wisconsin", 5771337], | |
[21, "Minnesota", 5489594], | |
[22, "Colorado", 5456574], | |
[23, "South Carolina", 4896146], | |
[24, "Alabama", 4858979], | |
[25, "Louisiana", 4670724], | |
[26, "Kentucky", 4425092], | |
[27, "Oregon", 4028977], | |
[28, "Oklahoma", 3911338], | |
[29, "Connecticut", 3590886], | |
[null, "Puerto Rico", 3474182], | |
[30, "Iowa", 3123899], | |
[31, "Utah", 2995919], | |
[32, "Mississippi", 2992333], | |
[33, "Arkansas", 2978204], | |
[34, "Kansas", 2911641], | |
[35, "Nevada", 2890845], | |
[36, "New Mexico", 2085109], | |
[37, "Nebraska", 1896190], | |
[38, "West Virginia", 1844128], | |
[39, "Idaho", 1654930], | |
[40, "Hawaii", 1431603], | |
[41, "New Hampshire", 1330608], | |
[42, "Maine", 1329328], | |
[43, "Rhode Island", 1056298], | |
[44, "Montana", 1032949], | |
[45, "Delaware", 945934], | |
[46, "South Dakota", 858469], | |
[47, "North Dakota", 756927], | |
[48, "Alaska", 738432 ], | |
[null, "District of Columbia", 672228], | |
[49, "Vermont", 626042], | |
[50, "Wyoming", 586107]] | |
state_elements = $('td.stateface a') | |
function state_el_by_name(name) { | |
for ( i = 0; i < state_elements.length; i++) { | |
if (state_elements[i].innerText == name) {return state_elements[i]}}} | |
function state_pop_by_name(name) { | |
for (i = 0; i < states.length; i++) { | |
if (states[i][1] == name) { return states[i][2]}}} | |
function strip_commas(n) { return parseInt(n.replace(/\,/g, ''), 10);} | |
function get_patients_from_row(row) { return strip_commas(row.find('td')[1].innerText)} | |
states_table = $("table")[0] | |
$('.provider_box')[0].style.marginLeft = "200px" | |
$(states_table).find('tr').each( | |
function(r) { | |
r = $(this); | |
if (r.find('.stateface').length > 0) { | |
var patients = get_patients_from_row(r); | |
var state_name = r.find('td.stateface a')[0].innerText | |
var population = state_pop_by_name(state_name); | |
var fraction = patients / population; | |
var pretty_fraction = fraction.toExponential(3) | |
r.append('<td class="num_column" data-sort="' + fraction + '">' + pretty_fraction + '</td>'); | |
} | |
} | |
) | |
$($(states_table).find('tr')[0]).append('<th class="sortyUp">Per Capita Usage</th>') | |
function sortTable(){ | |
var tbl = states_table.tBodies[0]; | |
var store = []; | |
for(var i=0, len=tbl.rows.length; i<len; i++){ | |
var row = tbl.rows[i]; | |
var sortnr = parseFloat(row.cells[4].getAttribute('data-sort')); | |
if(!isNaN(sortnr)) store.push([sortnr, row]); | |
} | |
store.sort(function(x,y){ | |
return - (x[0] - y[0]); | |
}); | |
for(var i=0, len=store.length; i<len; i++){ | |
tbl.appendChild(store[i][1]); | |
} | |
store = null; | |
} | |
sortTable(); | |
$(states_table).attr("width", "600px") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment