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

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);

svg
.append("rect")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("width", width - margin.right - margin.left)
.attr("height", priceThreshold - margin.top)
.style("opacity", 0.1)
.append("text");

svg
.append("text")
.attr("x", margin.left + 5)
.attr("y", priceThreshold - 5)
.attr("font-style", "sans-serif")
.style("font-size", "12px")
.text("Bitcoin Value >$15,000");

svg
.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", lineGenerator);

return svg.node();
}
Insert cell
Insert cell
data = Object.assign(
d3
.csvParse(await FileAttachment("bitcoin_prices.csv").text(), d3.autoType)
.map(({ date, close }) => ({ date, value: close }))
)
Insert cell
Insert cell
lineGenerator = d3
.line()
.x(d => xScale(xAccessor(d)))
.y(d => yScale(yAccessor(d)))
Insert cell
Insert cell
xAccessor = d => dateParser(d.date)
Insert cell
yAccessor = d => d.value
Insert cell
dateParser = d3.timeParse("%m/%e/%Y")
Insert cell
xAccessor(data[100])
Insert cell
yAccessor(data[100])
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
.scaleUtc()
.domain(d3.extent(data, xAccessor))
.range([margin.left, width - margin.right])
.nice()
Insert cell
yScale = d3
.scaleLog()
.domain(d3.extent(data, yAccessor))
.rangeRound([height - margin.bottom, margin.top])
.nice()
Insert cell
Insert cell
xAxis = d3.axisBottom(xScale).tickFormat(d3.timeFormat("%b"))
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("Bitcoin Value (USD)")
Insert cell
xTitle = g =>
g
.append("text")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("x", width - margin.right - 80)
.attr("y", height - margin.bottom - 5)
// .style("text-anchor", "left")
.html("Date (2019 - 2020)")
Insert cell
Insert cell
priceThreshold = yScale(15000)
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