Public
Edited
Nov 28, 2024
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data.filter(str => str.date.toString().includes('28'))
Insert cell
moveAvgData = mergeArr(data.map(d => d.date), moveAvg(data.map(d => d.value), 10, 10))
Insert cell
function moveAvg(array, countBefore, countAfter) { // from https://stackoverflow.com/questions/19981713/html5-js-chart-with-moving-average
if (countAfter == undefined) countAfter = 0;
const result = []
for (let i = 0; i < array.length; i++) {
const subArr = array.slice(Math.max(i - countBefore, 0), Math.min(i + countAfter + 1, array.length));
const avg = subArr.reduce((a,b) => a + (isNaN(b) ? 0 : b), 0) / subArr.length;
result.push(avg);
}
return result
}
Insert cell
function mergeArr(arr1, arr2, arr3 = []) {
const result = []
for(let i = 0; i < arr1.length; i++) {
let obj = {}
obj.date = arr1[i];
obj.value = arr2[i];
obj.conf = arr3[i];
result.push(obj);
}
return result
}
Insert cell
data[0]
Insert cell
data = Object.assign((d3.csvParse(await FileAttachment("aapl.csv").text(), d3.autoType)).map(({date, close}) => ({date, value: close})), {y: "$ Close"})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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