github.com/diadata-org/diadata@v1.4.593/frontend/interestRates/SOFR_old.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  function getHistoric(url, callback) {
     9      // getHistoric fetches historic data from the API
    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  function requestData(rate) {
    33  	console.log("my object is: ", 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              console.log("point is: ", point);
    45              
    46  			// Append a data point to the chart's series if the timestamp is new
    47              // L = chartSOFR.series[0].xData.length;
    48  			L = chartSOFR.series[0].xData.length;
    49  			
    50  			if(L == 0) {
    51  				chartSOFR.series[0].addPoint([date, point.Value]);
    52                  console.log("Initial fill: " + point.EffectiveDate);                
    53  			} else if(L > 0 && date != window['chart' + rate.symbol].series[0].xData[L-1]){
    54  				chartSOFR.series[0].addPoint([date, point.Value]);
    55  				console.log("Updated point at: " + point.EffectiveDate);
    56  			}
    57  
    58  			// Check for new data after @timeUpdate milliseconds
    59  			setTimeout(function(){requestData.call(this, rate)}, rate.timeUpdate); 
    60          },
    61  	});
    62  }
    63  
    64  
    65  // ------------------------------------------------------------------------------------------------
    66  // Rate specific information
    67  // ------------------------------------------------------------------------------------------------
    68  
    69  InfoSOFR = {
    70  	name: 'Secured Overnight Financing Rate',
    71  	symbol: 'SOFR',
    72  	timeUpdate: 10000,
    73  	urlHistoric: 'https://api.diadata.org/v1/interestrate/SOFR?dateInit=2018-11-01&dateFinal=' + yesterday,
    74  	urlActual: 'https://api.diadata.org/v1/interestrate/SOFR/' + today,
    75  }
    76  
    77  
    78  getHistoric(InfoSOFR.urlHistoric, function(obj) {
    79      // Each entry of obj corresponds to rate information at a specific timestamp.
    80  	prefillArray = []
    81      for(i = 0; i < obj.length; i++) {  
    82          prefillArray.push([Date.parse(obj[i].EffectiveDate), obj[i].Value]);
    83      }
    84      // Sort array by date ...
    85      prefillArray.sort()
    86      // ... and fill the chart.
    87  	chartSOFR.series[0].setData(prefillArray)
    88  });
    89  
    90  
    91  
    92  document.addEventListener('DOMContentLoaded', function() {
    93  	
    94      chartSOFR = Highcharts.stockChart('container', {
    95          chart: {
    96              type: 'spline',
    97              events: {
    98  				load: function(){
    99  					requestData.call(this, InfoSOFR)
   100  				}
   101  			}
   102  		},
   103  		credits: {
   104  			text: 'DIADATA',
   105  			href: 'https://diadata.org'
   106  		},
   107          title: {
   108              text: InfoSOFR.name,
   109          },
   110          xAxis: {
   111              tickPixelInterval: 150,
   112  			maxZoom: 20 * 1000,
   113  			title: {
   114  				text: 'Time',
   115  				margin: 10,
   116  			}
   117  		},
   118          yAxis: {
   119              minPadding: 0.2,
   120              maxPadding: 0.2,
   121              title: {
   122                  text: 'Index value',
   123                  margin: 80
   124  			}	
   125          },
   126          series: [
   127  			{
   128  				name: InfoSOFR.symbol,
   129  				data: []
   130  			},
   131  		]
   132  	});	
   133  });
   134