Skip to content

Commit fbc4554

Browse files
authored
feat(graph): enable radial mode (#1738)
1 parent 7be4774 commit fbc4554

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

docs/features/graph view.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Component.Graph({
3636
opacityScale: 1, // how quickly do we fade out the labels when zooming out?
3737
removeTags: [], // what tags to remove from the graph
3838
showTags: true, // whether to show tags in the graph
39+
enableRadial: false, // whether to constrain the graph, similar to Obsidian
3940
},
4041
globalGraph: {
4142
drag: true,
@@ -49,6 +50,7 @@ Component.Graph({
4950
opacityScale: 1,
5051
removeTags: [], // what tags to remove from the graph
5152
showTags: true, // whether to show tags in the graph
53+
enableRadial: true, // whether to constrain the graph, similar to Obsidian
5254
},
5355
})
5456
```

quartz/components/Graph.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface D3Config {
1818
removeTags: string[]
1919
showTags: boolean
2020
focusOnHover?: boolean
21+
enableRadial?: boolean
2122
}
2223

2324
interface GraphOptions {
@@ -39,6 +40,7 @@ const defaultOptions: GraphOptions = {
3940
showTags: true,
4041
removeTags: [],
4142
focusOnHover: false,
43+
enableRadial: false,
4244
},
4345
globalGraph: {
4446
drag: true,
@@ -53,10 +55,11 @@ const defaultOptions: GraphOptions = {
5355
showTags: true,
5456
removeTags: [],
5557
focusOnHover: true,
58+
enableRadial: true,
5659
},
5760
}
5861

59-
export default ((opts?: GraphOptions) => {
62+
export default ((opts?: Partial<GraphOptions>) => {
6063
const Graph: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps) => {
6164
const localGraph = { ...defaultOptions.localGraph, ...opts?.localGraph }
6265
const globalGraph = { ...defaultOptions.globalGraph, ...opts?.globalGraph }

quartz/components/scripts/graph.inline.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
forceCenter,
99
forceLink,
1010
forceCollide,
11+
forceRadial,
1112
zoomIdentity,
1213
select,
1314
drag,
@@ -87,6 +88,7 @@ async function renderGraph(container: string, fullSlug: FullSlug) {
8788
removeTags,
8889
showTags,
8990
focusOnHover,
91+
enableRadial,
9092
} = JSON.parse(graph.dataset["cfg"]!) as D3Config
9193

9294
const data: Map<SimpleSlug, ContentDetails> = new Map(
@@ -161,15 +163,20 @@ async function renderGraph(container: string, fullSlug: FullSlug) {
161163
})),
162164
}
163165

166+
const width = graph.offsetWidth
167+
const height = Math.max(graph.offsetHeight, 250)
168+
164169
// we virtualize the simulation and use pixi to actually render it
170+
// Calculate the radius of the container circle
171+
const radius = Math.min(width, height) / 2 - 40 // 40px padding
165172
const simulation: Simulation<NodeData, LinkData> = forceSimulation<NodeData>(graphData.nodes)
166173
.force("charge", forceManyBody().strength(-100 * repelForce))
167174
.force("center", forceCenter().strength(centerForce))
168175
.force("link", forceLink(graphData.links).distance(linkDistance))
169176
.force("collide", forceCollide<NodeData>((n) => nodeRadius(n)).iterations(3))
170177

171-
const width = graph.offsetWidth
172-
const height = Math.max(graph.offsetHeight, 250)
178+
if (enableRadial)
179+
simulation.force("radial", forceRadial(radius * 0.8, width / 2, height / 2).strength(0.3))
173180

174181
// precompute style prop strings as pixi doesn't support css variables
175182
const cssVars = [

0 commit comments

Comments
 (0)