Published
Edited
Feb 13, 2019
3 forks
Importers
30 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csv(link);
Insert cell
data[0]
Insert cell
Insert cell
Insert cell
Insert cell
parseColumnName = d3.timeParse("%Y-%m");
Insert cell
parseColumnName("1979-12");
Insert cell
Insert cell
Insert cell
convertRow(data[0], 0)
Insert cell
convertRow(data[0], 0).values[0]
Insert cell
Insert cell
converted = d3.csv(link, convertRow);
Insert cell
converted[0]
Insert cell
Insert cell
Insert cell
filtered = converted.filter(function(row) {
return row["Index"] === "Rent Affordability" &&
row["SizeRank"] > 0 && // excludes United States
row["SizeRank"] <= keep;
});
Insert cell
Insert cell
Insert cell
sorted = filtered.sort(function(a, b) {
return a[sortColumn] - b[sortColumn];
});
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
regions = sorted.map(function(row) { return row["RegionName"]; });
Insert cell
Insert cell
dates = {
let values = sorted[0].values;
let mapped = values.map(function(value) { return value.date; });
return mapped.sort(d3.ascending);
};
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
[params.svg.width, params["svg"]["width"], params.svg["width"], params["svg"].width]
Insert cell
Insert cell
d3["max"]([1, 2, 3])
Insert cell
d3.max([1, 2, 3])
Insert cell
Insert cell
createSVG = function(id) {
let svg = d3.select(DOM.svg(params.svg.width, params.svg.height));
svg.attr("id", id);
let plot = svg.append("g");
plot.attr("id", "plot");
plot.attr("transform", translate(params.plot.x, params.plot.y));
let rect = plot.append("rect");
rect.attr("id", "background");
rect.attr("x", 0);
rect.attr("y", 0);
rect.attr("width", params.plot.width);
rect.attr("height", params.plot.height);
// this returns an SVG for the notebook cell
return svg.node();
}
Insert cell
Insert cell
Insert cell
mutable scale = {
let x = d3.scaleBand();
x.domain(dates);
x.range([0, params.plot.width]);
let y = d3.scaleBand();
y.domain(regions);
y.range([params.plot.height, 0]);
return {x: x, y: y}
}
Insert cell
Insert cell
Insert cell
Insert cell
axis = {
let x = d3.axisBottom(scale.x);
x.tickPadding(0);
let y = d3.axisLeft(scale.y);
y.tickPadding(0);

return {x: x, y: y};
}
Insert cell
Insert cell
function dateFormatter(d) {
if (d.getMonth() < 3) {
return d.getFullYear();
}
}
Insert cell
dateFormatter(sorted[0].values[0].date);
Insert cell
axis.x.tickFormat(function(d) {
if (d.getMonth() < 3) {
return d.getFullYear();
}
});
Insert cell
Insert cell
function regionFormatter(d) {
let text = d;
let parts = text.split(/[,-]+/);
if (parts !== null) {
text = parts[0];
if (parts.length > 2) {
text = text + "+";
}
}
return text;
}
Insert cell
regionFormatter(sorted[0].RegionName);
Insert cell
Insert cell
axis.y.tickFormat(regionFormatter);
Insert cell
createPlot = function(id) {
let node = createSVG(id);
let svg = d3.select(node);

let gx = svg.append("g");
gx.attr("id", "x-axis");
gx.attr("class", "axis");
gx.attr("transform", translate(params.plot.x, params.plot.y + params.plot.height));
gx.call(axis.x);

let gy = svg.append("g");
gy.attr("id", "y-axis");
gy.attr("class", "axis");
gy.attr("transform", translate(params.plot.x, params.plot.y));
gy.call(axis.y);
return node;
}
Insert cell
createPlot("axis_svg");
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
// get all of the value objects (with date and value) from the rows
let values = sorted.map(function(d) { return d.values; });
// combine all of the individual object arrays into one
let merged = d3.merge(values);
// get only the value part of the objects
let mapped = merged.map(function(d) { return d.value; });
// calculate the min, max, and median
let min = d3.min(mapped);
let max = d3.max(mapped);
let mid = d3.mean(mapped);
scale.color = d3.scaleSequential(d3.interpolateViridis);
scale.color.domain([min, mid, max]);
return scale;
}
Insert cell
Insert cell
createHeatmap = function(id) {
let node = createPlot(id);
let svg = d3.select(node);
let plot = svg.select("g#plot");
// create one group per row
let rows = plot.selectAll("g.cell")
.data(sorted)
.enter()
.append("g");
rows.attr("class", "cell");
rows.attr("id", function(d) { return "Region-" + d.RegionID; });
// shift the entire group to the appropriate y-location
rows.attr("transform", function(d) {
return translate(0, scale.y(d["RegionName"]));
});
// create one rect per cell within row group
let cells = rows.selectAll("rect")
.data(function(d) { return d.values; })
.enter()
.append("rect");

cells.attr("x", function(d) { return scale.x(d.date); });
cells.attr("y", 0); // handled by group transform
cells.attr("width", scale.x.bandwidth());
cells.attr("height", scale.y.bandwidth());
// here is the color magic!
cells.style("fill", function(d) { return scale.color(d.value); });
cells.style("stroke", function(d) { return scale.color(d.value); });
return node;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3.version
Insert cell
Insert cell
Insert cell
Insert cell
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