github.com/diadata-org/diadata@v1.4.593/frontend/interestRates/createCharts/ownChart.js (about)

     1  // ------------------------------------------------------------------------------------------------
     2  // General variables and functions
     3  // ------------------------------------------------------------------------------------------------
     4  
     5  // // Load meta information on rates
     6  var dateUrl = 'https://api.diadata.org/v1/interestrates';
     7  $.holdReady(true);
     8  var firstPublications = null;
     9  $.getJSON(dateUrl, function(data) {
    10      firstPublications = data;
    11      $.holdReady(false);
    12  });
    13  
    14  let today = new Date().toISOString().slice(0, 10);
    15  var yourOwnChart;
    16  var firstPublications = {};
    17  
    18  function addDays(date, days) {
    19      var result = new Date(date).getTime();
    20      result += days * 864e5
    21      result = new Date(result);
    22      return result.toISOString()
    23  }
    24  
    25  // getHistoric fetches historic data from our API with address @url
    26  function getData(url, callback) {
    27      
    28  	// Instantiate request object
    29  	var request = new XMLHttpRequest()
    30  	request.open('GET', url, true)
    31  
    32  	// Load data in GET request
    33  	request.onload = function() {
    34  		var data = JSON.parse(this.response)
    35  		if(this.status == 200) {
    36  			if (typeof callback === "function") {
    37  				callback(data)
    38  			}
    39  		} else if(this.status == 404) {
    40  			console.log('Not found error')
    41  		}
    42  	}
    43  	request.onerror = function() {
    44  		console.log('Request error.')
    45      }
    46      request.send()
    47  }
    48  
    49  function makechart(rate) {
    50  	yourOwnChart = Highcharts.stockChart(rate.container, {
    51  		rangeSelector: {
    52  			buttonTheme: {
    53  				   width: 20,
    54  			},
    55  			inputBoxWidth: 75,
    56  	   	}, 
    57  		chart: {
    58              type: 'spline',
    59          
    60  		},
    61  		credits: {
    62  			text: 'DIADATA',
    63  			href: 'https://diadata.org'
    64  		},
    65          title: {
    66  			text: rate.name,
    67  			style: {
    68  				fontSize: '20px',
    69  			},
    70          },
    71          xAxis: {
    72              tickPixelInterval: 150,
    73  			maxZoom: 20 * 1000,
    74  			title: {
    75  				margin: 10,
    76  			}
    77  		},
    78          yAxis: {
    79              minPadding: 0.2,
    80              maxPadding: 0.2,
    81              title: {
    82                  text: 'Index value',
    83                  margin: 80
    84  			}	
    85          },
    86          series: [
    87  			{
    88  				name: rate.name,
    89  				data: []
    90              },
    91              // {
    92              //     name: 'ESTER',
    93  			// 	data: [[1, 1], [2,10], [3, 10], [4, 20], [5, 1]]
    94  			// }
    95  		]
    96  	});	
    97  }
    98  
    99  // ------------------------------------------------------------------------------------------------
   100  // First fill of chart when loading the page
   101  // ------------------------------------------------------------------------------------------------
   102  
   103  // Rate info for the first fill
   104  var RateInfo = {
   105  	name: 'SOFR30',
   106  	container: 'yourOwnContainer',
   107      firstPublication: "2018-04-03",
   108      url: 'https://api.diadata.org/v1/compoundedAvg/SOFR/30/360?dateInit=2018-05-14&dateFinal=' + today,    
   109  };
   110  
   111  // Initial fill
   112  getData(RateInfo.url, function(obj) {
   113      prefillArray = []
   114      for(i = 0; i < obj.length; i++) {  
   115          prefillArray.push([Date.parse(obj[i].EffectiveDate), obj[i].Value]);
   116      }
   117      prefillArray.sort()
   118      yourOwnChart.series[0].setData(prefillArray)
   119      // yourOwnChartSOFR.redraw();               
   120  });
   121  makechart(RateInfo);
   122  
   123  // ------------------------------------------------------------------------------------------------
   124  // Update upon clicking button
   125  // ------------------------------------------------------------------------------------------------
   126  function updateChart() {
   127  
   128      // Retrieve user data --------------------------------------------------------------------
   129      var lenPeriod = document.getElementById("lenPeriod").value;
   130      var dpy = document.getElementById("dpy").value;
   131      var symbol = document.getElementById("symbol").value;
   132      
   133  
   134      // update rate information ---------------------------------------------------------------
   135      RateInfo.name = symbol + lenPeriod;
   136      // retrieve first publication date
   137      const found = Object.values(firstPublications).find(element => element.Symbol == symbol);
   138      RateInfo.firstPublication = found.FirstDate.slice(0,10);
   139      // Increase initial date according to observation period
   140      dateInit = addDays(RateInfo.firstPublication, lenPeriod).slice(0,10);            
   141      RateInfo.url = 'https://api.diadata.org/v1/compoundedAvg/' + symbol + '/' + lenPeriod + '/' + dpy + '?dateInit=' + dateInit + '&dateFinal=' + today;
   142      
   143      // Fill the chart with updated information
   144      getData(RateInfo.url, function(obj) {
   145          var prefillArray = [];
   146          for(i = 0; i < obj.length; i++) {  
   147              prefillArray.push([Date.parse(obj[i].EffectiveDate), obj[i].Value]);
   148          }
   149          prefillArray.sort(function(a,b) { return a - b; });
   150          yourOwnChart.series[0].setData(prefillArray);               
   151      });
   152  
   153      //  // Fill the chart with reference rate
   154      //  getData('http://localhost:8081/v1/interestrate/' + symbol + '/' + '?dateInit=' + dateInit + '&dateFinal=' + today, function(obj) {
   155      //     var prefillArray = [];
   156      //     for(i = 0; i < obj.length; i++) {  
   157      //         prefillArray.push([Date.parse(obj[i].EffectiveDate), obj[i].Value]);
   158      //     }
   159      //     prefillArray.sort();
   160      //     yourOwnChart.series[1].setData(prefillArray);
   161                     
   162      // });
   163  
   164      makechart(RateInfo);
   165  
   166  };