Built with blockbuilder.org
Last active
December 11, 2019 12:55
-
-
Save Kashif1Naqvi/b29dd40c3dcbe8cb258d30d97bac79ab to your computer and use it in GitHub Desktop.
world map using d3 v5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.sphere { | |
fill:#4242e4; | |
} | |
.country{ | |
/* fill:lightgreen; */ | |
stroke:black; | |
stroke-width:1; | |
stroke-opacity:0.5; | |
} | |
.tooltips{ | |
position: absolute; | |
background-color: #fff; | |
opacity: .9; | |
width:auto; | |
text-align: center; | |
height: auto; | |
} | |
.tooltips ::after{ | |
content: ""; | |
position: absolute; | |
top: 100%; | |
left: 50%; | |
margin-left:-15px; | |
border-width:10px; | |
border-style: solid; | |
border-color: white transparent transparent transparent; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en" dir="ltr"> | |
<head> | |
<meta charset="utf-8"> | |
<title>World Map</title> | |
<link rel="stylesheet" href="index.css"> | |
<link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> | |
</head> | |
<body> | |
<div class="container-fluid"> | |
<h1 class="text-center mt-5 mb-5 text-info font-weight-bold"> World Map </h1> | |
<div id="map" ></div> | |
</div> | |
<script src="https://d3js.org/d3.v5.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/3.0.2/topojson.min.js" ></script> | |
<script src="index.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let height = 500; | |
let width = 1000; | |
let svg = d3.select("#map") | |
.append("svg") | |
.attr("class","container-fluid") | |
.attr("viewBox",[`0 0 ${width} ${height}`]) | |
/* | |
d3.geoAzimuthalEqualArea() | |
geoAzimuthalEquidistant() | |
d3.geoStereographic() | |
d3.geoConicConformal() | |
d3.geoConicEqualArea() | |
geoConicEquidistant | |
*/ | |
let projection = d3.geoNaturalEarth1() | |
let pathGenrator = d3.geoPath().projection(projection) | |
const tooltips = d3.select("#map").append("div").attr("class","tooltips"); | |
svg | |
.append("path") | |
.attr('class', 'sphere') | |
.attr("d",pathGenrator({type:'Sphere'})) | |
d3.json("data.json").then(data=>{ | |
let country = topojson.feature(data,data.objects.countries) | |
let map = country.features.map(d=>d.properties.name) | |
let color = country.features.map(d=>d.properties.color) | |
let id = country.features.map(d=>d.id) | |
svg.selectAll("path") | |
.data(country.features) | |
.enter() | |
.append("path") | |
.attr("d",pathGenrator) | |
.attr("class","country") | |
.attr("fill",(d,i)=>color[i]) | |
.on("mouseover",function(d,i){ | |
tooltips.html("<div>"+map[i]+"</div>") | |
.style("top",(d3.event.pageY - 33) + "px") | |
.style("left",(d3.event.pageX - 40) + "px") | |
d3.select(this) | |
.style("stroke-width",3) | |
.style("stroke","black") | |
.style("stroke-opacity",0.7) | |
}) | |
.on("mouseout",function(d,i){ | |
tooltips.html("") | |
d3.select(this) | |
.style("stroke-width",1) | |
.style("stroke","black") | |
.style("stroke-opacity",0.5) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment