Published
Edited
May 1, 2019
3 forks
Insert cell
Insert cell
Insert cell
chart = {
const width = 960;
const height = 600;
const projection = d3.geoAlbersUsa().scale(1200).translate([width/2, height/2])
const path = d3.geoPath().projection(projection)
const radius = d3.scaleSqrt().domain(extent).range([10, 30]);
const translate = (coords) => {
const [x, y] = projection([+coords.longitude, +coords.latitude])
return `translate(${x},${y})`
}

const svg = d3.create("svg")
.attr("viewBox", `0 0 ${width} ${height}`)
.style("width", "100%")
.style("height", "auto")
.style('background-color', null)

const states = svg.append("g")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.join("path")
.attr("fill", "#ccc")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path);
const bubbles = svg.append("g")
.attr("fill", "brown")
.attr("fill-opacity", 0.5)
.attr("stroke", "#fff")
.attr("stroke-width", 0.5)
.selectAll("circles")
.data(data)
.join("circle")
.attr("transform", translate)
.attr("r", d => radius(d.count))

const legend = svg.append("g")
.attr("fill", "#777")
.attr("transform", `translate(${width - 80},${height - 10})`)
.attr("text-anchor", "middle")
.style("font", "10px sans-serif")
.selectAll(".legend")
.data([10, 25, 50])
.join("g")

legend.append("circle")
.attr("fill", "none")
.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(d => d)//d3.format(".1s"));

return svg.node();
}
Insert cell
extent
Insert cell
Insert cell
Insert cell
topojson = require("topojson-client@3")
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csv('https://raw.githubusercontent.com/chrisdaly/map-data/master/socialgraphics_data.csv')
Insert cell
extent = d3.extent([...data].map(d => d.count))
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