Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active March 20, 2019 02:55

Revisions

  1. mbostock revised this gist Feb 9, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions .block
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    license: gpl-3.0
  2. mbostock revised this gist Oct 31, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@

    </style>
    <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script src="//d3js.org/d3.v3.min.js"></script>
    <script>

    var width = 960,
  3. mbostock revised this gist Jun 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@
    .linkDistance(function(d) { return radius(d.source.size) + radius(d.target.size) + 20; });

    d3.json("graph.json", function(error, graph) {
    if (error) return console.error(error);
    if (error) throw error;

    force
    .nodes(graph.nodes)
  4. mbostock revised this gist Jun 11, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@

    </style>
    <body>
    <script src="http://d3js.org/d3.v2.min.js?2.9.6"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script>

    var width = 960,
    @@ -43,7 +43,9 @@
    .charge(-400)
    .linkDistance(function(d) { return radius(d.source.size) + radius(d.target.size) + 20; });

    d3.json("graph.json", function(graph) {
    d3.json("graph.json", function(error, graph) {
    if (error) return console.error(error);

    force
    .nodes(graph.nodes)
    .links(graph.links)
  5. mbostock revised this gist Oct 17, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion graph.json
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    {"atom": "C", "size": 12},
    {"atom": "C", "size": 12},
    {"atom": "C", "size": 12},
    {"atom": "N", "size": 7},
    {"atom": "N", "size": 14},
    {"atom": "H", "size": 1},
    {"atom": "O", "size": 16},
    {"atom": "O", "size": 16},
  6. mbostock revised this gist Oct 12, 2012. 1 changed file with 0 additions and 0 deletions.
    Binary file added thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  7. mbostock revised this gist Jul 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@
    var color = d3.scale.category20();

    var radius = d3.scale.sqrt()
    .range([0, 8]);
    .range([0, 6]);

    var svg = d3.select("body").append("svg")
    .attr("width", width)
  8. mbostock revised this gist Jul 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@
    var force = d3.layout.force()
    .size([width, height])
    .charge(-400)
    .linkDistance(60);
    .linkDistance(function(d) { return radius(d.source.size) + radius(d.target.size) + 20; });

    d3.json("graph.json", function(graph) {
    force
  9. mbostock revised this gist Jul 4, 2012. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@
    var color = d3.scale.category20();

    var radius = d3.scale.sqrt()
    .range([0, 12]);
    .range([0, 8]);

    var svg = d3.select("body").append("svg")
    .attr("width", width)
    @@ -41,7 +41,7 @@
    var force = d3.layout.force()
    .size([width, height])
    .charge(-400)
    .linkDistance(40);
    .linkDistance(60);

    d3.json("graph.json", function(graph) {
    force
    @@ -73,6 +73,7 @@

    node.append("text")
    .attr("dy", ".35em")
    .attr("text-anchor", "middle")
    .text(function(d) { return d.atom; });

    function tick() {
  10. mbostock revised this gist Jul 4, 2012. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@
    var color = d3.scale.category20();

    var radius = d3.scale.sqrt()
    .range([0, 3]);
    .range([0, 12]);

    var svg = d3.select("body").append("svg")
    .attr("width", width)
    @@ -72,9 +72,8 @@
    .style("fill", function(d) { return color(d.atom); });

    node.append("text")
    .attr("x", function(d) { return radius(d.size) + 6; })
    .attr("dy", ".35em")
    .text(function(d) {return d.atom; });
    .text(function(d) { return d.atom; });

    function tick() {
    link.selectAll("line")
  11. mbostock created this gist Jul 3, 2012.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    This is a quick hack to draw two parallel links between nodes. Yes, you can scale it to more than two nodes, but it gets increasingly awkward. The alternative is to compute the paths manually, say by computing the [perpendicular vector](http://mathworld.wolfram.com/PerpendicularVector.html) of the vector between the node centers, and offsetting the links accordingly.
    31 changes: 31 additions & 0 deletions graph.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    {
    "nodes": [
    {"atom": "C", "size": 12},
    {"atom": "C", "size": 12},
    {"atom": "C", "size": 12},
    {"atom": "N", "size": 7},
    {"atom": "H", "size": 1},
    {"atom": "O", "size": 16},
    {"atom": "O", "size": 16},
    {"atom": "H", "size": 1},
    {"atom": "H", "size": 1},
    {"atom": "H", "size": 1},
    {"atom": "H", "size": 1},
    {"atom": "H", "size": 1},
    {"atom": "H", "size": 1}
    ],
    "links": [
    {"source": 0, "target": 1, "bond": 1},
    {"source": 1, "target": 2, "bond": 1},
    {"source": 1, "target": 3, "bond": 1},
    {"source": 2, "target": 5, "bond": 2},
    {"source": 2, "target": 6, "bond": 1},
    {"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}
    ]
    }
    89 changes: 89 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>

    .link line {
    stroke: #696969;
    }

    .link line.separator {
    stroke: #fff;
    stroke-width: 2px;
    }

    .node circle {
    stroke: #000;
    stroke-width: 1.5px;
    }

    .node text {
    font: 10px sans-serif;
    pointer-events: none;
    }

    </style>
    <body>
    <script src="http://d3js.org/d3.v2.min.js?2.9.6"></script>
    <script>

    var width = 960,
    height = 500;

    var color = d3.scale.category20();

    var radius = d3.scale.sqrt()
    .range([0, 3]);

    var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

    var force = d3.layout.force()
    .size([width, height])
    .charge(-400)
    .linkDistance(40);

    d3.json("graph.json", function(graph) {
    force
    .nodes(graph.nodes)
    .links(graph.links)
    .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 * 2 - 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 color(d.atom); });

    node.append("text")
    .attr("x", function(d) { return radius(d.size) + 6; })
    .attr("dy", ".35em")
    .text(function(d) {return d.atom; });

    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>