Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graph): enable radial mode #1738

Merged
merged 4 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/features/graph view.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Component.Graph({
opacityScale: 1,
removeTags: [], // what tags to remove from the graph
showTags: true, // whether to show tags in the graph
enableRadial: true, // Whether to constrain the graph, similar to Obsidian
},
})
```
Expand Down
5 changes: 4 additions & 1 deletion quartz/components/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface D3Config {
removeTags: string[]
showTags: boolean
focusOnHover?: boolean
enableRadial?: boolean
}

interface GraphOptions {
Expand All @@ -39,6 +40,7 @@ const defaultOptions: GraphOptions = {
showTags: true,
removeTags: [],
focusOnHover: false,
enableRadial: false,
},
globalGraph: {
drag: true,
Expand All @@ -53,10 +55,11 @@ const defaultOptions: GraphOptions = {
showTags: true,
removeTags: [],
focusOnHover: true,
enableRadial: false,
},
}

export default ((opts?: GraphOptions) => {
export default ((opts?: Partial<GraphOptions>) => {
const Graph: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps) => {
const localGraph = { ...defaultOptions.localGraph, ...opts?.localGraph }
const globalGraph = { ...defaultOptions.globalGraph, ...opts?.globalGraph }
Expand Down
11 changes: 9 additions & 2 deletions quartz/components/scripts/graph.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
forceCenter,
forceLink,
forceCollide,
forceRadial,
zoomIdentity,
select,
drag,
Expand Down Expand Up @@ -87,6 +88,7 @@ async function renderGraph(container: string, fullSlug: FullSlug) {
removeTags,
showTags,
focusOnHover,
enableRadial,
} = JSON.parse(graph.dataset["cfg"]!) as D3Config

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

const width = graph.offsetWidth
const height = Math.max(graph.offsetHeight, 250)

// we virtualize the simulation and use pixi to actually render it
// Calculate the radius of the container circle
const radius = Math.min(width, height) / 2 - 40 // 40px padding
const simulation: Simulation<NodeData, LinkData> = forceSimulation<NodeData>(graphData.nodes)
.force("charge", forceManyBody().strength(-100 * repelForce))
.force("center", forceCenter().strength(centerForce))
.force("link", forceLink(graphData.links).distance(linkDistance))
.force("collide", forceCollide<NodeData>((n) => nodeRadius(n)).iterations(3))

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

// precompute style prop strings as pixi doesn't support css variables
const cssVars = [
Expand Down
Loading