France Legislative Districts Cartogram
Here's a web app that converts France's legislative districts into a cartogram where each district is approximately the same size using D3.js:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>France Legislative Districts Cartogram</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://d3js.org/topojson.v3.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
#container {
display: flex;
flex-direction: column;
align-items: center;
}
#map-container {
margin-top: 20px;
border: 1px solid #ccc;
}
#controls {
margin-bottom: 20px;
}
button {
padding: 8px 16px;
margin: 0 5px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="container">
<h1>France Legislative Districts Cartogram</h1>
<div id="controls">
<button id="original">Original Map</button>
<button id="cartogram">Cartogram</button>
</div>
<div id="map-container"></div>
</div>
<script>
const width = 800;
const height = 600;
const targetArea = 1000; // Target area for each district
// Create SVG container
const svg = d3.select("#map-container")
.append("svg")
.attr("width", width)
.attr("height", height);
// Load the TopoJSON data
d3.json("https://raw.githubusercontent.com/AtelierCartographie/Khartis/refs/heads/master/public/data/map/FR-circ-2017/france.json")
.then(function(data) {
// Convert TopoJSON to GeoJSON
const districts = topojson.feature(data, data.objects.france);
// Create a projection and path generator
const projection = d3.geoMercator()
.fitSize([width, height], districts);
const path = d3.geoPath()
.projection(projection);
// Draw the original map
svg.append("g")
.selectAll("path")
.data(districts.features)
.enter()
.append("path")
.attr("d", path)
.attr("fill", "#69b3a2")
.attr("stroke", "white")
.attr("stroke-width", 0.5)
.attr("class", "district")
.attr("opacity", 0.7);
// Add hover effect
svg.selectAll(".district")
.on("mouseover", function() {
d3.select(this).attr("opacity", 1);
})
.on("mouseout", function() {
d3.select(this).attr("opacity", 0.7);
});
// Create cartogram when button is clicked
d3.select("#cartogram").on("click", function() {
createCartogram(districts);
});
// Show original map when button is clicked
d3.select("#original").on("click", function() {
svg.selectAll(".district").remove();
svg.append("g")
.selectAll("path")
.data(districts.features)
.enter()
.append("path")
.attr("d", path)
.attr("fill", "#69b3a2")
.attr("stroke", "white")
.attr("stroke-width", 0.5)
.attr("class", "district")
.attr("opacity", 0.7);
});
function createCartogram(geojson) {
// Clear the SVG
svg.selectAll("*").remove();
// Calculate bounding boxes for each district
const boxes = geojson.features.map(feature => {
const bounds = path.bounds(feature);
const width = bounds[1][0] - bounds[0][0];
const height = bounds[1][1] - bounds[0][1];
const area = width * height;
// Calculate scale factor to reach target area
const scale = Math.sqrt(targetArea / area);
return {
feature: feature,
x: bounds[0][0] + width / 2,
y: bounds[0][1] + height / 2,
width: width * scale,
height: height * scale,
originalWidth: width,
originalHeight: height
};
});
// Create a force simulation to position the boxes without overlap
const simulation = d3.forceSimulation(boxes)
.force("x", d3.forceX(width / 2).strength(0.05))
.force("y", d3.forceY(height / 2).strength(0.05))
.force("collide", d3.forceCollide(d => Math.max(d.width, d.height) / 2).iterations(10))
.stop();
// Run the simulation
for (let i = 0; i < 300; ++i) simulation.tick();
// Draw the cartogram
const g = svg.append("g");
g.selectAll("rect")
.data(boxes)
.enter()
.append("rect")
.attr("x", d => d.x - d.width / 2)
.attr("y", d => d.y - d.height / 2)
.attr("width", d => d.width)
.attr("height", d => d.height)
.attr("fill", "#69b3a2")
.attr("stroke", "white")
.attr("stroke-width", 0.5)
.attr("class", "district-rect")
.attr("opacity", 0.7);
// Add hover effect
g.selectAll(".district-rect")
.on("mouseover", function() {
d3.select(this).attr("opacity", 1);
})
.on("mouseout", function() {
d3.select(this).attr("opacity", 0.7);
});
}
})
.catch(function(error) {
console.error("Error loading the TopoJSON data:", error);
});
</script>
</body>
</html>
How This Works:
-
Data Loading: The app loads the TopoJSON data of France's legislative districts from the provided URL.
-
Original Map Display: It first displays the original geographic map using D3's geoPath generator.
-
Cartogram Creation:
- For each district, it calculates the bounding box and its area.
- It then scales each district's bounding box to have approximately the same area (controlled by the
targetArea variable).
- Uses D3's force simulation with collision detection to position these scaled rectangles without overlaps.
-
Interactivity:
- Buttons allow switching between the original map and the cartogram view.
- Hover effects highlight individual districts.
-
Visual Design:
- Districts are colored in a teal color (#69b3a2) with white borders.
- The cartogram uses rectangles to represent each district with equalized area.
You can adjust the targetArea variable to make the districts larger or smaller in the cartogram view. The force simulation parameters can also be tweaked to change how the rectangles are packed together.