Public
Edited
Feb 11
1 fork
Insert cell
Insert cell
chart.zoomTo(0,0,1)
Insert cell
chart = {
const zoom = d3.zoom()
.scaleExtent([1, 32])
.on("zoom", zoomed);

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

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

const textBox = gText.append("text")
.attr("transform", `translate(${25},${25})`)
.text("...")

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

const gDot = svg.append("g")
.attr("fill", "none")
.attr("stroke-linecap", "round")
const gGroup = gDot.selectAll("g")
.data(data)
.join("g")
.attr("transform", d => `translate(${x(d.x)},${y(d.y)})`)

gGroup.on("mouseover", function (e, d) {
textBox.text(`Cluster: ${d.cluster} | Texto: ${d.text}`);
})

gGroup.on("mouseout", () => {
// textBox.text("BBBB");
})

gGroup.append("path")
.attr("d", d => `M${0},${0}h0`)
.attr("stroke", d => z(d.cluster));

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

return Object.assign(svg.node(), {
zoomTo(xv,yv,k) {
zoomed({transform: d3.zoomTransform(svg.node())})
const transform = d3.zoomIdentity.translate(xv, yv).scale(k);
svg.transition()
.duration(1000)
.call(zoom.transform, transform);
return d3.zoomTransform(svg.node())
}
});
}
Insert cell
data = all_vectors
Insert cell
all_vectors (1).csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
x = d3.scaleLinear()
.domain([-100, 100])
.range([0, width])
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
y = d3.scaleLinear()
.domain([-70, 70])
.range([height, 0])
Insert cell
yAxis = (g, y) => g
.call(d3.axisRight(y).ticks(12))
.call(g => g.select(".domain").attr("display", "none"))
Insert cell
z = d3.scaleOrdinal()
.domain(d3.range(d3.extent(data, d => d.cluster))) // posible color de acuerdo con el cluster
.range(d3.schemeCategory10)
Insert cell
height = 600
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