|
| 1 | + <!DOCTYPE html> |
| 2 | + <meta charset="utf-8"> |
| 3 | + <style> |
| 4 | + |
| 5 | + .links path { |
| 6 | + fill: none; |
| 7 | + stroke: #666; |
| 8 | + stroke-width: 1.5px; |
| 9 | + } |
| 10 | + |
| 11 | + .nodes circle { |
| 12 | + fill: #ccc; |
| 13 | + stroke: #fff; |
| 14 | + stroke-width: 1.5px; |
| 15 | + } |
| 16 | + |
| 17 | + .nodes text { |
| 18 | + fill: #000; |
| 19 | + font: 10px sans-serif; |
| 20 | + pointer-events: none; |
| 21 | + } |
| 22 | + |
| 23 | + </style> |
| 24 | + <svg width="960" height="600"></svg> |
| 25 | + <script src="https://d3js.org/d3.v4.min.js"></script> |
| 26 | + <script src="/grammar/dot.js"></script> |
| 27 | + <script src="/d3-dot-graph.js"></script> |
| 28 | + <script> |
| 29 | + |
| 30 | +var line = d3.line() |
| 31 | + .curve(d3.curveCatmullRom.alpha(0.5)); |
| 32 | + |
| 33 | + var svg = d3.select("svg"), |
| 34 | + width = +svg.attr("width"), |
| 35 | + height = +svg.attr("height"); |
| 36 | + |
| 37 | + var color = d3.scaleOrdinal(d3.schemeCategory20); |
| 38 | + |
| 39 | + var simulation = d3.forceSimulation() |
| 40 | + .force("link", d3.forceLink().distance(100).id(function(d) { return d.id; })) |
| 41 | + .force("charge", d3.forceManyBody().strength(-10)) |
| 42 | + .force("collide", d3.forceCollide(50)) |
| 43 | + .force("center", d3.forceCenter(width / 2, height / 2)); |
| 44 | + |
| 45 | + d3.dot("summary.dot", function(graph) { |
| 46 | + //if (error) throw error; |
| 47 | + |
| 48 | + // build the arrow. |
| 49 | +svg.append("svg:defs").selectAll("marker") |
| 50 | + .data(["end"]) // Different link/path types can be defined here |
| 51 | + .enter().append("svg:marker") // This section adds in the arrows |
| 52 | + .attr("id", String) |
| 53 | + .attr("viewBox", "0 -5 10 10") |
| 54 | + .attr("refX", 15) |
| 55 | + .attr("refY", -1.5) |
| 56 | + .attr("markerWidth", 6) |
| 57 | + .attr("markerHeight", 6) |
| 58 | + .attr("orient", "auto") |
| 59 | + .append("svg:path") |
| 60 | + .attr("d", "M0,-5L10,0L0,5");; |
| 61 | + |
| 62 | +// add the links and the arrows |
| 63 | +var path = svg.append("svg:g").attr("class", "links").selectAll("path") |
| 64 | + .data(graph.links) |
| 65 | + .enter().append("svg:path") |
| 66 | +// .attr("class", function(d) { return "link " + d.type; }) |
| 67 | + .attr("marker-end", "url(#end)"); |
| 68 | + |
| 69 | + var node = svg.append("g") |
| 70 | + .attr("class", "nodes") |
| 71 | + .selectAll("circle") |
| 72 | + .data(graph.nodes) |
| 73 | + .enter().append("g") |
| 74 | + |
| 75 | + node |
| 76 | + .append("circle") |
| 77 | + .attr("r", 5) |
| 78 | + //.attr("fill", function(d) { return color(d.group); }) |
| 79 | + .call(d3.drag() |
| 80 | + .on("start", dragstarted) |
| 81 | + .on("drag", dragged) |
| 82 | + .on("end", dragended)); |
| 83 | + |
| 84 | + // add the text |
| 85 | + node.append("text") |
| 86 | + .attr("x", 12) |
| 87 | + .attr("dy", ".35em") |
| 88 | + .text(function(d) { return d.id; }); |
| 89 | + |
| 90 | + simulation |
| 91 | + .nodes(graph.nodes) |
| 92 | + .on("tick", ticked); |
| 93 | + |
| 94 | + simulation.force("link") |
| 95 | + .links(graph.links); |
| 96 | + |
| 97 | + let linkGen = d3.linkVertical().x(function(d) { return d.x; }) |
| 98 | + .y(function(d) { return d.y; });; |
| 99 | + |
| 100 | + var linkRad = d3.linkRadial() |
| 101 | + .angle(function(d) { return d.x; }) |
| 102 | + .radius(function(d) { return d.y; }); |
| 103 | + |
| 104 | + function ticked() { |
| 105 | + path.attr("d", |
| 106 | + (d)=>linkGen(d)) |
| 107 | + //(d) => line([[d.source.x, d.source.y], [d.target.x, d.target.y]])); |
| 108 | + |
| 109 | + node |
| 110 | + .attr("transform", function(d) { |
| 111 | + return "translate(" + d.x + "," + d.y + ")"; }); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + function dragstarted(d) { |
| 116 | + if (!d3.event.active) simulation.alphaTarget(0.3).restart(); |
| 117 | + d.fx = d.x; |
| 118 | + d.fy = d.y; |
| 119 | + } |
| 120 | + |
| 121 | + function dragged(d) { |
| 122 | + d.fx = d3.event.x; |
| 123 | + d.fy = d3.event.y; |
| 124 | + } |
| 125 | + |
| 126 | + function dragended(d) { |
| 127 | + if (!d3.event.active) simulation.alphaTarget(0); |
| 128 | + d.fx = null; |
| 129 | + d.fy = null; |
| 130 | + } |
| 131 | +</script> |
0 commit comments