Highcharts with promises from scratch
by Ryan J
HTML
<script src="https://content.rbcgam.com/application/themes/rbcgam/js/ie-polyfill.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<!-- <script src="https://code.highcharts.com/stock/modules/exporting.js"></script> -->
<div id="container" style="height: 400px; min-width: 310px">Loading</div>
CSS
.highcharts-navigator-mask-outside {
fill: rgba(255, 255, 255, 0.1);
}
.highcharts-navigator-mask-outside:first-child {
fill: rgba(255, 255, 255, 0.75);
}
JavaScript
///////////////
// INITIAL DATA
///////////////
var dataMaster = {
// "startDate": 1201755600000,
"startDate": Date.UTC(2010, (12-1), 15),
"endDate": Date.UTC(2017, (8-1), 15),
"distributions": true,
"funds": [{
"fundCode": "269",
"initVal": 10000,
"contrAmount": 500,
"contrInterval": 1, // 1 = month, 6 = semster, 12 = annual
"withdAmount": 0,
"withdInterval": 1
}, {
"fundCode": "268",
"initVal": 10000,
"contrAmount": 0,
"contrInterval": 1,
"withdAmount": 0,
"withdInterval": 1
}, {
"fundCode": "658",
"initVal": 10000,
"contrAmount": 500,
"contrInterval": 6,
"withdAmount": 0,
"withdInterval": 1
}]
};
// Dummy data for market events
var marketEvents = [{
"date": "2010-01-20",
"context": "Apple sells to Tesla"
}, {
"date": "2012-05-20",
"context": "Microsoft sells to Tesla"
}, {
"date": "2013-10-20",
"context": "Amazon sells to Tesla"
}, {
"date": "2017-04-20",
"context": "Tesla sells to Tesla"
}];
// API URLs
var baseUrl = {
'go10k':'https://content1.rbcgam.com/funds/growth-of-10k-nav?rbcFundCode=',
'distributions': 'https://content1.rbcgam.com/funds/distributions-monthly?rbcFundCode='
};
// Fund data after calculation
var fundInfo = new Object();
var fundArray = new Object();
var fundArrayContrib = new Object();
var fundArrayData = new Object();
var wasCashFlow = false;
var saveResults = new Object();
var k = 0;
// Array of info for end result
var calcInfo = new Object();
///////////////
// FETCHING THE DATA
///////////////
var promises = [];
dataMaster.funds.forEach(function(fund) {
let p = fetch(baseUrl.go10k + fund.fundCode)
.then(function(data){return data.json()})
.then(function(f){
f['fundDataInitial'] = fund; // Adding data from user json to fund data array
return f;
})
.then(function(final){
return final
})
promises.push(p);
let d = fetch(baseUrl.distributions +...