-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts.js
183 lines (151 loc) · 5.96 KB
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var data = [],
weights = {
population: {},
eligible: {},
votingage: {},
turnout: {}
};
function commaSeparate( numberString ) {
var string = numberString.toString(),
// split string of number by the decimal point
parts = string.split( '.' ),
// format the whole number part of the string with a regex
formattedValue = parts[0].replace( /(\d)(?=(\d{3})+(?!\d))/g, '$1,' );
// Add back decimal part if it exists
if ( typeof parts[1] !== 'undefined' ) {
formattedValue += '.' + parts[1];
}
return formattedValue;
}
function parseText( string ) {
var arr = string.split( ',' )
obj = {};
for ( var x=0; x < arr.length; x++ ) {
var line = arr[x].split( ':' ),
key = line[0].trim(),
value = line[1].trim();
obj[key] = value;
}
return obj;
}
function renderTable( ) {
for ( var x=0; x < data.length; x++ ) {
var row = data[x],
rowHtml = '<tr>',
population = row.population.replace(/\D/g,''),
ratio = commaSeparate( Math.round( population / (+row.electors) ) );
rowHtml += '<td class="cell_state"><strong>' + row.state + '</strong></td>';
rowHtml += '<td class="cell_centered cell_electors">' + row.electors + '</td>';
rowHtml += '<td class="cell_centered cell_population">' + commaSeparate( row.population ) + '</td>';
rowHtml += '<td class="cell_centered cell_ratio">' + commaSeparate( Math.round( row.population / row.electors ) ) + '</td>';
rowHtml += '<td class="cell_centered cell_votingage">' + commaSeparate( row.votingage );
rowHtml += ' (' + Math.round( row.votingage / row.population * 100 ) + '%)</td>';
rowHtml += '<td class="cell_centered cell_eligible">' + commaSeparate( row.eligible );
rowHtml += ' (' + Math.round( row.eligible / row.population * 100 ) + '%)</td>';
rowHtml += '<td class="cell_centered cell_turnout">' + commaSeparate( row.turnout );
rowHtml += ' (' + Math.round( row.turnout / row.eligible * 100 ) + '%)</td>';
rowHtml += '</tr>'
$( '#tabular tbody' ).append( rowHtml );
}
}
function fillWeights() {
var keys = ['population', 'eligible', 'votingage', 'turnout' ];
$.each( data, function() {
for ( var x=0; x < keys.length; x++ ) {
weights[keys[x]][this.state] = this[keys[x] + 'weight'];
}
} );
}
function compareStates( dataset ) {
var baseState = $( '#base-state option:selected' ).val(),
comparedState = $( '#compared-state option:selected' ).val(),
baseWeight = weights[dataset][baseState],
comparedWeight = weights[dataset][comparedState],
voteRatio = ( baseWeight / comparedWeight ),
voteText = " voters";
voteRatio = Math.round( ( voteRatio * 100 ) ) / 100;
if ( voteRatio === 1 ) {
voteText = " voter";
}
$( '#com-graph_base-state' ).text( baseState );
$( '#com-graph_comp-state' ).text( comparedState );
$( '#weighted-value' ).text( voteRatio + voteText );
drawComparison( baseWeight, comparedWeight );
}
function drawComparison( base, compared ) {
$( '#com-graph_base-bar' ).css( 'width', base * 100 + 'px');
$( '#com-graph_comp-bar' ).css( 'width', compared * 100 + 'px');
}
function drawStateBoxes( dataset ) {
var weightKeys = {
population: 'populationweight',
eligible: 'eligibleweight',
votingage: 'votingageweight',
turnout: 'turnoutweight'
};
var weight = weightKeys[dataset];
$( '#container div.state-box' ).remove();
var body = d3.select('#container')
.selectAll('div')
.data(
data.sort( function(x, y) {
return d3.descending( x[weight], y[weight] )
} )
).enter()
.append('div')
.attr('class',
function(d) { return 'state-box voted-' + d.voted.toLowerCase(); } )
.style('width', function(d) { return ( d[weight] * 200 ) + "px" } )
.style('height', function(d) { return Math.max((d[weight] * 10), 20) + 'px'} )
.style('background-color', function(d) {return 'rgb(' + (d[weight] * 50) + ',0,0)'} )
.style('font-size', function(d) { return Math.max((d[weight] * 10), 16) + 'px'})
.html( function(d) {
var content = '<p class="label"><strong>' + d.state + '</strong></p>';
content += '<p class="electors hidden">Electors: ' + d.electors + '</p>';
content += '<p class="population hidden">Population: ' + commaSeparate( d.population ) + '</p>';
return content;
} );
// sorted data by ratio, then compare the endpoint states
data.sort( function( x, y ) {
return d3.descending( x[weight], y[weight] );
} );
$( '#base-state').val( data[0].state );
$( '#compared-state').val( data[data.length - 1].state );
}
$( document ).ready( function() {
var url = 'https://spreadsheets.google.com/feeds/list/1Z-xlwrIGdbGBVyZwdXvfvtPqnMl50jvcpX_sffsb8L4/od6/public/basic?alt=json';
$.ajax({
dataType: "json",
url: url,
success: function( dump ) {
$( dump.feed.entry ).each( function() {
var obj = {};
obj.state = this.title.$t.trim();
if ( obj.state !== 'Total' ) {
$.extend( obj, parseText( this.content.$t ) );
data.push( obj );
}
} );
fillWeights();
renderTable();
drawStateBoxes( 'population' );
compareStates( 'population' );
$( '#viz-buttons button[data-data_set="population"]' ).attr( 'disabled', true );
$( '#com-buttons button[data-data_set="population"]' ).attr( 'disabled', true );
}
});
$( '#viz-buttons button' ).click( function() {
$( '#viz-buttons button' ).attr( 'disabled', false );
$( this ).attr( 'disabled', true );
drawStateBoxes( $( this ).attr( 'data-data_set' ) );
} );
$( '#com-buttons button' ).click( function() {
$( '#com-buttons button' ).attr( 'disabled', false );
$( this ).attr( 'disabled', true );
compareStates( $( this ).attr( 'data-data_set' ) );
} );
$( '#comparator' ).change( function() {
var dataset = $( '#com-buttons button[disabled]').attr( 'data-data_set' );
compareStates( dataset );
} );
} );