Skip to content

Commit

Permalink
Add support for labelheight for clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
osvalds committed Sep 28, 2023
1 parent 1373f59 commit d8c1350
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 27 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dagre",
"version": "1.0.4",
"version": "1.0.5-pre",
"main": [
"dist/dagre.core.js"
],
Expand Down
45 changes: 38 additions & 7 deletions dist/dagre.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ function updateInputGraph(inputGraph, layoutGraph) {
let graphNumAttrs = ["nodesep", "edgesep", "ranksep", "marginx", "marginy"];
let graphDefaults = { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: "tb" };
let graphAttrs = ["acyclicer", "ranker", "rankdir", "align"];
let nodeNumAttrs = ["width", "height"];
let nodeDefaults = { width: 0, height: 0 };
let nodeNumAttrs = ["width", "height", "labelheight"];
let nodeDefaults = { width: 0, height: 0, labelheight: 0 };
let edgeNumAttrs = ["minlen", "weight", "width", "height", "labeloffset"];
let edgeDefaults = {
minlen: 1, weight: 1, width: 0, height: 0,
Expand Down Expand Up @@ -901,9 +901,10 @@ function dfs(g, root, nodeSep, weight, height, depths, v) {
return;
}

let top = util.addBorderNode(g, "_bt");
let bottom = util.addBorderNode(g, "_bb");
let label = g.node(v);
// Adding custom border node to help with cluster label offset
let top = util.addWhimBorderNode(g, "_bt", label.labelheight);
let bottom = util.addBorderNode(g, "_bb");

g.setParent(top, v);
label.borderTop = top;
Expand Down Expand Up @@ -2184,8 +2185,23 @@ function positionY(g) {
return height;
}
}, 0);
layer.forEach(v => g.node(v).y = prevY + maxHeight / 2);
prevY += maxHeight + rankSep;
let borderTopSeen = false;
let labelheight = 0;
layer.forEach(v => {
let node = g.node(v);
if (node.dummy === "border" && node.whimNode) {
borderTopSeen = true;
labelheight = node.labelheight;
}
node.y = prevY + maxHeight / 2
});

if (borderTopSeen) {
// hard coding padding for cluster labels
prevY += maxHeight + labelheight + 24;
} else {
prevY += maxHeight + rankSep;
}
});
}

Expand Down Expand Up @@ -2648,6 +2664,7 @@ let Graph = require("@dagrejs/graphlib").Graph;

module.exports = {
addBorderNode,
addWhimBorderNode,
addDummyNode,
asNonCompoundGraph,
buildLayerMatrix,
Expand Down Expand Up @@ -2847,6 +2864,20 @@ function addBorderNode(g, prefix, rank, order) {
return addDummyNode(g, "border", node, prefix);
}

// labelHeight is a custom property to be used to offset cluster children nodes
// by the height of the cluster label
function addWhimBorderNode(g, prefix, labelHeight) {
const node = {
width: 0,
height: 0,
whimNode: true,
labelheight: labelHeight
};

return addDummyNode(g, "border", node, prefix);
}


function maxRank(g) {
return Math.max(...g.nodes().map(v => {
let rank = g.node(v).rank;
Expand Down Expand Up @@ -2948,7 +2979,7 @@ function zipObject(props, values) {
}

},{"@dagrejs/graphlib":29}],28:[function(require,module,exports){
module.exports = "1.0.4";
module.exports = "1.0.5-pre";

},{}],29:[function(require,module,exports){
/**
Expand Down
19 changes: 13 additions & 6 deletions dist/dagre.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ function updateInputGraph(inputGraph, layoutGraph) {
let graphNumAttrs = ["nodesep", "edgesep", "ranksep", "marginx", "marginy"];
let graphDefaults = { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: "tb" };
let graphAttrs = ["acyclicer", "ranker", "rankdir", "align"];
let nodeNumAttrs = ["width", "height"];
let nodeDefaults = { width: 0, height: 0 };
let nodeNumAttrs = ["width", "height", "labelheight"];
let nodeDefaults = { width: 0, height: 0, labelheight: 0 };
let edgeNumAttrs = ["minlen", "weight", "width", "height", "labeloffset"];
let edgeDefaults = {
minlen: 1, weight: 1, width: 0, height: 0,
Expand Down
5 changes: 3 additions & 2 deletions lib/nesting-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ function dfs(g, root, nodeSep, weight, height, depths, v) {
return;
}

let top = util.addBorderNode(g, "_bt");
let bottom = util.addBorderNode(g, "_bb");
let label = g.node(v);
// Adding custom border node to help with cluster label offset
let top = util.addWhimBorderNode(g, "_bt", label.labelheight);
let bottom = util.addBorderNode(g, "_bb");

g.setParent(top, v);
label.borderTop = top;
Expand Down
19 changes: 17 additions & 2 deletions lib/position/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,23 @@ function positionY(g) {
return height;
}
}, 0);
layer.forEach(v => g.node(v).y = prevY + maxHeight / 2);
prevY += maxHeight + rankSep;
let borderTopSeen = false;
let labelheight = 0;
layer.forEach(v => {
let node = g.node(v);
if (node.dummy === "border" && node.whimNode) {
borderTopSeen = true;
labelheight = node.labelheight;
}
node.y = prevY + maxHeight / 2;
});

if (borderTopSeen) {
// hard coding padding for cluster labels
prevY += maxHeight + labelheight + 24;
} else {
prevY += maxHeight + rankSep;
}
});
}

15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let Graph = require("@dagrejs/graphlib").Graph;

module.exports = {
addBorderNode,
addWhimBorderNode,
addDummyNode,
asNonCompoundGraph,
buildLayerMatrix,
Expand Down Expand Up @@ -205,6 +206,20 @@ function addBorderNode(g, prefix, rank, order) {
return addDummyNode(g, "border", node, prefix);
}

// labelHeight is a custom property to be used to offset cluster children nodes
// by the height of the cluster label
function addWhimBorderNode(g, prefix, labelHeight) {
const node = {
width: 0,
height: 0,
whimNode: true,
labelheight: labelHeight
};

return addDummyNode(g, "border", node, prefix);
}


function maxRank(g) {
return Math.max(...g.nodes().map(v => {
let rank = g.node(v).rank;
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "@dagrejs/dagre",
"name": "@whimsicalcode/dagre",
"version": "1.0.5-pre",
"description": "Graph layout for JavaScript",
"author": "Chris Pettitt <cpettitt@gmail.com>",
"contributors": [
"Matthew Dahl (https://github.com/sandersky)"
],
"publishConfig": {
"registry":"https://npm.pkg.github.com"
},
"license": "MIT",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -48,6 +51,6 @@
},
"repository": {
"type": "git",
"url": "https://github.com/dagrejs/dagre.git"
"url": "https://github.com/whimsicalcode/dagre.git"
}
}
2 changes: 1 addition & 1 deletion test/nesting-graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("rank/nestingGraph", () => {
expect(g.edge(g.outEdges(borderTop, "a")[0]).minlen).equals(1);
expect(g.outEdges("a", borderBottom)).to.have.length(1);
expect(g.edge(g.outEdges("a", borderBottom)[0]).minlen).equals(1);
expect(g.node(borderTop)).eqls({ width: 0, height: 0, dummy: "border" });
expect(g.node(borderTop)).eqls({ width: 0, height: 0, whimNode: true, labelheight: undefined, dummy: "border" });
expect(g.node(borderBottom)).eqls({ width: 0, height: 0, dummy: "border" });
});

Expand Down

0 comments on commit d8c1350

Please sign in to comment.