demo_network_visjs/client/db_graph.tsx

35 lines
732 B
TypeScript
Raw Normal View History

2024-01-23 14:13:26 +01:00
import { Ref, useEffect, useRef } from 'preact/hooks'
import * as vis from 'vis-network'
import { DbEntry } from '../datas.ts'
export function DbGraph({ db }: { db: DbEntry[] }) {
const graph = useRef<HTMLDivElement>(null)
useEffect(() => {
drawDb({ db, graph })
}, [])
return <div class='graph' ref={graph}></div>
}
function drawDb({ db, graph }: { db: DbEntry[]; graph: Ref<HTMLDivElement> }) {
const nodes = db.map(({ id, title, links }) => ({
id,
label: title,
value: links.length,
}))
const edges = db.map(({ id, links }) =>
links.map((link) => ({ from: id, to: link, value: id + link }))
).flat()
return new vis.Network(graph.current!, {
nodes,
edges,
}, {
nodes: {
shape: 'dot',
},
})
}