-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface___.html
More file actions
375 lines (338 loc) · 11.7 KB
/
Copy pathinterface___.html
File metadata and controls
375 lines (338 loc) · 11.7 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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>喜好歌單分析</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background-color: #f5f5f5;
}
.container {
display: flex;
padding: 20px;
background-color: #fff;
width: 80%;
max-width: 1200px;
}
.sidebar {
text-align: center;
padding: 10px;
margin-right: 20px;
border-right: 2px solid #333;
width: 30%;
height: 600px;
overflow-y: auto;
}
.sidebar h2 {
writing-mode: vertical-rl;
transform: rotate(180deg);
font-size: 24px;
}
.content {
padding: 10px;
position: relative;
width: 70%;
display: flex;
flex-direction: column;
align-items: center;
}
.buttons {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
width: 100%;
}
.buttons button {
padding: 10px;
font-size: 16px;
cursor: pointer;
}
.chart-container {
display: flex;
align-items: flex-start;
justify-content: space-between;
width: 100%;
}
.chart {
width: 250px;
height: 250px;
margin-right: 20px;
}
.right-panel {
display: flex;
flex-direction: column;
align-items: flex-start;
margin-top: 20px;
gap: 10px;
width: 200px;
}
.options {
text-align: left;
display: flex;
flex-direction: column;
justify-content: flex-start;
gap: 10px;
margin-bottom: 20px;
}
.options p {
cursor: pointer;
padding: 5px 10px;
color: #fff;
border-radius: 5px;
font-weight: bold;
}
.create-playlist {
margin-top: 20px;
text-align: center;
}
.create-playlist button {
padding: 10px;
font-size: 16px;
cursor: pointer;
position: relative;
bottom: 0;
text-align: center;
}
.song-list {
max-height: 400px;
overflow-y: auto;
}
.song-item {
margin: 5px 0;
display: flex;
align-items: center;
}
.song-item label {
margin-left: 10px;
font-size: 16px;
}
.upload-section {
margin-bottom: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
.upload-section h3 {
margin-bottom: 10px;
font-size: 18px;
color: #333;
}
</style>
</head>
<body>
<div class="container">
<!-- 側邊欄:顯示"喜好歌單"文字 -->
<div class="sidebar">
<!-- 第一格:上傳語言的 CSV 檔 -->
<div class="upload-section">
<h3>語言</h3>
<input type="file" id="languageCsvInput" accept=".csv" onchange="handleUserUpload(event, 'language')" />
</div>
<!-- 第二格:上傳風格的 CSV 檔 -->
<div class="upload-section">
<h3>風格</h3>
<input type="file" id="genreCsvInput" accept=".csv" onchange="handleUserUpload(event, 'genre')" />
</div>
<!-- 第三格:上傳 Emo 指數的 CSV 檔 -->
<div class="upload-section">
<h3>Emo 指數</h3>
<input type="file" id="emoCsvInput" accept=".csv" onchange="handleUserUpload(event, 'emotion')" />
</div>
<!-- 第四格:上傳歌手的 CSV 檔 -->
<div class="upload-section">
<h3>歌手</h3>
<input type="file" id="artistCsvInput" accept=".csv" onchange="handleUserUpload(event, 'artist')" />
</div>
</div>
<!-- 主內容區 -->
<div class="content">
<!-- 選擇按鈕 -->
<div class="buttons">
<button onclick="generateChart('language')">語言</button>
<button onclick="generateChart('genre')">風格</button>
<button onclick="generateChart('emotion')">Emo指數</button>
<button onclick="generateChart('artist')">歌手</button>
</div>
<div class="chart">
<canvas id="chartCanvas"></canvas>
</div>
<!-- 圓餅圖和右側選項區域 -->
<div class="chart-container">
<div class="chart">
<canvas id="languageChart"></canvas>
</div>
<!-- 右側選項和創建歌單 -->
<div class="right-panel">
<!-- 分析結果選擇區域 -->
<div class="options" id="options"></div>
<!-- 顯示歌單 -->
<div class="song-list"></div>
<!-- 創建歌單按鈕 -->
<div class="create-playlist">
<input type="button" value="創建歌單" onclick="createPlaylist()" />
</div>
</div>
</div>
</div>
</div>
<script>
let dataStore = {
language: [],
genre: [],
emotion: [],
artist: [],
};
function handleUserUpload(event, type) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (e) {
const text = e.target.result;
processCSVData(text, type);
};
reader.readAsText(file);
}
function processCSVData(csvText, type) {
console.log(`Processing ${type} data`);
const rows = csvText.split("\n").map(row => row.split(","));
const headers = rows.shift(); // 移除標題列
// 根據資料類型選擇相應的欄位索引
const columnIndex = {
language: 2, // 語言在第 3 欄
genre: 4, // 風格在第 5 欄
emotion: 2, // Emo 指數在第 3 欄
artist: 1 // 歌手在第 2 欄
}[type];
// 直接提取歌名和歌手名稱
if (type === 'emotion') {
dataStore[type] = rows.map(row => ({
value: parseFloat(row[columnIndex]?.trim().replace(/"/g, '')), // Emo 指數轉換為數字
song: row[0]?.trim() || '未知', // 歌名
artist: row[1]?.trim() || '未知' // 歌手
})).filter(item => !isNaN(item.value)); // 過濾無效的 Emo 指數數據
} else {
dataStore[type] = rows.map(row => ({
value: row[columnIndex]?.trim().replace(/"/g, ''), // 直接提取語言、風格等欄位
song: row[0]?.trim() || '未知', // 歌名
artist: row[1]?.trim() || '未知' // 歌手
})).filter(item => item.value); // 過濾掉空值
}
console.log(`${type} Data:`, dataStore[type]);
}
let currentChart = null;
function generateChart(type) {
if (!dataStore[type] || dataStore[type].length === 0) {
alert(`請先上傳 ${type} 的 CSV 檔案!`);
return;
}
let dataCounts = {};
if (type === 'emotion') {
dataCounts = { "0~0.25": 0, "0.25~0.5": 0, "0.5~0.75": 0, "0.75~1": 0 };
dataStore[type].forEach(item => {
const value = parseFloat(item.value);
if (value >= 0 && value < 0.25) {
dataCounts["0~0.25"]++;
} else if (value >= 0.25 && value < 0.5) {
dataCounts["0.25~0.5"]++;
} else if (value >= 0.5 && value < 0.75) {
dataCounts["0.5~0.75"]++;
} else if (value >= 0.75 && value <= 1) {
dataCounts["0.75~1"]++;
}
});
} else {
dataStore[type].forEach(item => {
const key = item.value;
dataCounts[key] = (dataCounts[key] || 0) + 1;
});
}
const optionsContainer = document.getElementById('options');
optionsContainer.innerHTML = '';
Object.keys(dataCounts).forEach(option => {
const optionElement = document.createElement('div');
optionElement.innerHTML = `
<label>
<input type="checkbox" value="${option}" />
${option} (${dataCounts[option]})
</label>
`;
optionsContainer.appendChild(optionElement);
});
const chartData = {
labels: Object.keys(dataCounts),
datasets: [{
data: Object.values(dataCounts),
backgroundColor: generateColors(Object.keys(dataCounts).length),
}],
};
if (currentChart) {
currentChart.destroy();
}
const ctx = document.getElementById("chartCanvas").getContext("2d");
currentChart = new Chart(ctx, {
type: "pie",
data: chartData,
options: {
responsive: true,
plugins: {
legend: { position: "top" },
},
},
});
}
function generateColors(count) {
const colors = [];
for (let i = 0; i < count; i++) {
colors.push(`hsl(${Math.floor(Math.random() * 360)}, 70%, 70%)`);
}
return colors;
}
function createPlaylist() {
const checkedOptions = Array.from(document.querySelectorAll('#options input:checked'))
.map(checkbox => checkbox.value);
if (checkedOptions.length === 0) {
alert("請至少選擇一個分類!");
return;
}
const filteredSongs = [];
for (const type in dataStore) {
filteredSongs.push(
...dataStore[type].filter(item => checkedOptions.includes(item.value))
);
}
displaySelectedSongs(filteredSongs);
localStorage.setItem('playlist', JSON.stringify(filteredSongs));
window.location.href = "newplaylist.html";
}
function displaySelectedSongs(songs) {
const songListContainer = document.querySelector('.song-list');
songListContainer.innerHTML = ''; // 清空當前顯示的歌曲
if (songs.length === 0) {
songListContainer.innerHTML = '<p>沒有符合條件的歌曲。</p>';
return;
}
songs.forEach(song => {
console.log(song);
const songItem = document.createElement('div');
songItem.classList.add('song-item');
songItem.innerHTML = `
<label>
${song.song} - ${song.artist}
</label>
`;
songListContainer.appendChild(songItem);
});
}
</script>
</body>
</html>