Published
Edited
Feb 13, 2021
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = Object.assign(
(await FileAttachment("median_female_income_homes_clean2@2.csv").csv()).map(
({ year, f_income_median, median_home, side }) => ({
name: year,
x: +f_income_median,
y: +median_home,
orient: side
})
),
{ x: "Median female income (thousands)", y: "Median home price (thousands)" }
)
Insert cell
Insert cell
xAccessor = d => d.x
Insert cell
yAccessor = d => d.y
Insert cell
Insert cell
xAccessor(data[0])
Insert cell
yAccessor(data[0])
Insert cell
Insert cell
line = d3
.line()
.curve(d3.curveCatmullRom)
.x(d => xScale(xAccessor(d)))
.y(d => yScale(yAccessor(d)))
Insert cell
Insert cell
margin = ({
top: 20,
bottom: 30,
left: 60,
right: 20
})
Insert cell
height = 600
Insert cell
width // built in ObservableHQ
Insert cell
Insert cell
xScale = d3
.scaleLinear()
.domain(d3.extent(data, xAccessor))
.nice()
.range([margin.left, width - margin.right])
Insert cell
yScale = d3
.scaleLinear()
.domain(d3.extent(data, yAccessor))
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale).ticks(width / 80, "$,f"))
.call(g => g.select(".domain").remove())
.call(g =>
g
.selectAll(".tick line")
.clone()
.attr("y2", -height)
.attr("stroke-opacity", 0.1)
)
.call(g =>
g
.append("text")
.attr("x", width - 4)
.attr("y", -4)
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.attr("fill", "black")
.text(data.x)
)
// .call(halo))
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(yScale).ticks(null, "$,f"))
.call(g => g.select(".domain").remove())
.call(g =>
g
.selectAll(".tick line")
.clone()
.attr("x2", width)
.attr("stroke-opacity", 0.1)
)
.call(g =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", 4)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.attr("fill", "black")
.text(data.y)
)
Insert cell
function length(path) {
return d3
.create("svg:path")
.attr("d", path)
.node()
.getTotalLength();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
import { chart as bostockChart } from '@d3/connected-scatterplot'
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