collab/mermaid/docs/community/new-diagram.mdblame
View source
6dd74de1> **Warning**
6dd74de2>
6dd74de3> ## THIS IS AN AUTOGENERATED FILE. DO NOT EDIT.
6dd74de4>
6dd74de5> ## Please edit the corresponding file in [/packages/mermaid/src/docs/community/new-diagram.md](../../packages/mermaid/src/docs/community/new-diagram.md).
6dd74de6
6dd74de7# Adding a New Diagram/Chart 📊
6dd74de8
6dd74de9### Examples
6dd74de10
6dd74de11Please refer to the following PRs on how to use Langium to add a new diagram grammar.
6dd74de12
6dd74de13- <https://github.com/mermaid-js/mermaid/pull/4839>
6dd74de14- <https://github.com/mermaid-js/mermaid/pull/4751>
6dd74de15
6dd74de16> **Warning**
6dd74de17> The below steps are a work in progress and will be updated soon.
6dd74de18
6dd74de19### Step 1: Grammar & Parsing
6dd74de20
6dd74de21### Step 2: Rendering
6dd74de22
6dd74de23Write a renderer that given the data found during parsing renders the diagram. To look at an example look at sequenceRenderer.js rather than the flowchart renderer as this is a more generic example.
6dd74de24
6dd74de25Place the renderer in the diagram folder.
6dd74de26
6dd74de27### Step 3: Detection of the new diagram type
6dd74de28
6dd74de29The second thing to do is to add the capability to detect the new diagram to type to the detectType in `diagram-api/detectType.ts`. The detection should return a key for the new diagram type.
6dd74de30[This key will be used to as the aria roledescription](#aria-roledescription), so it should be a word that clearly describes the diagram type.
6dd74de31For example, if your new diagram uses a UML deployment diagram, a good key would be "UMLDeploymentDiagram" because assistive technologies such as a screen reader
6dd74de32would voice that as "U-M-L Deployment diagram." Another good key would be "deploymentDiagram" because that would be voiced as "Deployment Diagram." A bad key would be "deployment" because that would not sufficiently describe the diagram.
6dd74de33
6dd74de34Note that the diagram type key does not have to be the same as the diagram keyword chosen for the [grammar](#grammar), but it is helpful if they are the same.
6dd74de35
6dd74de36### Common parts of a diagram
6dd74de37
6dd74de38There are a few features that are common between the different types of diagrams. We try to standardize the diagrams that work as similar as possible for the end user. The commonalities are:
6dd74de39
6dd74de40- Directives, a way of modifying the diagram configuration from within the diagram code.
6dd74de41- Accessibility, a way for an author to provide additional information like titles and descriptions to people accessing a text with diagrams using a screen reader.
6dd74de42- Themes, there is a common way to modify the styling of diagrams in Mermaid.
6dd74de43- Comments should follow mermaid standards
6dd74de44
6dd74de45Here are some pointers on how to handle these different areas.
6dd74de46
6dd74de47## Accessibility
6dd74de48
6dd74de49Mermaid automatically adds the following accessibility information for the diagram SVG HTML element:
6dd74de50
6dd74de51- aria-roledescription
6dd74de52- accessible title
6dd74de53- accessible description
6dd74de54
6dd74de55### aria-roledescription
6dd74de56
6dd74de57The aria-roledescription is automatically set to [the diagram type](#step-3--detection-of-the-new-diagram-type) and inserted into the SVG element.
6dd74de58
6dd74de59See [the definition of aria-roledescription](https://www.w3.org/TR/wai-aria-1.1/#aria-roledescription) in [the Accessible Rich Internet Applications W3 standard.](https://www.w3.org/WAI/standards-guidelines/aria/)
6dd74de60
6dd74de61### accessible title and description
6dd74de62
6dd74de63The syntax for accessible titles and descriptions is described in [the Accessibility documentation section.](../config/accessibility.md)
6dd74de64
6dd74de65The functions for setting title and description are provided by a common module. This is the import in flowDb.js:
6dd74de66
6dd74de67```
6dd74de68import {
6dd74de69 setAccTitle,
6dd74de70 getAccTitle,
6dd74de71 getAccDescription,
6dd74de72 setAccDescription,
6dd74de73 clear as commonClear,
6dd74de74} from '../../commonDb';
6dd74de75```
6dd74de76
6dd74de77The accessibility title and description are inserted into the SVG element in the `render` function in mermaidAPI.
6dd74de78
6dd74de79## Theming
6dd74de80
6dd74de81Mermaid supports themes and has an integrated theming engine. You can read more about how the themes can be used [in the docs](../config/theming.md).
6dd74de82
6dd74de83When adding themes to a diagram it comes down to a few important locations in the code.
6dd74de84
6dd74de85The entry point for the styling engine is in **src/styles.js**. The getStyles function will be called by Mermaid when the styles are being applied to the diagram.
6dd74de86
6dd74de87This function will in turn call a function _your diagram should provide_ returning the css for the new diagram. The diagram specific, also which is commonly also called getStyles and located in the folder for your diagram under src/diagrams and should be named styles.js. The getStyles function will be called with the theme options as an argument like in the following example:
6dd74de88
6dd74de89```js
6dd74de90const getStyles = (options) =>
6dd74de91 `
6dd74de92 .line {
6dd74de93 stroke-width: 1;
6dd74de94 stroke: ${options.lineColor};
6dd74de95 stroke-dasharray: 2;
6dd74de96 }
6dd74de97 // ...
6dd74de98 `;
6dd74de99```
6dd74de100
6dd74de101Note that you need to provide your function to the main getStyles by adding it into the themes object in **src/styles.js** like in the xyzDiagram in the provided example:
6dd74de102
6dd74de103```js
6dd74de104const themes = {
6dd74de105 flowchart,
6dd74de106 'flowchart-v2': flowchart,
6dd74de107 sequence,
6dd74de108 xyzDiagram,
6dd74de109 //...
6dd74de110};
6dd74de111```
6dd74de112
6dd74de113The actual options and values for the colors are defined in **src/theme/theme-\[xyz].js**. If you provide the options your diagram needs in the existing theme files then the theming will work smoothly without hiccups.
6dd74de114
6dd74de115## Examples
6dd74de116
6dd74de117The `@mermaid-js/examples` package contains a collection of examples that are used by tools like mermaid.live to help users get started with the new diagram.
6dd74de118
6dd74de119You can duplicate an existing diagram example file, eg: `packages/examples/src/examples/flowchart.ts`, and modify it with details specific to your diagram.
6dd74de120
6dd74de121Then you can import the example in the `packages/examples/src/index.ts` file and add it to the `examples` array.
6dd74de122
6dd74de123Each diagram should have at least one example, and that should be marked as default. It is good to add more examples to showcase different features of the diagram.