map = {
const svg = d3.create("svg");
const fontFamily = "'Helvetica Neue', Helvetica, Arial, sans-serif";
let cities = d3.selectAll(null);
var width = 960,
height = 500,
centered;
var color = d3
.scaleLinear()
.domain([1, 10])
.clamp(true)
.range(["#fff", "#409A99"]);
var projectionFylker = d3
.geoMercator()
.fitSize([width, height], GeoJSONfylker);
var projectionKommuner = d3
.geoMercator()
.fitSize([width, height], GeoJSONkommuner);
var projection = projectionKommuner;
var path = d3.geoPath().projection(projection);
var featuresFylker = GeoJSONfylker.features;
var featuresKommuner = GeoJSONkommuner.features;
var features = featuresKommuner;
svg.attr("width", width).attr("height", height);
svg
.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", clicked);
var g = svg.append("g");
var kommuneLayer = g
.append("g")
.classed("kommune-layer", true)
.style("overflow", "hidden");
var fylkeLayer = g.append("g").classed("fylker-layer", true);
var captionKommuner = g
.append("text")
.classed("big-text", true)
.attr("x", 20)
.attr("y", 45);
function nameFn(d) {
return d && d.properties ? d.properties.navn : null;
}
function nameLength(d) {
var n = nameFn(d);
return n ? n.length : 0;
}
function fillFn(d) {
return color(nameLength(d));
}
function clicked(d) {
var x,
y,
k,
delay = 0;
if (d && centered !== d) {
cities = kommuneLayer.selectAll("g").data([point1]).enter();
console.log("zoom in");
var centroid = path.centroid(d);
x = centroid[0];
y = centroid[1];
k = 4;
centered = d;
} else {
console.log("zoom out");
cities = kommuneLayer.selectAll("a").remove();
x = width / 2;
y = height / 2;
k = 1;
centered = null;
}
kommuneLayer.selectAll("path").style("fill", function (d) {
return centered && d === centered ? "#D5708B" : fillFn(d);
});
const cit = d3.selectAll(".city-link");
cit._groups[0].length == 0 ? (delay = 750) : (delay = 0);
placeCity(d);
g.transition()
.duration(750)
.delay(delay)
.attr(
"transform",
"translate(" +
width / 2 +
"," +
height / 2 +
")scale(" +
k +
")translate(" +
-x +
"," +
-y +
")"
);
}
function placeCity(d) {
const textAndCircle = cities
.append("a")
.attr(
"href",
(d) =>
"https://www.fjordnorway.com/en/destinations/the-stavanger-region"
)
.attr("target", "_blank")
.style("z-index", 10);
const circle = textAndCircle.append("circle");
circle
.classed("city-link", true)
.attr("cx", (d) => {
return projection(d.geometry.coordinates)[0];
})
.attr("cy", (d) => {
return projection(d.geometry.coordinates)[1];
})
.transition()
.duration(750)
.ease(d3.easeBounce)
.attr("r", 10)
.attr("fill", "blue");
textAndCircle
.append("text")
.attr("x", (d) => {
return projection(d.geometry.coordinates)[0] + 10;
})
.attr("y", (d) => {
return projection(d.geometry.coordinates)[1];
})
.attr("dy", "0.35em")
.style("font-family", fontFamily)
.style("font-size", 10)
.text((d) => "Stavanger")
.style("z-index", 9)
.style("background", "white");
}
function mouseover(d) {
d3.select(this).style("fill", "orange");
textCaption(nameFn(d));
}
function mouseout(d) {
kommuneLayer.selectAll("path").style("fill", function (d) {
return centered && d === centered ? "#D5708B" : fillFn(d);
});
captionKommuner.text("");
}
function textCaption(text) {
captionKommuner.style("font-family", fontFamily).text(text);
}
var filter = d3
.select("svg")
.append("defs")
.append("filter")
.attr("id", "my-filter")
.append("feGaussianBlur")
.attr("stdDeviation", 2);
color.domain([0, d3.max(features, nameLength)]);
kommuneLayer
.selectAll("path")
.data(features)
.enter()
.append("path")
.attr("d", path)
.attr("vector-effect", "non-scaling-stroke")
.style("fill", fillFn)
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.on("click", clicked);
fylkeLayer
.selectAll("path")
.data(featuresFylker)
.enter()
.append("path")
.attr("d", path)
.attr("vector-effect", "non-scaling-stroke")
.style("fill", "none")
.attr("stroke", "#777")
.attr("stroke-linejoin", "round");
return svg.node();
}