Skip to content

Instantly share code, notes, and snippets.

@gavlooth
Last active October 28, 2017 06:17
Show Gist options
  • Select an option

  • Save gavlooth/cd95db4376de64599e1b72a844d7018c to your computer and use it in GitHub Desktop.

Select an option

Save gavlooth/cd95db4376de64599e1b72a844d7018c to your computer and use it in GitHub Desktop.
Map Data Across the Globe
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.10.3/babel.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.5/d3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.js"></script>
<style>
body {
width: 100%;
height: 100%;
display: flex;
flex-flow: column;
align-items: center;
justify-content: center;
}
#tooltip {
color: black;
background: #7BCCB5;
padding: .5em;
/*text-shadow: #f5f5f5 0 1px 0;*/
border-radius: 2px;
box-shadow: 0px 0px 2px 0px #a6a6a6;
opacity: 0.8;
position: absolute;
font-size: 50%;
}
.invisible {
display: none;
}
.visible {
display: flex;
flex-flow: column;
justify-content: flex-start;
text-align: left;
align-items: flex-start;
}
#GLOBE {
margin: 10px 10%;
border: 2px solid #000;
border-radius: 5px;
height: 100%;
overflow: hidden;
background: #F0F8FF;
}
.bubble {
fill-opacity: .5;
fill: #ADD8E6;
stroke: #fff;
stroke-width: .5px;
}
.country {
fill: #806517;
}
.border {
fill: none;
stroke: #a6a6a6;
stroke-linejoin: round;
stroke-linecap: round;
}
.graticule {
fill: none;
stroke: #bbb;
stroke-width: .5px;
stroke-opacity: .5;
}
.equator {
stroke: #ccc;
stroke-width: 1px;
}
</style>
</head>
<body>
<div id="GLOBE">
<div id="tooltip" class="invisible">
<span id="Name">
</span>
<span id="Mass">
</span>
<span id="Status">
</span>
<span id="Class">
</span>
<span id="DiscovaryDate">
</span>
</div>
</div>
<script src="/code.js"></script>
</body>
$(document).ready(function() {
$.getJSON('https://raw.githubusercontent.com/hiebj/world-topo/master/world-topo-min.json',
function(wordMapData) {
$.getJSON('https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/meteorite-strike-data.json',
function(meteoriteData) {
const c = document.getElementById("GLOBE"),
width = 960,
height = 600,
countries = topojson.feature(wordMapData, wordMapData.objects.countries).features,
borders = topojson.mesh(wordMapData, wordMapData.objects.countries, (a, b) => a !== b),
graticule = d3.geoGraticule();
let radius = d3.scalePow().exponent(0.4).range([1, 20]).domain(d3.extent(meteoriteData.features, d => d.properties.mass == null ? 0.1 : Number(d.properties.mass)));
_projection = d3.geoMercator()
.translate([(width / 2), (height / 2)])
.scale(width / 2 / Math.PI);
path = d3.geoPath().projection(_projection);
globe = d3.select("#GLOBE")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
worldMap = globe.append("g");
globe.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
worldMap.append("path")
.datum({
type: "LineString",
coordinates: [
[-180, 0],
[-90, 0],
[0, 0],
[90, 0],
[180, 0]
]
})
.attr("class", "equator")
.attr("d", path);
worldMap.selectAll(".country")
.data(countries)
.enter()
.insert("path")
.attr("class", "country")
.attr("d", path)
.attr("id", function(d, i) {
return d.id;
})
.attr("title", function(d, i) {
return d.properties.name;
});
worldMap.append("path")
.datum(borders)
.attr("d", path)
.attr("class", "border");
worldMap.append("g")
.attr("class", "bubble")
.selectAll("circle")
.data(meteoriteData.features)
.enter().append("circle")
.attr("transform", function(d) {
return "translate(" + path.centroid(d) + ")";
})
.attr("r", d => radius(d.properties.mass))
.on("mouseover", d => {
d3.select("#Name").text(+d.properties["name"] == "null" ? "Not available" : ("Name: " + d.properties["name"]));
d3.select("#Mass").text(d.properties.mass == "null" ? "Not available" : ("Mass: " + d.properties.mass));
d3.select("#Class").text("Class: " + d.properties.recclass == "null" ? "Not available" : ("Class: " + d.properties.recclass));
d3.select("#DiscovaryDate").text(d.properties.year == "null" ? "Not available" : ("Discovary Date: " + d3.timeFormat("%B %d, %Y")(new Date(d.properties.year))))
d3.select("#tooltip").style("top", (d3.event.pageY - 10) + "px").style("left", (d3.event.pageX + 10) + "px");
})
.on("mousemove", d => d3.select("#tooltip").attr("class", "visible"))
.on("mouseout", d => d3.select("#tooltip").attr("class", "invisible"));
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment