Public
Edited
Mar 15, 2023
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
viewof position = Object.assign(html`<select>
<option value="alphabetical">Alphabetical Order</option>
<option value="value">Based on Value</option>
</select>`, {
value: new URLSearchParams(html`<a href>`.search).get("position") || "alphabetical"
});
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("style", "background-color: #212126");

const {nodes, links} = sankey_sorted(data);

svg.append("g")
.attr("stroke", "#212126")
.selectAll("rect")
.data(nodes)
.join("rect")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("height", d => d.y1 - d.y0)
.attr("width", d => {
if (d.targetLinks.length === 0) {
return d.x1 - d.x0 + (d.representation/8); // add 10 to width for left boxes
} else {
return d.x1 - d.x0; // use default width for other boxes
}
})
.attr("fill", color)
.append("title")
.text(d => `${d.name}\n${format(d.value)}`);

const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.5)
.selectAll("g")
.data(links)
.join("g");

if (edgeColor === "path") {
const gradient = link.append("linearGradient")
.attr("id", d => (d.uid = DOM.uid("link")).id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", d => d.source.x1)
.attr("x2", d => d.target.x0);

gradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", d => color(d.source));

gradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", d => color(d.target));
}

link.append("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => edgeColor === "none" ? "#aaa"
: edgeColor === "path" ? d.uid
: edgeColor === "input" ? color(d.source)
: color(d.target))
.attr("stroke-width", d => Math.max(1, d.width));

link.append("title")
.text(d => `${d.source.name} → ${d.target.name}\n${format(d.value)}`);

svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("fill", "#A6ACB0")
.selectAll("text")
.data(nodes)
.join("text")
.attr("x", d => {
if (d.targetLinks.length === 0) {
return d.x1 + (d.representation/8); // move label to the right of the box
} else {
return d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6;
}
})
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name);

return svg.node();
}
Insert cell
sankey = {
const sankey = d3.sankey()
.nodeId(d => d.name)
.nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.nodeWidth(15)
.nodePadding(10)
.extent([[1, 5], [width - 1, height - 5]]);
return ({nodes, links}) => sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.map(d => Object.assign({}, d))
});
}
Insert cell
sankey_sorted = {
const sankey = d3.sankey()
.nodeId(d => d.name)
.nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.nodeWidth(15)
.nodePadding(10)
.extent([[1, 5], [width - 1, height - 5]]);
return ({nodes, links}) => {
nodes.sort((a, b) => d3.ascending(a.name, b.name)); //nodes.sort((a, b) => b.money_invested - a.money_invested );
return sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.map(d => Object.assign({}, d))
})};
}
Insert cell
sankey2 = {
const sankey = d3.sankey()
.nodeId(d => d.name)
.nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.nodeWidth(15)
.nodePadding(10)
.extent([[1, 5], [width - 1, height - 5]]);
return ({nodes, links}) => {
// Calculate the height of nodes with the category "etf"
const etfHeight = (representation) => representation * 0.2;
// Update the nodes with the category "etf"
nodes.forEach(node => {
if (node.category === "etf") {
node.y0 = node.y1 = etfHeight(+node.representation);
}
});
return sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.map(d => Object.assign({}, d))
});
};
};
Insert cell
sankey3 = {
const sankey = d3.sankey()
.nodeId(d => d.name)
.nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.nodeWidth(15)
.nodePadding(10)
.extent([[1, 5], [width - 1, height - 5]]);

return ({nodes, links}) => {
// Calculate total representation for nodes with "etf" category
const etfRepresentationTotal = nodes
.filter(d => d.category === "etf")
.reduce((acc, cur) => acc + parseInt(cur.representation), 0);

// Calculate height scaling factor for nodes with "etf" category
const etfScalingFactor = d3.scaleLinear()
.domain([0, etfRepresentationTotal])
.range([0, height]);

// Calculate y0 and y1 positions for nodes with "etf" category
nodes.forEach(d => {
if (d.category === "etf") {
const nodeHeight = parseInt(d.representation) * 1.2;
d.y0 = etfScalingFactor(etfRepresentationTotal) - etfScalingFactor(parseInt(d.representation));
d.y1 = d.y0 + nodeHeight;
}
});

return sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.map(d => Object.assign({}, d))
});
};
}
Insert cell
sankey4 = {
const sankey = d3.sankey()
.nodeId(d => d.name)
.nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.nodeWidth(15)
.nodePadding(10)
.extent([[1, 5], [width - 1, height - 5]]);
return ({nodes, links}) => {
const etfNodes = nodes.filter(d => d.category === 'etf');
const totalValue = etfNodes.reduce((acc, d) => acc + +d.representation, 0);
const k = (height - 10) / totalValue; // Adjust constant to fit desired layout
let acc = 0;

const sankeyNodes = sankey({
nodes: nodes.map(d => {
if (d.category === 'etf') {
const value = +d.money_invested;
const y0 = d.y0 + k * acc;
const y1 = y0 + k * value;
acc += value;
return Object.assign({}, d, { y0, y1 });
} else {
return Object.assign({}, d);
}
}),
links: links.map(d => Object.assign({}, d))
}).nodes;

return { nodes: sankeyNodes, links };
};
}

Insert cell
format = {
const format = d3.format(",.0f");
return data.units ? d => `${format(d)} ${data.units}` : format;
}
Insert cell
color = {
const color = d3.scaleOrdinal(["#A88DF6", "#80C8FE", "#F98084", "#F99E71", "#ffe066"]);
return d => color(d.category === undefined ? d.name : d.category);
}
Insert cell
data = {
return {
nodes: [
{ name: "etf_1", category: "etf",money_invested: "500", representation: "25", expense_ratio: "1" },
{ name: "etf_2", category: "etf",money_invested: "500", representation: "25", expense_ratio: "0.5" },
{ name: "Vanguard_Information_Technology", category: "etf",money_invested: "1000", representation: "50", expense_ratio: "0.1" },
{ name: "category_1", category: "category",money_invested: "1000", representation: "10" },
{ name: "Technology_Equities", category: "category",money_invested: "1000", representation: "10" },
{ name: "Technology Hardware, Storage & Periphera", category: "sub_category", money_invested: "280",representation: "1" },
{ name: "Internet Software & Services", category: "sub_category",money_invested: "165",representation: "1"},
{ name: "Systems Software", category: "sub_category",money_invested: "1141",representation: "1"},
{ name: "other", category: "sub_category",money_invested: "414",representation: "1"},
],
links: [
{ source: "etf_1", target: "category_1", value: "500" },
{ source: "etf_2", target: "category_1", value: "500" },
{ source: "Vanguard_Information_Technology", target: "Technology_Equities", value: "1000" },
{ source: "Technology_Equities", target: "Technology Hardware, Storage & Periphera", value: "280" },
{ source: "Technology_Equities", target: "Internet Software & Services", value: "165" },
{ source: "Technology_Equities", target: "Systems Software", value: "141" },
{ source: "Technology_Equities", target: "other", value: "414" },
{ source: "category_1", target: "Systems Software", value: "1000" }
],
units: "instances"
}
}
Insert cell
width = 954
Insert cell
height = 600
Insert cell
d3 = require("d3@5", "d3-sankey@0.12")
Insert cell
Insert cell
sankey(data)
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