Published
Edited
Jun 17, 2022
Insert cell
Insert cell
data = Array.from({ length: 50 }).map((d, i) => ({
age: Math.random() * 150,
height: Math.random() * 2.0,
name: `Person ${i}`,
profession: ["Teacher", "Lawyer", "Coffee Grower"][
Math.floor(Math.random() * 3)
]
}))
Insert cell
{
const target = html`<svg id="target" viewBox="0 0 ${width} ${height}">
<circle cx="200" cy="200" r="30"></circle>
</svg>`;

const svg = d3.select(target); // also could have d3.select("#target"); to use the svg id tag

for (let d of data) {
svg
.append("circle")
.attr("cx", d.age * 5)
.attr("cy", d.height * 50)
.attr("r", 5)
.attr("fill", "steelblue");

svg
.append("text")
.attr("x", d.age * 5)
.attr("y", d.height * 50)
.style("font-size", "8pt")
.text(d.name);
}

return target;
}
Insert cell
md`## Data Binding`
Insert cell
{
const target = html`<svg id="target" viewBox="0 0 ${width} ${height}">
</svg>`;

const svg = d3.select(target);

const circles = svg
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => d.age * 5)
.attr("cy", d => d.height * 50)
.attr("r", 5)
.attr("fill", "steelblue");

return target;
}
Insert cell
md`## Scales`
Insert cell
{
const target = html`<svg id="target" viewBox="0 0 ${width} ${height}">
</svg>`;

const svg = d3.select(target);

const x = d3
.scaleLinear()
.domain([0, 120])
.range([0, width]);

const y = d3
.scaleLinear()
.domain([0, 2])
.range([0, height]);

const circles = svg
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => x(d.age))
.attr("cy", d => y(d.height))
.attr("r", 5)
.attr("fill", "steelblue");

return target;
}
Insert cell
md`## Axis`
Insert cell
{
// const target = html`<svg id="target" viewBox="0 0 ${width} ${height}">
//</svg>`;

const margin = { left: 50, right: 20, top: 20, bottom: 50 },
iwidth = width - margin.left - margin.right,
iheight = height - margin.top - margin.bottom;

//const svg = d3.select(target);
const svg = d3.create("svg").attr("width", width).attr("height", height);
const gDrawing = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

gDrawing
.append("rect")
.attr("width", iwidth)
.attr("height", iheight)
.attr("fill", "#eee");

const x = d3
.scaleLinear()
.domain([0, d3.max(data, d => d.age)])
.range([0, iwidth])
.nice();

const y = d3
.scaleLinear()
.domain([0, d3.max(data, d => d.height)])
.range([iheight, 0])
.nice();

gDrawing
.append("g")
.attr("class", "x--axis")
.attr("transform", `translate(0, ${iheight})`)
.call(d3.axisBottom(x));

gDrawing
.append("g")
.attr("class", "y--axis")
.call(d3.axisLeft(y));

gDrawing
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => x(d.age))
.attr("cy", d => y(d.height))
.attr("r", 5)
.attr("fill", "steelblue");

return svg.node();
}
Insert cell
md`## Categorical Scale`
Insert cell
{
//const target = html`<svg id="target" viewBox="0 0 ${width} ${height}">
//</svg>`;

const margin = { left: 50, right: 20, top: 20, bottom: 50 },
iwidth = width - margin.left - margin.right,
iheight = height - margin.top - margin.bottom;

//const svg = d3.select(target);

const svg = d3.create("svg").attr("width", width).attr("height", height);

const gDrawing = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

gDrawing
.append("rect")
.attr("width", iwidth)
.attr("height", iheight)
.attr("fill", "#eee");

const x = d3
.scaleLinear()
.domain([0, d3.max(data, d => d.age)])
.range([0, iwidth])
.nice();

const y = d3
.scaleLinear()
.domain([0, d3.max(data, d => d.height)])
.range([iheight, 0])
.nice();

const color = d3.scaleOrdinal(d3.schemeAccent);
// .domain(Array.from(new Set(data.map(d => d.profession)).values()));

gDrawing
.append("g")
.attr("class", "x--axis")
.attr("transform", `translate(0, ${iheight})`)
.call(d3.axisBottom(x));

gDrawing
.append("g")
.attr("class", "y--axis")
.call(d3.axisLeft(y));

const circles = gDrawing
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => x(d.age))
.attr("cy", d => y(d.height))
.attr("r", 5)
.attr("fill", d => color(d.profession));

return svg.node();
}
Insert cell
height = width * .5
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