Published
Edited
May 13, 2020
1 fork
1 star
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");
const dot = svg.append("g")
.attr("class", "toolTip")
.attr("display", "none");


dot.append("text")
.attr("font-family", "Helvetica")
.attr("font-size", 30)
.attr("text-anchor", "middle");

gDot.selectAll("path")
.data(data)
.join("path")
.attr("d", d => `M${x(d.val[0])},${y(d.val[1])}h0`)
.attr("stroke", d => z(d.val[2]))
.on('mousemove', function (d) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '0.5');
dot.attr("transform", `translate(${x(d.val[0])},${y(d.val[1])})`);
dot.attr("display", null)
.attr("class", "toolTip");
dot.select('text').text(d.word);
})
.on('mouseout', function (d) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '1');
dot.attr("display", "none");
});

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

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

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

function zoomed() {
const transform = d3.event.transform;
const zx = transform.rescaleX(x).interpolate(d3.interpolateRound);
const zy = transform.rescaleY(y).interpolate(d3.interpolateRound);
gDot.attr("transform", transform).attr("stroke-width", 20 / 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
data = {
const random = d3.randomNormal(0, 0.2);
const sqrt3 = Math.sqrt(3);
const pos = Array.from({length: 300}, () => [random() + sqrt3, random() + 1, 0]);
return pos.map(d => ({
word: random(),
val: d
}))
}
Insert cell
x = d3.scaleLinear()
.domain([-4.5, 4.5])
.range([0, width])
Insert cell
y = d3.scaleLinear()
.domain([-4.5 * k, 4.5 * k])
.range([height, 0])
Insert cell
z = d3.scaleOrdinal()
.domain(data.map(d => d.val[2]))
.range(d3.schemeCategory10)
Insert cell
xAxis = (g, x) => g
.attr("transform", `translate(0,${height})`)
.attr("font-size", 25)
.call(d3.axisTop(x).ticks(12))
.call(g => g.select(".domain").attr("display", "none"))
Insert cell
yAxis = (g, y) => g
.attr("font-size", 25)
.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.val))
.attr("x2", d => 0.5 + x(d.val)))
.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.val))
.attr("y2", d => 0.5 + y(d.val)));
Insert cell
k = height / width
Insert cell
height = 600
Insert cell
d3 = require("d3@5", "d3-array@2")
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