Published
Edited
Nov 18, 2019
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("viewBox", [-padding, 0, width, width]);

svg.append("style")
.text(`circle.hidden { fill: #000; fill-opacity: 1; r: 1px; }`);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

const cell = svg.append("g")
.selectAll("g")
.data(d3.cross(d3.range(columns.length), d3.range(columns.length)))
.join("g")
.attr("transform", ([i, j]) => `translate(${i * size},${j * size})`);

cell.append("rect")
.attr("fill", "none")
.attr("stroke", "#aaa")
.attr("x", padding / 2 + 0.5)
.attr("y", padding / 2 + 0.5)
.attr("width", size - padding)
.attr("height", size - padding);

cell.each(function([i, j]) {
d3.select(this).selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => x[i](d[columns[i]]))
.attr("cy", d => y[j](d[columns[j]]));
});

const circle = cell.selectAll("circle")
.attr("r", 3.5)
.attr("fill-opacity", 0.7)
.attr("fill", d => z(d.species));

cell.call(brush, circle);

svg.append("g")
.style("font", "bold 10px sans-serif")
.style("pointer-events", "none")
.selectAll("text")
.data(columns)
.join("text")
.attr("transform", (d, i) => `translate(${i * size},${i * size})`)
.attr("x", padding)
.attr("y", padding)
.attr("dy", ".71em")
.text(d => d);

return svg.node();
}
Insert cell
function brush(cell, circle) {
const brush = d3.brush()
.extent([[padding / 2, padding / 2], [size - padding / 2, size - padding / 2]])
.on("start", brushstarted)
.on("brush", brushed)
.on("end", brushended);

cell.call(brush);

let brushCell;

// Clear the previously-active brush, if any.
function brushstarted() {
console.log("brushstarted", brushCell === this);
if (brushCell !== this) {
d3.select(brushCell).call(brush.move, null);
brushCell = this;
}
}

// Highlight the selected circles.
function brushed([i, j]) {
if (d3.event.selection === null) return;
const [[x0, y0], [x1, y1]] = d3.event.selection;
circle.classed("hidden", d => {
return x0 > x[i](d[columns[i]])
|| x1 < x[i](d[columns[i]])
|| y0 > y[j](d[columns[j]])
|| y1 < y[j](d[columns[j]]);
});
}

// If the brush is empty, select all circles.
function brushended() {
if (d3.event.selection !== null) return;
circle.classed("hidden", false);
}
}
Insert cell
md`### Add small buffer around data`
Insert cell
x = columns.map(c => d3.scaleLinear()
// .domain(d3.extent(data, d => d[c])) // original
.domain(d3.extent(data, d => d[c]).map((d,i,a) => i == 0 ? d - .01 * (a[1] - a[0]) : d + .01 * (a[1] - a[0])) ) // capture extremes w/brush
.rangeRound([padding / 2, size - padding / 2]))
Insert cell
y = x.map(x => x.copy().range([size - padding / 2, padding / 2]))
Insert cell
z = d3.scaleOrdinal()
.domain(data.map(d => d.species))
.range(d3.schemeCategory10)
Insert cell
xAxis = {
const axis = d3.axisBottom()
.ticks(6)
.tickSize(size * columns.length);
return g => g.selectAll("g").data(x).join("g")
.attr("transform", (d, i) => `translate(${i * size},0)`)
.each(function(d) { return d3.select(this).call(axis.scale(d)); })
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").attr("stroke", "#ddd"));
}
Insert cell
yAxis = {
const axis = d3.axisLeft()
.ticks(6)
.tickSize(-size * columns.length);
return g => g.selectAll("g").data(y).join("g")
.attr("transform", (d, i) => `translate(0,${i * size})`)
.each(function(d) { return d3.select(this).call(axis.scale(d)); })
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").attr("stroke", "#ddd"));
}
Insert cell
Insert cell
// data = d3.csvParse(await FileAttachment("iris.csv").text(), d3.autoType)
import { data, columns } from "@pbogden/boston-housing-scatterplot-matrix"
Insert cell

//columns = data.columns.filter(d => d !== "species")
Insert cell
width = 954
Insert cell
size = (width - (columns.length + 1) * padding) / columns.length + padding
Insert cell
padding = 20
Insert cell
d3 = require("d3@5")
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