-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphVisualizer.js
More file actions
624 lines (548 loc) · 23.5 KB
/
Copy pathgraphVisualizer.js
File metadata and controls
624 lines (548 loc) · 23.5 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
import Graph from 'graphology';
import forceAtlas2 from "graphology-layout-forceatlas2";
import Sigma from "sigma";
import Papa from "papaparse";
import circular from "graphology-layout/circular";
import { degreeCentrality } from 'graphology-metrics/centrality/degree';
import eigenvectorCentrality from 'graphology-metrics/centrality/eigenvector';
import louvain from 'graphology-communities-louvain';
import { NodeBorderProgram } from "@sigma/node-border";
import seedrandom from 'seedrandom';
// 시드 고정
const rng = seedrandom('2024');
// CSV 데이터를 저장할 전역 변수
let csvData = [];
const yearFiles = {
'edges_2019': '/data/edges_btn_udt_2019_final.csv',
'edges_2020': '/data/edges_btn_udt_2020_final.csv',
'edges_2021': '/data/edges_btn_udt_2021_final.csv',
'edges_2022': '/data/edges_btn_udt_2022_final.csv',
'edges_2023': '/data/edges_btn_udt_2023_final.csv',
'edges_5yrs': '/data/edges_btn_udt_5yrs.csv'
};
const platinumFiles = {
'edges_2019': '/data/edges_btn_udt_2019_platinum.csv',
'edges_2020': '/data/edges_btn_udt_2020_platinum.csv',
'edges_2021': '/data/edges_btn_udt_2021_platinum.csv',
'edges_2022': '/data/edges_btn_udt_2022_platinum.csv',
'edges_2023': '/data/edges_btn_udt_2023_platinum.csv',
'edges_5yrs': '/data/edges_btn_udt_platinum_5yrs.csv'
};
const titleFiles = {
'edges_2019': '/data/title_to_tag_2019.csv',
'edges_2020': '/data/title_to_tag_2020.csv',
'edges_2021': '/data/title_to_tag_2021.csv',
'edges_2022': '/data/title_to_tag_2022.csv',
'edges_2023': '/data/title_to_tag_2023.csv',
};
const jaccardFiles = {
'jaccard_2019': '/data/jaccard_2019.csv',
'jaccard_2020': '/data/jaccard_2020.csv',
'jaccard_2021': '/data/jaccard_2021.csv',
'jaccard_2022': '/data/jaccard_2022.csv',
'jaccard_2023': '/data/jaccard_2023.csv',
};
document.addEventListener('DOMContentLoaded', function() {
console.log("CSV 파일 로드 시작");
// 초기 로드
loadMultipleCSVs([yearFiles['edges_2019']], 'chart-container1');
// 이벤트 리스너 추가
document.querySelectorAll('.checkbox-wrapper input[type="checkbox"]').forEach(checkbox => {
checkbox.addEventListener('change', handleOptionChange);
});
// 플래티넘 티어 체크박스 리스너 추가
document.querySelectorAll('.checkbox-container label input[type="checkbox"]').forEach(checkbox => {
if (checkbox.id.startsWith('platinum_')) {
checkbox.addEventListener('change', handlePlatinumChange);
}
});
// 범례 표시/숨기기 이벤트 리스너 추가
// document.getElementById('game-title-on').addEventListener('change', () => toggleLegend('tier-legend'));
document.getElementById('centrality-checkbox1').addEventListener('change', () => toggleCentralityTable('chart-container1'));
document.getElementById('community-checkbox1').addEventListener('change', () => toggleCommunityTable('chart-container1'));
document.getElementById('centrality-checkbox2').addEventListener('change', () => toggleCentralityTable('chart-container2'));
document.getElementById('community-checkbox2').addEventListener('change', () => toggleCommunityTable('chart-container2'));
// Chart switching
document.getElementById('button1').addEventListener('click', () => showChart('chart-container1'));
document.getElementById('button2').addEventListener('click', () => showChart('chart-container2'));
});
function toggleLegend(legendId) {
const legend = document.getElementById(legendId);
if (document.getElementById('game-title-on').checked) {
legend.classList.remove('hidden');
} else {
legend.classList.add('hidden');
}
}
function toggleCentralityTable(containerId) {
const centralityTable = document.getElementById('centrality-table');
const checkboxId = `centrality-checkbox${containerId === 'chart-container1' ? '1' : '2'}`;
if (document.getElementById(checkboxId).checked) {
computeCentrality(containerId);
centralityTable.classList.remove('hidden');
} else {
centralityTable.classList.add('hidden');
}
}
function toggleCommunityTable(containerId) {
const communityTable = document.getElementById('community-table');
const checkboxId = `community-checkbox${containerId === 'chart-container1' ? '1' : '2'}`;
if (document.getElementById(checkboxId).checked) {
communityAssign(containerId);
communityTable.classList.remove('hidden');
} else {
communityTable.classList.add('hidden');
}
}
function handleOptionChange(event) {
const isYearCheckbox = event.target.id.startsWith('edges_') || event.target.id.startsWith('jaccard_');
const isPlatinumCheckbox = event.target.id.startsWith('platinum_');
if (isYearCheckbox || isPlatinumCheckbox) {
// 모든 연도별 데이터와 플래티넘 티어 체크박스 해제
document.querySelectorAll('.checkbox-wrapper input[type="checkbox"]').forEach(checkbox => {
if (checkbox.id.startsWith('edges_') || checkbox.id.startsWith('platinum_') || checkbox.id.startsWith('jaccard_')) {
checkbox.checked = false;
}
});
// 현재 클릭된 체크박스만 체크
event.target.checked = true;
const selectedFiles = [];
const selectedTitleFiles = [];
const containerId = event.target.closest('.chart-container').id;
const suffix = containerId === 'chart-container1' ? 'edges' : 'jaccard';
// const isGameTitleOn = document.getElementById('game-title-on').checked;
if (isYearCheckbox) {
if (suffix === 'edges') {
for (const year in yearFiles) {
if (document.getElementById(`${year}`).checked) {
selectedFiles.push(yearFiles[year]);
// if (isGameTitleOn) {
// selectedTitleFiles.push(titleFiles[year]);
// }
}
}
} else if (suffix === 'jaccard') {
for (const year in jaccardFiles) {
if (document.getElementById(`${year}`).checked) {
selectedFiles.push(jaccardFiles[year]);
}
}
}
} else if (isPlatinumCheckbox) {
const platinumId = event.target.id.replace('platinum_', 'edges_');
selectedFiles.push(platinumFiles[platinumId]);
}
console.log(`CSV 파일 로드 시작: ${selectedFiles.concat(selectedTitleFiles).join(', ')}`);
loadMultipleCSVs(selectedFiles.concat(selectedTitleFiles), containerId);
}
}
function handlePlatinumChange(event) {
const platinumId = event.target.id.replace('platinum_', 'edges_');
document.querySelectorAll('.checkbox-wrapper input[type="checkbox"]').forEach(checkbox => {
if (checkbox.id.startsWith('edges_') || checkbox.id.startsWith('platinum_')) {
checkbox.checked = false;
}
});
event.target.checked = true;
loadMultipleCSVs([platinumFiles[platinumId]], 'chart-container1');
}
function loadMultipleCSVs(csvPaths, containerId) {
csvData = [];
Promise.all(csvPaths.map(path => fetch(path).then(response => response.arrayBuffer().then(buffer => ({ buffer, path }))))) // Return both buffer and path
.then(results => {
results.forEach(({ buffer, path }) => {
const encoding = detectEncoding(buffer);
const text = decodeText(buffer, encoding);
parseCSV(text, path.includes('title_to_tag'));
});
drawGraph(containerId);
const suffix = containerId === 'chart-container1' ? '1' : '2';
// 중심성 체크박스가 체크된 경우 중심성 계산 및 업데이트
if (document.getElementById(`centrality-checkbox${suffix}`).checked) {
computeCentrality(containerId);
}
if (document.getElementById(`community-checkbox${suffix}`).checked) {
communityAssign(containerId);
document.getElementById('community-table').classList.remove('hidden');
} else {
document.getElementById('community-table').classList.add('hidden');
}
})
.catch(error => {
console.error('Error loading CSV files:', error);
});
}
function detectEncoding(buffer) {
// 임시로 euc-kr를 반환 (필요한 경우 다른 인코딩 감지 로직 추가 가능)
return 'latin1';
}
function decodeText(arrayBuffer, encoding) {
const decoder = new TextDecoder(encoding);
return decoder.decode(new Uint8Array(arrayBuffer));
}
function parseCSV(text, isTitleFile) {
Papa.parse(text, {
header: true,
skipEmptyLines: true,
complete: function(results) {
results.data.forEach(row => {
row.isTitleFile = isTitleFile;
});
csvData = csvData.concat(results.data);
console.log("CSV 파일 로드 완료");
},
error: function(err) {
console.error("CSV 파일 로드 오류:", err);
}
});
}
let graphs = {};
let renderers = {};
let containers = {};
// Define color mapping for tiers
const tierColors = {
'Platinum': '#E5E4E2',
'Gold': '#FFD700',
'Silver': '#C0C0C0',
'Bronze': '#CD7F32'
};
const tierSizes = {
'Platinum': 15,
'Gold': 12,
'Silver': 10,
'Bronze': 8
};
function drawGraph(containerId) {
if (csvData && csvData.length > 0) {
const graph = new Graph();
graphs[containerId] = graph;
// Find the maximum weight
let maxWeight = 0;
csvData.forEach((row) => {
if (row.Weight > maxWeight) {
maxWeight = row.Weight;
}
});
// Normalize weights and add nodes/edges to the graph
const nodeSet = new Set();
csvData.forEach((row) => {
const { Source1, Source2, Weight, Tier, isTitleFile } = row;
// Check if Source1 and Source2 are defined
if (!Source1 || !Source2) {
console.warn('Source1 또는 Source2가 정의되지 않음:', row);
return;
}
const normalizedWeight = Weight / maxWeight; // Scale the weight
const sourceNode = Source1.trim();
const targetNode = Source2.trim();
nodeSet.add(sourceNode);
nodeSet.add(targetNode);
if (!graph.hasNode(sourceNode)) {
const color = isTitleFile ? (tierColors[Tier] || 'blue') : 'gray';
const size = isTitleFile ? (tierSizes[Tier] || 10) : 5;
graph.addNode(sourceNode, {
label: sourceNode,
color: color,
size: size,
isTitleFile: isTitleFile,
borderColor: isTitleFile ? 'red' : 'black', // Set border color based on isTitleFile
});
}
if (!graph.hasNode(targetNode)) {
graph.addNode(targetNode, {
label: targetNode,
color: 'black',
isTitleFile: isTitleFile,
borderColor: 'black',
});
}
if (!graph.hasEdge(sourceNode, targetNode)) {
graph.addEdge(sourceNode, targetNode, { size: normalizedWeight * 2 });
}
});
// Circle layout
circular.assign(graph);
// Degree-based node size adjustment
const degrees = graph.nodes().map((node) => graph.degree(node));
const minDegree = Math.min(...degrees);
const maxDegree = Math.max(...degrees);
const minSize = 3,
maxSize = 15;
graph.forEachNode((node) => {
const degree = graph.degree(node);
if (!graph.getNodeAttribute(node, 'size')) { // Only adjust size if not set by tier
graph.setNodeAttribute(
node,
'size',
minSize + ((degree - minDegree) / (maxDegree - minDegree)) * (maxSize - minSize)
);
}
});
// Run Force Atlas 2 for a fixed number of iterations
forceAtlas2.assign(graph, { iterations: 1000 });
// Clear previous graph if exists
if (renderers[containerId]) {
renderers[containerId].kill();
renderers[containerId] = null;
}
// Sigma.js settings to enlarge node labels and include node borders
const container = document.getElementById(containerId.replace('chart-container', 'sigma-container'));
container.innerHTML = '';
const renderer = new Sigma(graph, container, {
nodePrograms: {
border: NodeBorderProgram,
},
});
renderers[containerId] = renderer;
// State to manage hovered and selected nodes
const state = {
hoveredNode: undefined,
hoveredNeighbors: new Set(),
};
// Add event listeners for highlighting
renderer.on('enterNode', ({ node }) => {
setHoveredNode(node);
});
renderer.on('leaveNode', () => {
setHoveredNode(undefined);
});
function setHoveredNode(node) {
if (node) {
state.hoveredNode = node;
state.hoveredNeighbors = new Set(graph.neighbors(node));
} else {
state.hoveredNode = undefined;
state.hoveredNeighbors = new Set();
}
refreshGraph();
}
function refreshGraph() {
renderer.setSetting('nodeReducer', (node, data) => {
const res = { ...data };
if (state.hoveredNode && !state.hoveredNeighbors.has(node) && state.hoveredNode !== node) {
res.color = '#f6f6f6';
res.label = '';
} else {
res.color = data.color;
res.label = data.label;
res.borderColor = data.borderColor; // Ensure border color is maintained
}
return res;
});
renderer.setSetting('edgeReducer', (edge, data) => {
const res = { ...data };
if (state.hoveredNode && !graph.hasExtremity(edge, state.hoveredNode)) {
res.hidden = true;
} else {
res.hidden = false;
}
return res;
});
renderer.refresh();
}
} else {
console.error('불러온 CSV 데이터가 없습니다.');
}
}
function computeCentrality(containerId) {
const graph = graphs[containerId];
const centralityBody = document.getElementById('centrality-body');
centralityBody.innerHTML = ''; // 기존 내용 제거
let nodes = [];
const degreeCen = degreeCentrality(graph);
Object.keys(degreeCen).forEach(node => {
if (!graph.getNodeAttribute(node, 'isTitleFile')) { // Exclude title files from centrality calculation
graph.setNodeAttribute(node, 'degreeCentrality', parseFloat(degreeCen[node].toFixed(3)));
}
});
// Eigenvector Centrality 계산 및 할당
let eigenCen;
try {
eigenCen = eigenvectorCentrality(graph);
Object.keys(eigenCen).forEach(node => {
if (!graph.getNodeAttribute(node, 'isTitleFile')) { // Exclude title files from centrality calculation
graph.setNodeAttribute(node, 'eigenvectorCentrality', parseFloat(eigenCen[node].toFixed(3)));
}
});
} catch (error) {
console.error('Error calculating eigenvector centrality:', error);
graph.forEachNode(node => {
if (!graph.getNodeAttribute(node, 'isTitleFile')) { // Exclude title files from centrality calculation
graph.setNodeAttribute(node, 'eigenvectorCentrality', 'N/A');
}
});
}
// 노드를 중심성 기준으로 정렬 및 GameTitle 제외
graph.forEachNode((node, attributes) => {
if (!attributes.isTitleFile) { // Exclude title files from centrality calculation
nodes.push({
node: node,
degreeCentrality: attributes.degreeCentrality,
eigenvectorCentrality: attributes.eigenvectorCentrality
});
}
});
// 정렬: degreeCentrality를 우선, eigenvectorCentrality를 그 다음으로 기준
nodes.sort((a, b) => {
if (b.degreeCentrality !== a.degreeCentrality) {
return b.degreeCentrality - a.degreeCentrality;
} else {
if (a.eigenvectorCentrality === 'N/A') return 1;
if (b.eigenvectorCentrality === 'N/A') return -1;
return b.eigenvectorCentrality - a.eigenvectorCentrality;
}
});
// 중심성 테이블에 GameTitle을 제외하고 노드 추가
nodes.forEach(({ node, degreeCentrality, eigenvectorCentrality }) => {
const row = document.createElement('tr');
const nodeCell = document.createElement('td');
nodeCell.textContent = node;
const degreeCell = document.createElement('td');
degreeCell.textContent = degreeCentrality.toFixed(3);
const eigenCell = document.createElement('td');
eigenCell.textContent = eigenvectorCentrality === 'N/A' ? 'N/A' : eigenvectorCentrality.toFixed(3);
row.appendChild(nodeCell);
row.appendChild(degreeCell);
// row.appendChild(eigenCell);
centralityBody.appendChild(row);
});
}
let comResoulution = 1.0; // Default resolution value
function communityAssign(containerId) {
const graph = graphs[containerId];
const communityBody = document.getElementById('community-body');
communityBody.innerHTML = ''; // 기존 내용 제거
console.log('Using RNG function:', rng); // RNG 함수 확인
// Community detection using louvain algorithm with resolution and RNG
louvain.assign(graph, {
resolution: comResoulution,
rng: rng
});
let communityNodes = {};
let communityWeights = {}; // 집단별 가중치 합을 저장할 객체
let communityColors = {};
graph.forEachNode((node, attributes) => {
if (!attributes.isTitleFile) { // Exclude title files from community detection
const community = graph.getNodeAttribute(node, 'community');
if (!communityNodes[community]) {
communityNodes[community] = [];
communityWeights[community] = 0; // 초기화
communityColors[community] = getRandomColor(); // Assign a random color to the community
}
communityNodes[community].push(node);
graph.setNodeAttribute(node, 'community', community);
graph.setNodeAttribute(node, 'color', communityColors[community]); // Set the node color based on community
graph.setNodeAttribute(node, 'label', `${node} (Community ${community})`); // Set the node label to include community
}
});
// 집단별 가중치 합 계산
Object.keys(communityNodes).forEach(community => {
communityNodes[community].forEach(node => {
graph.forEachEdge(node, (edge, attributes, source, target) => {
if (communityNodes[community].includes(source) && communityNodes[community].includes(target)) {
communityWeights[community] += attributes.size; // 가중치 합 계산
}
});
});
});
updateCommunityNodes(communityNodes, communityWeights, communityBody);
refreshGraph(containerId);
}
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function updateCommunityNodes(communityNodes, communityWeights, communityBody) {
Object.keys(communityNodes).forEach(community => {
const row = document.createElement('tr');
const communityCell = document.createElement('td');
communityCell.textContent = community;
const nodesCell = document.createElement('td');
nodesCell.textContent = communityNodes[community].join(', ');
const weightCell = document.createElement('td');
weightCell.textContent = Math.round(communityWeights[community]); // 가중치 합 반올림하여 표시
row.appendChild(communityCell);
row.appendChild(nodesCell);
row.appendChild(weightCell);
communityBody.appendChild(row);
});
}
function refreshGraph(containerId) {
const renderer = renderers[containerId];
renderer.setSetting('nodeReducer', (node, data) => {
const res = { ...data };
res.label = data.label;
res.color = data.color;
res.borderColor = data.borderColor; // Ensure border color is maintained
return res;
});
renderer.setSetting('edgeReducer', (edge, data) => {
const res = { ...data };
res.hidden = false;
return res;
});
renderer.refresh();
}
document.getElementById('toggle-community').addEventListener('click', () => {
const containerId = document.querySelector('.chart-container:not(.hidden)').id;
communityAssign(containerId);
refreshGraph(containerId); // Refresh the graph to apply new colors
});
document.getElementById('increase-community').addEventListener('click', () => {
comResoulution += 0.1;
if (comResoulution > 4.0) {
alert('집단 수가 너무 많습니다');
comResoulution = 1.0;
}
console.log(comResoulution);
const containerId = document.querySelector('.chart-container:not(.hidden)').id;
communityAssign(containerId);
refreshGraph(containerId); // Refresh the graph to apply new colors
});
document.getElementById('decrease-community').addEventListener('click', () => {
comResoulution -= 0.1;
if (comResoulution < 0.1) {
alert('더 이상 줄일 수 없습니다.');
comResoulution = 1.0;
}
console.log(comResoulution);
const containerId = document.querySelector('.chart-container:not(.hidden)').id;
communityAssign(containerId);
refreshGraph(containerId); // Refresh the graph to apply new colors
});
function showChart(containerId) {
const containers = document.querySelectorAll('.chart-container');
containers.forEach(container => {
if (container.id === containerId) {
container.classList.remove('hidden');
} else {
container.classList.add('hidden');
}
});
const buttons = document.querySelectorAll('#chart button');
buttons.forEach(button => {
if (button.id === `button${containerId === 'chart-container1' ? '1' : '2'}`) {
button.classList.add('active');
} else {
button.classList.remove('active');
}
});
// Load CSV data for the selected container
const suffix = containerId === 'chart-container1' ? 'edges' : 'jaccard';
const selectedFiles = [];
const selectedTitleFiles = [];
// const isGameTitleOn = document.getElementById('game-title-on').checked;
for (const year in (suffix === 'edges' ? yearFiles : jaccardFiles)) {
if (document.getElementById(`${year}`).checked) {
selectedFiles.push(suffix === 'edges' ? yearFiles[year] : jaccardFiles[year]);
// if (isGameTitleOn && suffix === 'edges') {
// selectedTitleFiles.push(titleFiles[year]);
// }
}
}
loadMultipleCSVs(selectedFiles.concat(selectedTitleFiles), containerId);
}