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

              
                h1 National Contiguity Force Directed Graph
#svg-container
footer a freeCodeCamp project by kbaig
              
            
!

CSS

              
                body
	min-height: 90vh
	display: flex
	flex-direction: column
	justify-content: center
	align-items: center
	background: #eee

	.tooltip
		position: absolute
		background: #111
		border: 1px solid #555
		border-radius: 4px
		padding: 10px 15px
		text-align: center
		color: #fff
	
	#svg-container
		position: relative

		svg
			background: #fff
			border: 1px solid #aaa
			box-shadow: 1px 1px 10px 0 #aaa
	
		.flag-icon
			position: absolute
			transform: translate(-50%, -50%)
			cursor: pointer
			

	footer 
		order: 2
		margin-top: 25px
		font-family: sans-serif
		font-size: 14px
		color: #222


              
            
!

JS

              
                fetch("https://raw.githubusercontent.com/DealPete/forceDirected/master/countries.json")
	.then(res => res.json())
	.then(json => plot(json));

const plot = response => {
	const l = 1750;
	const padding = 20;
	
	const nodes = response.nodes;
	const links = response.links;
	
	const svg = d3
		.select('#svg-container')
		.append("svg")
		.attr("width", l)
		.attr("height", l);

	const simulation = d3
		.forceSimulation(nodes)
		.force('charge', d3.forceManyBody())
		.force('link', d3.forceLink(links))
		.force('center', d3.forceCenter(l / 2, l / 2 + 20));
	
	// tooltip div
	const tooltip = d3
		.select("body")
		.append("div")
		.attr("class", "tooltip")
		.style('display', 'none')
		.style("opacity", 0);

	// drag functions
	const dragStarted = d => {
		if (!d3.event.active) simulation.alphaTarget(0.3).restart();
		d.fx = d.x;
		d.fy = d.y;
	}

	const dragged = d => {
		d.fx = d3.event.x;
		d.fy = d3.event.y;
	}

	const dragEnded = d => {
		if (!d3.event.active) simulation.alphaTarget(0);
		d.fx = null;
		d.fy = null;
	}
	
	// append nodes and links
	const nodeSelector = d3
		.select('#svg-container')
		.selectAll('span')
		.data(nodes)
		.enter()
		.append('span')
		.attr('class', d => 'flag-icon flag-icon-' + d.code);
	
	nodeSelector	
		.call(d3.drag()
					.on('start', dragStarted)
					.on('drag', dragged)
					.on('end', dragEnded));
		
	nodeSelector
		.on("mouseover", d => {
			tooltip
				.transition()
				.duration(200)
				.style('display', 'block')
				.style("opacity", 0.8);
		
			tooltip
				.text(d.country)
				.style("left", d3.event.pageX + "px")
				.style("top", d3.event.pageY + "px");		
		})
		.on("mouseout", d => {
			tooltip
				.transition()
				.style('display', 'none')
				.style("opacity", 0)
				.duration(300);
		});
	
	const linkSelector = svg
		.selectAll('line')
		.data(links)
		.enter()
		.append('line')
		.attr('stroke', '#666');

	// reposition nodes and links on tick
	simulation.on('tick', () => {
		nodeSelector
			.style('left', d => d.x + 'px')
			.style('top', d => d.y + 'px');
		
		linkSelector
			.attr('x1', d => d.source.x)
			.attr('y1', d => d.source.y)
			.attr('x2', d => d.target.x)
			.attr('y2', d => d.target.y);
	});
	

};

              
            
!
999px

Console