|
// Generated by IcedCoffeeScript 1.3.3f |
|
(function() { |
|
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; |
|
|
|
window.AcyclicGraph = (function() { |
|
|
|
AcyclicGraph.prototype.color = d3.scale.category20(); |
|
|
|
function AcyclicGraph(width, height, elm, data) { |
|
this.tick = __bind(this.tick, this); |
|
|
|
this.processData = __bind(this.processData, this); |
|
|
|
var _this = this; |
|
this.svg = d3.select(elm).append("svg").attr("width", width).attr("height", height); |
|
this.defineArrowMarker(); |
|
this.processData(typeof data !== "string" ? data : JSON.parse(data)); |
|
this.force = d3.layout.force().nodes(d3.values(this.nodes)).links(this.links).charge(-500).linkDistance(120).size([width, height]).on("tick", this.tick).start(); |
|
this.path = this.svg.append("g").selectAll("path").data(this.force.links()).enter().append("path").style("stroke", "#666").style("stroke-width", "3px").attr("marker-end", "url(#link)"); |
|
this.circle = this.svg.append("g").selectAll("ellipse").data(this.force.nodes()).enter().append("ellipse").attr("class", "node").attr("ry", 20).attr("rx", 20).style("fill", function(d) { |
|
return _this.color(d.index % 20); |
|
}).call(this.force.drag); |
|
this.text = this.svg.append("g").selectAll("g").data(this.force.nodes()).enter().append("g"); |
|
this.text.append("text").attr("text-anchor", "middle").style("dominant-baseline", "central").text(function(d) { |
|
return d.name; |
|
}).style("stroke", function(d) { |
|
return _this.color(d.index % 20); |
|
}).style("stroke-width", ".5em").style("font", '1.1em/1em Optima, Segoe, "Segoe UI", Candara, Calibri, Arial, sans-serif'); |
|
this.text.append("text").attr("text-anchor", "middle").style("dominant-baseline", "central").text(function(d) { |
|
return d.name; |
|
}).style("font", '1.1em/1em Optima, Segoe, "Segoe UI", Candara, Calibri, Arial, sans-serif'); |
|
} |
|
|
|
AcyclicGraph.prototype.processData = function(data) { |
|
var link, _i, _len, _ref, _ref1, _results, |
|
_this = this; |
|
this.links = data; |
|
this.nodes = {}; |
|
_results = []; |
|
for (_i = 0, _len = data.length; _i < _len; _i++) { |
|
link = data[_i]; |
|
if (link.source != null) { |
|
link.source = (_ref = this.nodes[link.source]) != null ? _ref : (function() { |
|
return _this.nodes[link.source] = { |
|
name: link.source |
|
}; |
|
})(); |
|
} |
|
if ((link.target != null) && link.target !== "") { |
|
_results.push(link.target = (_ref1 = this.nodes[link.target]) != null ? _ref1 : (function() { |
|
return _this.nodes[link.target] = { |
|
name: link.target |
|
}; |
|
})()); |
|
} else { |
|
_results.push(void 0); |
|
} |
|
} |
|
return _results; |
|
}; |
|
|
|
AcyclicGraph.prototype.defineArrowMarker = function() { |
|
return this.svg.append("defs").selectAll("marker").data(["link", "highlight"]).enter().append("marker").attr("id", String).attr("viewBox", "0 -5 10 10").attr("refX", 20).attr("markerWidth", 6).attr("markerHeight", 6).attr("orient", "auto").style("fill", function(d) { |
|
if (String(d) === "link") { |
|
return "#666"; |
|
} else { |
|
return "#ff7f0e"; |
|
} |
|
}).append("path").attr("d", "M0,-5L10,0L0,5"); |
|
}; |
|
|
|
AcyclicGraph.prototype.tick = function() { |
|
this.path.attr("d", function(d) { |
|
var dr, dx, dy; |
|
dx = d.target.x - d.source.x; |
|
dy = d.target.y - d.source.y; |
|
dr = Math.sqrt(dx * dx + dy * dy); |
|
return "M" + d.source.x + "," + d.source.y + " L" + d.target.x + "," + d.target.y; |
|
}); |
|
this.circle.attr("transform", function(d) { |
|
return "translate(" + d.x + " , " + d.y + ")"; |
|
}); |
|
return this.text.attr("transform", function(d) { |
|
return "translate(" + d.x + " , " + d.y + ")"; |
|
}); |
|
}; |
|
|
|
return AcyclicGraph; |
|
|
|
})(); |
|
|
|
}).call(this); |