Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:49
Show Gist options
  • Save rfprod/1c86e2b03558b5e1961a to your computer and use it in GitHub Desktop.
Save rfprod/1c86e2b03558b5e1961a to your computer and use it in GitHub Desktop.
Meteorite Strke Data Across the Globe
<div class="container-fluid nopadding">
<nav class="navbar navbar-inverse navbar-fixed-top topnav" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#toggle-nav" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand font-effect-neon" target=_blank href="http://codepen.io/rfprod"><span class="glyphicon glyphicon-wrench"></span> RFProd</a>
</div>
<div class="collapse navbar-collapse" id="toggle-nav">
<div class="container-fluid">
<ul class="nav navbar-nav navbar-right font-effect-emboss">
<li class="nav-tabs"><a href="#approot"><span class="glyphicon glyphicon-stats"></span> Meteorite Strike Data Across the Globe</a></li>
<li class="nav-tabs"><a href="https://gist.github.com/rfprod/1c86e2b03558b5e1961a" target=_blank><span class="glyphicon glyphicon-download-alt" ></span> GIST</a></li>
</ul>
</div>
</div>
</nav>
<a name="approot"></a>
<div class="home sect">
<div class="container-fluid">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<h2 class="hidden-md hidden-lg hidden-xl"><span class="glyphicon glyphicon-stats"></span> Meteorite Strike Data Across the Globe</h2>
<div id="output">
Output
</div>
<span class="credits">info <a href="https://d3js.org/" target=_blank>D3.js</a> | <a href="https://data.nasa.gov/resource/y77d-th95.json" target=_blank>data</a> <br/>licence <a href="http://www.gnu.org/licenses/gpl-3.0.en.html" target=_blank>GPL 3.0</a></span>
</div>
</div>
</div>
</div>

Meteorite Strike Data Across the Globe

User can see where all Meteorites landed on a world map. User can tell the relative size of the meteorite, just by looking at the way it's represented on the map. User can mouseover the meteorite's data point for additional data. User can mouseover the country region for country name.

A Pen by V on CodePen.

License.

$(document).on('ready',function(){
(function (){
var UserInstructionsAndGDPChart = React.createClass({
render: function(){
return (
<span>
<p id="user-instructions">Meteorite strike data visualization using D3.js.</p>
<div id="map"></div>
</span>
);
}
});
ReactDOM.render(<UserInstructionsAndGDPChart />,document.getElementById('output'));
var chartContainerObj = d3.select('#map');
d3.json('https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/meteorite-strike-data.json', function(err,meteorJson){
if (err) console.log('error: '+err);
console.log('meteorite-strike-data.json loaded');
var meteorFeatures = meteorJson.features;
/* commented section is for debugging purposes only
for (var i=0;i<meteorFeatures.length;i++){
var meteorDataObj = meteorFeatures[i];
meteorDataObj.geometry != null ?
console.log(meteorDataObj.geometry.type+' / '+meteorDataObj.geometry.coordinates[0]+' - '+meteorDataObj.geometry.coordinates[1])
: console.log('no geometry data');
var meteorProps = meteorDataObj.properties;
console.log(meteorProps.id+' | '+meteorProps.name+' | '+meteorProps.fall+' | '+meteorProps.mass+' | '+meteorProps.recclass+' | '+meteorProps.reclat+' | '+meteorProps.reclong+' | '+meteorProps.year);
}
*/
d3.json('https://raw.githubusercontent.com/hiebj/world-topo/master/world-topo-min.json', function(err,mapJson){
if (err) console.log('error: '+err);
console.log('world-topo-min.json loaded');
var div = d3.select("body").append("div").attr("class", "tooltip").style("opacity", 0);
var tooltip = d3.select('.tooltip');
var xCursorPosition = 0, yCursorPosition = 0;
var width = $('#map').width()-20, height = 400;
var restHeight = $(window).height() - $('nav').height()*2 - $('.home').find('h2').height() - $('#user-instructions').height() - $('.credits').height()*2;
height = restHeight;
// add meteorites data
function addpoint(lat,lon,text) {
var gpoint = g.append("g").attr("class", "gpoint");
var x = projection([lat,lon])[0];
var y = projection([lat,lon])[1];
gpoint.append("svg:circle")
.attr("cx", x)
.attr("cy", y)
.attr("class","point")
.attr("r", 1.5);
if(text.length>0){
gpoint.append("text")
.attr("x", x+2)
.attr("y", y+2)
.attr("class","text")
.text(text);
}
}
// zooming START
function move() {
var t = d3.event.translate;
var s = d3.event.scale;
var zscale = s;
var h = height/4;
t[0] = Math.min((width/height) * (s - 1),Math.max(width * (1 - s), t[0]));
t[1] = Math.min(h * (s - 1) + h * s,Math.max(height * (1 - s) - h * s, t[1]));
zoom.translate(t);
d3.selectAll("path").attr("transform", "translate(" + t + ")scale(" + s + ")");
d3.selectAll("circle").attr("transform", "translate(" + t + ")scale(" + s + ")");
}
var zoom = d3.behavior.zoom()
.scaleExtent([1, 15])
.on("zoom", move);
// zooming END
chartContainerObj.append('svg').call(zoom).attr('class','chart');
//chartContainerObj.append('svg').attr('class','chart'); // without zoom
var chartObj = d3.select('.chart').attr("width", width).attr("height", height);
var projection = d3.geo.kavrayskiy7().scale(d3.min([$('.chart').height()/320,$('.chart').width()/640])*100).translate([width/2,height/2]),
color = d3.scale.category20(),
graticule = d3.geo.graticule();
var path = d3.geo.path().projection(projection);
chartObj.append('path')
.datum(graticule)
.attr('class','graticule')
.attr('d',path);
chartObj.append('path')
.datum(graticule.outline)
.attr('class','graticule outline')
.attr('d',path);
var countries = topojson.feature(mapJson,mapJson.objects.countries).features,
neighbours = topojson.neighbors(mapJson.objects.countries.geometries);
chartObj.selectAll('.country')
.data(countries).enter()
.append('path','.graticule')
.attr('class','country')
.attr('d',path)
.style('fill',function(val,i){
return color(val.color = d3.max(neighbours[i], function(n){return countries[n].color;})+1|0);
})
.on("mouseover", function(){
document.onmousemove = function(e){
xCursorPosition = e.pageX;
yCursorPosition = e.pageY;
}
d3.select(this).transition()
.duration(100)
.style("opacity", .3);
div.transition()
.duration(100)
.style("opacity", .9);
})
.on("mousemove", function(val){
tooltip.style("left",xCursorPosition+30+"px");
tooltip.style("top",yCursorPosition-30+"px");
tooltip.html(val.properties.name);
})
.on("mouseout", function(){
document.onmousemove = null;
d3.select(this).transition()
.duration(400)
.style("opacity", 1);
div.transition()
.duration(400)
.style("opacity", 0);
});
chartObj.selectAll('circle')
.data(meteorJson.features).enter()
.append('circle')
.attr('class', function(val,i){return 'meteor '+i;})
.attr('cx', function(val){return projection([val.properties.reclong, val.properties.reclat])[0];})
.attr('cy', function(val){return projection([val.properties.reclong, val.properties.reclat])[1];})
.attr('r', function(val){
var radius = 0;
if (val.properties.mass != null){
var ceiledString = Math.ceil(val.properties.mass).toString();
var k = ceiledString.length;
return (k-2)*0.5;
}else return 0.5;
})
.style('fill', 'red')
.style('fill-opacity', 0.5)
.style('stroke', 'black')
.style('stroke-width', 0.1)
.on("mouseover", function(){
document.onmousemove = function(e){
xCursorPosition = e.pageX;
yCursorPosition = e.pageY;
}
d3.select(this).transition()
.duration(100)
.style("opacity", .3);
div.transition()
.duration(100)
.style("opacity", .9);
})
.on("mousemove", function(){
tooltip.style("left",xCursorPosition+30+"px");
tooltip.style("top",yCursorPosition-30+"px");
var index = d3.select(this).attr('class').substr(7,d3.select(this).attr('class').length-1);
var dataObj = meteorFeatures[index].properties;
tooltip.html('Id: '+dataObj.id+'<br>Name: '+dataObj.name+'<br>Fall: '+dataObj.fall+'<br>Mass: '+dataObj.mass+'<br>Class: '+dataObj.recclass+'<br>Latitude: '+dataObj.reclat+'<br>Longitude: '+dataObj.reclong+'<br>Year: '+dataObj.year);
})
.on("mouseout", function(){
document.onmousemove = null;
d3.select(this).transition()
.duration(400)
.style("opacity", 1);
div.transition()
.duration(400)
.style("opacity", 0);
});
});
});
})();
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/d3.geo.projection.v0.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://datamaps.github.io/scripts/datamaps.world.min.js?v=1"></script>
$black: #000000
$white: #ffffff
$grey: #bfbfbf
$blue: #3366cc
$green: #33cc33
$darkblue1: #000066
$darkblue2: #0000ff
$lightyellow: #ffff99
$yellow: #ffff00
$orange: #ff6600
$red: #ff3300
body
color: $black
font-family: 'Play', sans-serif
font-size: 2.2em
overflow-x:hidden
.tooltip
position: absolute
text-align: center
width: auto !important
height: auto !important
padding: 2px
font: 12px sans-serif
background: lightsteelblue
border: 0px
border-radius: 8px
pointer-events: none
.nopadding
padding: 0
.navbar-brand
font-size: 1em
.home
min-height: 92vh
height: auto !important
.sect
padding-top: 8vh
.githublogo
height: 1em
h2
text-align: center
font-weight: bold
#output
text-align: center
width: 100%
height: auto !important
#user-instructions
text-align: center
font-size: 0.75em
margin-top: 1em
#map
display: block
background-color: $grey
padding-top: 1em
padding-left: 1em
.chart
width: 97%
.country
fill: #ccc
stroke: #fff
stroke-width: 0.25px
stroke-linejoin: round
.graticule
fill: none
stroke: #000
stroke-opacity: 0.3
stroke-width: 0.5px
.graticule.outline
stroke: #333
stroke-opacity: 1
stroke-width: 1.5px
.hidden
display: none
a
text-decoration: none
.credits
display: block
text-align: center
font-size: 0.75em
a:hover
text-decoration: none
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<link href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.3/animate.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Play&effect=neon" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment