|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Chemlambda movies productions presents:</title> |
| 5 | +<!-- <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> |
| 6 | + <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> --> |
| 7 | + |
| 8 | + <script src="d3.min.js" charset="utf-8"></script> |
| 9 | + <script src="jquery.min.js"></script> |
| 10 | + <style> |
| 11 | + |
| 12 | + |
| 13 | +.link line { |
| 14 | + stroke: #696969; |
| 15 | +} |
| 16 | + |
| 17 | +.link line.separator { |
| 18 | + stroke: #95A5A6; |
| 19 | + stroke-width: 2px; |
| 20 | +} |
| 21 | + |
| 22 | +.node circle { |
| 23 | + stroke: #95A5A6; |
| 24 | + stroke-width: 1.5px; |
| 25 | +} |
| 26 | + |
| 27 | +.node text { |
| 28 | + font: 10px sans-serif; |
| 29 | + pointer-events: none; |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + </style> |
| 35 | +</head> |
| 36 | +<body style="background-color: rgb(255, 255, 255); color: rgb(255, 255, 255);" |
| 37 | + alink="#ee0000" link="#000250" vlink="#551a8b"> |
| 38 | + |
| 39 | +<!-- <button onclick="addNodes()">Start!</button> --> |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +<script> |
| 45 | + var graph; |
| 46 | + function myGraph() { |
| 47 | + |
| 48 | + // Add and remove elements on the graph object |
| 49 | + this.addNode = function (id, atom, size, colour) { |
| 50 | + nodes.push({"id": id, "atom": atom, "size": size, "colour": colour}); |
| 51 | + update(); |
| 52 | + }; |
| 53 | + |
| 54 | + this.removeNode = function (id) { |
| 55 | + var i = 0; |
| 56 | + var n = findNode(id); |
| 57 | + while (i < links.length) { |
| 58 | + if ((links[i]['source'] == n) || (links[i]['target'] == n)) { |
| 59 | + links.splice(i, 1); |
| 60 | + } |
| 61 | + else i++; |
| 62 | + } |
| 63 | + nodes.splice(findNodeIndex(id), 1); |
| 64 | + update(); |
| 65 | + }; |
| 66 | + |
| 67 | + this.removeLink = function (source, target) { |
| 68 | + for (var i = 0; i < links.length; i++) { |
| 69 | + if (links[i].source.id == source && links[i].target.id == target) { |
| 70 | + links.splice(i, 1); |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + update(); |
| 75 | + }; |
| 76 | + |
| 77 | + this.removeallLinks = function () { |
| 78 | + links.splice(0, links.length); |
| 79 | + update(); |
| 80 | + }; |
| 81 | + |
| 82 | + this.removeAllNodes = function () { |
| 83 | + nodes.splice(0, links.length); |
| 84 | + update(); |
| 85 | + }; |
| 86 | + |
| 87 | + this.addLink = function (source, target, bond) { |
| 88 | + links.push({"source": findNode(source), "target": findNode(target), "bond": bond}); |
| 89 | + update(); |
| 90 | + }; |
| 91 | + |
| 92 | + var findNode = function (id) { |
| 93 | + for (var i in nodes) { |
| 94 | + if (nodes[i]["id"] === id) return nodes[i]; |
| 95 | + } |
| 96 | + ; |
| 97 | + }; |
| 98 | + |
| 99 | + var findNodeIndex = function (id) { |
| 100 | + for (var i = 0; i < nodes.length; i++) { |
| 101 | + if (nodes[i].id == id) { |
| 102 | + return i; |
| 103 | + } |
| 104 | + } |
| 105 | + ; |
| 106 | + }; |
| 107 | + |
| 108 | + // set up the D3 visualisation in the specified element |
| 109 | + var w = 500, |
| 110 | + h = 500; |
| 111 | + |
| 112 | +// var radius = d3.scale.sqrt() |
| 113 | +// .range([0, 4]); |
| 114 | + |
| 115 | + var vis = d3.select("body") |
| 116 | + .append("svg:svg") |
| 117 | + .attr("width", w) |
| 118 | + .attr("height", h) |
| 119 | + .attr("id", "svg") |
| 120 | + .attr("pointer-events", "all") |
| 121 | + .attr("viewBox", "0 0 " + w + " " + h) |
| 122 | + .attr("perserveAspectRatio", "xMinYMid") |
| 123 | + .append('svg:g'); |
| 124 | + |
| 125 | + var force = d3.layout.force(); |
| 126 | + |
| 127 | + var nodes = force.nodes(), |
| 128 | + links = force.links(); |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + var update = function () { |
| 133 | + var link = vis.selectAll("line") |
| 134 | + .data(links, function (d) { |
| 135 | + return d.source.id + "-" + d.target.id; |
| 136 | + }); |
| 137 | + |
| 138 | + link.enter().append("line") |
| 139 | + .style("stroke-opacity",0.4).style("fill-opacity",0.4) |
| 140 | + .attr("id", function (d) { |
| 141 | + return d.source.id + "-" + d.target.id; |
| 142 | + }) |
| 143 | + .attr("stroke-width", function (d) { |
| 144 | + return (d.bond * 2 - 1) * 2 + "px"; |
| 145 | + }) |
| 146 | + .attr("stroke", "#FFFFFF"); |
| 147 | +// #ADD8E6 |
| 148 | + link.filter(function(d) { return d.bond > 1; }).append("line") |
| 149 | +// .attr("class", "separator"); |
| 150 | + .attr("class", "link"); |
| 151 | +// link.append("title") |
| 152 | +// .text(function (d) { |
| 153 | +// return d.value; |
| 154 | +// }); |
| 155 | + link.exit().remove(); |
| 156 | + |
| 157 | + |
| 158 | + var node = vis.selectAll("g.node") |
| 159 | + .data(nodes, function (d) { |
| 160 | + return d.id; |
| 161 | + }); |
| 162 | + |
| 163 | + var nodeEnter = node.enter().append("g") |
| 164 | + .attr("class", "node") |
| 165 | + .call(force.drag); |
| 166 | + |
| 167 | + nodeEnter.append("svg:circle") |
| 168 | + .style("stroke-opacity",0.4).style("fill-opacity",0.8) |
| 169 | + .attr("r", function(d) { return d.size*2;}) |
| 170 | + .attr("id", function (d) { |
| 171 | + return "Node;" + d.id; |
| 172 | + }) |
| 173 | + .attr("class", "nodeStrokeClass") |
| 174 | + .attr("fill", function(d) { return d.colour; }) |
| 175 | +// .transition() |
| 176 | +// .delay(700000) |
| 177 | +// .attr("r", 2); |
| 178 | + |
| 179 | +// node.append("text") |
| 180 | +// .attr("dy", ".35em") |
| 181 | +// .attr("dx", function(d) { return (0 - ((12/4) * (d.size + 0.5 )* (d.size + 0.5)) );}) |
| 182 | +// // .attr("text-anchor", "middle") |
| 183 | +// .text(function(d) { return d.atom; }); |
| 184 | + |
| 185 | +node.on("dblclick", function(d) { d.fixed = false; }); |
| 186 | + |
| 187 | +node.on("mousedown", function(d) { d.fixed = true; }); |
| 188 | + |
| 189 | + node.exit().remove(); |
| 190 | + |
| 191 | + force.on("tick", function () { |
| 192 | + |
| 193 | + node.attr("transform", function (d) { |
| 194 | + return "translate(" + d.x + "," + d.y + ")"; |
| 195 | + }); |
| 196 | + |
| 197 | + link.attr("x1", function (d) { |
| 198 | + return d.source.x; |
| 199 | + }) |
| 200 | + .attr("y1", function (d) { |
| 201 | + return d.source.y; |
| 202 | + }) |
| 203 | + .attr("x2", function (d) { |
| 204 | + return d.target.x; |
| 205 | + }) |
| 206 | + .attr("y2", function (d) { |
| 207 | + return d.target.y; |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + // Restart the force layout. |
| 212 | + |
| 213 | + force |
| 214 | +// .friction(.01) |
| 215 | + .charge(-20) |
| 216 | + .gravity(.38) |
| 217 | + .linkStrength(8.8) |
| 218 | + .linkDistance( function(d) { return ((d.source.size + d.target.size + 3)/(40*d.bond)); } ) |
| 219 | + .size([w, h]) |
| 220 | + .start(); |
| 221 | + }; |
| 222 | + |
| 223 | + |
| 224 | + // Make it all go |
| 225 | + update(); |
| 226 | + } |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + function drawGraph() { |
| 231 | + |
| 232 | + graph = new myGraph("#svgdiv"); |
| 233 | + |
| 234 | + |
0 commit comments