Published
Edited
Jun 5, 2020
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

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

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

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

svg.append("path")
.datum(pdata_ba)
.attr("fill", "none")
.attr("stroke", "orange")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line_ba);
return svg.node();
}
Insert cell
// data = Object.assign((d3.csvParse(await FileAttachment("aapl.csv").text(), d3.autoType)).map(({date, close}) => ({date, value: close})), {y: "$ Close"})
Insert cell
sortOrder = ["v6", "v4"];
Insert cell
fieldName = "probesNum"
Insert cell
dd = await FileAttachment("data.csv").text() //.map(d => ({ ...d, date: new Date(d.date)}))
Insert cell
ddd = d3.csvParse(dd).flatMap(r => ([{ probesNum: 100 * Number(r["V6PROBES"]) / Number(r["V4PROBES"]), date: new Date(r['DATE']), name: 'v6' }, { probesNum: Number(r["V4PROBES"]), date: new Date(r['DATE']), name: 'v4'}]) )
Insert cell
data = {
const data = ddd
.filter(d => new Date(d.date) > new Date('2013-10-01'))
.map(d => ({ ...d, date: new Date(d.date)}))
.sort((a,b) => sortOrder.indexOf(a.name) < sortOrder.indexOf(b.name) && -1 || +1 );
const dates = Array.from(d3.group(data, d => +d.date).keys()).sort(d3.ascending);
return {
dates: dates.map(d => new Date(d)),
series: d3.groups(data, d => d.name).map(([name, values]) => {
const value = new Map(values.map(d => [+d.date, d[fieldName]]));
return {name, values: dates.map(d => value.get(d))};
})
};
}
Insert cell
pdata_si = data.series[0].values.map((d,i) => ({ date: data.dates[i], value: d}));
Insert cell
pdata_si[pdata_si.length-1]
Insert cell
pdata_ba = data.series[1].values.map((d,i) => ({ date: data.dates[i], value: d}));
Insert cell
pdata_ba[pdata_ba.length-1]
Insert cell
line_si = d3.line()
.defined(d => !isNaN(d.value))
.x(d => x_si(d.date))
.y(d => y(d.value))
Insert cell
line_ba = d3.line()
.defined(d => !isNaN(d.value))
.x(d => x_ba(d.date))
.y(d => y(d.value))
Insert cell
x_si = d3.scaleUtc()
.domain(d3.extent(pdata_si, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
x_ba = d3.scaleUtc()
.domain(d3.extent(pdata_si, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
// .domain([0, d3.max(data, d => d.value)]).nice()
.domain([0, 100]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x_si).ticks(width / 80).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.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(pdata_si.y))
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
height = 500
Insert cell
d3 = require("d3@5", "d3-array@2")
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