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

Update C4 Diagram to new renderer #6276

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from

Conversation

yari-dewalt
Copy link
Collaborator

📑 Summary

Updates the C4 diagram to use the new way of rendering as well as other missing features.

Added Features:

  • Layout algorithm
  • Direction statement
  • Themes
  • Style, class, classDef statements
  • Clickable links for nodes and relationships
  • Sprites
  • More macros for the C4 diagrams
  • Tags
  • Legend

Everything is documented in the updated documentation.

📏 Design Decisions

Since the diagram is originally based off of PlantUML's implementation I based the new features off that as well. While diagram feature specific, below are some design decisions that were made.

In this first version there are only two supported sprites: "person" and "database" but more can easily be added in the future.

The legend is placed at the bottom right of the diagram if shown and will show all tags and any elements that have an associated legend text.

Clickable links work for nodes but also relationships. A thicker invisible path is drawn on top that is clickable.

Added an octagon shape since one of the new macros is EightSidedShape() for the ?shape argument. This shape is C4 diagram specific as of now but is essentially the same as the hexagon shape and maybe should be registered as a flowchart shape as well.

Since there are multiple ways to style elements in the diagram there is a styling priority. The priority goes tags -> UpdateElementStyle() / UpdateRelStyle() -> Style and Class statements. Style and class statements are shared from the other updated diagrams and should be the highest priority since they are the recommended way to style elements.

Some examples showcasing the new rendering and some new features:
image
image
image

📋 Tasks

Make sure you

  • 📖 have read the contribution guidelines
  • 💻 have added necessary unit/e2e tests.
  • 📓 have added documentation. Make sure MERMAID_RELEASE_VERSION is used for all new features.
  • 🦋 If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Copy link

changeset-bot bot commented Feb 13, 2025

⚠️ No Changeset found

Latest commit: fb65b39

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

netlify bot commented Feb 13, 2025

Deploy Preview for mermaid-js ready!

Name Link
🔨 Latest commit fb65b39
🔍 Latest deploy log https://app.netlify.com/sites/mermaid-js/deploys/67b76eeb2512240008c7e83b
😎 Deploy Preview https://deploy-preview-6276--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.

Copy link

pkg-pr-new bot commented Feb 13, 2025

Open in Stackblitz

npm i https://pkg.pr.new/mermaid-js/mermaid@6276
npm i https://pkg.pr.new/mermaid-js/mermaid/@mermaid-js/layout-elk@6276
npm i https://pkg.pr.new/mermaid-js/mermaid/@mermaid-js/parser@6276
npm i https://pkg.pr.new/mermaid-js/mermaid/@mermaid-js/mermaid-zenuml@6276

commit: fb65b39

Copy link

codecov bot commented Feb 13, 2025

Codecov Report

Attention: Patch coverage is 0% with 1588 lines in your changes missing coverage. Please review.

Project coverage is 4.40%. Comparing base (ad2f172) to head (fb65b39).

Files with missing lines Patch % Lines
packages/mermaid/src/diagrams/c4/c4Db.ts 0.00% 1074 Missing ⚠️
packages/mermaid/src/diagrams/c4/renderingUtil.ts 0.00% 250 Missing ⚠️
...endering-util/rendering-elements/shapes/octagon.ts 0.00% 84 Missing ⚠️
packages/mermaid/src/diagrams/c4/styles.js 0.00% 55 Missing ⚠️
...ages/mermaid/src/diagrams/c4/c4Renderer-unified.ts 0.00% 41 Missing ⚠️
packages/mermaid/src/diagrams/c4/c4Icons.ts 0.00% 35 Missing ⚠️
...aid/src/rendering-util/rendering-elements/edges.js 0.00% 20 Missing ⚠️
packages/mermaid/src/diagrams/c4/c4Diagram.ts 0.00% 13 Missing ⚠️
packages/mermaid-layout-elk/src/render.ts 0.00% 9 Missing ⚠️
...id/src/rendering-util/rendering-elements/shapes.ts 0.00% 4 Missing ⚠️
... and 2 more
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           develop   #6276      +/-   ##
==========================================
- Coverage     4.46%   4.40%   -0.07%     
==========================================
  Files          384     389       +5     
  Lines        54264   55021     +757     
  Branches       623     602      -21     
==========================================
  Hits          2425    2425              
- Misses       51839   52596     +757     
Flag Coverage Δ
unit 4.40% <0.00%> (-0.07%) ⬇️

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

Files with missing lines Coverage Δ
packages/mermaid/src/docs/.vitepress/config.ts 0.00% <0.00%> (ø)
...d/src/rendering-util/rendering-elements/markers.js 0.00% <0.00%> (ø)
...id/src/rendering-util/rendering-elements/shapes.ts 0.00% <0.00%> (ø)
packages/mermaid-layout-elk/src/render.ts 0.00% <0.00%> (ø)
packages/mermaid/src/diagrams/c4/c4Diagram.ts 0.00% <0.00%> (ø)
...aid/src/rendering-util/rendering-elements/edges.js 0.00% <0.00%> (ø)
packages/mermaid/src/diagrams/c4/c4Icons.ts 0.00% <0.00%> (ø)
...ages/mermaid/src/diagrams/c4/c4Renderer-unified.ts 0.00% <0.00%> (ø)
packages/mermaid/src/diagrams/c4/styles.js 0.00% <0.00%> (ø)
...endering-util/rendering-elements/shapes/octagon.ts 0.00% <0.00%> (ø)
... and 2 more

... and 1 file with indirect coverage changes

Copy link

argos-ci bot commented Feb 15, 2025

The latest updates on your projects. Learn more about Argos notifications ↗︎

Build Status Details Updated (UTC)
default (Inspect) ⚠️ Changes detected (Review) 473 added, 416 removed Feb 20, 2025, 6:15 PM

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.

Please add a changeset.
Not familiar with C4, so haven't reviewed in detail.

Comment on lines +129 to +255
`,
options
);
});

it(`${description}should render a simple C4 diagram with the direction BT`, () => {
imgSnapshotTest(
`
C4Context
accTitle: C4 context demo
accDescr: Many large C4 diagrams

direction BT
title System Context diagram for Internet Banking System

Enterprise_Boundary(b0, "BankBoundary0") {
Person(customerA, "Banking Customer A", "A customer of the bank, with personal bank accounts.")

System(SystemAA, "Internet Banking System", "Allows customers to view information about their bank accounts, and make payments.")

Enterprise_Boundary(b1, "BankBoundary") {
System_Ext(SystemC, "E-mail system", "The internal Microsoft Exchange e-mail system.")
}
}

BiRel(customerA, SystemAA, "Uses")
Rel(SystemAA, SystemC, "Sends e-mails", "SMTP")
Rel(SystemC, customerA, "Sends e-mails to")

UpdateElementStyle(customerA, $fontColor="red", $bgColor="grey", $borderColor="red")
UpdateRelStyle(customerA, SystemAA, $textColor="blue", $lineColor="blue", $offsetX="5")
UpdateRelStyle(SystemC, customerA, $textColor="red", $lineColor="red", $offsetX="-50", $offsetY="20")
`,
options
);
});

it(`${description}should render a simple C4 diagram with the direction LR`, () => {
imgSnapshotTest(
`
C4Context
accTitle: C4 context demo
accDescr: Many large C4 diagrams

direction LR
title System Context diagram for Internet Banking System

Enterprise_Boundary(b0, "BankBoundary0") {
Person(customerA, "Banking Customer A", "A customer of the bank, with personal bank accounts.")

System(SystemAA, "Internet Banking System", "Allows customers to view information about their bank accounts, and make payments.")

Enterprise_Boundary(b1, "BankBoundary") {
System_Ext(SystemC, "E-mail system", "The internal Microsoft Exchange e-mail system.")
}
}

BiRel(customerA, SystemAA, "Uses")
Rel(SystemAA, SystemC, "Sends e-mails", "SMTP")
Rel(SystemC, customerA, "Sends e-mails to")

UpdateElementStyle(customerA, $fontColor="red", $bgColor="grey", $borderColor="red")
UpdateRelStyle(customerA, SystemAA, $textColor="blue", $lineColor="blue", $offsetX="5")
UpdateRelStyle(SystemC, customerA, $textColor="red", $lineColor="red", $offsetX="-50", $offsetY="20")
`,
options
);
});

it(`${description}should render a simple C4 diagram with the direction RL`, () => {
imgSnapshotTest(
`
C4Context
accTitle: C4 context demo
accDescr: Many large C4 diagrams

direction RL
title System Context diagram for Internet Banking System

Enterprise_Boundary(b0, "BankBoundary0") {
Person(customerA, "Banking Customer A", "A customer of the bank, with personal bank accounts.")

System(SystemAA, "Internet Banking System", "Allows customers to view information about their bank accounts, and make payments.")

Enterprise_Boundary(b1, "BankBoundary") {
System_Ext(SystemC, "E-mail system", "The internal Microsoft Exchange e-mail system.")
}
}

BiRel(customerA, SystemAA, "Uses")
Rel(SystemAA, SystemC, "Sends e-mails", "SMTP")
Rel(SystemC, customerA, "Sends e-mails to")

UpdateElementStyle(customerA, $fontColor="red", $bgColor="grey", $borderColor="red")
UpdateRelStyle(customerA, SystemAA, $textColor="blue", $lineColor="blue", $offsetX="5")
UpdateRelStyle(SystemC, customerA, $textColor="red", $lineColor="red", $offsetX="-50", $offsetY="20")
`,
options
);
});
Copy link
Member

Choose a reason for hiding this comment

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

Write this in a loop?

}
}

//type, from, to, label, ?techn, ?descr, ?sprite, ?tags, $link
Copy link
Member

Choose a reason for hiding this comment

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

remove?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants