Published
Edited
May 4, 2022
1 fork
4 stars
Also listed in…
Sankey
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
verticalPadding = fontSize / 2
Insert cell
viewBox = `${-leftMargin} ${-verticalPadding} ${width + 250} ${
height + verticalPadding
}`.split(" ")
Insert cell
leftMargin = 120
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable temp = []
Insert cell
Insert cell
Insert cell
data = structuredClone(DailyOptionsQuote)
Insert cell
tradingDayData = d3.pairs(data).map(([d1, d2]) => {
const retD1 = { ...d1 };
retD1.options = d1.options
.filter((x) => x["volume"] > valueLimit)
.map((each) => {
// return
each.newOI = d2.options.find(
(any) => any.alias === each.alias
)?.open_interest;
each.totalValue = each.open_interest * each.bid;
each.OIC = each.newOI - each.open_interest;
if (each.OIC < 0) {
each.OIC = Math.abs(each.OIC);
each.negOIC = true;
}
return each;
});
return retD1;
})
Insert cell
sumON = (d) => {
if (valueProperty === "value") {
return +d["volume"] * ((+d["bid"] + +d["ask"]) / 2).toPrecision(2);
} else {
return +d[valueProperty].toPrecision(2); //+(+d["open_interest"])
}
}
Insert cell
rollUpSeries = (eachDay) =>
d3.rollups(
eachDay,
(v) => d3.sum(v, sumON),
(d) => +d.expiry
)
Insert cell
rollupDay = (filteredData) =>
d3.rollups(
filteredData,
(v) => [v, d3.sum(v, (d) => d[valueProperty])],
(d) => +d.expiry,
(d) => d.strike
)
Insert cell
Insert cell
Insert cell
nodes = links.reduce((acc, each) => {
//source
const sourceMatch = acc.filter((d) => d.name === each.source);
if (sourceMatch.length === 0) {
acc.push({ name: each.source }); //dataItem: each.dataItem
}

//target
const targetMatch = acc.filter((d) => d.name === each.target);
if (targetMatch.length === 0) {
acc.push({ name: each.target }); //, dataItem: each.dataItem
}
return acc;
}, [])
Insert cell
SankeyLayout = {
return (
Sankey.sankey()
.nodeId((d) => d.name)
// .nodeSort((a, b) => {
// d3.descending(a.name, b.name);
// })
.nodeWidth(nodeWidth)
.nodePadding(padding + fontSize / 2)
.nodeId((d) => d.name)
.nodeAlign(Sankey.sankeyLeft)
.extent([
[0, fontSize],
[width - 5, height - 5]
])
);
}

// .linkSort((a, b) => d3.descending(a.value, b.value))
Insert cell
Insert cell
// selectedDataSearch = (search) =>
// tradingDayData[day_index].options.find((item) => item.OC.series === search)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
fontSize = 14
Insert cell
Insert cell
filteredData = d3.merge(tradingDayData.map((x) => x.options))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
FileAttachment("Screen Shot 2022-04-29 at 12.34.40 PM.png").image()
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