Published
Edited
Apr 7, 2020
1 fork
Insert cell
md`# Proportional Symbol Mapping
.
\nGEOG 3540 - Geographic Visualization, Hailey Quinn, University of Iowa`
Insert cell
proportionalSymbols = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.append("path")
.datum(topojson.feature(basepolygons, basepolygons.objects.UnitedStates))
.attr("fill", "#ccc")
.attr("d", path_basemap);
svg.append("path")
.datum(topojson.mesh(basepolygons, basepolygons.objects.UnitedStates, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path_basemap);
const legend = svg.append("g")
.attr("fill", "#777")
.attr("transform", "translate(150,450)")
.attr("text-anchor", "middle")
.style("font", "10px sans-serif")
.selectAll("g")
.data([d3.max(attribute)/9, d3.max(attribute)])
.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(format);
svg.append("g")
.selectAll("circle")
.data(points.features
.map(d => (d.value = d.properties[attributeName], d))
.sort((a, b) => b.value - a.value))
.join("circle")
.attr("transform", d => `translate(${path_points.centroid(d)})`)
.attr("r", d => radius(d.value))
.attr("fill", d=> colors(d.value))
.attr("fill-opacity", 1)
.attr("stroke", "#000")
.attr("stroke-width", 0.5)
.append("title")
.text(d => `${d.properties[idName]} : ${format(d.value)}`);
return svg.node();
}
Insert cell
height = 610
Insert cell
width = 975
Insert cell
path_points = d3.geoPath().projection(projection)
Insert cell
path_basemap = d3.geoPath().projection(projection)
Insert cell
projection = d3.geoAlbers()
Insert cell
format = d3.format(".2s")
Insert cell
//radius(50000000)
Insert cell
//proportional symbols
//radius = d3.scaleSqrt([0, d3.max(attribute)], [0, 30])
Insert cell
radius = d3.scaleThreshold()
.domain(naturalbreaks)
.range([0, 10, 20, 30])
Insert cell
colors = d3.scaleThreshold()
.domain(naturalbreaks)
.range(YlGnBu)
Insert cell
YlGnBu = ["#ffffcc", "#a1dab4", "#41b6c4", "#225ea8"]
Insert cell
naturalbreaks = simple.ckmeans(attribute, 4).map(v => v.pop())
Insert cell
d3.max(attribute)
Insert cell
attribute = Array.from(points.features, d=>Math.sqrt(d.properties[attributeName]))
Insert cell
attributeName = "BLACK"
Insert cell
idName = "STATE_NAME"
Insert cell
//import the point data (geojson)
points = FileAttachment("state48.points.geojson").json()
Insert cell
//import the polygon base map data
basepolygons = FileAttachment("UnitedStates.json").json()
Insert cell
simple = require("simple-statistics@7.0.7/dist/simple-statistics.min.js")
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5")
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