earthjs
Version:
D3 Earth JS using SVG, Canvas & THREE js, build with some plugins.
59 lines (57 loc) • 2.44 kB
HTML
<html>
<head>
<link type="text/css" rel="stylesheet" href="../css/earthjs.css">
<link type="text/css" rel="stylesheet" href="./style.css">
<script type='text/javascript' src="../js/d3.min.js"></script>
<script type='text/javascript' src="../js/topojson.min.js"></script>
<script type='text/javascript' src="../js/d3-scale-chromatic.v1.min.js"></script>
<script type='text/javascript' src='../js/earthjs.js'></script>
<style id="choropleth"></style>
</head>
<body>
<h4 id="title">CHOROPLETH</h4>
<div class="selected-country">
<div class="content"></div>
</div>
<div class="svg-container">
<svg id="earth-js" class="ej-center"/>
</div>
<script>
const {offsetWidth, offsetHeight} = d3.select('body').node();
const g = earthjs({width: offsetWidth, height: offsetHeight, padding:5})
.register(earthjs.plugins.choroplethCsv('../data/2010_alcohol_consumption_by_country.csv'))
.register(earthjs.plugins.mapSvg('../d/world-hi.json'));
g.choroplethCsv.ready = function(err, json) {
json.forEach(x => x.alcohol = +x.alcohol);
g.choroplethCsv.data(json);
}
g.ready(function(){
const countries = g.mapSvg.allData().countries;
g.choroplethCsv.countries(countries.features);
g.choroplethCsv.mergeData(countries, ['properties.cid:cid', 'properties.value:alcohol']);
g.choroplethCsv.colorize('alcohol', 'schemeReds');
g.choroplethCsv.setColorRange('body');
g.choroplethCsv.setCss('#choropleth');
g.create();
})
g.mapSvg.show = function(data, tooltip) {
const {name, value} = data.properties;
tooltip.html(`${name}<br>ALCOHOL: ${value}`);
_.hover = data;
return tooltip;
};
g.mapSvg.onCountry({
toggle(event, country) {
const {name, value, cid} = country.properties;
const selected = g.mapSvg.selectedCountry() ? 'block' : 'none';
const countryBox = d3.select('.selected-country').style('display', selected);
const title = `${name}<br>ALCOHOL:${value}`;
countryBox.select('.content').html(title);
}
})
d3.select('#title').on('click', function() {
g.mapSvg.resetZoom();
})
</script>
</body>
</html>