Public
Edited
Nov 21, 2023
4 forks
31 stars
Topographic MappingBubble mapChoroplethAccess to Family planningMD Counties Total Cases MapTendance de la production des déchets en Union EuropéenneAndy's Walgreens COVID-19 Tracker TrackerElection Maps for Incomplete ResultsA better U.S. house election results map?1983 Mayoral Election, Dot density mapsMastodon 🐘Cheat sheet bertinBertin.js: regular squaresWaterlinesNeumorphism Contour Density MapCartographic DoodlesStars and constellationsPlot: Grid choroplethHello Polygon Morphing
Mapping with pie charts
U.S. Geographic DataHow big are countries... like really!AttitudeB&W ChoroplethWeb Mercator Tile VisibilityMARTINI: Real-Time RTIN Terrain Mesh"Magnifying-Glass" projectionsTissot's indicatrixAntipodal mapMapping gridded data with a Voronoi diagramA Map of Every BuildingUrbano Monti’s Planisphere (1587)Bivariate choroplethDIY HillshadeMapbox Map MakerWorld tourHillshaderSimplified Earth with curved shapesHexbin mapInner glowNicolosi vs. StereographicData-driven projections: Darwin's worldSatellite ground track visualizerDirection to shoreHello, OpenLayers!U.S. airports VoronoiHello, NYC Geosearch API!Mapbox Fly-ToSpilhaus shoreline mapWalmart’s growthHow well does population density predict U.S. voting outcomes?Drawing maps from geodata with D3 & ObservableHexgrid maps with d3-hexgridTissot's indicatrixWorld airports VoronoiSwiss Elevation Line GraphsVector tilesVersor draggingOrthographicSolar TerminatorStreaming ShapefilesFake GlobesPeirce Quincuncial🍃 LeafletU.S.G.S. World Earthquake MapUsing Mapbox GL JSUsing Google Maps
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
totalExtent = d3.extent(totalDeathRate.map(r => r.value))
Insert cell
radiusFunction = d3
.scaleSqrt()
.domain(totalExtent)
.range([7, 17])
Insert cell
radius = 11
Insert cell
addPieChartsToMap = function (map) {
// Create segments for each region
data.forEach((region) => {
if (region.geometry) {
let x = projection(region.geometry.coordinates)[0];
let y = projection(region.geometry.coordinates)[1];
let arcs = pie(region.data);
map
.append("g")
.attr("transform", "translate(" + x + "," + y + ")")
.attr("stroke", "white")
.selectAll("path")
.data(arcs)
.join("path")
.attr("fill", (d) => sectorColor(d.data.name))
.attr(
"d",
d3.arc().innerRadius(0).outerRadius(radiusFunction(region.total))
)
.append("title")
// title will not actually be visible
.text((d) => `${d.data.name}: ${d.data.value}`);
}
});
}
Insert cell
arc = d3
.arc()
.innerRadius(0)
.outerRadius(radius)
Insert cell
pie = d3
.pie()
.sort(null)
.value(d => d.value)
Insert cell
sectorColor = function(value) {
if (value == "Cancer") {
return "#A4CDF8";
} else if (value == "Circulatory") {
return "#2E7AF9";
} else if (value == "Respiratory") {
return 'orange';
} else if (value == "Other") {
return '#FFCC80';
}
}
Insert cell
Insert cell
legendX = width - 25
Insert cell
Insert cell
html`<style>.legend-text{font-size:0.8rem;}</style>`
Insert cell
Insert cell
time = 2016
Insert cell
indexStats = function(data) {
const arr = Object.entries(data.dimension.geo.category.index)
// define our object structure
.map(([key, val]) => ({
id: key,
value: data.value[val] || null,
name: data.dimension.geo.category.label[key]
}));
//sort the array in ascending order by 'value'
return arr.sort((a, b) =>
a.value > b.value ? -1 : b.value > a.value ? 1 : 0
);
}
Insert cell
function mergeObjectsInArray(array, property) {
const newArray = new Map();
array.forEach((item) => {
const propertyValue = item[property];
newArray.has(propertyValue) ? newArray.set(propertyValue, { ...item, ...newArray.get(propertyValue) }) : newArray.set(propertyValue, item);
});
return Array.from(newArray.values());
}
Insert cell
Insert cell
data = {
//for each region, create object with each cause of death and its rate
let pieData = [];
let all = [
...cancerDeathRate,
...circulatoryDeathRate,
...respiratoryDeathRate,
...totalDeathRate,
...centroids
];
let merged = mergeObjectsInArray(all, "id").map(o => {
o.other = Math.round(o.total - o.cancer - o.circulatory - o.respiratory);
return o;
});
merged.filter(d => {
let countryCode = d.id.substring(0, 2);
if (!countryCode.includes("EU") && d.value !== null) {
pieData.push(
//pie chart data has to be an array of objects
{
id: d.id,
name: d.name,
geometry: d.geometry,
total: d.total,
data: [
{ name: "Circulatory", value: d.circulatory },
{ name: "Cancer", value: d.cancer },
{ name: "Respiratory", value: d.respiratory },
{ name: "Other", value: d.other }
]
}
);
return d;
}
});
return pieData;
}
Insert cell
Insert cell
circulatoryDeathRate = indexStats(
await d3.json(
`https://ec.europa.eu/eurostat/api/dissemination/statistics/1.0/data/hlth_cd_asdr2?geoLevel=nuts1&sex=T&unit=RT&time=${time}&age=TOTAL&icd10=I`
)
).map((d) => {
d.circulatory = d.value;
return d;
})
Insert cell
Insert cell
cancerDeathRate = indexStats(
await d3.json(
`https://ec.europa.eu/eurostat/api/dissemination/statistics/1.0/data/hlth_cd_asdr2?geoLevel=nuts1&sex=T&unit=RT&time=${time}&age=TOTAL&icd10=C`
)
).map((d) => {
d.cancer = d.value;
return d;
})
Insert cell
Insert cell
respiratoryDeathRate = indexStats(
await d3.json(
`https://ec.europa.eu/eurostat/api/dissemination/statistics/1.0/data/hlth_cd_asdr2?format=json&lang=en&geoLevel=nuts1&sex=T&unit=RT&time=${time}&age=TOTAL&icd10=J`
)
).map((d) => {
d.respiratory = d.value;
return d;
})
Insert cell
Insert cell
Insert cell
totalDeathRate = indexStats(
await d3.json(
`https://ec.europa.eu/eurostat/api/dissemination/statistics/1.0/data/hlth_cd_asdr2?format=json&lang=en&geoLevel=nuts1&sex=T&time=${time}&age=TOTAL&icd10=A-R_V-Y`
)
).map((d) => {
d.total = d.value;
return d;
})
Insert cell
Insert cell
Insert cell
centroidsData = (
await fetch(
`https://raw.githubusercontent.com/eurostat/Nuts2json/master/pub/v2/2016/3035/nutspt_1.json`
)
).json()
Insert cell
Insert cell
centroids = centroidsData.features.map(c => {
c.id = c.properties.id;
return c;
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more