-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
186 lines (172 loc) · 4.24 KB
/
map.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
184
185
186
var data = {},
demo = {};
var map;
var opacity = [ .05, .1, .15, .2, .25, .35, .45, .55, .75 ];
$( document ).ready( function()
{
$.getJSON( "data/margin.json", function( d )
{
data.vote = {};
data.vote.obj = d;
var sorted = get_sorted( d );
data.vote.keys = sorted.keys;
data.vote.raw = sorted.values;
$.getJSON( "data/population.json", function( d )
{
demo.obj = d;
sorted = get_sorted( d );
demo.raw = sorted.values;
demo.breaks = get_quant( demo.raw );
demo.name = "Population";
$("#y-axis").html("Population");
setup_map();
buildGraph();
});
});
$( ".box" ).click( function()
{
$( "#loader" ).show();
var f = parseFloat( $( this ).attr( "name" ) );
var n = $( this ).html().replace( "<br>", "" );
console.log( n );
$( ".box.on" ).removeClass( "on" );
$( this ).append( $( "#triangle" ) ).addClass( "on" );
$.getJSON( "data/" + $( this ).attr( "id" ) + ".json", function( d )
{
demo = {};
demo.obj = d;
demo.name = n;
$("#y-axis").html(n);
var sorted = get_sorted( d );
demo.raw = sorted.values;
demo.breaks = get_quant( demo.raw );
d3.selectAll( "#counties path" ).style( "fill-opacity", function( d )
{
return getOpacity( demo, d.properties.FIPS, f );
})
buildGraph();
$( "#loader" ).hide();
});
});
});
function setup_map()
{
var path = d3.geo.path()
.projection( d3.geo.albersUsa()
.scale( 1300 )
.translate( [ 460, 330 ] )
);
var svg = d3.select( "#body" )
.append( "svg:svg" )
.attr( "width", "960px" )
.attr( "height", "610px" );
var counties = svg.append( "svg:g" )
.attr( "id", "counties" );
var states = svg.append( "svg:g" )
.attr( "id", "states" );
d3.json( "data/counties.json", function( json )
{
counties.selectAll( "path" )
.data( topojson.feature( json, json.objects.counties ).features )
.enter()
.append( "svg:path" )
.attr( "class", function( d )
{
var p = getVote( data.vote, d.id );
return p;
})
.attr( "id", function( d )
{
return d.id;
})
.style( "fill-opacity", function( d )
{
return getOpacity( demo, d.id, 0.95 );
})
.attr( "d", path )
.on( "mousemove", showProbe )
.on( "mouseout", hideProbe )
.append( "svg:title" )
$( "#loader" ).hide();
});
d3.json( "data/states.json", function( json )
{
states.selectAll("path")
.data( topojson.feature( json, json.objects.states ).features )
.enter()
.append( "svg:path" )
.attr( "d", path );
});
}
function getVote( data, id )
{
var value = data.obj[ id ];
if( !value ) return null;
if( value > 0 )
{
return "obama";
}
else
{
return "romney";
}
}
function getOpacity( data, fips )
{
for( var i = 0; i < data.breaks.length; i++ )
{
if( data.obj[ fips ] <= data.breaks[ i ] )
{
return opacity[ i ];
}
}
}
function get_sorted( obj )
{
var arr = [];
for( var i in obj )
{
arr.push( {key: i, value: obj[ i ]} );
}
arr.sort( function( a, b ){ return ( a.value - b.value ); } )
return { keys: arr.map(function(d){return d.key}), values: arr.map(function(d){return d.value}) };
}
function not_zero( arr )
{
for( var i = 0; i < arr.length; i++ )
{
if( arr[ i ] > 0 )
{
return arr[ i ];
}
}
}
function get_quant( raw )
{
var arr = [];
var gap = raw.length / 10;
for( var i = 1; i < 10; i++ )
{
arr.push( raw[ Math.round( gap * i ) ] );
}
return arr;
}
var format = d3.format( "," );
function showProbe( d )
{
$( "#probe" ).show()
.html(
"<p>" + d.properties.NAME + " " + ( d.properties.LSAD || "County" ) + "</p>" +
"<p>Margin: " + Math.round( Math.abs( data.vote.obj[ d.id ] ) * 10000 ) / 100 + "% " + ( data.vote.obj[ d.id ] > 0 ? "Obama" : "Romney" ) + "</p>" +
"<p>" + demo.name + ": " + format( Math.round( Math.abs( demo.obj[ d.id ] ) * 100 ) / 100 ) + "</p>"
)
.css( { left : d3.event.pageX + 200 > $(window).width() ? d3.event.pageX - $("#probe").width() - 10 : d3.event.pageX + 10, top: d3.event.pageY - $("#probe").height() - 10 } );
$( "#graph-inner div[name='" + d.id + "']" ).addClass( "graph_highlight" );
}
function hideProbe( d )
{
$("#probe").hide();
$( ".graph_highlight" ).removeClass( "graph_highlight" );
var path = document.getElementById( d );
if( path ) path.setAttribute( "class", path.getAttribute( "class" ).replace( " hover", "" ) );
}