-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatent.js
285 lines (223 loc) · 9.73 KB
/
patent.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
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
function patents() {
var margin = {top: 50, right: 80, bottom: 250, left: 80},
width = 1000 - margin.left - margin.right,
height = 950 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#vis")
.select("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
d3.csv("https://raw.githubusercontent.com/6859-sp21/a4-worldbankdata/main/gdpPERcapita2.csv")
.then(function(data) {
// entries = data.columns.slice(1)
allCountries = data.columns.slice(1)
d3.select("#selectButton")
.selectAll('myOptions')
.data(allCountries)
.enter()
.append('option')
.text(function (d,i) { return i+1+"."+d; }) // text showed in the menu
.attr("value", function (d) { return d; })
entries = ["India"];
update()
function update(){
// let svg = d3.select("#vis").select("svg")
svg.selectAll("*").remove()
var cities = entries.map(function(id) {
return {
id: id,
values: data.map(function(d) {
return {date: d.date, gdpPercapita: parseInt(d[id]) || 0};
})
};
});
// console.log(cities)
// Scale X - time scale
// Scale Y - linear scale
// Scale Z - color categorical scale
var x = d3.scaleLinear().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
z = d3.scaleOrdinal(d3.schemeCategory10);
// D3 Line generator with curveBasis being the interpolator
var line = d3.line()
.curve(d3.curveLinear)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.gdpPercapita); });
// Using the initial data figure out the min / max dates
x.domain(d3.extent(data, function(d) { return d.date; }))
.nice();
y.domain([
d3.min(cities, function(c) { return d3.min(c.values, function(d) { return d.gdpPercapita; }); }),
d3.max(cities, function(c) { return d3.max(c.values, function(d) { return d.gdpPercapita; }); })
])
.nice();
// y.domain([120,9000]);
z.domain(cities.map(function(c) { return c.id; }));
// Create X Axis
svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).tickFormat(d3.format("d")))
.append('text')
.attr('text-anchor', 'end')
.attr('fill', 'black')
.attr('font-size', '24px')
.attr('font-weight', 'bold')
.attr('x', width )
.attr('y', 50)
.text('Year');
// Create Y Axis
// Add Text label to Y axis
// svg.selectAll("#axis axis--y"").remove();
svg.append("g")
.attr("class", "axis axis--y")
.attr("transform", "translate(0,0)")
// .transition()
// .duration(500)
.call(d3.axisLeft(y))
// svg.selectAll("axis axis--y")
.append("text")
.attr("transform", "rotate(-90)")
.attr('y', 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.attr('font-size', '24px')
.attr('font-weight', 'bold')
.text("GDP Per Capita (US $)");
// Create a <g> element for each city
var city = svg.selectAll(".city")
.data(cities)
.enter().append("g")
.attr("class", "city");
// Add a legend at the end of each line if there are multiple lines
if (cities.length > 1){
city.append("text")
.datum(function(d) { return {id: d.id, value: d.values[d.values.length - 1]}; })
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.gdpPercapita) + ")"; })
.attr("x", -60)
.attr("y", -10)
.attr("dy", "0.35em")
.style("font", "16px sans-serif")
.attr("fill", function(d) { return z(d.id); })
.text(function(d) { return d.id; });
}
if (cities.length == 1){
const yearLabel = svg.append('text')
.attr('x', 40)
.attr('y', margin.bottom + margin.top )
.attr('fill', '#ccc')
.attr('font-family', 'Helvetica Neue, Arial')
.attr('font-weight', 500)
.attr('font-size', 80)
.text(cities[0].id+"*");
}
// Create a <path> element inside of each city <g>
// Use line generator function to convert 366 data points into SVG path string
var path = city.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return z(d.id); })
path.attr("stroke-dasharray", function(d) {
// Get the path length of the current element
const pathLength = this.getTotalLength();
return `0 ${pathLength}`
})
.transition()
.duration(2500)
.attr("stroke-dasharray", function(d) {
// Get the path length of the current element
const pathLength = this.getTotalLength();
return `${pathLength} ${pathLength}`
});
path.on("click", function(d,i){
const index = entries.indexOf(d.id);
if (index > -1) {
entries.splice(index, 1);
update();
}
// entries.splice(i, 1);
});
// .append("div")
// .attr("class","tooltip")
// .style("opacity",0);
const symbol = d3.symbol();
// Add points at each location to provide tooltips
svg.selectAll("myDots")
.data(cities,function(d){console.log(d)})
.enter()
.append('g')
.attr("class", "myDots")
// .style("fill", function(d){ return z(d.id) })
// Second we need to enter in the 'values' part of this group
.selectAll("myPoints")
.data(function(d){
place = d.id
for (ii in d.values) {
d.values[ii].country = place;
}
return d.values })
.enter()
.append("circle")
.attr("class", "hiddencircles")
.attr("cx", function(d) { return x(d.date) } )
.attr("cy", function(d) { return y(d.gdpPercapita); } )
.attr("r", 5)
.style("opacity",0)
.attr("stroke", "white")
.on("mouseover",function(d,i){
d3.select(this).style("opacity",1);
d3.select('#tooltip')
.style('left', (d3.event.pageX + 10)+ 'px')
.style('top', (d3.event.pageY - 25) + 'px')
.style('display', 'inline-block')
.style("background", z(d.country))
.html("Country: " + d.country + "<br/> GDP Per Capita (US $): " + d.gdpPercapita + "<br/> Year: " + d.date)
d3.select(this)
.attr("fill", "black")
.attr("d", symbol.size(64 * 4));
})
.on("mouseout", function(d,i) {
d3.select(this).style("opacity",0);
d3.select('#tooltip')
.style('display', 'none')
// tooltip.transition()
// .duration(500)
// .style("opacity", 0);
d3.select(this)
.attr("fill", z(d.country))
.attr("d", symbol.size(64));
});
}//update function ends here
d3.select("#add-btn").on("click", function(e){
// entries = ["India"]
entries.push("India")
//Makes sure India doesn't get added multiple times to the
const unique = [...new Set(entries)];
entries = unique
update()
});
d3.select("#replay").on("click", function(e){
entries = ["India"]
update()
});
d3.select("#topten").on("click", function(e){
entries = ["Monaco","Luxembourg","Bermuda","India"]//,"Norway","Switzerland"] //,"Ireland","Denmark","Qatar","Singapore","Macao SAR, China"]
update()
});
d3.select("#lowerten").on("click", function(e){
entries = ["Burundi","Central African Republic","India"]
update()
});
d3.select("#selectButton").on("change", function(d) {
// recover the option that has been chosen
var selectedOption = d3.select(this).property("value")
// run the updateChart function with this selected option
entries = [selectedOption]
update()
})
}
);
}