-
Notifications
You must be signed in to change notification settings - Fork 0
/
radarChart.html
327 lines (284 loc) · 9.58 KB
/
radarChart.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
<!DOCTYPE html>
<script src="https://d3js.org/d3.v7.min.js"></script>
<html>
<header>
<img src="Pokewidia_logo.png" width=400><br>
<a href="index.html" class="button">Home</a>
<a href="list.html" class="button">List Pokémon</a>
<!--<a href="team.html" class="button">Create a team</a>-->
<!--<a href="about.html" class="button">About</a>-->
<a href="radarChart.html" class="button">Radar Chart</a>
<a href="celinaTest.html" class="button">View HP stats</a>
<a href="pokemon_select.html" class="button">Explore Pokémon</a>
</header>
<head>
<meta charset="UTF-8">
<title>Radar Chart</title>
<!-- <style>
/* Style the h2 element with a background color */#536EB580
h2 {
background-color: #CE231280;
display: inline-block;
padding: 10px;
border-radius: 10px;
}
</style> -->
</head>
<body>
<link rel="stylesheet" href="style.css">
<!-- <a href="index.html" class="button">Home</a> -->
<div style="display: flex; justify-content: center;">
<h1>Stats Comparison Example</h1>
</div>
<div style="display: flex; justify-content: center;">
<div style="width: 200px"; overflow-y: scroll>
<h2 style="background-color: #CE231280; display: inline-block; padding: 10px; border-radius: 10px;">Pokemon 1</h2>
<div>
<select id="pokemonSelect">
<option value="">Select a Pokemon</option>
</select>
<table>
<thead>
<tr>
<th>Stat</th>
<th>Value</th>
</tr>
</thead>
<tbody id="pokemonStats">
</tbody>
</table>
</div>
</div>
<div id="radarChart" style="float: left"></div>
<div style="width: 200px"; overflow-y: scroll>
<h2 style="background-color: #536EB580; display: inline-block; padding: 10px; border-radius: 10px;">Pokemon 2</h2>
<div>
<select id="pokemonSelect2">
<option value="">Select a Pokemon</option>
</select>
<table>
<thead>
<tr>
<th>Stat</th>
<th>Value</th>
</tr>
</thead>
<tbody id="pokemonStats2">
</tbody>
</table>
</div>
</div>
</div>
<script>
let maxHp = 250;
let maxAttack = 134;
let maxDefense = 180;
let maxSpeed = 150; //140 ??
let maxSpecial = 154;
let allPokemonData = [];
let data = [];
const allPokemonUrl = "https://pokeapi.co/api/v2/pokemon?limit=151";
fetch(allPokemonUrl)
.then(response => response.json())
.then(data => {
const pokemonUrls = data.results.map(result => result.url);
const pokemonPromises = pokemonUrls.map(url => fetch(url).then(response => response.json()));
Promise.all(pokemonPromises)
.then(pokemonData => {
allPokemonData = pokemonData.map(pokemon => ({
name: pokemon.name,
id: pokemon.id,
types: pokemon.types.map(type => type.type.name),
stats: pokemon.stats.map(stat => ({
name: stat.stat.name,
value: stat.base_stat
})),
hp: pokemon.stats.find(stat => stat.stat.name === "hp").base_stat,
attack: pokemon.stats.find(stat => stat.stat.name === "attack").base_stat,
defense: pokemon.stats.find(stat => stat.stat.name === "defense").base_stat,
speed: pokemon.stats.find(stat => stat.stat.name === "speed").base_stat,
specialAttack: pokemon.stats.find(stat => stat.stat.name === "special-attack").base_stat,
sprite: pokemon.sprites.front_default
}));
// Get the first dropdown list element
const pokemonSelect = document.getElementById("pokemonSelect");
// Get the second dropdown list element
const pokemonSelect2 = document.getElementById("pokemonSelect2");
// Loop through the pokemon data and add options to both dropdown lists
allPokemonData.forEach(pokemon => {
// Create a new option element
const option = document.createElement("option");
// Set the value and text of the option element
option.value = pokemon.name;
option.text = pokemon.name;
// Add the option to the first dropdown list
pokemonSelect.add(option);
// Clone the option element and add it to the second dropdown list
const clonedOption = option.cloneNode(true);
pokemonSelect2.add(clonedOption);
});
let data = [{HP: 0, ATT: 0, SPD: 0, SP: 0, DEF: 0}, {HP: 0, ATT: 0, SPD: 0, SP: 0, DEF: 0}];
pokemonSelect.addEventListener("change", event => {
const selectedPokemon = allPokemonData.find(pokemon => pokemon.name === event.target.value);
<!-- let temp = data[1]; -->
data[0] = {
HP: selectedPokemon.stats[0].value / maxHp * 100,
ATT: selectedPokemon.stats[1].value / maxAttack * 100,
SPD: selectedPokemon.stats[5].value / maxSpeed * 100,
SP: selectedPokemon.stats[3].value / maxSpecial * 100,
DEF: selectedPokemon.stats[2].value / maxDefense * 100
};
updateRadarChart(data);
const pokemonStats = document.getElementById("pokemonStats");
pokemonStats.innerHTML = "";
Object.entries(selectedPokemon).forEach(([key, value]) => {
if (["name", "stats", "sprite"].includes(key)) {
return;
}
const row = document.createElement("tr");
const statName = document.createElement("td");
statName.textContent = key;
const statValue = document.createElement("td");
statValue.textContent = value;
row.appendChild(statName);
row.appendChild(statValue);
pokemonStats.appendChild(row);
});
});
pokemonSelect2.addEventListener("change", event => {
const selectedPokemon2 = allPokemonData.find(pokemon => pokemon.name === event.target.value);
data[1] = {
HP: selectedPokemon2.stats[0].value / maxHp * 100,
ATT: selectedPokemon2.stats[1].value / maxAttack * 100,
SPD: selectedPokemon2.stats[5].value / maxSpeed * 100,
SP: selectedPokemon2.stats[3].value / maxSpecial * 100,
DEF: selectedPokemon2.stats[2].value / maxDefense * 100
};
updateRadarChart(data);
const pokemonStats2 = document.getElementById("pokemonStats2");
pokemonStats2.innerHTML = "";
Object.entries(selectedPokemon2).forEach(([key, value]) => {
if (["name", "stats", "sprite"].includes(key)) {
return;
}
const row = document.createElement("tr");
const statName = document.createElement("td");
statName.textContent = key;
const statValue = document.createElement("td");
statValue.textContent = value;
row.appendChild(statName);
row.appendChild(statValue);
pokemonStats2.appendChild(row);
});
});
})
.catch(error => console.error(error));
})
.catch(error => console.error(error));
</script>
<script>
let features = ["HP", "ATT", "SPD", "SP", "DEF"];
//generate the data
let width = 600;
let height = 600;
let svg = d3.select("#radarChart").append("svg")
.attr("width", width)
.attr("height", height);
let radialScale = d3.scaleLinear()
.domain([0, 100])
.range([0, 100]);
let ticks = [20, 40, 60, 80, 100];
let ticks2 = [1, 2, 3, 4, 5];
svg.selectAll("circle")
.data(ticks)
.join(
enter => enter.append("circle")
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("fill", "none")
.attr("stroke", "gray")
.attr("r", d => radialScale(d))
);
svg.selectAll(".ticklabel")
.data(ticks2)
.join(
enter => enter.append("text")
.attr("class", "ticklabel")
.attr("x", width / 2 + 5)
.attr("y", d => height / 2 - radialScale(d * 20)) //d
.text(d => d.toString())
);
function angleToCoordinate(angle, value) {
let x = Math.cos(angle) * radialScale(value);
let y = Math.sin(angle) * radialScale(value);
return { "x": width / 2 + x, "y": height / 2 - y };
}
let featureData = features.map((f, i) => {
let angle = (Math.PI / 2) + (2 * Math.PI * i / features.length);
return {
"name": f,
"angle": angle,
"line_coord": angleToCoordinate(angle, 100),
"label_coord": angleToCoordinate(angle, 140)
};
});
// draw axis line
svg.selectAll("line")
.data(featureData)
.join(
enter => enter.append("line")
.attr("x1", width / 2)
.attr("y1", height / 2)
.attr("x2", d => d.line_coord.x)
.attr("y2", d => d.line_coord.y)
.attr("stroke", "black")
);
// draw axis label
svg.selectAll(".axislabel")
.data(featureData)
.join(
enter => enter.append("text")
.attr("x", d => d.label_coord.x)
.attr("y", d => d.label_coord.y)
.text(d => d.name)
);
function getPathCoordinates(data_point) {
let coordinates = [];
for (var i = 0; i < features.length; i++) {
let ft_name = features[i];
let angle = (Math.PI / 2) + (2 * Math.PI * i / features.length);
coordinates.push(angleToCoordinate(angle, data_point[ft_name]));
}
return coordinates;
}
function updateRadarChart(data) {
let line = d3.line()
.x(d => d.x)
.y(d => d.y);
let colors = ["#ce2312", "#536eb5", "navy"];
// JOIN data with existing paths
let paths = svg.selectAll("path")
.data(data);
// UPDATE existing paths
paths.attr("d", d => line(getPathCoordinates(d)))
.attr("stroke-width", 3)
.attr("stroke", (_, i) => colors[i])
.attr("fill", (_, i) => colors[i])
.attr("stroke-opacity", 1)
.attr("opacity", 0.5);
// ENTER new paths
paths.enter().append("path")
.datum(d => getPathCoordinates(d))
.attr("d", line)
.attr("stroke-width", 3)
.attr("stroke", (_, i) => colors[i])
.attr("fill", (_, i) => colors[i])
.attr("stroke-opacity", 1)
.attr("opacity", 0.5);
// EXIT old paths
paths.exit().remove();
}
<!-- updateRadarChart(data); -->
</script>
</body>
</html>