Curved edges

This commit is contained in:
Knut Sveidqvist
2022-07-29 15:06:21 +02:00
parent 00fe5d477d
commit ffe520db06
5 changed files with 134 additions and 11 deletions

View File

@@ -0,0 +1,75 @@
import { imgSnapshotTest, renderGraph } from '../../helpers/util.js';
describe('Mindmap', () => {
it('square shape', () => {
imgSnapshotTest(
`
mindmap
root[
The root
]
`,
{}
);
cy.get('svg');
});
it('rounded rect shape', () => {
imgSnapshotTest(
`
mindmap
root((
The root
))
`,
{}
);
cy.get('svg');
});
it('circle shape', () => {
imgSnapshotTest(
`
mindmap
root(
The root
)
`,
{}
);
cy.get('svg');
});
it('default shape', () => {
imgSnapshotTest(
`
mindmap
The root
`,
{}
);
cy.get('svg');
});
it('adding children', () => {
imgSnapshotTest(
`
mindmap
The root
child1
child2
`,
{}
);
cy.get('svg');
});
it('adding grand children', () => {
imgSnapshotTest(
`
mindmap
The root
child1
child2
child3
`,
{}
);
cy.get('svg');
});
});

View File

@@ -44,6 +44,22 @@ journey
Sit down: 5: Mee Sit down: 5: Mee
</div> </div>
<div class="mermaid2" style="width: 50%;"> <div class="mermaid2" style="width: 50%;">
mindmap
The root
C1
c2
c3
C4
c5
c6
C7
c8
c9
C8
c10
c11
</div>
<div class="mermaid2" style="width: 50%;">
mindmap mindmap
root[ root[
The root where the things The root where the things

View File

@@ -160,7 +160,9 @@ function layoutMindmap(node, conf) {
// const bb = new BoundingBox(10, 10); // const bb = new BoundingBox(10, 10);
// const layout = new Layout(bb); // const layout = new Layout(bb);
// // const layout = new HorizontalLayout(bb); // // const layout = new HorizontalLayout(bb);
if (node.children.length === 0) {
return node;
}
const trees = []; const trees = [];
// node.children.forEach((child, index) => { // node.children.forEach((child, index) => {
// const tree = clone(node); // const tree = clone(node);
@@ -186,6 +188,13 @@ function layoutMindmap(node, conf) {
trees.push(layout(tree, dirFromIndex(i), conf)); trees.push(layout(tree, dirFromIndex(i), conf));
} }
} }
// Let each node know the direct of its tree for when we draw the branches.
trees.forEach((tree, index) => {
tree.result.direction = dirFromIndex(index);
eachNode(tree.result, (node) => {
node.direction = dirFromIndex(index);
});
});
// Merge the trees into a single tree // Merge the trees into a single tree
const result = mergeTrees(node, trees); const result = mergeTrees(node, trees);
@@ -197,6 +206,7 @@ function layoutMindmap(node, conf) {
// res.result.children.push(tree.result); // res.result.children.push(tree.result);
// }); // });
console.log('Trees', trees); console.log('Trees', trees);
eachNode;
return node; return node;
} }
/** /**

View File

@@ -53,7 +53,7 @@ const getStyles = (options) =>
stroke-width: 3; stroke-width: 3;
} }
${genSections(options)} ${genSections(options)}
.section-root rect, .section-root path { .section-root rect, .section-root path, .section-root circle {
fill: ${options.git0}; fill: ${options.git0};
} }
.section-root text { .section-root text {
@@ -65,6 +65,8 @@ const getStyles = (options) =>
justify-content: center; justify-content: center;
align-items: center; align-items: center;
} }
.edge {
fill: none;
}
`; `;
export default getStyles; export default getStyles;

View File

@@ -126,7 +126,8 @@ export const drawNode = function (elem, node, section, conf) {
.attr('text-anchor', 'middle') .attr('text-anchor', 'middle')
.call(wrap, node.width); .call(wrap, node.width);
const bbox = txt.node().getBBox(); const bbox = txt.node().getBBox();
node.height = bbox.height + conf.fontSize * 1.1 * 0.5 + node.padding; const fontSize = conf.fontSize.replace ? conf.fontSize.replace('px', '') : conf.fontSize;
node.height = bbox.height + fontSize * 1.1 * 0.5 + node.padding;
node.width = bbox.width + 2 * node.padding; node.width = bbox.width + 2 * node.padding;
if (node.icon) { if (node.icon) {
if (node.type === db.nodeType.CIRCLE) { if (node.type === db.nodeType.CIRCLE) {
@@ -206,14 +207,33 @@ export const drawNode = function (elem, node, section, conf) {
}; };
export const drawEdge = function drawEdge(edgesElem, mindmap, parent, depth, section, conf) { export const drawEdge = function drawEdge(edgesElem, mindmap, parent, depth, section, conf) {
// edgesElem
// .append('line')
// .attr('x1', parent.x + parent.width / 2)
// .attr('y1', parent.y + parent.height / 2)
// .attr('x2', mindmap.x + mindmap.width / 2)
// .attr('y2', mindmap.y + mindmap.height / 2)
// .attr('class', 'edge section-edge-' + section + ' edge-depth-' + depth);
//<path d="M100,250 Q250,100 400,250 T700,250" />
const sx = parent.x + parent.width / 2;
const sy = parent.y + parent.height / 2;
const ex = mindmap.x + mindmap.width / 2;
const ey = mindmap.y + mindmap.height / 2;
const mx = ex > sx ? sx + Math.abs(sx - ex) / 2 : sx - Math.abs(sx - ex) / 2;
const my = ey > sy ? sy + Math.abs(sy - ey) / 2 : sy - Math.abs(sy - ey) / 2;
const qx = ex > sx ? Math.abs(sx - mx) / 2 + sx : -Math.abs(sx - mx) / 2 + sx;
const qy = ey > sy ? Math.abs(sy - my) / 2 + sy : -Math.abs(sy - my) / 2 + sy;
edgesElem edgesElem
.append('line') .append('path')
.attr('x1', parent.x + parent.width / 2) .attr(
.attr('y1', parent.y + parent.height / 2) 'd',
.attr('x2', mindmap.x + mindmap.width / 2) parent.direction === 'TB' || parent.direction === 'BT'
.attr('y2', mindmap.y + mindmap.height / 2) ? `M${sx},${sy} Q${sx},${qy} ${mx},${my} T${ex},${ey}`
// .attr('stroke', color) : `M${sx},${sy} Q${qx},${sy} ${mx},${my} T${ex},${ey}`
// .attr('stroke-width', 15 - depth * 3) )
.attr('class', 'edge section-edge-' + section + ' edge-depth-' + depth); .attr('class', 'edge section-edge-' + section + ' edge-depth-' + depth);
}; };