Public
Edited
Feb 4, 2023
Insert cell
Insert cell
Plot.plot({
y: {
grid: true
},
x: {
grid: true
},
marks: [
Plot.line(averages, {
x: "cost",
y: "newAvg",
title: (d) => " Cost: " + d.cost + "\n Avg: " + d.newAvg + "\n Drop: " + d.avgDrop + "\n Gap: " + d.gap + "\n TotalIn: " + d.totalIn,
strokeWidth: 12,
opacity: 0.00
}),
Plot.line(averages, {
x: "cost",
y: "newAvg",
stroke: "red"
})
// Plot.line(averages, {
// x: "cost",
// y: "avgDrop",
// stroke: "blue"
// })
]
})
Insert cell
function averageAt(extraBuy) {
let currentPrice = 22.25;
let costAverage = 50.055;
let currentOwned = 56;
let costTotal = costAverage * currentOwned;
let currentValue = currentPrice * currentOwned;

let extraCost = extraBuy * currentPrice;

let fullTotal = costTotal + extraCost;

let fullAverage = fullTotal/(currentOwned + extraBuy);

let averageDrop = costAverage - fullAverage;

let gap = fullAverage - currentPrice;

//+ converts string to number. toFixed rounds the decimal
return { cost: +extraCost.toFixed(2),
newAvg: +fullAverage.toFixed(2),
avgDrop: +averageDrop.toFixed(2),
gap: +gap.toFixed(2),
totalIn: +fullTotal.toFixed(2)
};
}
Insert cell
averages = {
var avgs = [];

var currentAvg = {newAvg: 1000};

for (var i=1; currentAvg.newAvg > 27; i++) {
currentAvg = averageAt(i);
avgs.push(currentAvg)
}
return avgs
}
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