dagre-d3 icon indicating copy to clipboard operation
dagre-d3 copied to clipboard

render fails when an edge has no value

Open artarf opened this issue 10 years ago • 6 comments

Here's unit test I used:

diff --git a/test/bundle-test.js b/test/bundle-test.js
index c9377cf..8abc072 100644
--- a/test/bundle-test.js
+++ b/test/bundle-test.js
@@ -55,6 +55,13 @@ describe("dagreD3", function() {
       expect(d3.select("#b-lab").datum()).to.equal("b");
     });

+    it("are created for each edge even when edge has no value", function() {
+      g.setNode("a", {});
+      g.setNode("b", {});
+      g.setEdge({v:"a", w:"b"}, undefined);
+      dagreD3.render()(svg, g);
+    });
+
     it("are created for each edge", function() {
       g.setNode("a", {});
       g.setNode("b", {});

This situation occurs at least when graph is deserialised using graphlib.json.read(). It calls Graph.setEdge() with object and value (even if the value is undefined).

artarf avatar Oct 30 '14 17:10 artarf

Here is suggested fix:

diff --git a/lib/render.js b/lib/render.js
index 1610b89..a36aa8e 100644
--- a/lib/render.js
+++ b/lib/render.js
@@ -128,6 +128,10 @@ function preProcessGraph(g) {

   g.edges().forEach(function(e) {
     var edge = g.edge(e);
+    if (typeof edge === "undefined") {
+      g.setEdge(e, {});
+      edge = g.edge(e);
+    }
     if (!_.has(edge, "label")) { edge.label = ""; }
     _.defaults(edge, EDGE_DEFAULT_ATTRS);
   });

artarf avatar Oct 30 '14 17:10 artarf

Thank you, this helped me to resolve problem with this library.

corpix avatar Jan 29 '15 03:01 corpix

just ran into this problem... and this helped me solve it.

ruckc avatar Apr 11 '18 02:04 ruckc

I also run into this problem. The issue for me here is that the diagnostics here is nonexistent. All I get is TypeError: Cannot set property 'label' of undefined.

In my case this is always an error on my side. However, I would like to see what am I trying connect to a missing node. I suggest adding validation in the code to show some info (e.g. dump the other node id).

agladysh avatar Dec 12 '18 05:12 agladysh

My nodes has no "undefined" value :( image

fixed image

But the graph is not what I want. image

--- Got it: Because I setEdge's value is "undefined", fixed it.

lin-credible avatar Dec 02 '20 04:12 lin-credible

This happened to me too and this issue helped solve it, thank you! Long story short, don't forget to call setDefaultEdgeLabel even for a graph that was created using dagre.graphlib.json.read. Like this:

 let graph;
 if (localStorage.getItem('dagre')) {
   graph = dagre.graphlib.json.read(JSON.parse(localStorage.getItem('dagre')));
 }
 else {
   graph = new dagre.graphlib.Graph();
   graph.setGraph({});
-  graph.setDefaultEdgeLabel(() => ({}));
   graph.setNode('Root', measure('Root'));
   dagre.layout(graph);
   localStorage.setItem('dagre', JSON.stringify(dagre.graphlib.json.write(graph)));
 }

+graph.setDefaultEdgeLabel(() => ({}));

This ensures that setDefaultEdgeLabel is called for newly created as well as JSON-reconstructed graphs.

TomasHubelbauer avatar Jun 04 '21 19:06 TomasHubelbauer