Published
Edited
May 14, 2019
Fork of OPT MSAs
Insert cell
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
{
let indices = [];
for (var i in msa.features) {
let area = msa.features[i].properties.name;
if (area.includes("Iowa City")) {
indices.push(i);
}
}
return indices
}
Insert cell
{
let indices = [11, 44, 47];
let msas = [];
indices.forEach(i => (msas.push(msa.features[i])));
return msas;
}
Insert cell
colorScale(opt_by_msa[msa.features[44].properties.name])
Insert cell
strokeScale(opt_by_msa[msa.features[44].properties.name])
Insert cell
Insert cell
msa = d3.json('https://gist.githubusercontent.com/apaarty/70843364dbbb62b751e66cec56b73ad5/raw/ca77ba829fd05fef636fd1288c6a1bbe6d9e347d/cb_2013_us_cbsa_5m.geojson')
Insert cell
us.objects["msa"]=msa
Insert cell
usa = d3.json("https://gist.githubusercontent.com/nbailey/5a4d2fcb6d344a31b017dd98822e0ce1/raw/7a042b265e32c48f96254d6c4802f4e306b6e4bc/usa_states.geojson")
Insert cell
usa.features[11];
Insert cell
msa.features[11];
Insert cell
Insert cell
chosen_msa
Insert cell
viewof chosen_msa = {
let width = 960;
let height = 600;
let value = null;
// Create SVG
let svg = d3.select(DOM.svg(width, height));

let g1 = svg.append("g");
let g2 = 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);

const outlineClick = svg.append("path")
.attr("fill", "red")
.attr("stroke", "black")
.attr("pointer-events","none");
// //let path = d3.geoPath();
// // Bind TopoJSON data
g1.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
g2.selectAll("path")
.data(msa.features) // Bind TopoJSON data elements
.enter().append("path")
.attr("d", projPath)
.attr("class", "metro-area")
// .style("fill", "black").style("stroke", "white")
.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").attr("background", colorScale(OPTbyMSA[d.properties.name])).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"))
.on("click", d => {
const node = svg.node();
node.value = value = value === d.properties.name ? null : d.properties.name;
node.dispatchEvent(new CustomEvent("input"));
outlineClick.attr("d", value ? projPath(d) : null);
//show the country name and load the data
});

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(msa.features) // Bind TopoJSON data elements
.enter().append("path")
.attr("d", path)
.style("fill", d => OPTbyMSA[d.properties.name] ? "black" : "none") // 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"));

return svg.node();
}
Insert cell
colorScale = function(value) {
if (value == undefined) {
return "none";
} else {
return color(Math.log(value));
}
}
Insert cell
color = d3.scaleQuantize()
.domain([Math.log(d3.min(Object.values(OPTbyMSA)))-1, Math.log(d3.max(Object.values(OPTbyMSA)))])
.range(["#474557",
"#535166",
"#5E5C74",
"#6A6883",
"#767391",
"#817E9F",
"#8C89A7",
"#9795B0",
"#A3A1B9",
"#AEACC1",
"#BAB8CA",
"#C5C4D3",
"#D1D0DC",
"#DCDBE4",
"#E8E7ED",
"#F3F3F6"
].reverse());
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("color", "#e7eeef")
.style("padding", "5px")
.style("opacity", "1")
.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
strokeScale = function(value) {
if (value == undefined) {
return "none";
} else {
return "black";
}
}
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