Published unlisted
Edited
Jul 1, 2021
2 forks
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 980, 620]);

svg.append("path")
.datum(topojson.merge(tz, tz.objects.Regions.geometries))
.attr("fill", "#e0e0e0")
.attr("d", path);

svg.append("path")
.datum(topojson.mesh(tz, tz.objects.Regions, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path);

const legend = svg.append("g")
.attr("fill", "#777")
.attr("transform", "translate(915,608)")
.attr("text-anchor", "middle")
.style("font", "10px sans-serif")
.selectAll("g")
.data(radius.ticks(4).slice(1))
.join("g");

legend.append("circle")
.attr("fill", "none")
.attr("fill-opacity", 0.3)
.attr("stroke", "#ccc")
.attr("cy", d => -radius(d))
.attr("r",radius);

legend.append("text")
.attr("y", d => -2 * radius(d))
.attr("dy", "1.3em")
.text(radius.tickFormat(4, "s"));

svg.append("g")
.attr("fill", "brown")
.attr("fill-opacity", 0.5)
.attr("stroke", "#fff")
.attr("stroke-width", 0.5)
.selectAll("circle")
.data(data
.filter(d => d.position)
.sort((a, b) => d3.descending(a.value, b.value)))
.join("circle")
.attr("transform", d => `translate(${d.position})`)
.attr("r", d => radius(d.value))
.append("title")
.text(d => `${d.title}
${format(d.value)}`);

return svg.node();
}
Insert cell
data = (await FileAttachment("petrolStationSales2019.json").json()).slice(1).map(([population, state]) => {
const id = state;
const feature = features.get(id);
return {
id,
position: feature && path.centroid(feature),
title: feature && feature.properties.name,
value: +population
};
})
Insert cell
radius = d3.scaleSqrt([0, d3.max(data, d => d.value)], [0, 40])
Insert cell
features = new Map(topojson.feature(tz, tz.objects.Regions).features.map(d => [d.id, d]))
Insert cell
path = d3.geoPath().projection(proj2)
Insert cell
proj2 = d3.geoIdentity().reflectY(true).fitSize([980,620],topojson.feature(tz,tz.objects.Regions))
Insert cell
//spike = (length, width = 7) => `M${-width / 2},0L0,${-length}L${width / 2},0`
Insert cell
format = d3.format(",.0f")
Insert cell
tz = FileAttachment("petrolStation2020.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@6")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more