Published
Edited
Oct 3, 2020
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

svg.append("g").call(xAxis);

svg.append("g").call(yAxis);

svg.append("g").call(grid);

const dots = svg
.selectAll("circle")
.append("g")
.data(data)
.join("circle")
.attr("cx", d => xScale(xAccessor(d)))
.attr("cy", d => yScale(yAccessor(d)))
.attr("r", 2)
.attr("fill", d => colorScale(colorAccessor(d)));

svg
.selectAll(".voronoi")
.data(data)
.join("path")
.attr("class", "voronoi")
.attr("transform", `translate(${margin.left}, ${margin.top})`)
.attr("d", (d, i) => voronoi.renderCell(i))
.style("opacity", 0)
.style("fill", "transparent");
// .on("mouseenter", onMouseEnter)
// .on("mouseleave", onMouseLeave);

return svg.node();
}
Insert cell
Insert cell
Insert cell
xAccessor = d => d.following_count
Insert cell
yAccessor = d => d.followers_count
Insert cell
colorAccessor = d => d.gender
Insert cell
xScale = d3
.scaleLinear()
.domain(d3.extent(data, xAccessor))
.nice()
.range([margin.left, width - margin.right])
Insert cell
colorScale = d3
.scaleOrdinal()
.domain(d3.extent(data, colorAccessor))
.range(d3.schemeCategory10)
Insert cell
yScale = d3
.scaleLinear()
.domain(d3.extent(data, yAccessor))
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale).ticks(width / 80))
.call(g => g.select(".domain").remove())
.call(g =>
g
.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(data.x)
)
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(yScale))
.call(g => g.select(".domain").remove())
.call(g =>
g
.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(data.y)
)
Insert cell
grid = g =>
g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g =>
g
.append("g")
.selectAll("line")
.data(xScale.ticks())
.join("line")
.attr("x1", d => 0.5 + xScale(d))
.attr("x2", d => 0.5 + xScale(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom)
)
.call(g =>
g
.append("g")
.selectAll("line")
.data(yScale.ticks())
.join("line")
.attr("y1", d => 0.5 + yScale(d))
.attr("y2", d => 0.5 + yScale(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right)
)
Insert cell
Insert cell
width = 400
Insert cell
margin = ({ top: 20, right: 20, bottom: 20, left: 40 })
Insert cell
showTooltip = {
}
Insert cell
delaunay = d3.Delaunay.from(
data,
d => xScale(xAccessor(d)),
d => yScale(yAccessor(d))
)
Insert cell
voronoi = delaunay.voronoi()
Insert cell
(voronoi.xmax = width - margin.left - margin.right)
Insert cell
(voronoi.ymax = height - margin.top - margin.bottom)
Insert cell
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