Public
Edited
Apr 11, 2023
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);

svg.append("g")
.attr("stroke-width", 1.5)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("path")
.data(sdata)
.join("path")
.attr("transform", d => `translate(${x1(d.width)},${y1(d.length)})`)
.attr("fill", d => scolor(d.variety))
.attr("d", d => sshape(d.category));
svg.append("g")
.attr("stroke-width", 1.5)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("path")
.data(pdata)
.join("path")
.attr("transform", d => `translate(${x2(d.width)},${y2(d.length)})`)
.attr("fill", d => pcolor(d.variety))
.attr("d", d => pshape(d.category));

return svg.node();
}
Insert cell
data = d3.csvParse(await FileAttachment("iris_shape@3.csv").text())
Insert cell
sdata = d3.csvParse(await FileAttachment("iris_shape@3.csv").text())
Insert cell
label = Object.assign({y: "↑ Sepal length (cm)", x: "Sepal width (cm) →"})
Insert cell
pdata = d3.csvParse(await FileAttachment("iris_shape@3.csv").text())
Insert cell
keys = data.columns.slice(0,4)
Insert cell
x1 = d3.scaleLinear()
.domain(d3.extent(sdata, d => d.width)).nice()
.range([margin.left, width - margin.right])
Insert cell
x2 = d3.scaleLinear()
.domain(d3.extent(pdata, d => d.width)).nice()
.range([margin.left, width - margin.right])
Insert cell
y1 = d3.scaleLinear()
.domain(d3.extent(sdata, d => d.length)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
y2 = d3.scaleLinear()
.domain(d3.extent(pdata, d => d.length)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
scolor = d3.scaleOrdinal(sdata.map(d => d.variety), d3.schemeCategory10)
Insert cell
pcolor = d3.scaleOrdinal(pdata.map(d => d.variety), d3.schemeCategory10)
Insert cell
sshape = d3.scaleOrdinal(sdata.map(d => d.category), d3.symbols.map(s => d3.symbol().type(s)()))
Insert cell
pshape = d3.scaleOrdinal(pdata.map(d => d.category), d3.symbols.map(s => d3.symbol().type(s)()))
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x1).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(label.x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y1))
.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(label.y))
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x1.ticks())
.join("line")
.attr("x1", d => 0.5 + x1(d))
.attr("x2", d => 0.5 + x2(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y1.ticks())
.join("line")
.attr("y1", d => 0.5 + y1(d))
.attr("y2", d => 0.5 + y2(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

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