-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
253 lines (226 loc) · 8.11 KB
/
index.html
File metadata and controls
253 lines (226 loc) · 8.11 KB
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>MAD - Líneas de Investigación</title>
<script src="d3.v7.min.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="controls">
<label>
<input type="checkbox" id="toggleLabels" /> Mostrar etiquetas
</label>
<label>
<input type="range" id="linkLength" min="1" max="100" value="13" />
Largo
</label>
<label>
<input type="range" id="linkStrength" min="0" max=".45" step="0.001" value="0.01"
/>
Fuerza
</label>
</div>
<svg id="graph"></svg>
<script>
// Configuración inicial
var linkLength = document.getElementById("linkLength").value;
var linkStrength = document.getElementById("linkStrength").value;
var showLabels = false;
var simulation, link, node, label;
// Definir el tamaño y margen del SVG
const width = window.innerWidth,
height = window.innerHeight;
// Seleccionar el SVG y asignarle dimensiones
const svg = d3
.select("#graph")
.attr("width", width)
.attr("height", height);
// Cargar datos desde el archivo CSV
d3.csv(
"https://docs.google.com/spreadsheets/d/e/2PACX-1vRcrfsdeArUHSpYd5J7Pby6nn2bZgwGTy8KBl5NPdTCNsvkBTXdI54amrC39Q3PQd0AAO6KY-lhcplr/pub?gid=1147217014&single=true&output=csv"
).then(function (data) {
// Inicializar las variables nodes y links
let nodes = [];
let links = [];
let nodeMap = {};
// Mapa para mantener un registro del número de relaciones para cada línea
let relationCount = {};
// Paso 1: Inicializar todos los nodos con id y ead
data.forEach(function (row) {
if (!nodeMap[row.Línea]) {
nodeMap[row.Línea] = {
id: row.Línea,
count: 0, // Inicializado en 0
ead: row.EAD,
};
}
});
// Paso 2: Procesar las relaciones para construir los enlaces y actualizar los recuentos
data.forEach(function (row) {
// Inicializar el contador para la línea actual
relationCount[row.Línea] = 1;
Object.keys(row).forEach(function (column) {
if (column.startsWith("Relación") && row[column]) {
// Incrementar el contador para la línea actual
relationCount[row.Línea] += 1;
// Asegurarse de que el nodo destino también esté inicializado
if (!nodeMap[row[column]]) {
nodeMap[row[column]] = { id: row[column], count: 0, ead: "no" };
}
// Añadir el enlace solo si ambos nodos existen
if (nodeMap[row.Línea] && nodeMap[row[column]]) {
// Añadir la propiedad 'length' al enlace
links.push({
source: row.Línea,
target: row[column],
length: relationCount[row.Línea],
});
}
// Incrementar el recuento para el nodo destino
nodeMap[row[column]].count += 1;
}
});
});
// Crear la lista de nodos a partir del mapa de nodos
nodes = Object.values(nodeMap);
console.log("nodes: ");
console.log(nodes);
console.log("links: ");
console.log(links);
// Crear la simulación usando D3 force
simulation = d3
.forceSimulation(nodes)
.force(
"link",
d3
.forceLink(links)
.id((d) => d.id)
.distance((d) => d.length * linkLength) // Usar length como multiplicador
.strength(linkStrength)
)
.force("charge", d3.forceManyBody().strength(-linkLength))
.force("center", d3.forceCenter(width / 2, height / 2));
// Crear los elementos SVG para los nodos y las aristas
link = svg
.append("g")
.attr("class", "links")
.selectAll("line")
.data(links)
.enter()
.append("line")
.attr("stroke", "#00000022");
node = svg
.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(nodes)
.enter()
.append("circle")
.attr("r", (d) => Math.sqrt(d.count) * 4 + 4)
.attr("fill", (d) => {
// color según la columna EAD
if (d.ead === "específica") return "#222";
if (d.ead === "transversal") return "#e64d06";
if (d.ead === "central") return "#a92905";
});
label = svg
.append("g")
.attr("class", "labels")
.selectAll(".label")
.data(nodes)
.enter()
.append("text")
.attr("class", "label")
.text((d) => d.id)
.attr("text-anchor", "middle")
.attr("dy", ".35em")
.attr("visibility", "hidden")
.attr("font-size", (d) => Math.sqrt(d.count) * 3 + 14 + "px")
.style("font-weight", (d) => {
// peso según la columna EAD
if (d.ead === "específica") return "lighter";
if (d.ead === "transversal") return "bold";
if (d.ead === "central") return "normal";
})
.style("fill", (d) => {
// color según la columna EAD
if (d.ead === "específica") return "#222";
if (d.ead === "transversal") return "#000";
if (d.ead === "central") return "#c00";
});
// Actualizar la simulación
simulation.nodes(nodes);
simulation.force("link").links(links);
// Actualizar la posición de los nodos y aristas
simulation.on("tick", function () {
link
.attr("x1", (d) => d.source.x)
.attr("y1", (d) => d.source.y)
.attr("x2", (d) => d.target.x)
.attr("y2", (d) => d.target.y);
node
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.call(
d3
.drag()
.on("start", dragStarted)
.on("drag", dragged)
.on("end", dragEnded)
);
label.attr("x", (d) => d.x).attr("y", (d) => d.y);
});
});
// Control de arrastre
function dragStarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
function dragEnded(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
// Controles interactivos
document
.getElementById("toggleLabels")
.addEventListener("change", function () {
showLabels = !showLabels;
if (showLabels) {
node.attr("r", 0);
label.attr("visibility", "visible");
link.attr("visibility", "hidden");
} else {
node.attr("r", (d) => Math.sqrt(d.count) * 4 + 4);
label.attr("visibility", "hidden");
link.attr("visibility", "visible");
}
});
document
.getElementById("linkLength")
.addEventListener("input", function () {
linkLength = this.value;
simulation.force("link").distance((d) => d.length * linkLength);
simulation.force("charge").strength(-linkLength);
simulation.alpha(0.3).restart();
});
/*
.distance((d) => d.length * linkLength) // Usar length como multiplicador
.strength(linkStrength)
*/
document
.getElementById("linkStrength")
.addEventListener("input", function () {
linkStrength = this.value;
simulation.force("link").strength((d) => linkStrength);
simulation.alpha(0.3).restart();
});
</script>
</body>
</html>