Published
Edited
Apr 13, 2021
Insert cell
Insert cell
{
const svg = d3.select(DOM.svg(width, width))
.attr("viewBox", `${-padding} 0 ${width} ${width}`)
.style("max-width", "100%")
.style("height", "auto");

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)))
.enter().append("g")
.attr("transform", ([i, j]) => `translate(${(columns.length - i - 1) * 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)
.enter().append("circle")
.attr("cx", d => x[i](d[columns[i]]))
.attr("cy", d => y[j](d[columns[j]]))
.attr("r", 7)
.attr("fill-opacity", 0.6)
.attr("fill", d => d['Bechdel Score'] === '3' ? '#1b9e77' : 'black');
});

svg.append("g")
.style("font", "bold 24px Josefin Sans")
.selectAll("text")
.data(columns)
.enter().append("text")
.attr("transform", (d, i) => `translate(${(columns.length - i - 1) * size},${i * size})`)
.attr("x", padding)
.attr("y", padding)
.attr("dy", ".71em")
.text(d => d);

return svg.node();
}
Insert cell
x = columns.map(c => d3.scaleLinear()
.domain(c === 'Bechdel Score' ? [0, 3] : [0, 1])
.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).enter().append("g")
.style("font", "18px Josefin Sans")
.attr("transform", (d, i) => `translate(${(columns.length - i - 1) * 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).enter().append("g")
.style("font", "18px Josefin Sans")
.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(csvData)
Insert cell
columns = ["Cast Gender Bias", "Crew Gender Bias", "Bechdel Score"]
Insert cell
width = 964
Insert cell
size = (width - (columns.length + 1) * padding) / columns.length + padding
Insert cell
padding = 40
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