github.com/diadata-org/diadata@v1.4.593/frontend/interestRates/SOFR.js (about) 1 // ------------------------------------------------------------------------------------------------ 2 // General variables and functions 3 // ------------------------------------------------------------------------------------------------ 4 5 let today = new Date().toISOString().slice(0, 10); 6 let yesterday = new Date(Date.now() - 864e5).toISOString().slice(0,10); 7 8 // getHistoric fetches historic data from our API with address @url 9 function getHistoric(url, callback) { 10 11 // Instantiate request object 12 var request = new XMLHttpRequest() 13 request.open('GET', url, true) 14 15 // Load data in GET request 16 request.onload = function() { 17 var data = JSON.parse(this.response) 18 if(this.status == 200) { 19 if (typeof callback === "function") { 20 callback(data) 21 } 22 } else if(this.status == 404) { 23 console.log('Not found error') 24 } 25 } 26 request.onerror = function() { 27 console.log('Request error.') 28 } 29 request.send() 30 } 31 32 // requestData(rate) asynchrononously loads new data points into the chart. 33 function requestData(rate) { 34 $.ajax({ 35 cache: true, 36 url: rate.urlActual, 37 type: 'GET', 38 39 // If GET request is successful, add data to the chart 40 success: function(point) { 41 42 // convert time (string) to Unix time for plotting 43 var date = Date.parse(point.EffectiveDate); 44 45 // Append a data point to the chart's series if the timestamp is new 46 L = window['chart' + rate.symbol].series[0].xData.length; 47 48 if(L == 0) { 49 window['chart' + rate.symbol].series[0].addPoint([date, point.Value]); 50 } else if(L > 0 && date != window['chart' + rate.symbol].series[0].xData[L-1]){ 51 window['chart' + rate.symbol].series[0].addPoint([date, point.Value]); 52 } 53 console.log("Updated " + rate.symbol); 54 window['chart' + rate.symbol].redraw(); 55 setTimeout(function(){requestData.call(this, rate)}, rate.timeUpdate); 56 }, 57 }); 58 } 59 60 function makechart(rate) { 61 window['chart' + rate.symbol] = Highcharts.stockChart(rate.container, { 62 rangeSelector: { 63 buttonTheme: { 64 width: 20, 65 }, 66 inputBoxWidth: 75, 67 }, 68 chart: { 69 type: 'spline', 70 events: { 71 load: function(){ 72 requestData.call(this, rate) 73 } 74 } 75 }, 76 credits: { 77 text: 'DIADATA', 78 href: 'https://diadata.org' 79 }, 80 title: { 81 text: rate.name, 82 style: { 83 fontSize: '20px', 84 }, 85 }, 86 xAxis: { 87 tickPixelInterval: 150, 88 maxZoom: 20 * 1000, 89 title: { 90 margin: 10, 91 } 92 }, 93 yAxis: { 94 minPadding: 0.2, 95 maxPadding: 0.2, 96 title: { 97 text: 'Index value', 98 margin: 80 99 } 100 }, 101 series: [ 102 { 103 name: rate.symbol, 104 data: [] 105 }, 106 ] 107 }); 108 } 109 110 111 112 // ------------------------------------------------------------------------------------------------ 113 // Rate specific information 114 // ------------------------------------------------------------------------------------------------ 115 116 InfoSOFR = { 117 name: 'Secured Overnight Financing Rate', 118 symbol: 'SOFR', 119 container: 'containerSOFR', 120 timeUpdate: 10000, 121 urlHistoric: 'https://api.diadata.org/v1/interestrate/SOFR?dateInit=2018-11-01&dateFinal=' + yesterday, 122 urlActual: 'https://api.diadata.org/v1/interestrate/SOFR/' + today, 123 } 124 125 126 getHistoric(InfoSOFR.urlHistoric, function(obj) { 127 // Each entry of obj corresponds to rate information at a specific timestamp. 128 prefillArray = [] 129 for(i = 0; i < obj.length; i++) { 130 prefillArray.push([Date.parse(obj[i].EffectiveDate), obj[i].Value]); 131 } 132 // Sort array by date ... 133 prefillArray.sort() 134 // ... and fill the chart. 135 window['chart' + InfoSOFR.symbol].series[0].setData(prefillArray) 136 // Redraw for correct display of boxes 137 window['chart' + InfoSOFR.symbol].redraw(); 138 }); 139 140 document.addEventListener('DOMContentLoaded', makechart(InfoSOFR)); 141 142 143 144 145 InfoESTR = { 146 name: 'Euro Short-Term Rate', 147 symbol: '€STR', 148 container: 'containerESTR', 149 timeUpdate: 10000, 150 urlHistoric: 'https://api.diadata.org/v1/interestrate/ESTER?dateInit=2018-10-01&dateFinal=' + yesterday, 151 urlActual: 'https://api.diadata.org/v1/interestrate/ESTER/' + today, 152 } 153 154 155 getHistoric(InfoESTR.urlHistoric, function(obj) { 156 prefillArray = [] 157 for(i = 0; i < obj.length; i++) { 158 prefillArray.push([Date.parse(obj[i].EffectiveDate), obj[i].Value]); 159 } 160 prefillArray.sort() 161 window['chart' + InfoESTR.symbol].series[0].setData(prefillArray) 162 window['chart' + InfoESTR.symbol].redraw(); 163 }); 164 165 document.addEventListener('DOMContentLoaded', makechart(InfoESTR));