-
Notifications
You must be signed in to change notification settings - Fork 1
Heat Map with Backend Data #77
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
base: heat-map
Are you sure you want to change the base?
Changes from all commits
7e10d73
6ae7245
026b854
e1c815c
f0e1e0f
c7292af
1c2dc05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,23 @@ | ||
| /** | ||
| * fetches youtube api calls from YoutubeServlet.java | ||
| * @file fetches youtube api calls from YoutubeServlet.java | ||
| * and displays on youtube-genre.html | ||
| */ | ||
|
|
||
|
|
||
| /** | ||
| * fetches genre count hashmap from /api/youtube and updates html | ||
| * fetches and returns genre analysis object from /api/youtube | ||
| * | ||
| * @returns {Promise<object>} obj containing Youtube genre data and stats | ||
| */ | ||
| async function displayMusicGenre() { | ||
| const genreBlock = document.getElementById('genres'); | ||
|
|
||
| // keep track of num_videos in URL w/o reload | ||
| history.pushState('', '', `youtube-genre.html`); | ||
|
|
||
| const response = await fetch(`/api/youtube`); | ||
| async function fetchMusicGenre() { | ||
| const response = await fetch('/api/youtube'); | ||
| if (response.status == 401) { | ||
| // no oauth login so redirect to new page | ||
| window.open('/api/oauth/login/youtube'); | ||
| } | ||
|
|
||
| const genreCount = await response.text(); | ||
| genreBlock.innerHTML = genreCount; | ||
| return JSON.parse(genreCount); | ||
| } | ||
|
|
||
| export const GENRE_ANALYSIS_PROMISE = fetchMusicGenre(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import {GENRE_ANALYSIS} from '/js/genre.js'; | ||
|
|
||
| class HeatMapRow { | ||
| /** | ||
| * format for a row for heat map values obj | ||
|
|
@@ -35,28 +37,26 @@ function makeBinaryArr(arr, total) { | |
| /** | ||
| * turns array of data into heat map | ||
| * @param {number[]} data array of all 0s/1s heat map data | ||
| * @param {number} dataLength size of data array | ||
| * @returns {HeatMapRow[]} square matrix of HeatMapValues for HeatMap | ||
| */ | ||
| function createHeatMapValues(data, dataLength) { | ||
| function createHeatMapValues(data) { | ||
| // heat map should have equal length and width | ||
| const numRows = Math.ceil(Math.sqrt(dataLength)); | ||
| const numRows = Math.ceil(Math.sqrt(data.length)); | ||
|
|
||
| const heatMapValues = []; | ||
| for (let i = 0; i < dataLength; i += numRows) { | ||
| for (let i = 0; i < data.length; i += numRows) { | ||
| heatMapValues.push(new HeatMapRow(data.slice(i, i + numRows))); | ||
| } | ||
| return heatMapValues.reverse(); | ||
| } | ||
|
|
||
| /** | ||
| * renders heat map onto html | ||
| * @param {number[]} data array of all 0s/1s heat map data | ||
| * @param {number} dataLength size of data array | ||
| * @param {number[]} allBinaryData array of all 0s/1s heat map data | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider renaming to just |
||
| */ | ||
| function createHeatMap(data, dataLength) { | ||
| function createHeatMap(allBinaryData) { | ||
| const options = { | ||
| series: createHeatMapValues(data, dataLength), | ||
| series: createHeatMapValues(allBinaryData), | ||
| chart: { | ||
| height: '100%', | ||
| width: '100%', | ||
|
|
@@ -88,4 +88,9 @@ function createHeatMap(data, dataLength) { | |
| chart.render(); | ||
| } | ||
|
|
||
| createHeatMap(likedMusicBinaryHist, TOTAL_LIKED); | ||
| GENRE_ANALYSIS.then((genreAnalysisInfo) => { | ||
| const LIKED_MUSIC_BINARY_HIST = makeBinaryArr( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I don't think this is a true constant, it should just use lowerCamelCase |
||
| genreAnalysisInfo.likedMusicHistory, | ||
| genreAnalysisInfo.totalLiked); | ||
| createHeatMap(LIKED_MUSIC_BINARY_HIST); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See earlier PR about not calling a function with side effects (such as making a web request) on import