Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the filter on the chart, find no issue #16

Merged
merged 5 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions client/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const clientId = 'd9827efb2c79463b92becb457a635a04';
const clientSecret = '6f2f0a36046f4f21980873a48c7bdab0';
// --------------------- API dialogue functions ---------------------


function getRandomIntInclusive(min, max) {
const newMin = Math.ceil(min);
const newMax = Math.floor(max);
Expand Down Expand Up @@ -70,7 +69,6 @@ async function getTracks(token, tracksEndPoint, limit) {
return data;
}


// --------------------- Data handling functions ---------------------

async function initSongs(plalylistSg, genre, token) {
Expand All @@ -81,7 +79,6 @@ async function initSongs(plalylistSg, genre, token) {
return tracks.items.map((obj) => ({ ...obj, gen: genre }));
}


function songsToArray(songs) {
console.log(songs);
const array = [];
Expand All @@ -98,7 +95,6 @@ function songsToArray(songs) {
// console.log(array)
}


function songLenArray(list) {
const array = [];
list.forEach((element) => {
Expand Down Expand Up @@ -163,11 +159,9 @@ function injectImages(list) {
});
}


function initChart(songs, songlength) {
const ctx = document.getElementById('myChart');


// eslint-disable-next-line no-new
const m_chart = new Chart(ctx, {
type: 'bar',
Expand All @@ -176,7 +170,7 @@ function initChart(songs, songlength) {
labels: songs,
datasets: [
{
label: 'Length of the song',
label: 'Length of the song(ms)',
data: songlength,
borderWidth: 4
}
Expand Down Expand Up @@ -229,7 +223,6 @@ async function init() {
);
const playlist = await getPlaylistsByGenre(token, genreId, 1);


// store the track endpoint of the playlist
playlistEndpoint = `${playlist[0].href}/tracks`;
console.log(playlistEndpoint);
Expand All @@ -248,7 +241,6 @@ async function init() {
const sample = getRandomTen(songArray);
const sample_name = songNameArray(sample);
const sample_len = songLenArray(sample);
console.log(sample);
injectHTML(sample);
injectImages(sample);
document.getElementById('GeneratedContents').style.display = 'flex';
Expand All @@ -257,7 +249,23 @@ async function init() {

myChart.destroy();
myChart = initChart(sample_name, sample_len);

filter.addEventListener('click', (z) => {
z.preventDefault();
const filterData = myChart.data.datasets[0].data.filter((value) => value > 180000);

const filterLabels = [];
let i = 0;
for (i; i < filterData.length; i += 1) {
const result = myChart.data.datasets[0].data.indexOf(filterData[i]);
const label = myChart.data.labels[result];
filterLabels.push(label);
}
myChart.data.datasets[0].data = filterData;
myChart.data.labels = filterLabels;
myChart.update();
});
});
}

document.addEventListener('DOMContentLoaded', async () => init());
document.addEventListener('DOMContentLoaded', async () => init());
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ <h1> New Horizons </h1>
<div class="chart_container">
<canvas id="myChart"></canvas>
</div>
<button type="button", id="filter">Filter Length > 180000ms</button>
</div>

</div>
Expand Down