Published
Edited
Feb 27, 2020
Insert cell
md`# Assignment 1`
Insert cell
d3 = require("d3@5")
Insert cell
topojson = require("topojson-client@3")
Insert cell
Alabama = FileAttachment("Alabama.json").json()
Insert cell
Alabama_features = topojson.feature(Alabama, Alabama.objects.Alabama)
Insert cell
csv_data = d3.csvParse(await FileAttachment("Alabama.csv").text(),({FIPS,HISPANIC,MALES}) => [FIPS, [+HISPANIC, +HISPANIC/+MALES]])
Insert cell
perhispanic_M = Array.from(csv_data.values(), d => d[1][1])
Insert cell
data = Object.assign(new Map(csv_data), {title: ["percent", "Percent of Hispanic Males"]})
Insert cell
md`# Linear Scale`
Insert cell
linear = d3.scaleLinear()
.domain(d3.extent(perhispanic_M))
.range(["white", "red"])
Insert cell
chart(perhispanic_M, linear)
Insert cell
function chart(data, scale) {
const w = 30,
cols = Math.floor(Math.min(600, width) / w),
lines = Math.ceil(100 / cols);
const chart = d3
.create("svg")
.attr("width", cols * w)
.attr("height", lines * w);

chart
.append("g")
.attr("transform", "translate(2,2)")
.attr("style", "stroke:black; fill:white;")
.selectAll("rect")
.data(data)
.join("rect")
.attr("width", w - 3)
.attr("height", w - 3)
.attr("x", (_, i) => w * (i % cols))
.attr("y", (_, i) => w * ((i / cols) | 0))
.style("fill", d => (scale ? scale(d) : "#ddd"));
return chart.node();
}
Insert cell
function numericSort(x) {
return (
x .slice()
.sort((a, b) => a - b)
);
}
Insert cell
md`# Quantile`
Insert cell
quantile = d3.scaleQuantile()
.domain(perhispanic_M)
.range(["white", "pink", "red"])
Insert cell
chart(numericSort(perhispanic_M), quantile)
Insert cell
md`# Natural Breaks`
Insert cell
threshold = d3.scaleThreshold()
.domain([0, d3.max(perhispanic_M)])
.range(["white", "pink", "red"])
Insert cell
chart(numericSort(perhispanic_M), threshold)
Insert cell
md`# Equal Interval`
Insert cell
quantize = d3.scaleQuantize()
.domain(d3.extent(perhispanic_M))
.range(["white", "pink", "red"])
Insert cell
chart(numericSort(perhispanic_M), quantize)
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