Published
Edited
Aug 19, 2022
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height * 2]);

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

//=========================================================
const node = svg
.selectAll("g")
.attr("transform", d => `translate(${d})`)
.data(nodes)
.join("g");

node
.append("rect")
.attr("stroke", "#000")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("height", d => d.y1 - d.y0)
.attr("width", d => 20)
.attr("fill", color)
.attr("id", function(d, i) {
d.id = i;
return "rect-" + i;
})
.append("title")
.text(function(d, i) {
return `Подсистема: ${nodes[i].appName}\nСХД: ${nodes[i].dbName}\nСущность: ${nodes[i].entityName}\nОписание: ${nodes[i].entityDesc}`;
});

node
.append("text")
.attr("font-family", "'Source Sans Pro', sans-serif")
.attr("font-size", 8)
.attr("x", d => (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(function(d, i) {
return `Подсистема: ${nodes[i].appName}`;
})
.attr("id", function(d, i) {
d.id = i;
return "text-" + i;
})
.append("svg:tspan")
.attr("x", d => (d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6))
.attr("y", d => (d.y1 + d.y0) / 2 + 12)
.attr("id", function(d, i) {
d.id = i;
return "text-shd-" + i;
})
.text(function(d, i) {
return nodes[i].dbName === "" ? "" : `СХД: ${nodes[i].dbName}`
})
.append("svg:tspan")
.attr("x", d => (d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6))
.attr("y", d => (d.y1 + d.y0) / 2 + 20)
.attr("id", function(d, i) {
d.id = i;
return "text-tbl-" + i;
})
.text(function(d, i) {
return nodes[i].entityName === "" ? "" : `Сущность: ${nodes[i].entityName}`
});

node.on("click", highlight_node_links)

//=========================================================
const link = svg
.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.5)
.attr("transform", d => `translate(${d})`)
.selectAll("g")
.data(links)
.join("g")
.style("mix-blend-mode", "multiply");

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("transform", d => `translate(${d})`)
.attr("d", d3.sankeyLinkHorizontal())
.attr("id", function(d, i) {
d.id = i;
return "link-" + i;
})
.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}`);
.text(function(d, i) {
if (links[i].srcTaskName === "null") {
return `${d.source.name} → ${d.target.name}`
}
return `Задача:${links[i].srcTaskName}\nОписание:${links[i].srcTaskDesc}\nВремя запуска:${links[i].srcStartedAt}\nВремя завершения:${links[i].srcEndedAt}`;
})

//=========================================================

svg.call(d3.zoom()
.extent([[0, 0], [width, height]])
.scaleExtent([1, 8])
.on("zoom", zoomed1));

function zoomed({transform}) {
node.attr("transform", d => `translate(${transform.apply(d)})`);
link.attr("transform", d => `translate(${transform.apply(d)})`);
}

function zoomed1({transform}) {
node.attr("transform", transform);
link.attr("transform", transform);
}

function highlightNodes(node, name) {
let opacity = 0.3;

if (node.name == name) {
opacity = 1;
}
node.sourceLinks.forEach(function(link) {
if (link.target.name == name) {
opacity = 1;
}
});
node.targetLinks.forEach(function(link) {
if (link.source.name == name) {
opacity = 1;
}
});

return opacity;
}
return svg.node();
}
Insert cell
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
format = {
const format = d3.format(",.0f");
return data.units ? d => `${format(d)}` : format;
}
Insert cell
color = {
const color = d3.scaleOrdinal(d3.schemeTableau10);
return d => color(d.category === undefined ? d.name : d.category);
}
Insert cell
data = {
const links = await FileAttachment("02_layer@8.csv").tsv({typed: true});
const names = Array.from(new Set(links.flatMap(l => [l.source, l.target])), name => ({name, category: name}));
const nodes = names//.filter(l => l.name.includes("__"))
.flatMap(l => {
return [links.filter(n => n.source === l.name || n.target === l.name)
.flatMap(n => {
if (n.source === l.name) {
const name = l.name;
const category = l.category;
const appName = n.srcAppName == "null" ? l.name : n.srcAppName;
const dbName = n.srcDbName == "null" ? "" : n.srcDbName;
const entityDesc = n.srcEntityDesc == "null" ? "" : n.srcEntityDesc;
const entityName = l.name == appName ? "" : l.name;
return (entityName,{name, category, appName, dbName, entityName, entityDesc})
} else {
const name = l.name;
const category = l.category;
const appName = n.trgAppName == "null" ? l.name : n.trgAppName;
const dbName = n.trgDbName == "null" ? "" : n.trgDbName;
const entityDesc = n.trgEntityDesc == "null" ? "" : n.trgEntityDesc;
const entityName = l.name == appName ? "" : l.name;
return (entityName,{name, category, appName, dbName, entityName, entityDesc})
}
})[0]];
});
return {nodes, links, units: ""};
}
Insert cell
width = 4096
Insert cell
height = 1200
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@6", "d3-sankey@0.12")
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