Public
Edited
Nov 20, 2023
Fork of Line chart
1 fork
Insert cell
Insert cell
chart = {
// Declare the chart dimensions and margins.
const width = 928;
const height = 500;
const marginTop = 20;
const marginRight = 30;
const marginBottom = 30;
const marginLeft = 40;

// Declare the x (horizontal position) scale.
const x = d3.scaleUtc(
d3.extent(aapl, (d) => d.date),
[marginLeft, width - marginRight]
);

// Declare the y (vertical position) scale.
const y = d3.scaleLinear(
[0, d3.max(aapl, (d) => d.close)],
[height - marginBottom, marginTop]
);

// Declare the line generator.
const line = d3
.line()
.x((d) => x(d3.utcDay.offset(d.date, 15)))
.y((d) => y(d.close));

// Create the SVG container.
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto; height: intrinsic;");

addTimeAxis(x, svg, `translate(0,${height - marginBottom})`);

// Add the y-axis, remove the domain line, add grid lines and a label.
svg
.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y).ticks(height / 40))
.call((g) => g.select(".domain").remove())
.call((g) =>
g
.selectAll(".tick line")
.clone()
.attr("x2", width - marginLeft - marginRight)
.attr("stroke-opacity", 0.1)
)
.call((g) =>
g
.append("text")
.attr("x", -marginLeft)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("↑ Daily close ($)")
);

// Append a path for the line.
svg
.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("d", line(aapl));

svg
.selectAll("circle")
.data(aapl)
.join("circle")
.attr("cx", (d) => x(d3.utcDay.offset(d.date, 15)))
.attr("cy", (d) => y(d.close))
.attr("r", 3)
.attr("fill", "steelblue");

return svg.node();
}
Insert cell
function addTimeAxis(x, svg, xAxisTranslate) {
let xTickMarkValues = x.ticks(d3.timeYear);

// Get halfway points between tick marks
let xLabelValues = xTickMarkValues
.slice(0, -1)
.map(
(d, i) => new Date((d.getTime() + xTickMarkValues[i + 1].getTime()) / 2)
);

// Add the x-axis without text.
let xAxisWithoutText = svg
.append("g")
.attr("transform", xAxisTranslate)
.call(d3.axisBottom(x).tickValues(xTickMarkValues).tickSizeOuter(0))
.selectAll("text")
.style("display", "none");

// Add the x-axis text.
let xAxisText = svg
.append("g")
.attr("transform", xAxisTranslate)
.call(
d3
.axisBottom(x)
.tickValues(xLabelValues)
.tickSizeOuter(0)
.tickFormat(d3.utcFormat("%Y"))
);
xAxisText.selectAll("path").style("display", "none");
xAxisText.selectAll("line").style("display", "none");
}
Insert cell
aapl = (await FileAttachment("aapl.csv").csv({typed: true})).filter(d => d.date.getDate() <= 3)
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