summaryrefslogtreecommitdiff
path: root/src/index.js
blob: 6cc83e4ac7f7220c7a6d3f039f84bc84b1d3f05e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * No.6092 site for Charles Stankievech
 * - load db.json
 * - map the nodes to a graph
 * - click the nodes to open them
 * - some of the nodes are images, others are 3D
 */

import ForceGraph3D from "3d-force-graph";

async function main() {
  const db = await loadDB();

  const trees = {};
  const data = { nodes: [], links: [] };

  console.log(db);

  db.page.forEach((item, index) => {
    data.nodes.push({
      id: index,
    });
    for (let tagIndex = 0; tagIndex < 8; tagIndex += 1) {
      const value = item["tag_" + tagIndex];
      if (value) {
        if (value in trees) {
          data.links.push({
            source: choice(trees[value]),
            target: index,
          });
          // don't link to the root node more than once!
          if (trees[value][0]) {
            trees[value].push(index);
          } else {
            trees[value] = [index];
          }
        } else {
          trees[value] = [index];
        }
      }
    }
  });

  let graph = ForceGraph3D();
  graph(document.querySelector("#graph")).graphData(data);
}

const randint = (limit) => Math.floor(Math.random() * limit);
const choice = (list) => list[randint(list.length)];

async function loadDB() {
  const request = await fetch("/db.json");
  return await request.json();
}

main();