Last active
August 29, 2015 14:08
-
-
Save chemplexity/20ba7498015358682480 to your computer and use it in GitHub Desktop.
Dynamic Molecules - Cytosine
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": "N", "size": 6, "color": "#0033CC"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "N", "size": 6, "color": "#0033CC"}, | |
{"atom": "C", "size": 5, "color": "#272727"}, | |
{"atom": "N", "size": 6, "color": "#0033CC"}, | |
{"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": "O", "size": 6, "color": "#FF1919"}, | |
{"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": 1}, | |
{"source": 4, "target": 5, "bond": 1}, | |
{"source": 5, "target": 6, "bond": 2}, | |
{"source": 6, "target": 1, "bond": 1}, | |
{"source": 7, "target": 0, "bond": 1}, | |
{"source": 8, "target": 0, "bond": 1}, | |
{"source": 9, "target": 3, "bond": 2}, | |
{"source": 10, "target": 4, "bond": 1}, | |
{"source": 11, "target": 5, "bond": 1}, | |
{"source": 12, "target": 6, "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 = 550 - 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("cytosine.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(-350) | |
.gravity(0.15) | |
.linkDistance(20) | |
.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(0.5)) | |
} | |
var padding = 1; | |
function collide(alpha) { | |
var quadtree = d3.geom.quadtree(graph.nodes); | |
return function(d) { | |
var rb = 2*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