|
<!DOCTYPE html> |
|
<meta charset="utf-8" /> |
|
<style type="text/css"> |
|
.municipis { |
|
stroke: black; |
|
stroke-width: 0.5px; |
|
stroke-opacity: 0.2; |
|
} |
|
.municipis:hover { |
|
fill-opacity: 0.7; |
|
} |
|
.d3-tip { |
|
font-weight: bold; |
|
font-size: 12px; |
|
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; |
|
padding: 10px; |
|
background: rgba(0, 0, 0, 0.7); |
|
color: #fff; |
|
border-radius: 2px; |
|
} |
|
.d3-tip:after { |
|
box-sizing: border-box; |
|
display: inline; |
|
font-size: 10px; |
|
width: 100%; |
|
line-height: 1; |
|
color: rgba(0, 0, 0, 0.8); |
|
content: "\25BC"; |
|
position: absolute; |
|
text-align: center; |
|
} |
|
.d3-tip.n:after { |
|
margin: -1px 0 0 0; |
|
top: 100%; |
|
left: 0; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
|
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
<script src="https://unpkg.com/[email protected]/index.js"></script> |
|
<script type="text/javascript"> |
|
var margin = { top: 40, right: 10, bottom: 10, left: 10 }, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom; |
|
|
|
var projection = d3 |
|
.geoMercator() |
|
.center([2.5, 41.6]) |
|
.scale(64000); |
|
|
|
var path = d3.geoPath().projection(projection); |
|
|
|
var color = d3 |
|
.scaleQuantize() |
|
.range([ |
|
"rgb(239,243,255)", |
|
"rgb(189,215,231)", |
|
"rgb(107,174,214)", |
|
"rgb(49,130,189)", |
|
"rgb(8,81,156)" |
|
]); |
|
|
|
var tip = d3 |
|
.tip() |
|
.attr("class", "d3-tip") |
|
.offset([-5, 0]) |
|
.html(function(d) { |
|
return ( |
|
"<strong>" + |
|
d.properties.NOM_MUNI + |
|
"</strong>" + |
|
"<br />" + |
|
"<span>Foreign population: <span>" + |
|
"<span style='color: rgb(189,215,231)'>" + |
|
d.properties.percentatge + |
|
"%</span>" |
|
); |
|
}); |
|
|
|
var svg = d3 |
|
.select("body") |
|
.append("svg") |
|
.attr("width", width + margin.left + margin.right) |
|
.attr("height", height + margin.top + margin.bottom) |
|
.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
svg.call(tip); |
|
|
|
d3.csv("dades.csv", function(data) { |
|
color.domain([ |
|
d3.min(data, function(d) { |
|
return d.percentatge; |
|
}), |
|
d3.max(data, function(d) { |
|
return d.percentatge; |
|
}) |
|
]); |
|
|
|
d3.json("maresme.json", function(json) { |
|
for (var i = 0; i < data.length; i++) { |
|
var dataState = data[i].municipi; |
|
var dataValue = parseFloat(data[i].percentatge); |
|
|
|
for (var j = 0; j < json.features.length; j++) { |
|
var jsonState = json.features[j].properties.NOM_MUNI; |
|
|
|
if (dataState == jsonState) { |
|
json.features[j].properties.percentatge = dataValue; |
|
break; |
|
} |
|
} |
|
} |
|
|
|
svg |
|
.selectAll("path") |
|
.data(json.features) |
|
.enter() |
|
.append("path") |
|
.attr("d", path) |
|
.attr("class", "municipis") |
|
.on("mouseover", tip.show) |
|
.on("mouseout", tip.hide) |
|
.attr("fill", function(d) { |
|
var value = d.properties.percentatge; |
|
|
|
if (value) { |
|
return color(value); |
|
} else { |
|
return "#ccc"; |
|
} |
|
}); |
|
}); |
|
}); |
|
</script> |