Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

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.

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

              
                <div class="container">
	<div class="header">
		<h1>National Contiguity</h1>
		<h3>using Force Directed Graph</h3>
	</div>
	
	<div class="graph">
		<div class="flagbox">
		</div>
	</div>
	<div id="tooltip">
	</div>
</div>

              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Abril+Fatface')

body
	background-color: #FAEBD7
	font-family: 'Abril Fatface', cursive

h1
	margin-bottom: 0px
	text-shadow: 2px 1px 3px #40E0D0
h3
	margin-top: 5px

.header
	text-align: center

.flag
	position: absolute
	border: 1px solid #00ced1

.flagbox
	position: absolute

#tooltip
	display: none
	padding: 3px
	border-radius: 2px
	box-shadow: -2px 2px 10px black
	z-index: 1
	color: #fff
	position: absolute
	background-color: #000

.graph
	width: 900px
	height: 800px
	margin: auto
	box-shadow: -3px 3px 20px blac
	background-color: #483d8b

.link
	stroke-width: 1px
	stroke: #00ced1
              
            
!

JS

              
                const animationStep = 200;
const nodeRadius = 4;
const forceCharge = -100;
const linkDistance = 50;

var graph = d3.select('.graph')

let width = parseInt(graph.style("width"));
let height = parseInt(graph.style("height"));

var svg = graph.append('svg')
	.attr("width", width)
	.attr("height", height);

var tooltip = d3.select("#tooltip")

let request = new XMLHttpRequest();
request.addEventListener("load", loaded);

function loaded() {
	const data = JSON.parse(request.responseText);

	var force = d3.layout.force()
		.size([width, height])
		.nodes(data.nodes)
		.links(data.links)
		.linkDistance(linkDistance)
		.charge(forceCharge);

	var link = svg.selectAll('.link')
		.data(data.links)
		.enter()
		.append('line')
		.attr('class', 'link');

	var node = graph.select('.flagbox').selectAll('.node')
		.data(data.nodes)
		.enter()
		.append('img')
		.attr('class', d => 'flag flag-' + d.code)
		.on("mouseover", (d) => {
			tooltip.style("display", "block");
			tooltip.html(d.country)
				.style("left", d3.event.pageX + "px")
				.style("top", (d3.event.pageY - 28) + "px");
		})
		.on("mouseout", (d) => {
			tooltip.style("display", "none");
		})
		.call(force.drag);
	force.on('tick', function() {
		node.style('left', d => (d.x - 8) + "px")
			.style('top', d => (d.y - 5) + "px");
			link.attr('x1', d => d.source.x)
			.attr('x2', d => d.target.x)
			.attr('y1', d => d.source.y)
			.attr('y2', d => d.target.y);
	});

	force.start();
};

request.open('GET', "https://www.cs.mun.ca/~h65ped/Public/country%20data%20for%20force%20directed%20graph/countries.json", true);
request.send(null);
              
            
!
999px

Console