cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="wrapper">
  <div class="content">
    <h1 class="header">A Simple Kevin Bacon Graph</h1>
  </div>
</div>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
            
          
!
            
              body {
  font-family: 'Avenir', 'Droid Sans', serif;
  width: 100%;
}
.wrapper {
  display: block;
  margin: 0 auto;
  margin-top: 20px;
}
.header {
  text-align: center;
}
.content {
  margin-left: 30px;
  margin-right: 30px;
}

/* SVG styles */
.main {
  display: block;
  margin: auto;
  margin-top: 50px;
}

            
          
!
            
              var w = 800;
var h = 500;
var r = 5;  // node radius

var nodes = [
    { "actor": "Kevin Bacon" },
    { "actor": "Tom Hanks" },
    { "actor": "Meg Ryan" },
    { "actor": "Parker Rosey" },
    { "actor": "Lisa Kudrow" },
    { "actor": "W.C. Fields" },
    { "actor": "George Burns" },
    { "actor": "Michael Fassbender" },
    { "actor": "James McAvoy" }
];

var links = [
  { source: nodes[0], target: nodes[1] },
  { source: nodes[0], target: nodes[2] },
  { source: nodes[1], target: nodes[2] },
  { source: nodes[0], target: nodes[3] },
  { source: nodes[2], target: nodes[3] },  
  { source: nodes[3], target: nodes[4] },  
  { source: nodes[2], target: nodes[3] },  
  { source: nodes[5], target: nodes[6] }, 
  { source: nodes[7], target: nodes[8] }, 
  { source: nodes[0], target: nodes[7] }, 
  { source: nodes[0], target: nodes[8] }    
];

var svg = d3.selectAll(".wrapper")
  .append("svg")
  .attr("class", "main")
  .attr("width", w)
  .attr("height", h);

var force = d3.layout.force()
  .nodes(nodes)
  .links([])
  .gravity(0.1)
  .charge(-700)
  .size([w, h]);

var link = svg.selectAll(".link")
  .data(links)
  .enter().append("line")
  .attr("class", "link")
  .attr("stroke", "#011627")
  .attr("fill", "none");

var node = svg.selectAll(".node")
  .data(nodes)
  .enter().append("g")
  .attr("class", "node")
  .on("mouseover", function(d, i) {
    if (i > 0) {
      // Node
      d3.select(this).selectAll("circle")
      .transition()
      .duration(250)
      .style("cursor", "none")     
      .attr("r", r + 3)
      .attr("fill", "#E71D36");

      // Text
      d3.select(this).select("text")
      .transition()
      .style("cursor", "none")     
      .duration(250)
      .style("cursor", "none")     
      .attr("font-size","1.5em")
      .attr("x", 15 )
      .attr("y", 5 )
    } 
    else {
      // Node
      d3.select(this).selectAll("circle")
      .style("cursor", "none")     

      // Text
      d3.select(this).select("text")
      .style("cursor", "none")     
    }
  })
  .on("mouseout", function(d, i) {
    if (i > 0) {
      // Node
      d3.select(this).selectAll("circle")
        .transition()
        .duration(250)
        .attr("r", r)
        .attr("fill", "#E71D36");

        // Text
      d3.select(this).select("text")
        .transition()
        .duration(250)
        .attr("font-size","1em")
        .attr("x", 8 )
        .attr("y", 4 )
    }
  })
  .call(force.drag);

node.append("circle")
  .attr("cx", function(d) { return d.x; })
  .attr("cy", function(d) { return d.y; })
  .attr("r", r)
  .attr("fill", "#E71D36");

node.append("text")
  .text(function(d, i) { return d.actor; })
  .attr("x", function(d, i) { return r + 5; })
  .attr("y", function(d, i) { 
    if (i > 0) { 
      return r; 
    }
    else { 
      return 8; 
    } 
  })
  .attr("font-family",  "Avenir")
  .attr("fill", function(d, i) { 
    return "#011627"; 
  })
  .attr("font-size", function(d, i) { 
    return "1em"; 
  })
  .attr("text-anchor", function(d, i) { 
    if (i>0) { 
      return "beginning"; 
    }      
    else { 
      return "end"
    } 
  });

force.on("tick", function(e) {
  node.attr("transform", function(d, i) {     
    return "translate(" + d.x + "," + d.y + ")"; 
  });
    
   link.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; })
});

force.start();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console