/**
* Copyright (c) 2019
*
* @summary vis data
* @author Elitza Vasileva
* @author Bernhard Pointner
*/
class DataCSV {
constructor() {}
/**
* Reads the data from a .csv file
* @param _callback
*/
readAll(_callback) {
d3.csv("data/set_movies_radialset.csv").then(data => {
this.readData(data, _callback);
});
}
/**
* Processes the read data and stores it in an array of objects
* @param response response from the previous function
* @param _callback
*/
readData(response, _callback){
let array = response;
let entries = [];
let genres = [];
let properties = Object.getOwnPropertyNames(array[0]);
let index = 1;
properties.forEach(function (key) {
if(key !== "AvgRating" && key !== "Watches" && key !== "Name" && key !== "ReleaseDate"){
let genre = {key: ""+index, title: key};
genres.push(genre);
index++;
}
});
array.forEach(function (movie, i) {
let temp = {};
for (let gen in genres) {
let g = genres[gen].title;
if (movie.hasOwnProperty(g)) {
let increment = +gen + 1; // necessary so that g starts from g1 and not from g0
let position = "g" + increment;
temp[position] = movie[g];
if(movie[g] !== "0" && movie[g] !== "1") {
console.error(movie,"ERROR in data set!!!");
}
}
}
let title = {mId: ""+(i+1), t: ""+movie.Name, rating: (+movie.AvgRating).toFixed(2), watched: +movie.Watches};
let singleEntry = Object.assign(title, temp);
entries.push(singleEntry);
});
let entry = {entries};
let genre = {genres};
let result = Object.assign(entry, genre);
_callback(result);
}
}