dagrejs / dagre-d3

A D3-based renderer for Dagre
MIT License
2.86k stars 592 forks source link

render fails when an edge has no value #111

Open artarf opened 10 years ago

artarf commented 10 years ago

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 commented 10 years ago

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);
   });
corpix commented 9 years ago

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

ruckc commented 6 years ago

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

agladysh commented 5 years ago

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).

lin-credible commented 3 years ago

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.

TomasHubelbauer commented 3 years ago

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.