Skip to content

Commit

Permalink
Style graph nodes by type (#449)
Browse files Browse the repository at this point in the history
* added styling of graph nodes by type

* removed unused css file

* added style by type to documentation
  • Loading branch information
riccardoferretti authored Jan 14, 2021
1 parent cd92468 commit e0bcb6b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 44 deletions.
Binary file added docs/assets/images/style-node-by-type.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 29 additions & 1 deletion docs/features/graph-visualisation.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,41 @@ A sample configuration object is provided below:
"node": {
"note": "#277da1",
"placeholder": "#545454",
"unknown": "#f94144"
}
```

### Style nodes by type
It is possible to customize the style of a node based on the `type` property in the YAML frontmatter of the corresponding document.

For example the following `backlinking.md` note:

```
---
type: feature
---
# Backlinking
...
```

And the following `settings.json`:
```json
"foam.graph.style": {
"node": {
"feature": "red",
}
}
```

Will result in the following graph:

![Style node by type](../assets/images/style-node-by-type.png)

### Markdown Links
Another extension that provides a great graph visualisation is [Markdown Links](https://marketplace.visualstudio.com/items?itemName=tchayen.markdown-links).
The extension doesn't use the Foam model, so discrepancies might arise, but it's a great visualisation extension nonetheless!

- Use the `Markdown Links: Show Graph` command to see the graph
![Demo of graph visualiser](../assets/images/foam-navigation-demo.gif)


2 changes: 1 addition & 1 deletion packages/foam-vscode/src/features/dataviz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function generateGraphData(foam: Foam) {
const links = foam.notes.getForwardLinks(n.uri);
graph.nodes[n.uri.path] = {
id: n.uri.path,
type: 'note',
type: n.properties.type ?? 'note',
uri: n.uri,
title: cutTitle(n.title),
};
Expand Down
Empty file.
63 changes: 21 additions & 42 deletions packages/foam-vscode/static/graphs/default/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const styleFallback = {
node: {
note: '#277da1',
placeholder: '#545454',
unknown: '#f94144',
},
};

Expand All @@ -28,6 +27,21 @@ const labelAlpha = d3
.range([0, 1])
.clamp(true);

const defaultStyle = {
background: getStyle(`--vscode-panel-background`) ?? styleFallback.background,
fontSize:
parseInt(getStyle(`--vscode-font-size`) ?? styleFallback.fontSize) - 2,
highlightedForeground:
getStyle('--vscode-list-highlightForeground') ??
styleFallback.highlightedForeground,
node: {
note: getStyle('--vscode-editor-foreground') ?? styleFallback.node.note,
placeholder:
getStyle('--vscode-list-deemphasizedForeground') ??
styleFallback.node.placeholder,
},
};

let model = {
selectedNodes: new Set(),
hoverNode: null,
Expand All @@ -42,23 +56,7 @@ let model = {
* It tries to be set using VSCode values,
* in the case it fails, use the fallback style values.
*/
style: {
background:
getStyle(`--vscode-panel-background`) ?? styleFallback.background,
fontSize:
parseInt(getStyle(`--vscode-font-size`) ?? styleFallback.fontSize) - 2,
highlightedForeground:
getStyle('--vscode-list-highlightForeground') ??
styleFallback.highlightedForeground,
node: {
note: getStyle('--vscode-editor-foreground') ?? styleFallback.node.note,
placeholder:
getStyle('--vscode-list-deemphasizedForeground') ??
styleFallback.node.placeholder,
unknown:
getStyle('--vscode-editor-foreground') ?? styleFallback.node.unknown,
},
},
style: defaultStyle,
};
const graph = ForceGraph();

Expand Down Expand Up @@ -142,30 +140,11 @@ const Actions = {
return;
}
model.style = {
background:
newStyle.background ??
getStyle(`--vscode-panel-background`) ??
styleFallback.background,
fontSize:
newStyle.fontSize ??
parseInt(getStyle(`--vscode-font-size`) ?? styleFallback.fontSize) - 2,
highlightedForeground:
newStyle.highlightedForeground ??
getStyle('--vscode-list-highlightForeground') ??
styleFallback.highlightedForeground,
...defaultStyle,
...newStyle,
node: {
note:
newStyle.node?.note ??
getStyle('--vscode-editor-foreground') ??
styleFallback.node.note,
placeholder:
newStyle.node?.placeholder ??
getStyle('--vscode-list-deemphasizedForeground') ??
styleFallback.node.placeholder,
unknown:
newStyle.node?.unknown ??
getStyle('--vscode-editor-foreground') ??
styleFallback.node.unknow,
...defaultStyle.node,
...newStyle.node,
},
};
graph.backgroundColor(model.style.background);
Expand Down Expand Up @@ -244,7 +223,7 @@ function augmentGraphInfo(data) {
function getNodeColor(nodeId, model) {
const info = model.nodeInfo[nodeId];
const style = model.style;
const typeFill = style.node[info.type || 'unknown'];
const typeFill = style.node[info.type ?? 'note'] ?? style.node['note'];
switch (getNodeState(nodeId, model)) {
case 'regular':
return { fill: typeFill, border: typeFill };
Expand Down

0 comments on commit e0bcb6b

Please sign in to comment.