> [!column|flex no-title] >> [!menu-dark-red|ttl-c] [[Obsidian TTRPG Tutorials]] / [[Plugin Tutorials]] / [[Dataview]] / [[DataviewJS - Dynamic Mermaid Relationship Chart]] > [!column|4 no-title] >> [!menu-green-1|ttl-c] [[Getting Started]] > >> [!menu-green-2|ttl-c] [[Plugin Tutorials]] > >> [!menu-green-3|ttl-c] [[Community Supported Games]] > >> [!menu-green-4|ttl-c] [[Obsidian TTRPG Tutorials/Templates/Templates\|Templates]] > [!column|3 no-title] >> [!patreon|ttl-c] [Patreon](https://www.patreon.com/JPlunkett) ([Starter Vault](https://www.patreon.com/posts/obsidian-patreon-96801399)) > >> [!discord|ttl-c] [Obsidian TTRPG Community Discord](https://discord.gg/CdM9UCJdwU) > >> [!discord|ttl-c] [Obsidian Official Discord](https://discord.gg/8AF29UBUCa) ## Dynamically Create Relationship Graph This example can be placed in your note to dynamically map a visual relationship chart. The nodes are clickable assuming you have a note of the same name. ![](https://youtu.be/Tqm-l0R5erc) ![[DataviewJS - Dynamic Mermaid Relationship Chart-20241116100826449.webp]] In this example the code would go inside of my NPC notes so that it dynamically maps their relationships. I would add this to my NPC template so that every NPC I create has one of these added when I create their note. ## Properties - Each note has the following [[Front Matter (YAML) and Tags|Frontmatter]]. Frontmatter is listed at the top of a note between the --- and ---. ```` --- parents: - Josh - Suzi children: - Bob - Fred - Karen enemies: - Zander allies: - Emyerson - Jessica - Madie siblings: - Brad partner: - Jane --- ```` This is an example of the Frontmatter from one of the journal notes. Note that it uses the List property type. ![[DataviewJS - Dynamic Mermaid Relationship Chart-20241116101058562.webp]] ## Plugins This code requires the following plugins: - [[Dataview]] with JavaScript Queries enabled in the Dataview settings. ## The Code In the body of the note you should paste this code. > [!important]+ Important > Always paste code with Ctrl + Shift + V! ```` ```dataviewjs var parents = dv.current().parents ?? []; var children = dv.current().children ?? []; var enemies = dv.current().enemies ?? []; var allies = dv.current().allies ?? []; var siblings = dv.current().siblings ?? []; var current = dv.current().file.name; var partner = dv.current().partner ?? []; dv.paragraph("```mermaid\nflowchart LR\n" + // Parents with internal-link on individual nodes only (parents.length > 0 ? parents.map((parent, index) => `P${index + 1}[${parent}]:::internal-link\nP${index + 1} --> Current\n`).join('') : '') + // Current node `Current[${current}]\n` + // Partner group node (no internal-link applied) (partner.length > 0 ? `PT[Partner]\nCurrent --> PT\n` : '') + // Individual partners with internal-link (partner.length > 0 ? partner.map((p, index) => `PT${index + 1}[${p}]:::internal-link\nPT --> PT${index + 1}\n`).join('') : '') + // Children group node (no internal-link applied) (children.length > 0 ? `C[Children]\nCurrent --> C\n${children.map((child, index) => `C${index + 1}[${child}]:::internal-link\nC --> C${index + 1}\n`).join('')}` : '') + // Siblings group node (no internal-link applied) (siblings.length > 0 ? `S[Siblings]\nCurrent --> S\n${siblings.map((sibling, index) => `S${index + 1}[${sibling}]:::internal-link\nS --> S${index + 1}\n`).join('')}` : '') + // Enemies group node (no internal-link applied) (enemies.length > 0 ? `E[Enemies]\nCurrent --> E\n${enemies.map((enemy, index) => `E${index + 1}[${enemy}]:::internal-link\nE --> E${index + 1}\n`).join('')}` : '') + // Allies group node (no internal-link applied) (allies.length > 0 ? `A[Allies]\nCurrent --> A\n${allies.map((ally, index) => `A${index + 1}[${ally}]:::internal-link\nA --> A${index + 1}\n`).join('')}` : '') + // Styling: Apply internal-link only to individual nodes, not group nodes `class ${parents.length > 0 ? parents.map((_, index) => `P${index + 1},`).join('') : ''}Current${children.length > 0 ? children.map((_, index) => `C${index + 1},`).join('') : ''}${siblings.length > 0 ? siblings.map((_, index) => `S${index + 1},`).join('') : ''}${enemies.length > 0 ? enemies.map((_, index) => `E${index + 1},`).join('') : ''}${allies.length > 0 ? allies.map((_, index) => `A${index + 1},`).join('') : ''} internal-link;` ) ``` ```` > [!NOTE] How Can I Change This For My Own Needs? > I did not write this code myself. I simply had a conversation with ChatGPT and the AI wrote it for me. It took about 4 hours to get it right going back and forth with the AI. > If you would like to change this code, you can try and do the same. ## How To Use Now you can simply add more names to the properties of the note and the relationship chart will update. The nodes will auto link to other notes if the name of the note matches the name used in the Properties. ![[Obsidian_L5mJsMSoGT.gif]]