Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/4935 subgraph title margin config option #5041

Conversation

mathbraga
Copy link
Contributor

📑 Summary

Adds the option to introduce top and/or bottom margins for subgraphs title.

Resolves #4935

📏 Design Decisions

The approach is to increase height of subgraphs while keeping its children centered. Doing so creates space to move the
label down which creates the margin. The label only moves down by the value provided for top margin, and everything else moves by the sum of top + bottom margins.

The height of the subgraph is increased by 2 * total margin because of how the y axis is calculated for nodes.
y = node.y - height/2 means that if height is increased by twice the margin, the node will move up by the value of total margin, which is compensated in the code with node.y + total margin, returning the node to the center of its parent.

In the code, I chose to move the y axis of clusters themselves, that way everything inside of the cluster also moves, atleast that's what I understood from the code, so I went for that route for simplicity, other options would require me to also move every child node and edges inside each cluster.

This approach fails if a cluster has external connections, since it is considered a normal node with children, meaning it can't be repositioned as a cluster, from what I attempted, interacting with its y axis only moves its rectangle and not the whole node. I might be missing something, but from my understanding, all of its child nodes and externally connected nodes have to be repositioned along with every related edge individually. For this reason I asked in #4935 if title margins for this case could be moved to a new separate issue.

In summary, if I want to add 10 margin for top and 5 for bottom. I increase height by 30 (15x2), keep children centered, and move subgraph label down by 10 (only top margin):
image

I didn't add anything to the docs since I couldn't find how exactly it works with config changes. I looked around in the code and assumed config docs are generated automatically based on types.

📋 Tasks

Make sure you

Copy link

netlify bot commented Nov 18, 2023

Deploy Preview for mermaid-js ready!

Name Link
🔨 Latest commit 5718be5
🔍 Latest deploy log https://app.netlify.com/sites/mermaid-js/deploys/65653fb0461075000865de37
😎 Deploy Preview https://deploy-preview-5041--mermaid-js.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added the Type: Enhancement New feature or request label Nov 18, 2023
Copy link

codecov bot commented Nov 18, 2023

Codecov Report

Merging #5041 (5718be5) into develop (25e9bb3) will increase coverage by 36.39%.
The diff coverage is 85.71%.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##           develop    #5041       +/-   ##
============================================
+ Coverage    42.91%   79.31%   +36.39%     
============================================
  Files           23      166      +143     
  Lines         5029    13877     +8848     
  Branches        21      706      +685     
============================================
+ Hits          2158    11006     +8848     
+ Misses        2870     2719      -151     
- Partials         1      152      +151     
Flag Coverage Δ
e2e 85.20% <85.71%> (?)
unit 42.91% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
packages/mermaid/src/dagre-wrapper/clusters.js 88.67% <100.00%> (ø)
packages/mermaid/src/dagre-wrapper/index.js 98.90% <100.00%> (ø)
packages/mermaid/src/dagre-wrapper/edges.js 84.52% <71.42%> (ø)
packages/mermaid/src/utils/subGraphTitleMargins.ts 60.00% <60.00%> (ø)

... and 157 files with indirect coverage changes

Copy link
Member

@sidharthv96 sidharthv96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @mathbraga ! Love the tests.

Most of the comments are minor, but we need to dig in why it's not working for basic subgraphs without external connections.

Live Editor

image

packages/mermaid/src/utils/getSubGraphTitleMargins.ts Outdated Show resolved Hide resolved
packages/mermaid/src/dagre-wrapper/index.js Outdated Show resolved Hide resolved
packages/mermaid/src/utils.spec.ts Outdated Show resolved Hide resolved
packages/mermaid/src/dagre-wrapper/clusters.js Outdated Show resolved Hide resolved
packages/mermaid/src/dagre-wrapper/clusters.js Outdated Show resolved Hide resolved
@mathbraga
Copy link
Contributor Author

mathbraga commented Nov 19, 2023

@sidharthv96 First time I clicked your link to the live preview I got the same result as shown on your image. Then I checked a second time and now it seems to work all of a sudden.

image

Edit: Tested again on a fresh browser, the first load renders without the margins, then it fixes itself after refreshing the page.

A few other things that I noticed when trying to export the layout. If I choose to copy image to clipboard, download as png or svg, the images are generated correctly with the margins. The other options to create an export link in png, svg, kroki or copy markdown, all generate the image without the margins.

packages/mermaid/src/dagre-wrapper/clusters.js Outdated Show resolved Hide resolved
packages/mermaid/src/dagre-wrapper/clusters.js Outdated Show resolved Hide resolved
@sidharthv96
Copy link
Member

I noticed that setting the subGraphTitleMargin.bottom extends the bottom of the whole subgraph as well, that should be fixed.

image

@mathbraga
Copy link
Contributor Author

mathbraga commented Nov 24, 2023

@sidharthv96 I'm not sure I understand what you mean. If you set either top or bottom, both will extend the height of the whole subgraph. I implemented this way following the idea on the PR's design decision, where I stretch the subgraph around its children, making it so content stays centered.

Should I discard the idea to keep everything centered? I did this just to maintain the layout consistent to the original without margins, and because of the simplicity of implementation, since I don't have to move any of the children nodes, edges, edge labels, etc.

Without keeping content centered, it looks something like this (top: 20, bottom:10):
image

Is this what you mean?

@sidharthv96
Copy link
Member

I think the extra space at the bottom came due to the workaround used in the issue.

If we're adding a margin to the top/bottom of the title, I don't see the need to add extra padding to the bottom of the subgraph itself.

In case we need padding for the subgraph itself, they should be exposed as extra options.

Is keeping the children centred a requirement I missed?

Anyways, this is my personal opinion, we can discuss and finalize.

@mathbraga
Copy link
Contributor Author

Keeping content centered is not a requirement. I decided to do so just for consistency with the original layout.

So if the original looks centered, so will the layout with margins:
image

But I think I get your point, so just to make sure we are on the same page, from the 2 layouts below, you prefer the one on the left? (both have same margin values)
image

@sidharthv96
Copy link
Member

@mathbraga, yes I think the left one is the more accurate implementation of "Subgraph Title margin", the bottom margin should be a separate config.

image

@mathbraga
Copy link
Contributor Author

@sidharthv96 It shouldn't have the excessive spacing now. I also added one more test for subgraphs with edge labels and added string templates on a few lines I spotted on a function I was working on. Tell me what you think.

@sidharthv96 sidharthv96 added this to the v10 milestone Nov 28, 2023
@sidharthv96 sidharthv96 requested a review from a team November 28, 2023 06:14
Copy link
Member

@aloisklink aloisklink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me! Thanks for adding this feature!

I didn't add anything to the docs since I couldn't find how exactly it works with config changes. I looked around in the code and assumed config docs are generated automatically based on types.

Yep, that's right, they're generated automatically from the schema, so what you've done is perfect! This is what the docs will look like:

Screenshot of auto-generated docs for flowchart.subGraphTitleMargin config

image

@sidharthv96 sidharthv96 added this pull request to the merge queue Nov 28, 2023
Merged via the queue into mermaid-js:develop with commit 252a8a7 Nov 28, 2023
18 checks passed
Copy link

mermaid-bot bot commented Nov 28, 2023

@mathbraga, Thank you for the contribution!
You are now eligible for a year of Premium account on MermaidChart.
Sign up with your GitHub account to activate.

@sidharthv96
Copy link
Member

@mathbraga I'm afraid this PR has caused a regression. Do you think there is an easy fix for this?

#5188 (comment)

@mathbraga
Copy link
Contributor Author

mathbraga commented Jan 16, 2024

@sidharthv96 The issue with the state diagram is a missplaced parenthesis at line 193.

The issue with the sequence diagram are the changes made to sequenceRenderer.ts that came from merging from develop at 42ac630 (these changes seem to have been reverted).

So we just need to either move the parenthesis to the right place (right after subGraphTitleTopMargin) or change that line to a string template, and then revert 42ac630.

Do I have to open a new PR?

@sidharthv96
Copy link
Member

Can you see the Revert button in #5197 ?
If so, please use it and push the fixes to that PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Graph: Flow Type: Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

titleTopMargin option for flowchart does not affect subgraph titles
3 participants