Skip to content

Commit c90d014

Browse files
committed
update ForceGraph
1 parent e1fd329 commit c90d014

File tree

12 files changed

+622
-22
lines changed

12 files changed

+622
-22
lines changed

app/ForceGraph.jsx

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"use client"
2+
3+
import { useEffect, useRef } from "react"
4+
import * as d3 from "d3"
5+
6+
export default function ForceGraph({ data }) {
7+
const svgRef = useRef(null)
8+
9+
useEffect(() => {
10+
if (!data) return
11+
12+
const width = 928
13+
const height = 600
14+
15+
// Remove old SVG content
16+
d3.select(svgRef.current).selectAll("*").remove()
17+
18+
// Build hierarchy
19+
const root = d3.hierarchy(data)
20+
const links = root.links()
21+
const nodes = root.descendants()
22+
23+
// Force simulation
24+
const simulation = d3
25+
.forceSimulation(nodes)
26+
.force(
27+
"link",
28+
d3
29+
.forceLink(links)
30+
.id((d) => d.id)
31+
.distance(0)
32+
.strength(1)
33+
)
34+
.force("charge", d3.forceManyBody().strength(-50))
35+
.force("x", d3.forceX())
36+
.force("y", d3.forceY())
37+
38+
// Create SVG
39+
const svg = d3
40+
.select(svgRef.current)
41+
.attr("width", width)
42+
.attr("height", height)
43+
.attr("viewBox", [-width / 2, -height / 2, width, height])
44+
.style("max-width", "100%")
45+
.style("height", "auto")
46+
47+
// Draw links
48+
const link = svg.append("g").attr("stroke", "#999").attr("stroke-opacity", 0.6).selectAll("line").data(links).join("line")
49+
50+
// Drag behavior
51+
function drag(simulation) {
52+
function dragstarted(event, d) {
53+
if (!event.active) simulation.alphaTarget(0.3).restart()
54+
d.fx = d.x
55+
d.fy = d.y
56+
}
57+
function dragged(event, d) {
58+
d.fx = event.x
59+
d.fy = event.y
60+
}
61+
function dragended(event, d) {
62+
if (!event.active) simulation.alphaTarget(0)
63+
d.fx = null
64+
d.fy = null
65+
}
66+
return d3.drag().on("start", dragstarted).on("drag", dragged).on("end", dragended)
67+
}
68+
69+
// Draw nodes
70+
const node = svg
71+
.append("g")
72+
.attr("fill", "#fff")
73+
.attr("stroke", "#000")
74+
.attr("stroke-width", 1.5)
75+
.selectAll("circle")
76+
.data(nodes)
77+
.join("circle")
78+
.attr("fill", (d) => (d.children ? null : "#000"))
79+
.attr("stroke", (d) => (d.children ? null : "#fff"))
80+
.attr("r", 3.5)
81+
.call(drag(simulation))
82+
83+
node.append("title").text((d) => d.data.name)
84+
85+
// Update positions on tick
86+
simulation.on("tick", () => {
87+
link
88+
.attr("x1", (d) => d.source.x)
89+
.attr("y1", (d) => d.source.y)
90+
.attr("x2", (d) => d.target.x)
91+
.attr("y2", (d) => d.target.y)
92+
93+
node.attr("cx", (d) => d.x).attr("cy", (d) => d.y)
94+
})
95+
96+
return () => simulation.stop()
97+
}, [data])
98+
99+
return <svg ref={svgRef}></svg>
100+
}

app/globals.css

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
@import "./fonts.css";
44
@import "./colors.css";
55

6+
@import "../styles/cards.css";
67
@import "../styles/subheading-anchor.css";
78
@import "../styles/scrollbar.css";
89
@import "../styles/steps.css";
910
@import "../styles/code-block.css";
11+
@import "../styles/image-zoom.css";
1012
@import "../styles/utilities.css";
1113

14+
@import "../styles/hamburger.css";
15+
@import "../styles/typesetting-article.css";
1216

13-
/* @import "../styles/cards.css"; */
14-
/* @import "../styles/hamburger.css"; */
15-
/* @import "../styles/typesetting-article.css"; */
1617
@custom-variant dark (&:where(.dark, .dark *));
1718
@custom-variant light (&:where(.light, .light *));
1819

app/homepage.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import EvaluationProcess from "@/components/EvaluationProcess"
33
import Image from "next/image"
44
import { Bleed } from "nextra/components"
55

6-
# Survey With Code
6+
{/* # Survey With Code */}
77

88
### What is Survey With Code?
99

app/page.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
import React from "react"
1+
"use client"
2+
3+
import React, { useEffect, useState } from "react"
24
import HomePage from "./homepage.mdx"
35
import Body from "@/components/body"
6+
import ForceGraph from "./ForceGraph"
47

58
export default function Page() {
9+
const [data, setData] = useState([])
10+
11+
useEffect(() => {
12+
fetch("/data/flare-2.json")
13+
.then((res) => res.json())
14+
.then((data) => setData(data))
15+
}, [])
16+
617
return (
718
<Body>
819
<main className="w-full min-w-0 px-2 pt-4">
920
<div className="grid grid-cols-3 mt-4">
1021
<div className="col-span-2 w-full pl-20 pr-8">
1122
<HomePage />
1223
</div>
13-
24+
<ForceGraph data={data} />
1425
</div>
1526
</main>
1627
</Body>

components/menuheader.jsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export default function MenuHeader() {
77
<div className="flex gap-4 overflow-x-auto nextra-scrollbar py-1.5 max-md:hidden">
88
<a
99
className="focus-visible:nextra-focus text-sm contrast-more:text-gray-700 contrast-more:dark:text-gray-100 whitespace-nowrap text-gray-600 hover:text-black dark:text-gray-400 dark:hover:text-gray-200 ring-inset transition-colors aria-[current]:font-medium aria-[current]:subpixel-antialiased aria-[current]:text-current"
10-
href="/docs"
10+
href="/taxonomy"
1111
>
12-
Documentation
12+
Taxonomy
1313
</a>
1414
{/* <a
1515
className="focus-visible:nextra-focus text-sm contrast-more:text-gray-700 contrast-more:dark:text-gray-100 whitespace-nowrap text-gray-600 hover:text-black dark:text-gray-400 dark:hover:text-gray-200 ring-inset transition-colors aria-[current]:font-medium aria-[current]:subpixel-antialiased aria-[current]:text-current"
@@ -33,27 +33,27 @@ export default function MenuHeader() {
3333
</button> */}
3434
<a
3535
className="focus-visible:nextra-focus text-sm contrast-more:text-gray-700 contrast-more:dark:text-gray-100 whitespace-nowrap text-gray-600 hover:text-black dark:text-gray-400 dark:hover:text-gray-200 ring-inset transition-colors aria-[current]:font-medium aria-[current]:subpixel-antialiased aria-[current]:text-current"
36-
href="/blog"
36+
href="/leaderboard"
3737
>
38-
Blog
38+
Leaderboard
3939
</a>
4040
<a
4141
className="focus-visible:nextra-focus text-sm contrast-more:text-gray-700 contrast-more:dark:text-gray-100 whitespace-nowrap text-gray-600 hover:text-black dark:text-gray-400 dark:hover:text-gray-200 ring-inset transition-colors aria-[current]:font-medium aria-[current]:subpixel-antialiased aria-[current]:text-current"
42-
href="/about"
42+
href="/dataset"
4343
>
44-
About
44+
Dataset
4545
</a>
4646
<a
4747
className="focus-visible:nextra-focus text-sm contrast-more:text-gray-700 contrast-more:dark:text-gray-100 whitespace-nowrap text-gray-600 hover:text-black dark:text-gray-400 dark:hover:text-gray-200 ring-inset transition-colors aria-[current]:font-medium aria-[current]:subpixel-antialiased aria-[current]:text-current"
48-
href="/showcase"
48+
href="/stages"
4949
>
50-
Showcase
50+
Stages
5151
</a>
5252
<a
5353
className="focus-visible:nextra-focus text-sm contrast-more:text-gray-700 contrast-more:dark:text-gray-100 whitespace-nowrap text-gray-600 hover:text-black dark:text-gray-400 dark:hover:text-gray-200 ring-inset transition-colors aria-[current]:font-medium aria-[current]:subpixel-antialiased aria-[current]:text-current"
54-
href="/sponsors"
54+
href="/contribute"
5555
>
56-
Sponsors
56+
Contribute
5757
</a>
5858
</div>
5959
)

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@types/mdx": "^2.0.13",
2020
"axios": "^1.9.0",
2121
"clsx": "^2.1.1",
22+
"d3": "^7.9.0",
2223
"estree-util-value-to-estree": "^3.3.3",
2324
"framer-motion": "^12.9.2",
2425
"github-slugger": "^2.0.0",
@@ -37,6 +38,7 @@
3738
"react": "^19",
3839
"react-dom": "^19",
3940
"react-dropzone": "^14.3.8",
41+
"react-medium-image-zoom": "^5.3.0",
4042
"rehype-katex": "^7.0.1",
4143
"rehype-pretty-code": "^0.14.1",
4244
"rehype-raw": "^7.0.0",

0 commit comments

Comments
 (0)