-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.html
165 lines (137 loc) · 6.66 KB
/
list.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
<!DOCTYPE html>
<html>
<head>
<title>Pokemon list</title>
</head>
<body>
<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>
<a href="demo.html" class="button">Demo</a>
</header>
<h1>Explore the pokémon from generation 1</h1>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://unpkg.com/pokeapi-js-wrapper/dist/index.js"></script>
<link rel="stylesheet" href="style.css">
<!--script that pulls images from pokeapi and puts them in table-->
<script>
async function get_pokemons(n) {
response1 = await fetch("https://pokeapi.co/api/v2/pokemon?limit=" + n + "&offset=0")
response1_json = await response1.json()
return response1_json.results
}
async function get_pokemon_species(pokemon) {
response1 = await fetch("https://pokeapi.co/api/v2/pokemon-species/" + pokemon)
response1_json = await response1.json()
return response1_json
}
async function get_pokemon(pokemon) {
response2 = await fetch("https://pokeapi.co/api/v2/pokemon/" + pokemon)
response2_json = await response2.json()
return response2_json
}
async function get_pokemon_type(pokemon) {
response3 = await fetch("https://pokeapi.co/api/v2/pokemon/" + pokemon)
response3_json = await response3.json()
return response3_json.types[0].type.name
}
async function get_pokemon_color(pokemon) {
response3 = await fetch("https://pokeapi.co/api/v2/pokemon-species/" + pokemon)
response3_json = await response3.json()
return response3_json.color
}
async function get_pokemon_image(pokemon) {
response3 = await fetch("https://pokeapi.co/api/v2/pokemon/" + pokemon)
response3_json = await response3.json()
return response3_json.sprites.front_default
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
(async () => {
var loading = d3.select('body').append('h2').text('Loading table...')
const list_names = await get_pokemons(151)
var list = new Map()
var species_list = new Map()
for (let i = 0; i < list_names.length; i++) {
res = await get_pokemon(list_names[i].name)
res2 = await get_pokemon_species(list_names[i].name)
list[list_names[i].name] = res
species_list[list_names[i].name] = res2
}
pokemon_images = new Map()
types_pokemon_map = new Map()
type_colors = new Map()
for (let i = 0; i < list_names.length; i++) {
pokemon_images[list_names[i].name] = list[list_names[i].name].sprites.other.dream_world.front_default
}
for (let i = 0; i < list_names.length; i++) {
res = list[list_names[i].name].types[0].type.name
if (types_pokemon_map[res] == undefined) {
types_pokemon_map[res] = []
types_pokemon_map[res].push(list_names[i].name)
} else {
types_pokemon_map[res].push(list_names[i].name)
}
}
for (let i = 0; i < Object.keys(types_pokemon_map).length; i++){
first = types_pokemon_map[Object.keys(types_pokemon_map)[i]][0]
res = await get_pokemon_color(first)
type_colors[Object.keys(types_pokemon_map)[i]] = res.name
}
var table = d3.select('body').append('table').attr("class", "exploreTable")
var thead = table.append('thead')
var tbody = table.append('tbody')
// add key to first column and values to other columns
thead.append('tr')
.selectAll('th')
.data(['Type', 'Pokemons'])
.enter()
.append('th')
.text(function (column) { return column; });
// create a row for each object in the data
var rows = tbody.selectAll()
.data(Object.keys(types_pokemon_map))
.enter()
.append('tr').attr("style", function (d) { return "background-color: " + type_colors[d];});
// create a tooltip
var tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("visibility", "hidden")
.style("border", "1px solid black")
.style("background-color", "white")
.style("font-size", "20px")
.html("");
// create a cell in each row for each column
var type_cell = rows.selectAll()
.data(function (row) {
return [row]
})
.enter()
.append('td')
.attr("class", "expoleTableTd")
.html(function (d) { return capitalizeFirstLetter(d); });
var pokemon_cell = rows.selectAll()
.data(function (row) {
return types_pokemon_map[row]
})
.enter()
.append('td')
.attr("class", "expoleTableTd")
.html(function (d) { return "<img id = \"pokemon_img\" src="+pokemon_images[d] +">"; })
var images = d3.selectAll("#pokemon_img").attr("width", "50px").attr("height", "50px")
.on("mouseover", function (d) {tooltip.style("visibility", "visible"); })
.on("mousemove", function (d) {tooltip.html(capitalizeFirstLetter(d.srcElement.parentElement.__data__) + "<br>Height: " + list[d.srcElement.parentElement.__data__].height + "dm<br>Weight: " + list[d.srcElement.parentElement.__data__].weight + "hg");console.log(d.srcElement.parentElement.__data__); tooltip.style("top", (event.pageY+20) + "px").style("left", (event.pageX+20) + "px"); })
.on("mouseout", function (d) {tooltip.style("visibility", "hidden"); });
loading.remove()
})();
</script>
</body>
</html>