D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
chemplexity
Full window
Github gist
Dynamic Molecules - Benzo[b]fluoranthene
<!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="https://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>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js