davidardz07 commited on
Commit
f17e9d8
·
1 Parent(s): 742e0c6

Escalamiento Ky

Browse files
Files changed (1) hide show
  1. script.js +33 -5
script.js CHANGED
@@ -215,7 +215,16 @@ d3.sankey = function() {
215
  }
216
 
217
  function computeNodeBreadths() {
218
- var remainingNodes = nodes,
 
 
 
 
 
 
 
 
 
219
  nextNodes,
220
  x = 0;
221
 
@@ -234,8 +243,17 @@ d3.sankey = function() {
234
  ++x;
235
  }
236
 
 
237
  moveSinksRight(x);
 
 
238
  scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
 
 
 
 
 
 
239
  }
240
 
241
  function moveSinksRight(x) {
@@ -508,8 +526,13 @@ function initDiagram() {
508
  // Generate initial layout
509
  sankey.nodes(graph.nodes).links(graph.links).layout(32);
510
 
511
- // Apply proportional scaling to node heights
512
- const nodesByX = d3.groups(graph.nodes, d => d.x);
 
 
 
 
 
513
  const ky = d3.min(nodesByX, ([x, nodes]) =>
514
  (height - (nodes.length - 1) * sankey.nodePadding()) /
515
  d3.sum(nodes, d => d.value)
@@ -517,8 +540,13 @@ function initDiagram() {
517
 
518
  // Apply the scaling to nodes and links
519
  graph.nodes.forEach(node => {
520
- const nodeHeight = node.value * ky;
521
- node.dy = nodeHeight;
 
 
 
 
 
522
  });
523
 
524
  graph.links.forEach(link => {
 
215
  }
216
 
217
  function computeNodeBreadths() {
218
+ // Separate linked and unlinked nodes
219
+ var linkedNodes = nodes.filter(function(node) {
220
+ return node.sourceLinks.length > 0 || node.targetLinks.length > 0;
221
+ });
222
+ var unlinkedNodes = nodes.filter(function(node) {
223
+ return node.sourceLinks.length === 0 && node.targetLinks.length === 0;
224
+ });
225
+
226
+ // Process only linked nodes for layout
227
+ var remainingNodes = linkedNodes,
228
  nextNodes,
229
  x = 0;
230
 
 
243
  ++x;
244
  }
245
 
246
+ // Move sinks right
247
  moveSinksRight(x);
248
+
249
+ // Scale the layout for linked nodes
250
  scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
251
+
252
+ // Position unlinked nodes off-screen or at a specific position
253
+ unlinkedNodes.forEach(function(node) {
254
+ node.x = -1; // Position off-screen
255
+ node.dx = 0; // No width
256
+ });
257
  }
258
 
259
  function moveSinksRight(x) {
 
526
  // Generate initial layout
527
  sankey.nodes(graph.nodes).links(graph.links).layout(32);
528
 
529
+ // Filter only connected nodes for ky calculation
530
+ const nodesForKy = graph.nodes.filter(node =>
531
+ node.sourceLinks.length > 0 || node.targetLinks.length > 0
532
+ );
533
+
534
+ // Group connected nodes by x coordinate
535
+ const nodesByX = d3.groups(nodesForKy, d => d.x);
536
  const ky = d3.min(nodesByX, ([x, nodes]) =>
537
  (height - (nodes.length - 1) * sankey.nodePadding()) /
538
  d3.sum(nodes, d => d.value)
 
540
 
541
  // Apply the scaling to nodes and links
542
  graph.nodes.forEach(node => {
543
+ // Only apply height to connected nodes
544
+ if (node.sourceLinks.length > 0 || node.targetLinks.length > 0) {
545
+ const nodeHeight = node.value * ky;
546
+ node.dy = nodeHeight;
547
+ } else {
548
+ node.dy = 0; // Unlinked nodes get zero height
549
+ }
550
  });
551
 
552
  graph.links.forEach(link => {