I have created a worldmap using the d3 and now able to create the specific countries to have hover effect , however I have also created the tooltip what I want to do now is to get the country map in the tooltip (the country which is hovered) i have used d3 v4 to do all this.
I have made changes suggested by CodeSmit but it seems I'm missing a lot of things.
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script src="https://d3js.org/d3-queue.v3.min.js"></script>
<style>
#country_name{
border:none;
}
.bigmap {
width: 100%;
height: 100%;
position: center;
background-color: #080e1e;
float:right;
}
.hidden {
display: none;
}
div.tooltip {
color: #222;
background: #19202d;
border-radius: 3px;
box-shadow: 0px 0px 2px 0px #a6a6a6;
padding: .2em;
text-shadow: #f5f5f5 0 1px 0;
opacity: 0.9;
position: absolute;
}
div {
color: #fff;
font-family: Tahoma, Verdana, Segoe, sans-serif;
padding: 5px;
}
.container {
display:flex;
}
.fixed {
text-align:center;
border-width: 1px;
vertical-align:middle;
border-style: solid;
border-color:#55a4bf ;
width: 80px;
margin-right:10px;
}
.flex-item {
border-width: 1px;
text-align:center;
vertical-align:middle;
border-style: solid;
border-color:#55a4bf ;
//background-color:#7887AB;
width: 120px;
}
</style>
</head>
<svg class= "bigmap"width="760" height="340"></svg>
<div class="tooltip"></div>
<body>
<script>
var margin = {top: 0, right: 10, bottom: 10, left: 10};
var width = 760 ;
var height = 400 ;
var projection = d3.geoNaturalEarth1()
.center([0, 15])
.rotate([-11,0])
.scale([150])
.translate([750,350]);
var path = d3.geoPath()
.projection(projection);;
var svg = d3.select(".bigmap")
.append("g")
.attr("width", width)
.attr("height", height);
var tooltip = d3.select("div.tooltip");
d3.queue()
.defer(d3.json, "https://cdn.rawgit.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/world-110m.json")
.defer(d3.tsv, "https://cdn.rawgit.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/world-country-names.tsv")
.await(ready);
function ready(error, world, names) {
if (error) throw error;
var countries1 = topojson.feature(world, world.objects.countries).features;
countries = countries1.filter(function(d) {
return names.some(function(n) {
if (d.id == n.id) return d.name = n.name;
})});
console.log("countries",countries);
var arr = ["India","Sri Lanka","Afghanistan","Russian Federation"];
svg.selectAll("path")
.data(countries.filter(d => d.name !== "Antarctica"))
.enter()
.append("path")
.attr("stroke","#080e1e")
.attr("stroke-width",1)
.attr("fill", "#0d2331")
.attr("d", path )
.on("mouseover",function(d,i){
if (arr.includes(d.name)){
var tipSVG = tooltip.append("svg")
.attr("width", 220)
.attr("height", 55);
var bbox =tipSVG.append("path")
.attr("stroke","#080e1e")
.attr("stroke-width",1)
.attr("fill", "#0d2331")
.attr("d", path(d) )
.node().getBBox()
tipSVG.attr("viewBox", `${bbox.x} ${bbox.y} ${bbox.width} ${bbox.height}`);
tooltip.classed("hidden", false)
.attr("fill","#19202d")
.style("top", 150+ "px")
.style("left", 20 + "px")
.html('<div class="container"><div class="fixed" id="country_name">'+d.name+'</div><div class="flex-item">Fixed 2</div></div><div class="container"><div class="fixed">Fixed 3</div><div class="flex-item">Fixed 4</div></div><div class="container"><div class="fixed">Fixed 5</div><div class="flex-item">Fixed 6</div></div><div class="container"><div class="fixed">Fixed 7</div><div class="flex-item">Fixed 8</div></div>');
d3.select(this).attr("fill","#0d2331").attr("stroke-width",2).style("stroke","#b72c2f")
return tooltip.style("hidden", false).html(d.name);
}
})
.on("mousemove",function(d){
})
.on("mouseout",function(d,i){
if (arr.includes(d.name)){
d3.select(this).attr("fill","#0d2331").attr("stroke-width",1).style("stroke","#080e1e")
tooltip.classed("hidden", true);
}
});
var g = svg.append('g');
g.selectAll('circle')
.data(countries)
.enter().append('circle')
.attr("fill","white")
.attr('transform', function(d) { return 'translate(' + path.centroid(d) + ')'; })
.attr('r',function(d){
if (arr.includes(d.name)){
return "3"
}
return "0";
}
);
};
</script>
</body>
Any guidance or help is greatly appreciated and thanks in advance