-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider_chart.html
166 lines (153 loc) · 5.5 KB
/
spider_chart.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
<!DOCTYPE html>
<html>
<head>
<title>Gráfica de Telaraña con D3.js</title>
<meta charset="utf-8" />
<meta http-equiv="Permissions-Policy" content="interest-cohort=()" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap"
/>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<svg id="chart" width="190" height="190" style="padding: 0px"></svg>
<script>
// Carga el archivo CSV
d3.csv("data.csv")
.then((data) => {
const countryCounts = {};
data.forEach((d) => {
const country = d.País;
countryCounts[country] = (countryCounts[country] || 0) + 1;
});
// Obtener el top 10 de más repetidos
const topCountries = Object.keys(countryCounts)
.sort((a, b) => countryCounts[b] - countryCounts[a])
.slice(0, 10);
// Obtener la mayor cantidad de repeticiones
const maxCount = d3.max(topCountries.map((d) => countryCounts[d]));
// Representar la gráfica de telaraña
const width = 190;
const height = 190;
const svg = d3.select("#chart");
// Escalas
const angleScale = d3
.scaleBand()
.domain(topCountries)
.range([0, Math.PI * 2]);
const radiusScale = d3
.scaleLinear()
.domain([0, maxCount])
.range([0, height / 3]);
// Calcular el total de repeticiones
const totalCount = topCountries.reduce(
(acc, country) => acc + countryCounts[country],
0
);
// Crear los ejes radiales
const axes = svg
.append("g")
.attr("transform", `translate(${width / 2},${height / 2})`);
axes
.selectAll(".axis")
.data(topCountries)
.enter()
.append("line")
.attr("class", "axis")
.attr("x1", 0)
.attr("y1", 0)
.attr(
"x2",
(d) =>
radiusScale(maxCount) * Math.cos(angleScale(d) - Math.PI / 2)
)
.attr(
"y2",
(d) =>
radiusScale(maxCount) * Math.sin(angleScale(d) - Math.PI / 2)
)
.style("stroke", "#2c2c2c")
.style("stroke-width", "3px")
.style("opacity", "0.4");
// Crear los ejes circulares
const circles = svg
.append("g")
.attr("transform", `translate(${width / 2},${height / 2})`);
circles
.selectAll(".circle-axis")
.data(d3.range(1, 11)) // Crear 10 ejes circulares (del 10% al 100%)
.enter()
.append("circle")
.attr("class", "circle-axis")
.attr("cx", 0)
.attr("cy", 0)
.attr("r", (d) => {
const circleRadius = radiusScale(totalCount) * 0.1 * d;
return circleRadius <= radiusScale(maxCount) ? circleRadius : 0; // Establecer el radio a 0 si es mayor al radio máximo
})
.style("stroke", "#2c2c2c")
.style("stroke-width", "3px")
.style("fill", "none")
.style("opacity", "0.4");
// Crear el área sombreada que une los puntos
const area = d3
.areaRadial()
.angle((d) => angleScale(d))
.outerRadius((d) => radiusScale(countryCounts[d]));
const path = svg
.append("path")
.datum(topCountries)
.attr("fill", "#3E73B9")
.attr("fill-opacity", "0.5")
.attr("transform", `translate(${width / 2},${height / 2})`)
.attr("d", area);
// Crear las etiquetas con emojis
const emojis = {
Argentina: "\u{1F1E6}\u{1F1F7}",
Colombia: "\u{1F1E8}\u{1F1F4}",
Perú: "\u{1F1F5}\u{1F1EA}",
México: "\u{1F1F2}\u{1F1FD}",
Chile: "\u{1F1E8}\u{1F1F1}",
Brasil: "\u{1F1E7}\u{1F1F7}",
Arabia: "\u{1F1E6}\u{1F1EA}",
India: "\u{1F1EE}\u{1F1F3}",
China: "\u{1F1E8}\u{1F1F3}",
España: "\u{1F1EA}\u{1F1F8}",
Venezuela: "\u{1F1FB}\u{1F1EA}",
// Agregar más emojis de países aquí...
};
const labels = svg
.append("g")
.attr("transform", `translate(${width / 2},${height / 2})`);
labels
.selectAll(".label")
.data(topCountries)
.enter()
.append("text")
.attr("class", "label")
.attr(
"x",
(d) =>
(radiusScale(maxCount) + 20) *
Math.cos(angleScale(d) - Math.PI / 2)
)
.attr(
"y",
(d) =>
(radiusScale(maxCount) + 20) *
Math.sin(angleScale(d) - Math.PI / 2)
)
.text((d) => emojis[d])
.style("font-size", "20px")
.attr("text-anchor", "middle")
.attr("alignment-baseline", "middle");
// Tipografía
svg.selectAll("text").style("font-family", "Montserrat, sans-serif");
})
.catch((error) =>
console.error("Error al cargar el archivo CSV:", error)
);
</script>
</body>
</html>