Published
Edited
Jul 11, 2019
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

// const g = svg.append("g")
// .attr("stroke-linecap", "round")
// .attr("stroke", "black")
// .selectAll("g")
// .data(data)
// .join("g")
// .attr("transform", d => `translate(${x(d.date)},0)`)
// .attr("stroke", d => d.open > d.close ? d3.schemeSet1[0]
// : d.close > d.open ? d3.schemeSet1[2]
// : d3.schemeSet1[8]);
const g = svg.append("g")
.attr("stroke-linecap", "round")
.attr("stroke", "black")
.selectAll("g")
.data(data)
.join("g")
.attr("transform", d => `translate(${x(d.date)},0)`)
.attr("stroke", d => d.open > d.close ? d3.schemeSet1[0]
: d.close > d.open ? d3.schemeSet1[2]
: d3.schemeSet1[8]);

g.append("line")
.attr("y1", d => y(d.low))
.attr("y2", d => y(d.high));

// g.append("line")
// .attr("y1", d => y(d.open))
// .attr("y2", d => y(d.close))
// .attr("stroke-width", x.bandwidth());
g.append("line")
.attr("y1", d => y(d.open))
.attr("y2", d => y(d.close))
.attr("stroke-width", x.bandwidth());

g.append("title")
.text(d => `${formatDate(d.date)}
Open: ${formatValue(d.open)}
Close: ${formatValue(d.close)} (${formatChange(d.open, d.close)})
Low: ${formatValue(d.low)}
High: ${formatValue(d.high)}`);


return svg.node();
}
Insert cell
height = 600
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
Insert cell
// x = d3.scaleBand()
// .domain(d3.timeDay
// .range(data[0].date, +data[data.length - 1].date + 1)
// .filter(d => d.getDay() !== 0 && d.getDay() !== 6))
// .range([margin.left, width - margin.right])
// .padding(0.2)

x = d3.scaleBand()
.domain(d3.timeDay
.range(data[0].date, +data[data.length - 1].date + 1)
.filter(d => d.getDay() !== 0 && d.getDay() !== 6))
.range([margin.left, width - margin.right])
.padding(0.2)
Insert cell
Insert cell
// y = d3.scaleLog()
// .domain([d3.min(data, d => d.low), d3.max(data, d => d.high)])
// .rangeRound([height - margin.bottom, margin.top])

y = d3.scaleLog()
.domain([d3.min(data, d => d.low), d3.max(data, d => d.high)])
.rangeRound([height - margin.bottom, margin.top])
Insert cell
Insert cell
// xAxis = g => g
// .attr("transform", `translate(0,${height - margin.bottom})`)
// .call(d3.axisBottom(x)
// .tickValues(d3.timeMonday
// .every(width > 720 ? 1 : 2)
// .range(data[0].date, data[data.length - 1].date))
// .tickFormat(d3.timeFormat("%-m/%-d")))
// .call(g => g.select(".domain").remove())

xAxis = g => g
.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(x)
.tickValues(d3.timeMonday
.every(width > 720 ? 1 : 2)
.range(data[0].date, data[data.length -1].date))
.tickFormat(d3.timeFormat("%-m/%-d")))
.call(g => g.select(".domain").remove())
Insert cell
// yAxis = g => g
// .attr("transform", `translate(${margin.left},0)`)
// .call(d3.axisLeft(y)
// .tickFormat(d3.format("$~f"))
// .tickValues(d3.scaleLinear().domain(y.domain()).ticks()))
// .call(g => g.selectAll(".tick line").clone()
// .attr("stroke-opacity", 0.2)
// .attr("x2", width - margin.left - margin.right))
// .call(g => g.select(".domain").remove())

yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y)
.tickFormat(d3.format("$~f"))
.tickValues(d3.scaleLinear().domain(y.domain()).ticks()))
.call(g => g.selectAll(".tick line").clone()
.attr("stroke-opacity",0.2)
.attr("x2", width - margin.left - margin.right))
.call(g => g.select(".domain").remove())

Insert cell
formatDate = d3.timeFormat("%B %-d, %Y")
Insert cell
formatValue = d3.format(".2f")
Insert cell
// formatChange = {
// const f = d3.format("+.2%");
// return (y0, y1) => f((y1 - y0) / y0);
// }

formatChange = {
const f = d3.format("+.2f");
return (y0, y1) => f((y1 - y0) / y0);
}
Insert cell
parseDate = d3.timeParse("%Y-%m-%d")
Insert cell
data = (await d3.csv(`https://gist.githubusercontent.com/mbostock/696604b8395aa68d0e7dcd74abd21dbb/raw/55c17dab8461cde25ca8c735543fba839b0c940b/AAPL.csv`, d => {
const date = parseDate(d["Date"]);
return {
date,
high: +d["High"],
low: +d["Low"],
open: +d["Open"],
close: +d["Close"]
};
})).slice(-120)
Insert cell

console.log(Object.keys(data));


Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more