-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
316 lines (277 loc) · 9.53 KB
/
Copy pathscript.js
File metadata and controls
316 lines (277 loc) · 9.53 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
let chart;
let data = { labels: [], values: [] };
let currentFile = '';
let availableFiles = [];
// Function to fetch available data files
async function fetchAvailableFiles() {
try {
const response = await fetch('data/');
const text = await response.text();
const parser = new DOMParser();
const htmlDoc = parser.parseFromString(text, 'text/html');
const links = htmlDoc.querySelectorAll('a');
availableFiles = Array.from(links)
.map(link => link.href)
.filter(href => href.endsWith('.txt'))
.map(href => href.split('/').pop());
// Update the file selector
const fileSelect = document.getElementById('dataFile');
fileSelect.innerHTML = '';
availableFiles.forEach(file => {
const option = document.createElement('option');
option.value = `data/${file}`;
option.textContent = file.replace('.txt', '').replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
fileSelect.appendChild(option);
});
if (availableFiles.length > 0) {
currentFile = `data/${availableFiles[0]}`;
fileSelect.value = currentFile;
}
return availableFiles;
} catch (error) {
console.error('Error fetching available files:', error);
return [];
}
}
// Function to fetch and parse the data file
async function fetchData(fileName = currentFile) {
try {
// First check if we have saved changes in localStorage
const savedData = localStorage.getItem(`saved_${fileName}`);
if (savedData) {
data = JSON.parse(savedData);
return data;
}
// If no saved changes, fetch from file
const response = await fetch(fileName);
const text = await response.text();
const lines = text.split('\n').filter(line => line.trim());
data.labels = [];
data.values = [];
lines.forEach(line => {
const [label, value] = line.split(' ');
if (label && value) {
data.labels.push(label);
data.values.push(parseInt(value));
}
});
return data;
} catch (error) {
console.error('Error fetching data:', error);
return { labels: [], values: [] };
}
}
// Function to save data changes
function saveDataChanges() {
localStorage.setItem(`saved_${currentFile}`, JSON.stringify(data));
}
// Function to create the chart
async function createChart() {
const ctx = document.getElementById('myChart').getContext('2d');
const chartType = document.getElementById('chartType').value;
// Destroy existing chart if it exists
if (chart) {
chart.destroy();
}
// Common chart options
const commonOptions = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: chartType !== 'bar' && chartType !== 'line'
}
}
};
// Type-specific options
const typeSpecificOptions = {
bar: {
scales: {
y: {
beginAtZero: true
}
}
},
line: {
scales: {
y: {
beginAtZero: true
}
}
},
pie: {},
doughnut: {},
radar: {
scales: {
r: {
beginAtZero: true
}
}
}
};
// Combine options
const options = {
...commonOptions,
...typeSpecificOptions[chartType]
};
// Create chart
chart = new Chart(ctx, {
type: chartType,
data: {
labels: data.labels,
datasets: [{
label: 'Values',
data: data.values,
backgroundColor: [
'rgba(255, 99, 132, 0.7)',
'rgba(54, 162, 235, 0.7)',
'rgba(255, 206, 86, 0.7)',
'rgba(75, 192, 192, 0.7)',
'rgba(153, 102, 255, 0.7)',
'rgba(255, 159, 64, 0.7)',
'rgba(199, 199, 199, 0.7)',
'rgba(83, 102, 255, 0.7)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)',
'rgba(199, 199, 199, 1)',
'rgba(83, 102, 255, 1)'
],
borderWidth: 1
}]
},
options: options
});
}
// Function to update the chart
function updateChart() {
const chartType = document.getElementById('chartType').value;
if (chart) {
chart.destroy();
}
createChart();
}
// Function to render the data list
function renderDataList() {
const dataList = document.getElementById('dataList');
dataList.innerHTML = '';
data.labels.forEach((label, index) => {
const item = document.createElement('div');
item.className = 'data-item';
item.innerHTML = `
<div class="item-content">
<span>${label}:</span>
<span>${data.values[index]}</span>
</div>
<div class="item-actions">
<button class="edit-btn" data-index="${index}">Edit</button>
<button class="delete-btn" data-index="${index}">Delete</button>
</div>
`;
dataList.appendChild(item);
});
// Add event listeners for edit and delete buttons
document.querySelectorAll('.edit-btn').forEach(button => {
button.addEventListener('click', handleEdit);
});
document.querySelectorAll('.delete-btn').forEach(button => {
button.addEventListener('click', handleDelete);
});
}
// Function to handle edit button click
function handleEdit(event) {
const index = event.target.dataset.index;
const item = event.target.closest('.data-item');
const content = item.querySelector('.item-content');
content.innerHTML = `
<input type="text" value="${data.labels[index]}" class="edit-label">
<input type="number" value="${data.values[index]}" class="edit-value">
`;
const saveBtn = document.createElement('button');
saveBtn.className = 'save-btn';
saveBtn.textContent = 'Save';
saveBtn.addEventListener('click', () => {
const newLabel = content.querySelector('.edit-label').value;
const newValue = parseInt(content.querySelector('.edit-value').value);
if (newLabel && !isNaN(newValue)) {
data.labels[index] = newLabel;
data.values[index] = newValue;
updateChart();
saveDataChanges();
renderDataList();
}
});
item.querySelector('.item-actions').innerHTML = '';
item.querySelector('.item-actions').appendChild(saveBtn);
}
// Function to handle delete button click
function handleDelete(event) {
const index = event.target.dataset.index;
data.labels.splice(index, 1);
data.values.splice(index, 1);
updateChart();
saveDataChanges();
renderDataList();
}
// Function to handle add data button click
document.getElementById('addData').addEventListener('click', () => {
const newLabel = document.getElementById('newLabel').value;
const newValue = parseInt(document.getElementById('newValue').value);
if (newLabel && !isNaN(newValue)) {
data.labels.push(newLabel);
data.values.push(newValue);
updateChart();
saveDataChanges();
renderDataList();
// Clear input fields
document.getElementById('newLabel').value = '';
document.getElementById('newValue').value = '';
}
});
// Function to handle chart type change
document.getElementById('chartType').addEventListener('change', updateChart);
// Function to handle data file change
document.getElementById('dataFile').addEventListener('change', async (event) => {
currentFile = event.target.value;
await fetchData(currentFile);
updateChart();
renderDataList();
});
// Add a reset button to clear saved changes
document.addEventListener('DOMContentLoaded', () => {
const resetButton = document.createElement('button');
resetButton.id = 'resetData';
resetButton.textContent = 'Reset to Original Data';
resetButton.style.marginTop = '10px';
resetButton.style.padding = '8px 16px';
resetButton.style.backgroundColor = '#f44336';
resetButton.style.color = 'white';
resetButton.style.border = 'none';
resetButton.style.borderRadius = '4px';
resetButton.style.cursor = 'pointer';
resetButton.addEventListener('click', async () => {
localStorage.removeItem(`saved_${currentFile}`);
await fetchData(currentFile);
updateChart();
renderDataList();
});
document.querySelector('.edit-section').appendChild(resetButton);
});
// Initialize the application
async function init() {
await fetchAvailableFiles();
if (availableFiles.length > 0) {
await fetchData();
await createChart();
renderDataList();
} else {
console.error('No data files found in the data directory');
}
}
// Start the application when the page loads
document.addEventListener('DOMContentLoaded', init);