Published
Edited
Jan 7, 2019
Importers
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
svg.append("g")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("fill", "none")
.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y))
.attr("r", 2);
svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("text")
.data(data)
.enter().append("text")
.attr("x", d => x(d.x))
.attr("y", d => y(d.y))
.text(d => d.name)
// .call(dodge);

return svg.node();
}
Insert cell
// function dodge(text, iterations = 300) {
// const nodes = text.nodes();
// const left = text => text.attr("text-anchor", "start").attr("dy", "0.32em");
// const right = text => text.attr("text-anchor", "end").attr("dy", "0.32em");
// const top = text => text.attr("text-anchor", "middle").attr("dy", "0.0em");
// const bottom = text => text.attr("text-anchor", "middle").attr("dy", "0.8em");
// const points = nodes.map(node => ({fx: +node.getAttribute("x"), fy: +node.getAttribute("y")}));
// const labels = points.map(({fx, fy}) => ({x: fx, y: fy}));
// const links = points.map((source, i) => ({source, target: labels[i]}));

// const simulation = d3.forceSimulation(points.concat(labels))
// .force("charge", d3.forceManyBody().distanceMax(80))
// .force("link", d3.forceLink(links).distance(4).iterations(4))
// .stop();

// for (let i = 0; i < iterations; ++i) simulation.tick();

// text
// .attr("x", (_, i) => labels[i].x)
// .attr("y", (_, i) => labels[i].y)
// .each(function(_, i) {
// const a = Math.atan2(labels[i].y - points[i].fy, labels[i].x - points[i].fx);
// d3.select(this).call(
// a > Math.PI / 4 && a <= Math.PI * 3 / 4 ? bottom
// : a > -Math.PI / 4 && a <= Math.PI / 4 ? left
// : a > -Math.PI * 3 / 4 && a <= Math.PI * 3 / 4 ? top
// : right);
// });
// }
Insert cell
height = 600
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data, d => d.x)).nice()
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.y)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", width - margin.right)
.attr("y", -4)
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(data.x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 4)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y))
Insert cell
data = {
const mtcars = await require("@observablehq/mtcars");
const data = mtcars.map(({name, mpg: x, hp: y}) => ({name, x, y}));
data.x = "Miles per gallon";
data.y = "Horsepower";
return data;
}
Insert cell
d3 = require("d3@5")
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