-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
219 lines (187 loc) · 6.92 KB
/
index.html
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="image" property="og:image" content="preview.png">
<meta name="description" property="og:description" content="D3 scatterplot of 2020 United States election results by median income per county.">
<meta name="author" content="Hayden Linder">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2020 United States Election Results by Income</title>
<link href="App.css" rel="stylesheet">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="merged-data-flat.js"></script>
</head>
<body class="App">
<div class="App-header">
<p>
2020 United States Election Results by 2019 Median Household Income per County
</p>
<div class='charts-container'>
<div class="tooltip">Info</div>
<svg height=600 width=300>
</svg>
<br />
<svg height=600 width=300 class='b'>
</svg>
</div>
<div>% vote is calculated using majority/(Biden+Trump)</div>
<br>
<a href="merged-data-flat.json" download='merged-data-flat.json' class="App-link">
Download merged data
</a>
<br />
income data: <a class="App-link" href="https://www.ers.usda.gov/data-products/county-level-data-sets/download-data/"
target="_blank" rel="noopener noreferrer">
https://www.ers.usda.gov/data-products/county-level-data-sets/download-data/
</a>
<br />
election data: <a class="App-link" href="https://embeds.ddhq.io/api/v2/2020presidential" target="_blank"
rel="noopener noreferrer">
https://embeds.ddhq.io/api/v2/2020presidential
</a>
<br>
github: <a class="App-link" href="https://github.com/haydenlinder/2020-election-by-income" target="_blank"
rel="noopener noreferrer">
https://github.com/haydenlinder/2020-election-by-income
</a>
</div>
</body>
<script >
var margin = { top: 30, right: 30, bottom: 60, left: 80 }
var w = 300 - margin.left - margin.right
var h = 600 - margin.top - margin.bottom
function draw() {
let maxX = 0, maxY = 0
var tooltip = document.querySelector('.tooltip')
data.forEach(county => {
var { biden, trump, income } = county
var p = Math.max(biden, trump) / (biden + trump)
maxX = p > maxX ? p : maxX
maxY = income > maxY ? income : maxY
})
maxY += 20000
maxX = 1
var svg1 = d3.select('svg')
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
var svg2 = d3.select('svg.b')
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
data.forEach(item => {
var { biden, trump, state, county, income } = item
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0
});
var winner = biden >= trump ? 'Biden' : 'Trump'
var stroke = biden >= trump ? 'blue' : 'red'
var fill = biden >= trump ? 'lightblue' : 'pink'
var svg = biden >= trump ? svg1 : svg2
var p = Math.max(biden, trump) / (biden + trump)
function startToolTip() {
var d = d3.event
d3.select(this).raise()
d.target.setAttribute('fill-opacity', 1)
d.target.fillOpacity = 0.5
tooltip.style.visibility = 'visible'
tooltip.innerHTML = `
State: ${state}
<br/>
County: ${county}
<br/>
Median Income: ${formatter.format(income)}
<br/>
Biden Votes: ${biden}
<br/>
Trump Votes: ${trump}
<br/>
% in favor of ${winner}: ${(100 * p).toFixed(2)}%
`
}
function moveToolTip () {
var d = d3.event
tooltip.style.left = d.pageX + 25 + 'px'
tooltip.style.top = d.pageY + 15 + 'px'
}
function endToolTip () {
var d = d3.event
tooltip.style.visibility = 'hidden'
d.target.setAttribute('fill-opacity', 0.1)
}
svg
.append('circle')
.attr('cx', (w / 0.6) * (p - 0.4))
.attr('cy', h - (income * h / maxY))
.attr('r', 10)
.attr('fill', fill)
.attr('fill-opacity', 0.1)
.attr('stroke', stroke)
.attr('stroke-width', 0.5)
.on('mouseover', startToolTip)
.on('touchstart', startToolTip)
.on('mousemove', moveToolTip)
.on('touchmove', moveToolTip)
.on('mouseout', endToolTip)
.on('touchend', endToolTip)
})
var x = d3.scaleLinear()
.domain([40, 100])
.range([0, w])
var y = d3.scaleLinear()
.domain([0, maxY])
.range([0, -h])
svg1.append("g")
.attr("transform", `translate(0, ${h})`)
.call(d3.axisBottom(x))
svg1.append("g")
.attr("transform", `translate(0, ${h})`)
.call(d3.axisLeft(y))
svg1.append('text')
.attr("class", "label")
.attr("text-anchor", "middle")
.attr("y", -55)
.attr("x", -h / 2)
.attr("transform", "rotate(-90)")
.text("2019 Median Household Income (USD)")
svg1.append('text')
.attr("class", "label")
.attr("text-anchor", "middle")
.attr("y", h+40)
.attr("x", w/2)
.text("% vote for Biden 2020")
svg1.append('text')
.attr("class", "label")
.attr("text-anchor", "middle")
.attr("y", 10)
.attr("x", w/2)
.text("Majority Biden")
svg2.append("g")
.attr("transform", "translate(0," + h + ")")
.call(d3.axisBottom(x))
svg2.append("g")
.attr("transform", "translate(0," + h + ")")
.call(d3.axisLeft(y))
svg2.append('text')
.attr("class", "label")
.attr("text-anchor", "middle")
.attr("y", -55)
.attr("x", -h / 2)
.attr("transform", "rotate(-90)")
.text("2019 Median Household Income (USD)")
svg2.append('text')
.attr("class", "label")
.attr("text-anchor", "middle")
.attr("y", h + 40)
.attr("x", w / 2)
.text("% vote for Trump 2020")
svg2.append('text')
.attr("class", "label")
.attr("text-anchor", "middle")
.attr("y", 10)
.attr("x", w / 2)
.text("Majority Trump")
}
draw()
</script>
</html>