Published
Edited
Dec 27, 2019
Insert cell
Insert cell
legend = {
const id = DOM.uid().id;

return html`<style>

.${id} {
display: flex;
min-height: 33px;
font: 20px sans-serif;
margin-left: ${margin.left}px;
}

.${id}-item {
display: flex;
align-items: center;
margin-right: 10px;
}

</style>
<div class="${id}">${color.domain().map((name, i) => html`
<div class="${id}-item" title="${data.labels === undefined ? "" : (data.labels[i] + "").replace(/"/g, "&quot;")}">
<svg viewBox="-10 -10 20 20" width="20" height="20" style="margin-right: 3px;">
<path fill="${color(name)}" d="${shape(name)}"></path>
</svg>
${document.createTextNode(name)}
</div>`)}
</div>`;
}
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80)) // control the number of ticks highlighted on axis
.call(g => g.select(".domain").remove()) // if comment out this line, I can find in element tag I can find a Path with className "domain"
.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
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);

svg
.append("g")
.attr("stroke-width", 1.5)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("g")
.data(data)
.join("g")
.attr("transform", d => `translate(${x(d.x)},${y(d.y)})`)
.call(g =>
g
.append("path")
.attr("fill", d => color(d.category))
.attr("d", d => shape(d.category))
)
.call(g =>
g
.append("text") // it is useful but will make a mess
.attr("dy", "0.35em")
.attr("x", 10)
.attr("fill", d => color(d.category)) // use name to
//.text(d => d.category)
.text(d => d.name)
);

return svg.node();
}
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("iris.csv").text(), ({species: category, sepalLength: x, sepalWidth: y}) => ({category, x: +x, y: +y})), {x: "Sepal length (cm) →", y: "↑ Sepal width (cm)"})
Insert cell
data.x
Insert cell
data.y
Insert cell
d3.csvParse(await FileAttachment("iris.csv").text())
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
color = d3.scaleOrdinal(data.map(d => d.category), d3.schemeCategory10)
Insert cell
d3.symbols.map(s => d3.symbol().type(s)())
Insert cell
shape(data[0].category)
Insert cell
shape = d3.scaleOrdinal(data.map(d => d.category), d3.symbols.map(s => d3.symbol().type(s)()))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.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
x.ticks()
Insert cell
grid = g => g
.attr("stroke", "currentColor")// red is fine too
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
margin = ({top: 25, right: 20, bottom: 35, left: 40})
Insert cell
height = 600
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