Files
cs5890_data_visualization/hw2/js/script.js
2019-09-16 20:36:30 -06:00

26 lines
721 B
JavaScript

/**
* Requests the file and executes a callback with the parsed result once
* it is available
* @param {path} file path
* @param {callback} callback to execute once the result is available
*/
function fetchJSONFile(path, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
let data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
httpRequest.open('GET', path);
httpRequest.send();
}
//call fetchJSONFile then build and render a tree
fetchJSONFile('data/Tree.json', function(data) {
let tree = new Tree(data);
tree.buildTree();
tree.renderTree();
});