Published
Edited
Apr 22, 2019
Fork of OPT MSAs
Insert cell
md`# OPT MSAs`
Insert cell
vegalite = require("@observablehq/vega-lite@0.1")
Insert cell
z = require('https://bundle.run/zebras@0.0.11')
Insert cell
d3 = require("d3@5")
Insert cell
opt = d3.csv("https://gist.githubusercontent.com/apaarty/45aef751a3124c57e23ca971f67916b8/raw/d4827970c216a5ee8e49b449d454e18a1ad8ca8c/gistfile1.txt")
Insert cell
+"8700"
Insert cell
z.head(5,opt)
Insert cell
vegalite({
data: {values: opt},
mark: "circle",
encoding: {
x: {field: "Lon", type: "quantitative",scale: {domain:[-170,-50]}},
y: {field: "Lat", type: "quantitative",scale: {domain:[20,50]}}
}
})
Insert cell
topojson = require("topojson-client@3")
Insert cell
us = d3.json("https://unpkg.com/us-atlas@1/us/10m.json")
Insert cell
msa = d3.json('https://gist.githubusercontent.com/apaarty/70843364dbbb62b751e66cec56b73ad5/raw/ca77ba829fd05fef636fd1288c6a1bbe6d9e347d/cb_2013_us_cbsa_5m.geojson')
Insert cell
usa = d3.json("https://gist.githubusercontent.com/nbailey/5a4d2fcb6d344a31b017dd98822e0ce1/raw/7a042b265e32c48f96254d6c4802f4e306b6e4bc/usa_states.geojson")
Insert cell
msa2 = d3.json("https://gist.githubusercontent.com/nbailey/364f8cf138350685c9ea02092bde178f/raw/42709a800d293d9d46ddf4464859f33a7a8b3325/us_2013_cbsa_5m.json")
Insert cell
us.objects['msa'] = msa2.objects.cb_2013_us_cbsa_5m;
Insert cell
{
let width = 960;
let height = 600;
// Create SVG
let svg = d3.select(DOM.svg(width, height));

let g = svg.append("g");
// Create empty object for holding dataset
const OPTbyMSA = {};
// Create property for each ID, give it value from rate
opt.forEach(d => (OPTbyMSA[d["Metro area"]] = +d["Total number who worked in area following school"]));

let projPath = d3.geoPath().projection(projection);
//let path = d3.geoPath();
// Bind TopoJSON data
g.selectAll("path")
.data(usa.features) // Bind TopoJSON data elements
.enter().append("path")
.attr("d", projPath)
.style("fill", "none") // get rate value for property matching data ID
// pass rate value to color function, return color based on domain and range
.style("stroke", "black")

//let path = d3.geoPath();
// Bind TopoJSON data
g.selectAll("path")
.data(msa.features) // Bind TopoJSON data elements
.enter().append("path")
.attr("d", projPath)
.style("fill", d => colorScale(OPTbyMSA[d.properties.name])) // get rate value for property matching data ID
// pass rate value to color function, return color based on domain and range
.style("stroke", d => strokeScale(OPTbyMSA[d.properties.name]))
// .style("stroke", "none")
.on("mouseover", d => tooltip.style("visibility", "visible").text(d.properties.name + " " + OPTbyMSA[d.properties.name]))
.on("mousemove", d => tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px").html(d.properties.name + " " + OPTbyMSA[d.properties.name]))
.on("mouseout", d => tooltip.style("visibility", "hidden"));

return svg.node();
}
Insert cell
{
let width = 960;
let height = 600;
// Create SVG
let svg = d3.select(DOM.svg(width, height));

let g = svg.append("g");
// Create empty object for holding dataset
const OPTbyMSA = {};
// Create property for each ID, give it value from rate
opt.forEach(d => (OPTbyMSA[d["Metro area"]] = +d["Total number who worked in area following school"]));

//let path = d3.geoPath();
// Bind TopoJSON data
g.selectAll("path")
.data(topojson.feature(us, us.objects.msa).features) // Bind TopoJSON data elements
.enter().append("path")
.attr("d", path)
.style("fill", d => colorScale(OPTbyMSA[d.properties.name])) // get rate value for property matching data ID
// pass rate value to color function, return color based on domain and range
.style("stroke", "white")
.on("mouseover", d => tooltip.style("visibility", "visible").text(d.properties.name + " " + OPTbyMSA[d.properties.name]))
.on("mousemove", d => tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px").html(d.properties.name + " " + OPTbyMSA[d.properties.name]))
.on("mouseout", d => tooltip.style("visibility", "hidden"));
g.append("path")
.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "black")
.attr("d", path);

return svg.node();
}
Insert cell
msa.features
Insert cell
topojson.mesh(us, us.objects.states, (a, b) => a !== b)
Insert cell
colorScale = function(value) {
if (value == undefined) {
return "none";
} else {
return color(Math.log(value));
}
}
Insert cell
strokeScale = function(value) {
if (value == undefined) {
return "none";
} else {
return "black";
}
}
Insert cell
color = d3.scaleQuantize()
.domain([0, Math.log(d3.max(Object.values(OPTbyMSA)))])
.range(d3.schemePurples[5]);
Insert cell
OPTbyMSA = {
const OPTbyMSA = {};
// Create property for each ID, give it value from rate
opt.forEach(d => (OPTbyMSA[d["Metro area"]] = +d["Total number who worked in area following school"]));
return OPTbyMSA
}

Insert cell
tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("font-family", "'Open Sans', sans-serif")
.style("font-size", "12px")
.style("background","white")
.style("padding", "5px")
.style("opacity", ".6")
.style("z-index", "10")
.style("visibility", "hidden");
Insert cell
projection = d3.geoAlbers()
Insert cell
path = d3.geoPath()//.projection(projection)
Insert cell
{
let width = 960;
let height = 600;
// Create SVG
let svg = d3.select(DOM.svg(width, height));

let g = svg.append("g");
// Bind TopoJSON data
g.selectAll("path")
.data(msa.features) // Bind TopoJSON data elements
.enter().append("path")
.attr("d", path)
.style("fill", "white")
.style("stroke", "black");

return svg.node();
}
Insert cell
topojson.feature(msa, msa)
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