Last active
August 29, 2015 14:09
-
-
Save chemplexity/6a9da491103b0f841479 to your computer and use it in GitHub Desktop.
Dynamic Molecules - Benzo[b]fluoranthene
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nodes": [ | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"}, | |
{"atom": "H", "size": 3, "color": "#E9E9E9"} | |
], | |
"links": [ | |
{"source": 0, "target": 1, "bond": 1}, | |
{"source": 1, "target": 2, "bond": 2}, | |
{"source": 2, "target": 3, "bond": 1}, | |
{"source": 3, "target": 4, "bond": 2}, | |
{"source": 4, "target": 5, "bond": 1}, | |
{"source": 5, "target": 6, "bond": 2}, | |
{"source": 6, "target": 7, "bond": 1}, | |
{"source": 7, "target": 8, "bond": 2}, | |
{"source": 8, "target": 9, "bond": 1}, | |
{"source": 9, "target": 10, "bond": 2}, | |
{"source": 10, "target": 11, "bond": 1}, | |
{"source": 11, "target": 12, "bond": 2}, | |
{"source": 12, "target": 13, "bond": 1}, | |
{"source": 13, "target": 14, "bond": 1}, | |
{"source": 14, "target": 15, "bond": 2}, | |
{"source": 15, "target": 16, "bond": 1}, | |
{"source": 16, "target": 17, "bond": 2}, | |
{"source": 17, "target": 18, "bond": 1}, | |
{"source": 18, "target": 0, "bond": 2}, | |
{"source": 17, "target": 19, "bond": 1}, | |
{"source": 5, "target": 19, "bond": 1}, | |
{"source": 13, "target": 19, "bond": 2}, | |
{"source": 7, "target": 12, "bond": 1}, | |
{"source": 4, "target": 18, "bond": 1}, | |
{"source": 20, "target": 0, "bond": 1}, | |
{"source": 21, "target": 1, "bond": 1}, | |
{"source": 22, "target": 2, "bond": 1}, | |
{"source": 23, "target": 3, "bond": 1}, | |
{"source": 24, "target": 6, "bond": 1}, | |
{"source": 25, "target": 8, "bond": 1}, | |
{"source": 26, "target": 9, "bond": 1}, | |
{"source": 27, "target": 10, "bond": 1}, | |
{"source": 28, "target": 11, "bond": 1}, | |
{"source": 29, "target": 14, "bond": 1}, | |
{"source": 30, "target": 15, "bond": 1}, | |
{"source": 31, "target": 16, "bond": 1} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.link line { | |
stroke: #000000; | |
stroke-opacity: 0.7; | |
stroke-width:1.5; | |
} | |
.link line.separator { | |
stroke: #fff; | |
stroke-width: 2px; | |
} | |
.node circle { | |
stroke: #000; | |
stroke-width: 3px; | |
} | |
.node text { | |
font: 14px sans-serif; | |
pointer-events: none; | |
} | |
</style> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<body> | |
<div id="chart"></div> | |
<script> | |
var margin = {top: -5, right: -5, bottom: -5, left: -5}; | |
var width = 500 - margin.left - margin.right, | |
height = 350- margin.top - margin.bottom, | |
r = 15; | |
var radius = d3.scale.sqrt() | |
.range([0, 6]); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var vis = d3.select("#chart") | |
.append("svg:svg") | |
.attr("width", width) | |
.attr("height", height) | |
.append('svg:g'); | |
vis.append('svg:rect') | |
.attr('width', width) | |
.attr('height', height) | |
.attr('fill', 'white'); | |
d3.json("benzobfluoranthene.json", function(graph) { | |
var force = d3.layout.force() | |
.nodes(graph.nodes) | |
.size([width + margin.left + margin.right, height + margin.top + margin.bottom]) | |
.links(graph.links) | |
.charge(-300) | |
.gravity(0.2) | |
.linkDistance(-1) | |
.on("tick", tick) | |
.start() | |
var link = vis.selectAll(".link") | |
.data(graph.links) | |
.enter().append("g") | |
.attr("class", "link"); | |
link.append("line") | |
.style("stroke-width", function(d) { return (d.bond * 3 - 1) * 2 + "px"; }); | |
link.filter(function(d) { return d.bond > 1; }).append("line") | |
.attr("class", "separator"); | |
var node = vis.selectAll(".node") | |
.data(graph.nodes) | |
.enter().append("circle") | |
.attr("class", "node") | |
.call(force.drag) | |
.style("fill", function(d) { return d.color; }) | |
.attr("r", function(d) { return radius(d.size); }) | |
.style("stroke", "black")//function(d) {return d3.rgb(d.color).darker(); }) | |
.style("stroke-width", 3) | |
.style("stroke-opacity", 0.9); | |
node.append("text") | |
.attr("dy", ".35em") | |
.attr("text-anchor", "middle") | |
.text(function(d) { return d.atom; }) | |
.style("fill", function(d) { return d.color; }); | |
function tick() { | |
link.selectAll("line") | |
.attr("x1", function(d) { return d.source.x; }) | |
.attr("y1", function(d) { return d.source.y; }) | |
.attr("x2", function(d) { return d.target.x; }) | |
.attr("y2", function(d) { return d.target.y; }) | |
node.attr("cx", function(d) { return d.x = Math.max(r, Math.min(width - r, d.x)); }) | |
.attr("cy", function(d) { return d.y = Math.max(r, Math.min(height - r, d.y)); }) | |
node.each(collide(-100.5)) | |
} | |
var padding = 1; | |
function collide(alpha) { | |
var quadtree = d3.geom.quadtree(graph.nodes); | |
return function(d) { | |
var rb = 1*radius + padding, | |
nx1 = d.x - rb, | |
nx2 = d.x + rb, | |
ny1 = d.y - rb, | |
ny2 = d.y + rb; | |
quadtree.visit(function(quad, x1, y1, x2, y2) { | |
if (quad.point && (quad.point !== d)) { | |
var x = d.x - quad.point.x, | |
y = d.y - quad.point.y, | |
l = Math.sqrt(x * x + y * y); | |
if (l < rb) { | |
l = (l - rb) / l * alpha; | |
d.x -= x *= l; | |
d.y -= y *= l; | |
quad.point.x += x; | |
quad.point.y += y; | |
} | |
} | |
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1; | |
}); | |
}; | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment