geo2square = {
let svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height)
.attr('id', 'mapArea');
let bg = svg
.append('rect')
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height)
.attr('fill', '#000');
let mapGroup = svg.append("g").attr("id", "map");
let areaScale = d3
.scaleLinear()
.domain(
d3.extent(areas.features, d => parseFloat(d.properties.area_numbe))
);
let toggle = 0;
mapGroup
.selectAll('.areas')
.data(areas.features)
.enter()
.append('path')
.attr('class', 'areas')
.attr('d', d => d.properties.geoPath)
.attr('opacity', 1)
.attr('fill', d =>
d3.interpolateWarm(areaScale(parseFloat(d.properties.area_numbe)))
)
.on('click', function(clickedArea) {
if (toggle == 0) {
d3.selectAll('.areas')
.data(areas.features)
.transition()
.duration(1000)
.attr('opacity', function(d) {
if (d.properties.area_numbe == 1) {
return 0;
} else {
return 1;
}
})
.transition()
.duration(1000)
.attr('fill', d =>
d3.interpolateCool(areaScale(parseFloat(d.properties.area_numbe)))
)
.attr('d', function(d) {
if (d.properties.area_numbe == 1) {
return d.properties.geoPath;
} else {
return d.properties.squarePath;
}
})
.attr('transform', function(d) {
let xMove =
width / 2 - d.properties.squareX + parseInt(d.properties.x);
let yMove = parseInt(d.properties.y) - d.properties.squareY;
return 'translate(' + xMove + ',' + yMove + ')';
});
toggle = 1;
} else {
mapGroup
.selectAll('.areas')
.data(areas.features)
.transition()
.duration(1000)
.attr('fill', d =>
d3.interpolateCool(areaScale(parseFloat(d.properties.area_numbe)))
)
.attr('d', d => d.properties.geoPath)
.attr('fill', d =>
d3.interpolateWarm(areaScale(parseFloat(d.properties.area_numbe)))
)
.attr('transform', "")
.transition()
.duration(1000)
.attr('opacity', 1);
toggle = 0;
}
});
return svg.node();
}