-
Notifications
You must be signed in to change notification settings - Fork 0
/
orientedCircles.html
397 lines (357 loc) · 14.7 KB
/
orientedCircles.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CPSC 583 Group Project: Implementation</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="OrientedCircleStyle.css">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="radar-chart.js"></script>
</head>
<body>
<h1>Pokemon: "Extended" Pokedex</h1>
<div id="div_page_desc">
<p>Encompasses Pokemon and which type they fall under: Bug, Dark, Dragon, etc. <br>
In addition, which generation each Pokemon is mapped along the y-axis, and their respective strengths are mapped through sizing.
</p>
</div>
<div id="div_visuals" style="min-height: 0px;">
<div id="div_data_desc">
<div class='my-legend'>
<div class='legend-title'>All Pokemon Types</div>
<div class='legend-colours'>
<ul class='legend-labels'>
<li><span style='background:#A8B820;'></span>Bug</li>
<li><span style='background:#705848;'></span>Dark</li>
<li><span style='background:#7038F8;'></span>Dragon</li>
<li><span style='background:#F8D030;'></span>Electric</li>
<li><span style='background:#EE99AC;'></span>Fairy</li>
<li><span style='background:#C03028;'></span>Fighting</li>
<li><span style='background:#F08030;'></span>Fire</li>
<li><span style='background:#A890F0;'></span>Flying</li>
<li><span style='background:#705898;'></span>Ghost</li>
<li><span style='background:#78C850;'></span>Grass</li>
<li><span style='background:#E0C068;'></span>Ground</li>
<li><span style='background:#98D8D8;'></span>Ice</li>
<li><span style='background:#A8A878;'></span>Normal</li>
<li><span style='background:#A040A0;'></span>Poison</li>
<li><span style='background:#F85888;'></span>Psychic</li>
<li><span style='background:#B8A038;'></span>Rock</li>
<li><span style='background:#B8B8D0;'></span>Steel</li>
<li><span style='background:#6890F0;'></span>Water</li>
</ul>
</div>
</div>
</div>
<div id="my_dataviz"></div>
<div id="radar_chart"><div/>
<div id="interaction_wrapper" style="float: right;">
<div id="search" style="padding: 10px">
<b style="color:black">Search For Pokemon:</b> <input type="text" id="searchValue" value="" placeholder="Enter Pokemon's Name…">
</div>
<div id="toggle" style="padding: 10px">
<button type="button" id="toggleButton" style="background-color: darkturquoise;">Toggle Types: Primary</button>
</div>
</div>
</div>
</div>
<script>
let width = 1624;
let height = 560
let radius = 10;
var primaryTyping = true;
// append the svg object to the body of the page
let svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", "150%")
.attr("height", height+10)
.style("margin-left", "10px")
.style("margin-right", "10px")
var radarQueue = [];
let emptyOptions = [{
axes: [
{axis: "HP"},
{axis: "Attack"},
{axis: "Defense"},
{axis: "Special Attack"},
{axis: "Special Defense"},
{axis: "Speed"}
]}
]
var radarChartOptions = {
w: 900,
h: 350,
levels: 5,
labelFactor: 1.05,
format: '.0f',
dotRadius: 6,
color: d3.scaleOrdinal().range(["#4ac53a", "#ff6600", "#2a2fd4"])
};
var svg_radar1 = RadarChart("#radar_chart", emptyOptions, radarChartOptions);
// create dummy data -> just one element per circle
d3.csv("Pokemon.csv", function(d) {
return {
name: d.Name,
type: d.Type1,
type2: d.Type2,
gen: d.Generation,
strength: d.Total,
hp: d.HP,
attack: d.Attack,
defense: d.Defense,
spAtk: d.SpAtk,
spDef: d.SpDef,
speed: d.Speed
};
}).then(function(internalData) {
let data = internalData;
// ** SETTING UP SCALES ** //
// x position
let x = d3.scaleOrdinal()
.domain(["Bug", "Dark", "Dragon", "Electric", "Fairy", "Fighting", "Fire", "Flying", "Ghost",
"Grass", "Ground", "Ice", "Normal", "Poison", "Psychic", "Rock", "Steel", "Water"])
.range([fractionalScale(0), fractionalScale(1), fractionalScale(2), fractionalScale(3), fractionalScale(4),
fractionalScale(5), fractionalScale(6), fractionalScale(7), fractionalScale(8), fractionalScale(9),
fractionalScale(10), fractionalScale(11), fractionalScale(12), fractionalScale(13), fractionalScale(14),
fractionalScale(15), fractionalScale(16), fractionalScale(17)])
// y position
let y = d3.scaleLinear()
.domain([6,1])
.range([20,520])
// A color scale
let color = d3.scaleOrdinal()
.domain(["Bug", "Dark", "Dragon", "Electric", "Fairy", "Fighting", "Fire", "Flying", "Ghost",
"Grass", "Ground", "Ice", "Normal", "Poison", "Psychic", "Rock", "Steel", "Water"])
.range(['#A8B820', '#705848', '#7038F8', '#F8D030', '#EE99AC',
'#C03028', '#F08030', '#A890F0', '#705898', '#78C850',
'#E0C068', '#98D8D8', '#A8A878', '#A040A0', '#F85888',
'#B8A038', '#B8B8D0', '#6890F0'])
// A size scale
let size = d3.scaleLinear()
.domain([200, 800])
.range([5, 15]);
d3.select("#toggleButton").on("click", toggleTypes)
d3.select("#searchValue")
.attr("transform", "translate(100,100)")
.on("input", searchForPokemon)
var simulation = d3.forceSimulation()
createSimulation()
function createSimulation() {
setupAxes()
// Initialize the circle: all located at the center of the svg area
let node = svg.append("g")
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("r", function(d) { return size(d.strength)})
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("id", function(d) { return d.name }) // to select nodes by ID
.style("fill", function(d){
if(primaryTyping)
return color(d.type)
else {
if(d.type2 !== "") {
return color(d.type2)
}
else
return color(d.type)
}
})
.attr("stroke", "white")
.style("stroke-width", 2)
.on("click", clicked)
.call(d3.drag() // call specific function when circle is dragged
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
// Features of the forces applied to the nodes:
simulation.force("x", d3.forceX().strength(2.5).x( function(d){
if(primaryTyping)
return x(d.type)
else {
if(d.type2 !== "")
return x(d.type2)
else
return x(d.type)
}
}))
.force("y", d3.forceY().strength(2.5).y( function(d){ return y(parseInt(d.gen))}))
.force("charge", d3.forceManyBody().strength(10)) // Nodes are attracted one each other of value is > 0
.force("collide", d3.forceCollide().strength(.5).radius(function(d){return size(d.strength) + 3 }).iterations(2)) // Force that avoids circle overlapping
// Apply these forces to the nodes and update their positions.
// Once the force algorithm is happy with positions ('alpha' value is low enough), simulations will stop.
simulation
.nodes(data)
.on("tick", function(d){
node
.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
})
.alphaTarget(.015);
}
function setupAxes() {
var yRange = d3.scaleLinear().domain([1,6])
.range([height,0]);
var y_axis = d3.axisRight().scale(yRange).ticks(6);
svg.append("g")
.attr("transform", "translate(1660,5)")
.attr("height", height)
.call(y_axis);
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 1680)
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.style("font-family", "'Major Mono Display' , sans-serif")
.style("font-size", "18px")
.style("font-weight", "bold")
.style("margin-right", "20px")
.text("Generation");
}
// What happens when a circle is dragged?
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(.01).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(.01);
d.fx = null;
d.fy = null;
}
function clicked(d, i) {
if (d3.event.defaultPrevented) return; // dragged
var node = null
// Node is not present in our queue
if(!radarQueue.includes(d)) {
// Max size reached, remove first added element
if(radarQueue.length > 1) {
node = selectNodeByName(radarQueue.shift().name)
highlightNode(node, false)
}
radarQueue.push(d)
node = selectNodeByName(d.name)
highlightNode(node, true)
}
// Node is present and will be removed from the queue
else {
var index = radarQueue.indexOf(d);
if (index > -1) {
radarQueue.splice(index, 1);
node = selectNodeByName(d.name)
highlightNode(node, false)
}
}
// Radar ends up being empty
if (radarQueue === undefined || radarQueue.length == 0) {
radarChartOptions.legend = false
svg_radar1 = RadarChart("#radar_chart", emptyOptions, radarChartOptions)
}
else {
radarChartOptions.legend = {title: "Pokemon", translateX: -675, translateY: 25 }
var radar = generateRadarChartParams(radarQueue)
svg_radar1 = RadarChart("#radar_chart", radar, radarChartOptions);
}
}
function toggleTypes() {
if(primaryTyping) {
d3.select(this).text("Toggle Types: Secondary");
}
else {
d3.select(this).text("Toggle Types: Primary");
}
var newTyping = !primaryTyping
primaryTyping = newTyping
svg.selectAll("*").remove()
createSimulation()
radarQueue = []
}
})
function generateRadarChartParams(queue) {
var radarParams = []
for(let i in queue) {
radarParams.push({
name: queue[i].name,
axes: [
{axis: "HP", value: queue[i].hp},
{axis: "Attack", value: queue[i].attack},
{axis: "Defense", value: queue[i].defense},
{axis: "Special Attack", value: queue[i].spAtk},
{axis: "Special Defense", value: queue[i].spDef},
{axis: "Speed", value: queue[i].speed}
]
})
}
return radarParams
}
function selectNodeByName(name) {
var node = d3.selectAll("circle").filter(function (d) {
if(d.name === name) {
return d.name === name
}
})
return node
}
function highlightNode(node, highlight) {
var radius = getNodeRadius(node)
if(highlight) {
node.attr("r", radius * 1.5)
.attr("stroke", "black")
.attr("stroke-width", "4px")
}
else {
node.attr("r", radius / 1.5)
.attr("stroke", "white")
.attr("stroke-width", "2px")
}
}
function getNodeRadius(node) {
return node.attr("r")
}
function fractionalScale(numerator) {
if(numerator === 0)
return 50
else
return (numerator/18 * width) + 55;
return (numerator/18 * width) + 55;
}
function searchForPokemon() {
// A color scale
let color = d3.scaleOrdinal()
.domain(["Bug", "Dark", "Dragon", "Electric", "Fairy", "Fighting", "Fire", "Flying", "Ghost",
"Grass", "Ground", "Ice", "Normal", "Poison", "Psychic", "Rock", "Steel", "Water"])
.range(['#A8B820', '#705848', '#7038F8', '#F8D030', '#EE99AC',
'#C03028', '#F08030', '#A890F0', '#705898', '#78C850',
'#E0C068', '#98D8D8', '#A8A878', '#A040A0', '#F85888',
'#B8A038', '#B8B8D0', '#6890F0'])
let search = this.value.toLowerCase()
svg.selectAll("circle").filter(function (d) {
console.log(this.value)
var node = selectNodeByName(d.name)
if(d.name.toLowerCase().includes(search)) {
node.style("fill", function(d){
if(primaryTyping)
return color(d.type)
else {
if(d.type2 !== "")
return color(d.type2)
else
return color(d.type)
}
})
}
else {
node.style("fill", "#808080")
}
})
}
</script>
</body>
</html>