-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
221 lines (171 loc) · 6.77 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
220
221
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://d3js.org/topojson.v3.min.js"></script>
<style>
.state {
fill: lightgrey;
}
.graticule {
fill: none;
stroke: grey;
stroke-width: 1px;
}
.outline {
fill: none;
stroke: black;
stroke-width: 1px;
}
</style>
</head>
<body>
<form id = 'text-form'>
<input id = 'input-field' type = 'text'></input>
<!-- <input id = 'input-submit' type = 'submit'></input> -->
<button id = 'submit' type = 'button'>Submit</button>
</form>
<svg id="choropleth" height="900" width="900"></svg>
<script>
const svg = d3.select('#choropleth');
// Get the attributes of the svg
const svgWidth = svg.attr('width');
const svgHeight = svg.attr('height');
// Create choropleth map attributes
const margin = { left: 10, right: 10, top: 10, bottom: 10 };
const plotWidth = svgWidth - margin.left - margin.right;
const plotHeight = svgHeight - margin.top - margin.bottom;
const plot = svg.append('g').attr('transform', `translate(${margin.left}, ${margin.right})`);
let tweetStateData;
let freq = {};
// Create the popup
let popupWidth = 120;
let popupHeight = 50;
let momesh = plot.append('path')
.attr('class', 'mouseover outline')
.attr('d', '')
let popup = plot.append('g')
.attr('class', 'popup')
.attr('visiblity', 'hidden')
popup.append('rect')
.attr('fill', 'black')
.attr('opacity', 0.8)
.attr('x', -popupWidth / 2)
.attr('y', 0)
.attr('width', popupWidth)
.attr('height', popupHeight)
popup.style('visibility','hidden');
let stateText = popup.append('text')
.attr("fill", "white")
.attr("text-anchor","middle")
.attr("alignment-baseline","hanging")
.attr("x", 0)
.attr("y", 10)
let freqText = popup.append('text')
.attr("fill", "white")
.attr("text-anchor","middle")
.attr("alignment-baseline","hanging")
.attr("x", 0)
.attr("y", 30)
let path, data;
const makeChoropleth = async function() {
// Load twitter data for choropleth map
tweetStateData = await d3.json('twitterStateMentions.json', d3.autotype);
//console.log('DATA');
//console.log(tweetStateData);
// Modeled after topojson lecture code by Prof Rz.
data = await d3.json('us-smaller.json');
let states = topojson.feature(data, data.objects.states);
let statesMesh = topojson.mesh(data, data.objects.states);
let projection2d = d3.geoAlbersUsa().fitSize([plotWidth, plotHeight], states);
path = d3.geoPath().projection(projection2d);
let graticule = d3.geoGraticule10();
plot.append('path')
.attr('class', 'graticule')
.attr('d', path(graticule))
plot.selectAll('path.state')
.data(states.features)
.join('path')
.attr('class', 'state')
.attr('d', path)
plot.append('path')
.datum(statesMesh)
.attr('class', 'outline')
.attr('d', path)
let submitButton = d3.select('#submit')
.on('click', updateChoropleth)
let emptyData = await d3.csv('empty-states.csv', d3.autotype);
stateMap = {}
emptyData.forEach( obj => {
stateMap[obj.state_code] = obj.state_name;
})
//(emptyData);
//console.log(stateMap)
d3.selectAll('.state').on('mouseenter', function() {
return mouseEnterState(this, stateMap)
});
d3.selectAll('.state').on('mouseout', mouseExitState);
updateChoropleth(); // update once with the cumulative data
}
function mouseEnterState(obj, stateMap) {
//console.log('ENTER');
popup.style('visibility', 'visible');
popup.raise();
let currentState = d3.select(obj);
let currentStateId = currentState.datum().id;
stateText.text(stateMap[currentStateId]);
freqText.text(d3.format('.4f')(freq[currentStateId]) + '%')
//console.log(freq);
let currentStateBounds = path.bounds(currentState.datum());
// Ceter it above the current state
let xPos = (currentStateBounds[0][0] + currentStateBounds[1][0]) / 2;
let yPos = currentStateBounds[0][1] - popupHeight + 10;
popup.attr('transform', `translate(${xPos}, ${yPos})`);
//console.log(xPos, yPos);
let mo = topojson.mesh(data, data.objects.states, function(a, b) { return a.id === currentStateId || b.id === currentStateId; });
momesh.datum(mo).attr("d", path)
}
function mouseExitState() {
//('EXIT');
popup.style('visibility','hidden');
momesh.attr("d", "");
}
const updateChoropleth = async function() {
console.log("UPDATING");
console.log(tweetStateData);
let inputField = d3.select('#input-field');
let username = inputField.property('value');
if (username.length === 0) { // submit hasn't been pressed yet
username = 'total';
}
let total = Number(tweetStateData['Num_Tweets'][username]);
let emptyStateData = await d3.csv('empty-states.csv', d3.autotype);
//console.log(emptyStateData);
// Create dictionaries to store the plot data - modeled after Prof Rz's lecture
counts = {};
names = {};
freq = {};
let count;
// Update emptyStateData based on the selected user
emptyStateData.forEach( (obj) => {
count = tweetStateData[obj.state_name][username];
obj.total = count;
counts[obj.state_code] = count;
names[obj.state_code] = obj.state_name;
freq[obj.state_code] = count * 100 / total;
})
//console.log("NEW DATA");
//console.log(emptyStateData);
console.log('COUNTS');
console.log(counts);
// Styling
let colorScale = d3.scaleQuantile()
.domain(Object.values(counts))
.range(["#fff","#d1e8ed","#adc2da","#8879b3","#762b80"]);
// Color the choropleth map
plot.selectAll('path.state')
.style('fill', d => colorScale(counts[d.id]))
}
makeChoropleth();
</script>
</body>
</html>