Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>If you can see this, something broke</h1>
<h2>World State Contiguity Graph</h2>
<div class="root"></div>
<button id="dispToggle">Toggle Basic Display</button>
<p>Background Image Credit: <a href="http://coolinfographics.com/blog/2016/6/3/the-global-air-transportation-network.html" target="_blank">http://coolinfographics.com/blog/2016/6/3/the-global-air-transportation-network.html</a></p>
              
            
!

CSS

              
                body {
  background-color: #ecf0f1;
  // background-image: url("https://www.martingrandjean.ch/wp-content/uploads/2016/05/airports-map.png");
  // background-image: url("https://images.squarespace-cdn.com/content/v1/5bfc8dbab40b9d7dd9054f41/1546536945064-I274NMOL541FUC7PQW75/ke17ZwdGBToddI8pDm48kLAc1mZZ8w29KdMldU4BXwdZw-zPPgdn4jUwVcJE1ZvWQUxwkmyExglNqGp0IvTJZUJFbgE-7XRK3dMEBRBhUpxcdTlaiiQyCdZv_-k0-ue9n3jqPGJOHB8BX8cyPfQJLw-USXOwGRIEZxkJYp5BL3E/airports-map.png?format=750w");
  // background-image: url("https://cdn.pixabay.com/photo/2018/07/08/17/51/network-3524352_960_720.jpg");
  background-image: url("https://cdn.pixabay.com/photo/2020/02/15/14/33/network-4851119_960_720.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;

}

h2 {
  color: white;
  text-align: center;
  font-family: 'Poiret One', 'Open sans', 'Josefin Sans', Arial
}

.links line {
  stroke: #999;
  stroke-opacity: 0.6;
}

.nodes circle {
  stroke: #fff;
  stroke-width: 1.5px;
}

.nodes rect {
  stroke: #fff;
  stroke-width: 1.5px;
}

#dispToggle {
  position: fixed;
  right: 0;
  bottom: 0;
}

p, a {
  color: #7e8c8d;
  position: fixed;
  bottom: 0;
  &:hover, &:visited, &:link {
    color: #7e8c8d;
    text-decoration: none;
  }
}
              
            
!

JS

              
                $(document).ready(function () {

d3.selectAll('h1').style("display", "none");

var url = "https://raw.githubusercontent.com/DealPete/forceDirected/master/countries.json";

$.getJSON(url, function (json) {

var schema = {
  nodes: [
    {
      "country": "Canada",
      "code": "ca"
    }
  ],
  links: [
    {
      "target": 66,
      "source": 0
    }
  ]
}

var graphHeight   = Math.min(600, ($(window).height()-20)),
    graphWidth    = Math.min(960, ($(window).width()-20)),
    radius        = 5,
    simpleDisplay = false;

var canvas = d3.select('.root')
                .append('svg')
                .attr("width", graphWidth)
                .attr("height", graphHeight)
                .attr("transform", "translate(" + (($(window).width() - 0 - graphWidth)/2) + ", " + (($(window).height() - 100 - graphHeight)/2) + ")")
                .style("background-color", "rgba(255,255,255,0.9)")
                .style("border", "1px solid black");

var simulation = d3.forceSimulation()
                    .nodes(json.nodes);

var customManyBody = d3.forceManyBody()
                        .strength(-50)
                        .distanceMax(130);

var customForceCenter = d3.forceCenter(graphWidth/2, graphHeight/2)
                          //.strength(1);

var link_force = d3.forceLink(json.links)
                    .strength(1.3)
                    // .id(function (d) { return d.country; });

var colide_force = d3.forceCollide(radius)
                      .strength(4);

simulation.force("charge_force", customManyBody)
          .force("center_force", customForceCenter) //d3.forceCenter(graphWidth/2, graphHeight/2)
          .force("links", link_force)
          .force("colide", colide_force);


var link = canvas.append('g')
                  .attr("class", "links")
                  .selectAll('line')
                  .data(json.links)
                  .enter()
                  .append('line')
                  .attr("stroke-width", 2);

var node = canvas.append('g')
                  .attr("class", "nodes")
                  .selectAll('image')
                  .data(json.nodes)
                  .enter()
                  // .append('img')
                  // .attr("class", function (d) {
                  //   return "flag flag-" + d.code
                  // })

                  // .append('image')
                  // .attr("href", "https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png")
                  // .attr("transform", "translate(" + -radius + ", " + -radius + ")")
                  // .attr("width", radius*2)
                  // .attr("height", radius*2);

                  .append('image')
                  .attr("href", function (d) {
                    return "https://www.geonames.org/flags/x/" + d.code + ".gif";
                  })
                  .attr("class", "flag flag-cz")
                  .attr("transform", "translate(" + -radius + ", " + -radius + ")")
                  .attr("width", radius*2)
                  .attr("height", radius*2)

                  .on("mouseover", function (d) {
                    var xPos = d3.mouse(this)[0] + 10;
                    var yPos = d3.mouse(this)[1] - 40;
                    toolTip.style("display", null)
                            .attr("transform", "translate(" + xPos + ", " + yPos + ")")
                    toolTipText.text(d.country)
                    toolTipBack.attr("width", function () { return d.country.length * 8 })
                  })
                  .on("mousemove", function (d) {
                    var xPos = d3.mouse(this)[0] + 10;
                    var yPos = d3.mouse(this)[1] - 40;
                    toolTip.attr("transform", "translate(" + xPos + ", " + yPos + ")")
                  })
                  .on("mouseout", function() {
                    toolTip.style("display", "none")
                  })


                  // .append('rect')
                  // .attr("transform", "translate(" + -radius + ", " + -radius + ")")
                  // .attr("width", radius*2)
                  // .attr("height", radius*2)
                  // .attr("fill", "red");

var toolTip = canvas.append('g')
                    .style("display", "none")

var toolTipBack = toolTip.append('rect')
                         .attr("width", "100")
                         .attr("height", "18")
                         .attr("fill", "white");

var toolTipText = toolTip.append("text")
                          .text("placeholder")
                          .attr("x", "4")
                          .attr("y", "14")


function tickActions() {
  node.attr("x", function(d) { return d.x = Math.max(radius, (Math.min(graphWidth-(radius*2), d.x))); })
      .attr("y", function(d) { return d.y = Math.max(radius, (Math.min(graphHeight-radius, 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 })
}

function drag_start(d) {
  if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  d.fx = d.x;
  d.fy = d.y;
}

function drag_drag(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function drag_end(d) {
  if (!d3.event.active) simulation.alphaTarget(0).restart();
  d.fx = null;
  d.fy = null;
}

var drag_handler = d3.drag()
                      .on("start", drag_start)
                      .on("drag", drag_drag)
                      .on("end", drag_end);

drag_handler(node);

simulation.on("tick", tickActions);


var testFlag = canvas.append('img')
                      //.attr("src", "blank.gif")
                      .attr("class", "flag flag-cz")
                      .attr("alt", "Czech Republic")
                      //.style("display", "block")
                      .attr("x", "30")
                      .attr("y", "30");
      //redners but is invisible whatup w that


// var iestImg = canvas//.append('rect')
// // .attr("width", "200")
// // .attr("height", "200")
// // .attr("fill", "none")
//                     .append('image')
//                     .attr("href", "https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png")
//                     .attr("width", "200")
//                     .attr("height", "200")
//                     .attr("x", "0")
//                     .attr("y", "0");


function toggleDisplay() {
  switch(simpleDisplay) {
    case false:
    d3.select('body')
      .style("background-image", 'none')
      .style("background-color", "white");
    canvas.style("background-color", "white")
    d3.selectAll('h2')
      .style("color", "black")
      .style("font-family", "arial")
    simpleDisplay = true;
    break;
    case true:
    d3.select('body')
      .style("background-image", null)
      .style("background-color", "none");
    canvas.style("background-color", "rgba(255,255,255,0.9)");
    d3.selectAll('h2')
      .style("color", null)
      .style("font-family", null)
    simpleDisplay = false;
    break;

  }
}

$('#dispToggle').on("click", function () {
  toggleDisplay();
})



});



});

              
            
!
999px

Console