Skip to content

Instantly share code, notes, and snippets.

@chemplexity
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemplexity/4839327d6bfa25ba458d to your computer and use it in GitHub Desktop.
Save chemplexity/4839327d6bfa25ba458d to your computer and use it in GitHub Desktop.
Dynamic Molecules - Alanine
{
"nodes": [
{"atom": "C", "size": 5, "color": "#272727"},
{"atom": "C", "size": 5, "color": "#272727"},
{"atom": "C", "size": 5, "color": "#272727"},
{"atom": "N", "size": 6, "color": "#0033CC"},
{"atom": "H", "size": 3, "color": "#E9E9E9"},
{"atom": "O", "size": 6, "color": "#FF1919"},
{"atom": "O", "size": 6, "color": "#FF1919"},
{"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": 1},
{"source": 1, "target": 3, "bond": 1},
{"source": 2, "target": 5, "bond": 1},
{"source": 2, "target": 6, "bond": 2},
{"source": 1, "target": 4, "bond": 1},
{"source": 3, "target": 10, "bond": 1},
{"source": 3, "target": 11, "bond": 1},
{"source": 0, "target": 7, "bond": 1},
{"source": 0, "target": 8, "bond": 1},
{"source": 0, "target": 9, "bond": 1},
{"source": 5, "target": 12, "bond": 1}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link line {
stroke: #000000;
stroke-opacity: 0.75;
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>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 500,
height = 350;
var color = d3.scale.category20();
var radius = d3.scale.sqrt()
.range([0, 6]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.size([width, height])
.charge(-400)
.linkDistance(function(d) { return radius(d.source.size) + radius(d.target.size) + 8; });
d3.json("alanine.json", function(graph) {
force.nodes(graph.nodes)
.links(graph.links)
.charge(-450)
.gravity(0.25)
.distance(20)
.on("tick", tick)
.start();
var link = svg.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 = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", function(d) { return radius(d.size); })
.style("fill", function(d) { return d.color; });
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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment