scatterplotMatrix = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg.append("style").text("circle.hidden { fill: #000; fill-opacity: 0.1;}");
const cell = svg
.append("g")
.selectAll("g")
.data(d3.cross(d3.range(attributes.length), d3.range(attributes.length)))
.join("g")
.attr(
"transform",
([i, j]) =>
`translate(${padding + i * (padding + cellWidth)},${padding +
j * (padding + cellHeight)})`
);
cell
.append("rect")
.attr("fill", "#ECEFF1")
.attr("stroke", "none")
.attr("width", cellWidth)
.attr("height", cellHeight);
cell.each(function([i, j]) {
// TODO: draw the scatterplot for this cell here
//d3.select(this) creates a 1-element selection containing the current node. This allows you to use D3's operators to modify the element
//https://groups.google.com/forum/#!topic/d3-js/OexNvDXNFuk
//In this case, it return the current cell we are working on.
d3.select(this)
//https://www.tutorialspoint.com/d3js/d3js_data_join.htm
//map the elements of the existing document with the given data set
//In our case: "circle" element to each object in dataset
.selectAll("circle").data(data)
//https://observablehq.com/@d3/selection-join?collection=@d3/d3-selection
//selection.join appends a new SVG circle element for each data set object
.join("circle")
//Add the attributes using the attr
.attr("cx", d => x[i]( d[ attributes[i] ] ) )
//To change the diagonal of the scatterplot matrix:
//.attr("cy", d => y[3-j]( d[ attributes[3-j] ] ) )
.attr("cy", d => y[j]( d[ attributes[j] ] ) )
// create an x-axis below each cell
d3.select(this)
.append("g")
.attr("transform", `translate(0, ${cellHeight})`)
.call(d3.axisBottom(x[i]).ticks(cellWidth / 50));
// create a y-axis to the left of each cell
d3.select(this)
.append("g")
.call(d3.axisLeft(y[j]).ticks(cellHeight / 30));
});
// set attributes of the circles
const circle = cell
.selectAll("circle")
.attr("r", 4.5)
.attr("fill-opacity", 0.5)
.attr("fill", d => color(d.species));
// setup a brush for each cell
cell.call(brush, circle);
// create a label for each attribute on the diagonal
svg
.append("g")
.style("font", "18px Source Serif Pro")
.style("pointer-events", "none")
.selectAll("text")
.data(attributes)
.join("text")
.attr(
"transform",
(d, i) =>
`translate(${padding + i * (padding + cellWidth)},${padding +
i * (padding + cellHeight)})`
)
.attr("x", 10)
.attr("y", 10)
.attr("dy", ".71em")
.text(d => d);
return svg.node();
}