-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscience_backup.html
325 lines (281 loc) · 9.88 KB
/
science_backup.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<html>
<head>
<title>GDP vs Patents</title>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v6.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.4/themes/flick/jquery-ui.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/css/bootstrap-select.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/js/bootstrap-select.js"></script>
<style>
body {
background-color: white;
}
div.tooltip {
position: absolute;
text-align: center;
width: auto;
height: auto;
padding: 2px;
font: 12px sans-serif;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
#legend {
list-style: none;
margin-top: 30px;
width: 500px;
}
#legend span {
border: 1px solid #ccc;
float: left;
width: 12px;
height: 12px;
margin: 2px;
}
.bootstrap-select:not([class*="col-"]):not([class*="form-control"]):not(.input-group-btn) {
width: 100%;
}
.lastLabel {
float: right;
}
body {
width: 1300px;
margin: auto;
padding-bottom: 30px;
}
</style>
</head>
<body>
<h4>Select year</h4>
<p id="yearSelect"><b>Selected year:</b> <span id="yearSelected">2010</span></p>
<div id="year"></div>
1970<span class="lastLabel">2012</span>
<h4>Select Y Axis variable</h4>
<select name="yVar" id="yVar" data-live-search="true" data-live-search-style="contains"
class="selectpicker"></select>
<script>
let year = 1970;
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 600);
var height = 600;
var width = 960;
const margin = {
top: 80,
right: 0,
bottom: 5,
left: 0
};
const Y_VARS = {
"Foreign direct investment, net inflows (BoP, current US$)": true,
"Total Patents": false,
"Exports of goods and services (BoP, current US$)": false,
"Foreign direct investment, net (BoP, current US$)": false,
"Trademark applications, total": false,
}
function createDropdown() {
const select = document.getElementById("yVar");
Object.keys(Y_VARS).forEach((yVar) => {
const option = document.createElement("option");
option.text = option.value = yVar;
select.add(option);
})
}
createDropdown();
function allToFloat(data) {
data.map(d => {
for (const key in d) d[key] = !isNaN(parseFloat(d[key])) ? parseFloat(d[key]) : d[key];
if ([1970, 1980, 1990].includes(d["Year"])) {
for (const key in d) {
if (key === "Year") continue;
if (typeof d[key] === 'number') {
d[key] = d[key] / 2;
}
}
}
return d;
});
return data;
}
d3.csv("https://raw.githubusercontent.com/geetkalra/weblab/master/gdpScience.csv").then((gdpScience) => {
gdpScience = allToFloat(gdpScience);
const g7Countries = ['India', 'China', 'France', 'Germany', 'Japan', 'United Kingdom', 'United States']
// console.log(gdpScience)
// Setting parameters
let popMin = 10000, popMax = 1000000000;
let xVar = "GDP";
let yVar = Y_VARS[0];
let country = "India";
// Setting up variables that describe chart space
// const height = 500;
// const width = 600;
const margin = ({ top: 10, right: 20, bottom: 50, left: 50 });
// Create scatterplot SVG
const colorScale = (countryName) => d3.schemeTableau10[g7Countries.includes(countryName) ? 1 : 0]
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// Add year background label
const yearLabel = svg.append('text');
// Initialize points
const symbol = d3.symbol();
const g = svg.append('g');
// Initialize x-axis
let xScale = d3.scaleLog()
.domain([10000000, d3.max(gdpScience, d => d[xVar])])
// .domain(d3.extent(gdpScience, d => d[xVar]))
.range([margin.left, width - margin.right])
.nice();
const xAxisG = svg.append('g')
.attr('transform', `translate(0, ${height - margin.bottom})`);
const xAxisText = xAxisG.call(d3.axisBottom(xScale))
.append('text');
// Initialize y-axis
let yScale = d3.scaleLog()
.domain([1, d3.max(gdpScience, d => d[yVar])])
// .domain(d3.extent(gdpScience, d => d[yVar]))
.range([height - margin.bottom, margin.top])
.nice();
const yAxisG = svg.append('g')
.attr('transform', `translate(${margin.left + 30}, 0)`);
const yAxisText = yAxisG.call(d3.axisLeft(yScale))
.append('text');
function updateScatter() {
yVar = document.getElementById("yVar").value;
const data = gdpScience.filter(d => d["Year"] === year && d[xVar] >= 1 && d[yVar] >= 1);
// Draw x-axis
xScale = d3.scaleLog()
.domain([10000000, d3.max(gdpScience, d => d[xVar])])
.domain([10000000, 1e+17])
.range([margin.left, width - margin.right])
.nice();
xAxisG.call(d3.axisBottom(xScale));
xAxisText
.attr('transform', `translate(0, 40)`)
.attr('text-anchor', 'end')
.attr('fill', 'black')
.attr('font-size', '12px')
.attr('font-weight', 'bold')
.attr('x', width - margin.right)
.attr('y', -10)
.text(xVar);
// Draw y-axis
if (Y_VARS[yVar])
yScale = d3.scaleLog()
.domain([1, d3.max(gdpScience, d => d[yVar])])
.range([height - margin.bottom, margin.top])
.nice();
else
yScale = d3.scaleLinear()
.domain([1, d3.max(gdpScience, d => d[yVar])])
.range([height - margin.bottom, margin.top])
.nice();
yAxisG.call(d3.axisLeft(yScale));
yAxisText
.attr('transform', `translate(-40, ${margin.top}) rotate(-90)`)
.attr('text-anchor', 'end')
.attr('fill', 'black')
.attr('font-size', '12px')
.attr('font-weight', 'bold')
.text(yVar);
// Draw points
g
.selectAll('path')
.data(data)
.join('path')
.attr('transform', d => `translate(${xScale(d[xVar])}, ${yScale(d[yVar])})`)
.attr('fill', d => colorScale(d["Country Name"]))
.attr('d', d => symbol())
.on("mouseover", function (event, d) {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(`${d["Country Name"]}`)
.style("left", (event.pageX) + "px")
.style("background", colorScale(d["Country Name"]))
.style("top", (event.pageY - 28) + "px");
d3.select(this)
.attr("fill", "black")
.attr("d", symbol.size(64 * 4));
})
.on("mouseout", function (event, d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
d3.select(this)
.attr("fill", colorScale(d["Country Name"]))
.attr("d", symbol.size(64));
})
.on("click", function (event, d) {
country = d["Country Name"];
updatePie();
});
// Draw year label
yearLabel
.attr('x', 80)
.attr('y', height - margin.bottom - 20)
.attr('fill', '#ccc')
.attr('font-family', 'Helvetica Neue, Arial')
.attr('font-weight', 500)
.attr('font-size', 60)
.text(year);
}
document.getElementById("yVar").addEventListener("change", updateScatter);
$(function () {
$("#population").slider({
range: true,
min: 0,
max: 1000,
values: [0, 1000],
slide: function (event, ui) {
const minp = 0;
const maxp = 1000;
const minv = Math.log(10000);
const maxv = Math.log(1000000000);
const scale = (maxv - minv) / (maxp - minp);
popMin = Math.round(Math.exp(minv + scale * (ui.values[0] - minp)));
popMax = Math.round(Math.exp(minv + scale * (ui.values[1] - minp)));
document.getElementById("popSelected").innerText = `${Number(popMin).toLocaleString()} – ${Number(popMax).toLocaleString()}`
updateScatter();
}
});
});
$(function () {
$("#year").slider({
range: false,
min: 1970,
max: 2012,
value: 2010,
slide: function (event, ui) {
year = ui.value;
document.getElementById("yearSelected").innerText = `${year}`
updateScatter();
}
});
});
updateScatter();
let tickDuration = 500
// let year = 1970;
let ticker = d3.interval(
e => {
console.log("imhere")
updateScatter();
if(year == 2010) ticker.stop();
year = year+1;
// year = d3.format('.2f')((+year) + 1);
},
tickDuration/5);
// *********************************************************************
// Initialize pie graphics
// *********************************************************************
});
</script>
</body>
</html>