Midpoint marker
http://stackoverflow.com/questions/15729856/display-an-arrow-head-in-the-middle-of-a-link-with-d3-js
CSS
path.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
path.marker_only {
fill: none;
stroke: red;
stroke-opacity: .5;
stroke-width: 1.5px;
}
marker#licensing {
fill: green;
}
path.link.licensing {
stroke: green;
}
path.link.resolved {
stroke-dasharray: 0, 2 1;
}
circle {
fill: #ccc;
stroke: #333;
stroke-width: 1.5px;
}
text {
font: 10px sans-serif;
pointer-events: none;
}
text.shadow {
stroke: #fff;
stroke-width: 3px;
stroke-opacity: .8;
}
JavaScript
var links = [{
source: "Microsoft",
target: "Amazon",
type: "licensing"
}, {
source: "Samsung",
target: "Kodak",
type: "resolved"
}, {
source: "LG",
target: "Kodak",
type: "resolved"
}, {
source: "RIM",
target: "Kodak",
type: "suit"
}, {
source: "Sony",
target: "LG",
type: "suit"
}];
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function (link) {
link.source = nodes[link.source] || (nodes[link.source] = {
name: link.source
});
link.target = nodes[link.target] || (nodes[link.target] = {
name: link.target
});
});
var w = 460,
h = 500;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([w, h])
.linkDistance(90)
.charge(-300)
.on("tick", tick)
.start();
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h);
// Per-type markers, as they don't inherit styles.
svg.append("svg:defs").selectAll("marker")
.data(["suit", "licensing", "resolved"])
.enter().append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 1.5)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
var path = svg.append("svg:g").selectAll("path.link")
.data(force.links())
.enter().append("svg:path")
.attr("class", function (d) {
return "link " + d.type;
});
var markerPath = svg.append("svg:g").selectAll("path.marker")
.data(force.links())
.enter().append("svg:path")
.attr("class", function (d) {
return "marker_only " + d.type;
})
.attr("marker-end", function (d) {
return "url(#" + d.type + ")";
});
var circle = svg.append("svg:g").selectAll("circle")
.data(force.nodes())
.enter().append("svg:circle")
.attr("r", 6)
.call(force.drag);
var text =...