#815 Adding labels to subgraphs

This commit is contained in:
Knut Sveidqvist
2022-12-08 19:41:17 +01:00
parent ebf76e3d1f
commit 0b01c3376d
2 changed files with 48 additions and 12 deletions

View File

@@ -106,6 +106,7 @@ cyto TD
core1 --- mem core1 --- mem
exi --- rtc exi --- rtc
rtc{{rtc}}
</pre </pre
> >
<pre id="diagram" class="mermaid2"> <pre id="diagram" class="mermaid2">

View File

@@ -63,6 +63,7 @@ export const addVertices = function (vert, svgId, root, doc, diagObj, parentLook
// We create a SVG label, either by delegating to addHtmlLabel or manually // We create a SVG label, either by delegating to addHtmlLabel or manually
let vertexNode; let vertexNode;
const labelData = { width: 0, height: 0 };
if (evaluate(getConfig().flowchart.htmlLabels)) { if (evaluate(getConfig().flowchart.htmlLabels)) {
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that? // TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
const node = { const node = {
@@ -72,6 +73,10 @@ export const addVertices = function (vert, svgId, root, doc, diagObj, parentLook
), ),
}; };
vertexNode = addHtmlLabel(svg, node).node(); vertexNode = addHtmlLabel(svg, node).node();
const bbox = vertexNode.getBBox();
labelData.width = bbox.width;
labelData.height = bbox.height;
labelData.labelNode = vertexNode;
vertexNode.parentNode.removeChild(vertexNode); vertexNode.parentNode.removeChild(vertexNode);
} else { } else {
const svgLabel = doc.createElementNS('http://www.w3.org/2000/svg', 'text'); const svgLabel = doc.createElementNS('http://www.w3.org/2000/svg', 'text');
@@ -88,6 +93,10 @@ export const addVertices = function (vert, svgId, root, doc, diagObj, parentLook
svgLabel.appendChild(tspan); svgLabel.appendChild(tspan);
} }
vertexNode = svgLabel; vertexNode = svgLabel;
const bbox = vertexNode.getBBox();
labelData.width = bbox.width;
labelData.height = bbox.height;
labelData.labelNode = vertexNode;
} }
let radious = 0; let radious = 0;
@@ -176,12 +185,14 @@ export const addVertices = function (vert, svgId, root, doc, diagObj, parentLook
nodeEl = insertNode(nodes, node, vertex.dir); nodeEl = insertNode(nodes, node, vertex.dir);
boundingBox = nodeEl.node().getBBox(); boundingBox = nodeEl.node().getBBox();
} }
const data = { const data = {
id: vertex.id, id: vertex.id,
// labelStyle: styles.labelStyle, // labelStyle: styles.labelStyle,
// shape: _shape, // shape: _shape,
// labelText: vertexText, labelText: vertexText,
labels: [{ text: vertexText }, { text: vertexText }], labelData,
// labels: [{ text: vertexText }],
// rx: radious, // rx: radious,
// ry: radious, // ry: radious,
// class: classStr, // class: classStr,
@@ -615,8 +626,12 @@ export const draw = function (text, id, _version, diagObj) {
id: 'root', id: 'root',
layoutOptions: { layoutOptions: {
'elk.hierarchyHandling': 'INCLUDE_CHILDREN', 'elk.hierarchyHandling': 'INCLUDE_CHILDREN',
'org.eclipse.elk.padding': '[top=100, left=100, bottom=110, right=110]',
// 'org.eclipse.elk.layered.spacing.nodeNodeBetweenLayers': 120,
// 'elk.layered.spacing.nodeNodeBetweenLayers': '140',
'elk.layered.spacing.edgeNodeBetweenLayers': '30',
// 'elk.algorithm': 'layered', // 'elk.algorithm': 'layered',
// 'elk.direction': 'DOWN', 'elk.direction': 'DOWN',
// 'elk.port.side': 'SOUTH', // 'elk.port.side': 'SOUTH',
// 'nodePlacement.strategy': 'SIMPLE', // 'nodePlacement.strategy': 'SIMPLE',
// 'org.eclipse.elk.spacing.labelLabel': 120, // 'org.eclipse.elk.spacing.labelLabel': 120,
@@ -679,8 +694,20 @@ export const draw = function (text, id, _version, diagObj) {
if (!node.parent) { if (!node.parent) {
graph.children.push(node); graph.children.push(node);
} }
// node.nodePadding = [120, 50, 50, 50];
// node['org.eclipse.elk.spacing.nodeNode'] = 120;
// Subgraph
if (parentLookupDb.childrenById[nodeId] !== undefined) { if (parentLookupDb.childrenById[nodeId] !== undefined) {
// console.log('UGH node', node); node.labels = [
{
text: node.labelText,
layoutOptions: {
'nodeLabels.placement': '[H_CENTER, V_TOP, INSIDE]',
},
width: node.labelData.width,
height: node.labelData.height,
},
];
delete node.x; delete node.x;
delete node.y; delete node.y;
delete node.width; delete node.width;
@@ -730,8 +757,15 @@ export const draw = function (text, id, _version, diagObj) {
const drawNodes = (relX, relY, nodeArray, svg, subgraphsEl, diagObj) => { const drawNodes = (relX, relY, nodeArray, svg, subgraphsEl, diagObj) => {
nodeArray.forEach(function (node) { nodeArray.forEach(function (node) {
if (node) { if (node) {
nodeDb[node.id].offset = {
posX: node.x + relX,
posY: node.y + relY,
x: relX,
y: relY,
};
if (node.type === 'group') { if (node.type === 'group') {
subgraphsEl const subgraphEl = subgraphsEl.insert('g').attr('class', 'subgraph');
subgraphEl
.insert('rect') .insert('rect')
.attr('class', 'subgraph node') .attr('class', 'subgraph node')
.attr('style', 'fill:#ccc;stroke:black;stroke-width:1') .attr('style', 'fill:#ccc;stroke:black;stroke-width:1')
@@ -739,7 +773,14 @@ const drawNodes = (relX, relY, nodeArray, svg, subgraphsEl, diagObj) => {
.attr('y', node.y + relY) .attr('y', node.y + relY)
.attr('width', node.width) .attr('width', node.width)
.attr('height', node.height); .attr('height', node.height);
log.info('Id (UGH)= ', node.type, node.domId, svg.select(`[id="${node.domId}"]`)); const label = subgraphEl.insert('g').attr('class', 'label');
label.attr(
'transform',
`translate(${node.labels[0].x + relX + node.x}, ${node.labels[0].y + relY + node.y})`
);
label.node().appendChild(node.labelData.labelNode);
log.info('Id (UGH)= ', node.type, node.labels);
} else { } else {
log.info('Id (UGH)= ', node.id); log.info('Id (UGH)= ', node.id);
node.el.attr( node.el.attr(
@@ -747,12 +788,6 @@ const drawNodes = (relX, relY, nodeArray, svg, subgraphsEl, diagObj) => {
`translate(${node.x + relX + node.width / 2}, ${node.y + relY + node.height / 2})` `translate(${node.x + relX + node.width / 2}, ${node.y + relY + node.height / 2})`
); );
} }
nodeDb[node.id].offset = {
posX: node.x + relX,
posY: node.y + relY,
x: relX,
y: relY,
};
} }
}); });
nodeArray.forEach(function (node) { nodeArray.forEach(function (node) {