Public
Edited
Mar 6, 2022
2 stars
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")
// .attr("stroke", "#000")
// .attr("stroke-opacity", 0.1)
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => x(d.date))
.attr("cy", d => y(d.value))
.attr("fill", d => z(d.value))
.attr("r", 2.5);



return svg.node()
}
Insert cell
height = 500
Insert cell
margin = ({top: 20, right: 40, bottom: 30, left: 30})
Insert cell
x = d3.scaleTime()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.value)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
z = {
const max = d3.max(data, d => Math.abs(d.value));
return d3.scaleSequential(d3.interpolateRdBu).domain([max, -max]);
}
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 60))
.call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${width - margin.right + 10},0)`)
.call(d3.axisRight(y).ticks(null, "+"))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line")
.filter(d => d === 0)
.clone()
.attr("x2", -width + margin.right + margin.left)
.attr("stroke", "#454545"))
.attr('stroke-width', 1.5)
.call(g => g.append("text")
.attr("fill", "#000")
.attr("x", -5)
.attr("y", margin.top)
.attr("dy", "0.32em")
.attr("text-anchor", "end")
.attr("font-weight", "bold")
.text("Anomaly (°C)"))
Insert cell
data = {
const data = []
await d3.csvParse(await FileAttachment("HadCRUT@1.5.0.1.0.analysis.summary_series.global.monthly.csv").text(), (d) => {
data.push({
date : d3.timeParse('%Y-%m')(d.date),
value: +d.value
})
})
return data
}
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