Published
Edited
Mar 6, 2021
1 fork
Insert cell
Insert cell
Insert cell
chart = {
const zoom = d3.zoom()
.scaleExtent([0.5, 32])
.on("zoom", zoomed);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const gGrid = svg.append("g");

/* const gDot = svg.append("g")
.attr("fill", "none")
.attr("stroke-linecap", "round");

gDot.selectAll("path")
.data(data)
.join("path")
.attr("d", d => `M${x(d[0])},${y(d[1])}h0`)
.attr("stroke", d => z(d[2]));*/
var gDot = svg.selectAll("g.dot")
.data(data)
.enter().append('g');
gDot.append("circle")
.attr("class", "dot")
.attr("r", 4)
.attr("cx", function (d) {
return x(d[0]);
})
.attr("cy", function (d) {
return y(d[1]);
})
.style("fill", function (d) {
return z(d[2]);
});
gDot.append("text")
.attr("font-size", 12)
.text(function(d){
return d[3];
})
.attr("x", function (d) {
return x(d[0]);
})
.attr("y", function (d) {
return y(d[1]);
});
const gx = svg.append("g");

const gy = svg.append("g");

svg.call(zoom).call(zoom.transform, d3.zoomIdentity);

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);
}

return Object.assign(svg.node(), {
reset() {
svg.transition()
.duration(750)
.call(zoom.transform, d3.zoomIdentity);
}
});
}
Insert cell
reset, chart.reset()
Insert cell
data1 = FileAttachment("unemp_4c.csv").csv()
Insert cell
data = data1.map(item => [item.X,item.Y,item.cluster,item.State])
Insert cell
x = d3.scaleLinear()
.domain([5, 14])
.range([0, width])
Insert cell
y = d3.scaleLinear()
.domain([2,10])
.range([height, 0])
Insert cell
z = d3.scaleOrdinal()
.domain(data.map(d => d[2]))
.range(d3.schemeCategory10)
Insert cell
xAxis = (g, x) => g
.attr("transform", `translate(0,${height})`)
.call(d3.axisTop(x).ticks(12))
.call(g => g.select(".domain").attr("display", "none"))
Insert cell
yAxis = (g, y) => g
.call(d3.axisRight(y).ticks(12 * k))
.call(g => g.select(".domain").attr("display", "none"))
Insert cell
grid = (g, x, y) => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g
.selectAll(".x")
.data(x.ticks(12))
.join(
enter => enter.append("line").attr("class", "x").attr("y2", height),
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(12 * 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)));
Insert cell
k = height / width
Insert cell
height = 600
Insert cell
d3 = require("d3@6")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more