Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support markers at start and midpoint of edges (resolving conflicts in #316) #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions demo/arrows.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ <h1>Dagre D3 Demo: Arrows</h1>
});
});

// Reverse direction (arrowtail)
["normal", "vee"].forEach(function(arrowtail) {
g.setNode(arrowtail + "3", { label: " " });
g.setNode(arrowtail + "4", { label: " " });
g.setEdge(arrowtail + "3", arrowtail + "4", {
arrowhead: "undirected",
arrowtail: arrowtail,
label: arrowtail + " (arrowtail)"
});
});
// Arrowhead in the middle (arrowmid)
["normal", "vee"].forEach(function(arrowmid) {
g.setNode(arrowmid + "5", { label: " " });
g.setNode(arrowmid + "6", { label: " " });
g.setEdge(arrowmid + "5", arrowmid + "6", {
arrowhead: "undirected",
arrowmid: arrowmid,
label: arrowmid + " (arrowmid)"
});
});

var svg = d3.select("svg"),
inner = svg.select("g");

Expand Down
10 changes: 8 additions & 2 deletions demo/user-defined.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,25 @@ <h1>Dagre D3 Demo: User-defined Shapes and Arrows</h1>
};

// Add our custom arrow (a hollow-point)
// Note we have to draw it upside down if this is a
// reverse direction arrow (arrowtail)
render.arrows().hollowPoint = function normal(parent, id, edge, type) {
var invert = id.indexOf("arrowtail") !== -1;
var marker = parent.append("marker")
.attr("id", id)
.attr("viewBox", "0 0 10 10")
.attr("refX", 9)
.attr("refX", invert ? 0 : 9)
.attr("refY", 5)
.attr("markerUnits", "strokeWidth")
.attr("markerWidth", 8)
.attr("markerHeight", 6)
.attr("orient", "auto");

var movements = (invert ?
"M 10 0 L 0 5 L 10 10 z" :
"M 0 0 L 10 5 L 0 10 z");
var path = marker.append("path")
.attr("d", "M 0 0 L 10 5 L 0 10 z")
.attr("d", movements)
.style("stroke-width", 1)
.style("stroke-dasharray", "1,0")
.style("fill", "#fff")
Expand Down
11 changes: 8 additions & 3 deletions lib/arrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ module.exports = {
};

function normal(parent, id, edge, type) {
var invert = id.indexOf("arrowtail") !== -1;
var marker = parent.append("marker")
.attr("id", id)
.attr("viewBox", "0 0 10 10")
.attr("refX", 9)
.attr("refX", invert ? 0 : 9)
.attr("refY", 5)
.attr("markerUnits", "strokeWidth")
.attr("markerWidth", 8)
.attr("markerHeight", 6)
.attr("orient", "auto");

var movements = (invert ?
"M 10 0 L 0 5 L 10 10 z" :
"M 0 0 L 10 5 L 0 10 z");
var path = marker.append("path")
.attr("d", "M 0 0 L 10 5 L 0 10 z")
.attr("d", movements)
.style("stroke-width", 1)
.style("stroke-dasharray", "1,0");
util.applyStyle(path, edge[type + "Style"]);
Expand All @@ -29,10 +33,11 @@ function normal(parent, id, edge, type) {
}

function vee(parent, id, edge, type) {
var invert = id.indexOf("arrowtail") !== -1;
var marker = parent.append("marker")
.attr("id", id)
.attr("viewBox", "0 0 10 10")
.attr("refX", 9)
.attr("refX", invert ? 0 : 9)
.attr("refY", 5)
.attr("markerUnits", "strokeWidth")
.attr("markerWidth", 8)
Expand Down
12 changes: 12 additions & 0 deletions lib/create-edge-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ function createEdgePaths(selection, g, arrows) {
.each(function(e) {
var edge = g.edge(e);
edge.arrowheadId = _.uniqueId("arrowhead");
edge.arrowmidId = _.uniqueId("arrowmid");
edge.arrowtailId = _.uniqueId("arrowtail");

var domEdge = d3.select(this)
.attr("marker-end", function() {
return "url(" + makeFragmentRef(location.href, edge.arrowheadId) + ")";
})
.attr("marker-mid", function() {
return "url(" + makeFragmentRef(location.href, edge.arrowmidId) + ")";
})
.attr("marker-start", function() {
return "url(" + makeFragmentRef(location.href, edge.arrowtailId) + ")";
})
.style("fill", "none");

util.applyTransition(domEdge, g)
Expand All @@ -54,7 +62,11 @@ function createEdgePaths(selection, g, arrows) {
.each(function(e) {
var edge = g.edge(e);
var arrowhead = arrows[edge.arrowhead];
var arrowmid = arrows[edge.arrowmid];
var arrowtail = arrows[edge.arrowtail];
arrowhead(d3.select(this), edge.arrowheadId, edge, "arrowhead");
arrowmid(d3.select(this), edge.arrowmidId, edge, "arrowhead");
arrowtail(d3.select(this), edge.arrowtailId, edge, "arrowhead");
});

return svgPaths;
Expand Down
2 changes: 2 additions & 0 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ var NODE_DEFAULT_ATTRS = {

var EDGE_DEFAULT_ATTRS = {
arrowhead: "normal",
arrowmid: "undirected",
arrowtail: "undirected",
curve: d3.curveLinear
};

Expand Down