-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_sampleDataLoader.js
More file actions
113 lines (99 loc) · 3.86 KB
/
Copy path3_sampleDataLoader.js
File metadata and controls
113 lines (99 loc) · 3.86 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
import Papa from "papaparse";
function loadData(csvPath, className) {
const csvFilePath = csvPath; // CSV 파일 경로
fetch(csvFilePath)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
const encoding = detectEncoding(arrayBuffer);
const text = decodeText(arrayBuffer, encoding); // 인코딩된 텍스트 디코딩
parseCSV(text);
showDescription(className); // 설명을 표시
})
.catch(error => {
console.error('Error loading CSV file:', error);
});
}
function showDescription(className) {
// 모든 설명을 숨깁니다.
const descriptions = document.querySelectorAll('.description');
descriptions.forEach(function(description) {
description.style.display = 'none';
});
// 해당 설명을 표시합니다.
const selectedDescription = document.querySelector('.' + className);
if (selectedDescription) {
selectedDescription.style.display = 'block';
}
}
window.loadData = loadData;
function detectEncoding(buffer) {
return 'euc-kr';
}
function decodeText(arrayBuffer, encoding) {
const decoder = new TextDecoder(encoding);
return decoder.decode(new Uint8Array(arrayBuffer));
}
const errorLoadingCsv = document.getElementById('error-csv-message');
const tableDisplay = document.getElementById('csvDataDisplay');
const dataCount = document.getElementById('data-count');
function parseCSV(text) {
Papa.parse(text, {
header: true,
dynamicTyping: true,
complete: function(results) {
if (!results.data[0] || results.data[0].Source1 === undefined || results.data[0].Source2 === undefined || results.data[0].Weight === undefined) {
errorLoadingCsv.textContent = "CSV file의 각 열제목은 Source1, Source2, Weight이어야 함";
errorLoadingCsv.style.display = 'block'; // 에러 메시지를 보이게 설정
tableDisplay.style.display = 'none'
dataCount.style.display = 'none'
return;
} else{
errorLoadingCsv.style.display = 'none';
tableDisplay.style.display = 'block'
dataCount.style.display = 'block'
}
// Filter out rows with any empty values
const filteredData = results.data.filter(row => {
return Object.values(row).every(value => value !== null && value !== '');
}).map((row, index) => ({
ID: index + 1,
Source1: row.Source1,
Source2: row.Source2,
Weight: row.Weight
}));
displayTable(filteredData);
displayDataCount(filteredData.length);
window.csvData = filteredData; // Store data globally for graph visualization
console.log(csvData);
}
});
}
function displayDataCount(count) {
dataCount.textContent = '전체 데이터 수: ' + count + '개의 선분 데이터';
}
function displayTable(data) {
const csvHeader = document.getElementById('csvHeader');
const csvBody = document.getElementById('csvBody');
// Clear existing table data
csvHeader.innerHTML = '';
csvBody.innerHTML = '';
if (data.length > 0) {
// Create header row
const headers = Object.keys(data[0]);
headers.forEach(header => {
const th = document.createElement('th');
th.textContent = header;
csvHeader.appendChild(th);
});
// Create data rows
data.forEach(row => {
const tr = document.createElement('tr');
headers.forEach(header => {
const td = document.createElement('td');
td.textContent = row[header];
tr.appendChild(td);
});
csvBody.appendChild(tr);
});
}
}