function zoomableScatterplot() {
const leftMargin = 50, bottomMargin = 50, topMargin = 10;
const visWidth = scatter_sq - leftMargin, visHeight = scatter_sq - bottomMargin;
const initialValue = games;
const xCol = "averageweight", xLabel = "Complexity";
const yCol = "average", yLabel = "Rating";
function zoomed({transform}) {
const zx = transform.rescaleX(x).interpolate(d3.interpolateRound);
const zy = transform.rescaleY(y).interpolate(d3.interpolateRound);
gDot.attr("transform", transform).attr("stroke-width", 5 / transform.k);
gx.call(xAxis, zx);
gy.call(yAxis, zy);
gGrid.call(grid, zx, zy);
}
const x = d3.scaleLinear()
.domain([0, d3.max(initialValue, d => d[xCol])]).nice()
.range([0, visWidth]);
const k = visHeight / visWidth;
const y = d3.scaleLinear()
.domain([0, d3.max(initialValue, d => d[yCol])]).nice()
.range([visHeight, 0]);
function xAxis(g, x){
return g.attr("transform", `translate(0,${visHeight})`)
.call(d3.axisTop(x).ticks(12))
.call(g => g.select(".domain").attr("display", "none"))
.append('text')
// add the label. inset temporarily while dealing with viewBox woes
.attr('x', visWidth / 2)
.attr('y', -20)
.attr('fill', 'black')
.attr('text-anchor', 'middle')
.attr('font-weight', 'bold')
.text(xLabel)
}
function yAxis(g, y){
return g.call(d3.axisRight(y).ticks(12 * k))
.call(g => g.select(".domain").attr("display", "none"))
// add the label. inset temporarily while dealing with viewBox woes
.append('text')
.attr("transform", "rotate(-90)") // now x, y flipped in terms of positioning
.attr("x", -1 * (visHeight / 2))
.attr("y", 20)
.attr('fill', 'black')
.attr('dominant-baseline', 'middle')
.attr('font-weight', 'bold')
.text(yLabel);
}
function grid(g, x, y) {return g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g
.selectAll(".x")
.data(x.ticks(10))
.join(
enter => enter.append("line").attr("class", "x").attr("y2", visHeight),
update => update,
exit => exit.remove()
)
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d)))
.call(g => g
.selectAll(".y")
.data(y.ticks(10 * k))
.join(
enter => enter.append("line").attr("class", "y").attr("x2", width),
update => update,
exit => exit.remove()
)
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d)))}
// zoom level & function trigger
const zoom = d3.zoom()
.extent([[0,0],[visWidth, visHeight]])
.on("zoom", zoomed);
const svg = d3.create("svg")
.attr("width", scatter_sq - leftMargin)
.attr("height", scatter_sq - bottomMargin);
const g = svg.append("g")
.attr("transform", `translate(${leftMargin}, ${topMargin})`);
const gDot = svg.append("g")
.attr("fill", "none")
.attr("stroke-linecap", "round");
const radius = 1;
gDot.selectAll("path")
.data(games)
.join("path")
.attr("d", d => `M${x(d[xCol])},${y(d[yCol])}h0`)
.attr("stroke", "#69b3a2")
.attr('stroke-width', 2)
.attr('r', radius);
const gGrid = svg.append("g");
const gx = svg.append("g");
const gy = svg.append("g");
svg.call(zoom).call(zoom.transform, d3.zoomIdentity);
/////////////////////////////////////////////////////////////////////////////////////
function update(data) {
// update the number of cars for each origin
const originCounts = d3.rollup(
data,
group => group.length,
d => d.Origin
);
// transition smoothly to look cute
const t = svg.transition()
.ease(d3.easeLinear)
.duration(200);
// draw bars
barsGroup.selectAll("rect")
.data(originCounts, ([origin, count]) => origin)
.join("rect")
.attr("fill", ([origin, count]) => carColor(origin))
.attr("height", y.bandwidth())
.attr("x", 0)
.attr("y", ([origin, count]) => y(origin))
.transition(t)
.attr("width", ([origin, count]) => x(count))
}
/////////////////////////////////////////////////////////////////////////////////////
return Object.assign(svg.node(), {
reset() {
svg.transition()
.duration(750)
.call(zoom.transform, d3.zoomIdentity);
}, update
});
}