Published
Edited
Aug 17, 2020
1 star
Insert cell
Insert cell
chart = {
const minX = x(data[0].date);
const maxX = x(data[data.length - 1].date);
const overwidth = maxX - minX + margin.left + margin.right;
console.log(minX, data[0].date, maxX, data[data.length - 1].date);


const parent = d3.create("div");

parent.append("svg")
.attr("width", width)
.attr("height", height)
.style("position", "absolute")
.style("pointer-events", "none")
.style("z-index", 1)
.call(svg => svg.append("g").call(yAxis));
parent.append("svg")
.attr("width", width)
.attr("height", height)
.style("position", "absolute")
.style("pointer-events", "none")
.style("z-index", 1)
.call(svg => svg.append("g").call(y2Axis));

const body = parent.append("div")
.style("overflow-x", "scroll")
.style("-webkit-overflow-scrolling", "touch");

body.append("svg")
.attr("width", overwidth)
.attr("height", height)
.style("display", "block")
.call(svg => svg.append("g").call(xAxis))
.append("path")
.datum(data)
.attr("fill", "steelblue")
.attr("d", area);

yield parent.node();

// Initialize the scroll offset after yielding the chart to the DOM.
body.node().scrollBy(overwidth, 0);
}
Insert cell
height = 420
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 30})
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width * 6 - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)]).nice(6)
.range([height - margin.bottom, margin.top])
Insert cell
y2 = d3.scaleLinear()
.domain(d3.extent(data2, d=> d.value)).nice(6)
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(d3.utcMonth.every(1200 / width)).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(6))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y))
Insert cell
y2Axis = g => g
.attr("transform", `translate(${width - margin.right},0)`)
.call(d3.axisRight(y2))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", margin.right)
.attr("y", 13)
.attr("fill", "black")
.attr("text-anchor", "end")
.text(data2.y2))
Insert cell
area = d3.area()
.curve(d3.curveStep)
.x(d => x(d.date))
.y0(y(0))
.y1(d => y(d.value))
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("aapl.csv").text(), d3.autoType).map(({date, close}) => ({date, value: close})), {y: "$ Close"})
Insert cell
d3 = require("d3@5")
Insert cell
data2 = Object.assign(d3.csvParse(await FileAttachment("data.csv").text(), d3.autoType), {y1: "↑ Debt as % of GDP", y2: "Debt hike in US$ trillion↑"})
Insert cell
line = d3.line()
.x(d => x(d.year) + x.bandwidth() / 2)
.y(d => y2(d.value))
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