Published
Edited
Feb 13, 2021
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

svg
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => xScale(xAccessor(d)))
.attr("cy", d => yScale(yAccessor(d)))
.attr("r", radius)
.attr("fill", d => colorScale(colorAccessor(d)));

svg
.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(xAxis);

svg
.append("g")
.call(yAxis)
.attr("transform", `translate(${margin.left},0)`);

const yLabel = svg.append("g").call(yTitle);

const xLabel = svg.append("g").call(xTitle);

return svg.node();
}
Insert cell
Insert cell
data = await d3.json("https://assets.codepen.io/4506684/my_weather_data.json")
Insert cell
Insert cell
xAccessor = d => d.temperatureMin;
Insert cell
yAccessor = d => d.temperatureMax
Insert cell
dateParser = d3.timeParse("%Y-%m-%d")
Insert cell
colorAccessor = d => dateParser(d.date)
Insert cell
console.log(dateParser(data[10]))
Insert cell
Insert cell
width
Insert cell
height = 500
Insert cell
margin = ({ top: 20, right: 20, bottom: 30, left: 40 })
Insert cell
Insert cell
xScale = d3
.scaleLinear()
.domain(d3.extent(data, xAccessor))
.rangeRound([margin.left, width - margin.right])
.nice()
Insert cell
yScale = d3
.scaleLinear()
.domain(d3.extent(data, yAccessor))
.rangeRound([height - margin.bottom, margin.top])
.nice()
Insert cell
colorScale = d3
.scaleLinear()
.domain(d3.extent(data, colorAccessor))
.range(["#ccc", "#000"])
Insert cell
Insert cell
xAxis = d3.axisBottom(xScale)
Insert cell
yAxis = d3.axisLeft(yScale)
Insert cell
Insert cell
yTitle = g =>
g
.append("text")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("y", 10)
.html("Max. Temperature (°F)")
Insert cell
xTitle = g =>
g
.append("text")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("x", width - margin.right - 95)
.attr("y", height - 3)
// .style("text-anchor", "left")
.html("Min. Temperature (°F)")
Insert cell
Insert cell
Insert cell
Insert cell
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