How to save and load Graph View groups

Published with Share Note 🔸 Part of Obsidian guides

Getting your current groups

Put this code into a note to retrieve your current group settings:

```dataviewjs
const settings = await app.internalPlugins.plugins.graph.loadData()
dv.paragraph('```json\n' + JSON.stringify(settings, null, 2) + '\n```')
```

It will show you something like this:

{
  "collapse-filter": true,
  "search": "",
  "showTags": false,
  "showAttachments": false,
  "hideUnresolved": false,
  "showOrphans": true,
  "collapse-color-groups": false,
  "colorGroups": [
    {
      "query": "tag:#is-tagged",
      "color": {
        "a": 1,
        "rgb": 4468453
      }
    },
    {
      "query": "-tag:#is-tagged",
      "color": {
        "a": 1,
        "rgb": 14684176
      }
    }
  ],
  "collapse-display": true,
  "showArrow": false,
  "textFadeMultiplier": 0,
  "nodeSizeMultiplier": 1,
  "lineSizeMultiplier": 1,
  "collapse-forces": true,
  "centerStrength": 0.518713248970312,
  "repelStrength": 10,
  "linkStrength": 1,
  "linkDistance": 250,
  "scale": 1,
  "close": false
}

Youi can change any of the settings here. For our demo, we’re just going to be updating the groups, which are in the colorGroups property.

Updating the groups

Take the code below and put it into a note. It will update all your graph groups to be the two groups specified, #this-is-the-new-query and #another-new-query.

```dataviewjs
const graph = app.internalPlugins.plugins.graph
const settings = await graph.loadData()
settings.colorGroups = [
  {
    "query": "#this-is-the-new-query",
    "color": {
      "a": 1,
      "rgb": 16185856
    }
  },
  {
    "query": "#another-new-query",
    "color": {
      "a": 1,
      "rgb": 16187906
    }
  }
]
await graph.saveData(settings)
await graph.disable()
await graph.enable()
```

If you follow the structure inside the settings.colorGroups section, you can update the groups as you wish.

To change between different groups profiles, you just need to launch the Javascript code above. You could:

  • Create a couple of different notes with the groups that you want, and just load one of the notes to change all your groups.
  • Put the code in a Templater script and launch it from a hotkey.
  • Or one of the many other plugins which let you run arbitrary Javascript in Obsidian.