File size: 9,876 Bytes
826afd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Paper Universe 3D Viewer</title>
  <style>
    html, body { margin:0; padding:0; width:100%; height:100%; overflow:hidden; background:#07111f; color:#e2e8f0; }
    #app { position:fixed; inset:0; }
    #controls {
      position: fixed;
      top: 10px;
      left: 10px;
      z-index: 10;
      background: rgba(255,255,255,0.92);
      color:#0f172a;
      padding: 10px 12px;
      border-radius: 10px;
      font-family: sans-serif;
      display:flex;
      flex-wrap:wrap;
      gap:8px;
      align-items:center;
      max-width: calc(100vw - 24px);
      box-shadow: 0 18px 45px rgba(2, 8, 23, 0.20);
    }
    #status {
      min-width: 280px;
      color:#334155;
    }
    select, input {
      border: 1px solid #cbd5e1;
      border-radius: 6px;
      padding: 4px 6px;
      background: white;
    }
    .hint {
      color:#475569;
      font-size: 12px;
    }
  </style>
  <script src="https://unpkg.com/deck.gl@latest/dist.min.js"></script>
</head>
<body>
  <div id="controls">
    <label for="viewMode">View:</label>
    <select id="viewMode">
      <option value="papers">Papers</option>
      <option value="categories">Categories</option>
      <option value="papers+categories" selected>Papers + Categories</option>
      <option value="all">All</option>
    </select>
    <label for="paperLevel">Paper LOD:</label>
    <select id="paperLevel"></select>
    <label for="colorMode">Color:</label>
    <select id="colorMode">
      <option value="category" selected>Primary Category</option>
      <option value="year">Year</option>
      <option value="uniform">Uniform</option>
    </select>
    <label for="categoryFilter">Filter Category:</label>
    <select id="categoryFilter">
      <option value="">All Categories</option>
    </select>
    <span class="hint">Click a category anchor to filter papers.</span>
    <span id="status">loading…</span>
  </div>
  <div id="app"></div>
  <script>
    const {Deck, ScatterplotLayer, TextLayer, OrbitView, OrbitController} = deck;
    const assetRoot = './interactive/';
    const viewModeEl = document.getElementById('viewMode');
    const paperLevelEl = document.getElementById('paperLevel');
    const colorModeEl = document.getElementById('colorMode');
    const categoryFilterEl = document.getElementById('categoryFilter');
    const statusEl = document.getElementById('status');
    let deckgl = null;
    let manifest = null;
    let cached = {};
    let currentViewState = null;

    function hashColor(str) {
      let h = 0;
      for (let i = 0; i < str.length; i++) h = (h * 31 + str.charCodeAt(i)) | 0;
      const r = (Math.abs(h) % 160) + 60;
      const g = (Math.abs(h >> 8) % 160) + 60;
      const b = (Math.abs(h >> 16) % 160) + 60;
      return [r, g, b];
    }

    function yearColor(year) {
      const y = Number(year || 0);
      const base = ((y % 17) * 13) % 255;
      return [40 + (base % 180), 120 + ((base * 2) % 100), 255 - (base % 120)];
    }

    function paperColor(row) {
      if (colorModeEl.value === 'uniform') return [29, 78, 216];
      if (colorModeEl.value === 'year') return yearColor(row.year);
      return hashColor(row.primary_category || 'unknown');
    }

    function categoryColor(row) {
      return hashColor(row.category_id || 'category');
    }

    async function loadJson(path) {
      if (!cached[path]) {
        cached[path] = fetch(path).then(r => r.json());
      }
      return cached[path];
    }

    async function ensureManifest() {
      if (!manifest) {
        manifest = await loadJson(assetRoot + 'manifest.json');
        for (const level of manifest.paper_levels) {
          const opt = document.createElement('option');
          opt.value = level.path;
          opt.textContent = level.label;
          paperLevelEl.appendChild(opt);
        }
        const cats = await loadJson(assetRoot + manifest.categories.path);
        for (const row of cats) {
          const opt = document.createElement('option');
          opt.value = row.category_id;
          opt.textContent = `${row.category_id} (${row.paper_count.toLocaleString()})`;
          categoryFilterEl.appendChild(opt);
        }
      }
      return manifest;
    }

    function ensureDeck() {
      if (deckgl) return deckgl;
        deckgl = new Deck({
          parent: document.getElementById('app'),
          views: [new OrbitView({orbitAxis: 'Z'})],
          controller: new OrbitController(),
          initialViewState: {target: [0, 0, 0], rotationX: 25, rotationOrbit: 35, zoom: 0.2},
          getTooltip: ({object, layer}) => {
          if (!object) return null;
          if (layer && layer.id === 'papers') {
            return {
              html: `<b>${object.title || object.canonical_paper_id}</b><br/>${object.primary_category || 'unknown'}<br/>year: ${object.year || 'n/a'}`,
              style: {backgroundColor: 'rgba(15, 23, 42, 0.92)', color: '#f8fafc'}
            };
          }
          if (layer && layer.id === 'categories') {
            return {
              html: `<b>${object.category_id}</b><br/>papers: ${(object.paper_count || 0).toLocaleString()}`,
              style: {backgroundColor: 'rgba(15, 23, 42, 0.92)', color: '#f8fafc'}
            };
          }
          if (layer && layer.id === 'years') {
            return {
              html: `<b>${object.year}</b><br/>papers: ${(object.paper_count || 0).toLocaleString()}`,
              style: {backgroundColor: 'rgba(15, 23, 42, 0.92)', color: '#f8fafc'}
            };
          }
          return null;
        }
      });
      return deckgl;
    }

    function maybeResetView(rows) {
      if (currentViewState || !rows.length) return;
      const xs = rows.map(r => r.x);
      const ys = rows.map(r => r.y);
      const zs = rows.map(r => r.z);
      const center = [
        (Math.min(...xs) + Math.max(...xs)) / 2,
        (Math.min(...ys) + Math.max(...ys)) / 2,
        (Math.min(...zs) + Math.max(...zs)) / 2
      ];
      currentViewState = {target: center, rotationX: 25, rotationOrbit: 35, zoom: 0.2};
      ensureDeck().setProps({initialViewState: currentViewState});
    }

    async function render() {
      const man = await ensureManifest();
      const paperPath = assetRoot + paperLevelEl.value;
      const papersRaw = await loadJson(paperPath);
      const categories = await loadJson(assetRoot + man.categories.path);
      const years = await loadJson(assetRoot + man.years.path);
      const activeView = viewModeEl.value;
      const categoryFilter = categoryFilterEl.value;
      const papers = categoryFilter ? papersRaw.filter(r => r.primary_category === categoryFilter) : papersRaw;
      maybeResetView(papers.length ? papers : categories);

      const showPapers = activeView === 'papers' || activeView === 'papers+categories' || activeView === 'all';
      const showCategories = activeView === 'categories' || activeView === 'papers+categories' || activeView === 'all';
      const showYears = activeView === 'all';

      const layers = [];
      if (showPapers) {
        layers.push(new ScatterplotLayer({
          id: 'papers',
          data: papers,
          pickable: true,
          opacity: 0.28,
          radiusUnits: 'pixels',
          getPosition: d => [d.x, d.y, d.z],
          getRadius: _ => 1.1,
          getFillColor: d => [...paperColor(d), 190],
          radiusMinPixels: 1,
          radiusMaxPixels: 3
        }));
      }
      if (showCategories) {
        layers.push(new ScatterplotLayer({
          id: 'categories',
          data: categories,
          pickable: true,
          opacity: 0.95,
          radiusUnits: 'pixels',
          getPosition: d => [d.x, d.y, d.z],
          getRadius: d => Math.max(6, Math.min(18, 6 + Math.log10((d.paper_count || 1) + 1) * 3)),
          getFillColor: d => [...categoryColor(d), 235],
          onClick: info => {
            if (info.object) {
              categoryFilterEl.value = info.object.category_id || '';
              render();
            }
          }
        }));
        layers.push(new TextLayer({
          id: 'category-labels',
          data: categories.slice(0, 48),
          pickable: false,
          getPosition: d => [d.x, d.y, d.z],
          getText: d => d.category_id,
          getColor: _ => [15, 23, 42, 255],
          getSize: _ => 14,
          sizeUnits: 'pixels',
          getTextAnchor: _ => 'start',
          getAlignmentBaseline: _ => 'center'
        }));
      }
      if (showYears) {
        layers.push(new ScatterplotLayer({
          id: 'years',
          data: years,
          pickable: true,
          opacity: 0.95,
          radiusUnits: 'pixels',
          getPosition: d => [d.x, d.y, d.z],
          getRadius: _ => 8,
          getFillColor: d => [...yearColor(d.year), 240]
        }));
        layers.push(new TextLayer({
          id: 'year-labels',
          data: years,
          pickable: false,
          getPosition: d => [d.x, d.y, d.z],
          getText: d => String(d.year),
          getColor: _ => [6, 78, 59, 255],
          getSize: _ => 14,
          sizeUnits: 'pixels',
          getTextAnchor: _ => 'middle',
          getAlignmentBaseline: _ => 'center'
        }));
      }

      ensureDeck().setProps({
        layers,
        onViewStateChange: ({viewState}) => {
          currentViewState = viewState;
        }
      });
      statusEl.textContent = `papers shown: ${papers.length.toLocaleString()} | categories: ${categories.length.toLocaleString()} | years: ${years.length.toLocaleString()}`;
    }

    viewModeEl.addEventListener('change', render);
    paperLevelEl.addEventListener('change', render);
    colorModeEl.addEventListener('change', render);
    categoryFilterEl.addEventListener('change', render);
    ensureManifest().then(() => render());
  </script>
</body>
</html>