1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| let svg = d3.select("svg"), inner = svg.select("g");
this.g = new dagreD3.graphlib.Graph({}); this.g.setGraph({}); this.g.setDefaultEdgeLabel(function () { return {}; }); this.g.graph().transition = function (selection) { return selection.transition().duration(500); };
var zoom = d3.behavior.zoom().on("zoom", function () { inner.attr("transform", "translate(" + d3.event.translate + ")" + "scale(" + d3.event.scale + ")"); }); svg.call(zoom);
this.g.setNode(0, {label: 'VVV'}); this.g.setNode(1, {label: "A"}); this.g.setNode(2, {label: "B"}); this.g.setNode(3, {labelType:"html",label: "<i class=\"fa fa-database\"></i>B"});
this.g.setEdge(0, 1); this.g.setEdge(0, 2); this.g.setEdge(2, 3);
this.render(inner, this.g);
this.g.nodes().forEach((v) => { let node = this.g.node(v); console.log(`Node ${v}: Label:${node.label},X:${node.x},Y:${node.y}`); });
svg.selectAll("g.node rect") .attr("id", function (d) { return "node" + d; }); svg.selectAll("g.edgePath path") .attr("id", function (e) { return e.v + "-" + e.w; }); svg.selectAll("g.edgeLabel g") .attr("id", function (e) { return 'label_' + e.v + "-" + e.w; });
this.g.nodes().forEach((v) => { var node = this.g.node(v); node.customId = "node" + v; }); this.g.edges().forEach((e) => { var edge = this.g.edge(e.v, e.w); edge.customId = e.v + "-" + e.w });
function dragstart(d) { d3.event.sourceEvent.stopPropagation(); }
let dragmover = (currentThis, d) => { this.dragmove(currentThis, d); };
function dragmove(d) { dragmover(this, d) }
let nodeDrag = d3.behavior.drag() .on("dragstart", dragstart) .on("drag", dragmove);
let edgeDrag = d3.behavior.drag() .on("dragstart", dragstart) .on('drag', (d) => { this.translateEdge(this.g.edge(d.v, d.w), d3.event.dx, d3.event.dy); $('#' + this.g.edge(d.v, d.w).customId).attr('d', this.calcPoints(d)); });
nodeDrag.call(svg.selectAll("g.node")); edgeDrag.call(svg.selectAll("g.edgePath"));
|