github.com/diadata-org/diadata@v1.4.593/frontend/interestRates/createCharts/ownChart_v2.js (about) 1 // ------------------------------------------------------------------------------------------------ 2 // General variables and functions 3 // ------------------------------------------------------------------------------------------------ 4 5 // // Load meta information on rates before page loads 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, loading) { 50 51 yourOwnChart = Highcharts.stockChart(rate.container, { 52 rangeSelector: { 53 buttonTheme: { 54 width: 20, 55 }, 56 inputBoxWidth: 75, 57 }, 58 chart: { 59 type: 'spline', 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 }); 93 if(loading) { 94 yourOwnChart.showLoading(); 95 } 96 } 97 98 // ------------------------------------------------------------------------------------------------ 99 // First fill of chart when loading the page 100 // ------------------------------------------------------------------------------------------------ 101 102 // Rate info for the first fill 103 var RateInfo = { 104 name: 'SOFR30', 105 container: 'yourOwnContainer', 106 firstPublication: "2018-04-03", 107 url: 'https://api.diadata.org/v1/compoundedAvg/SOFR/30/360?dateInit=2018-05-14&dateFinal=' + today, 108 }; 109 110 // Initial fill 111 getData(RateInfo.url, function(obj) { 112 prefillArray = [] 113 for(i = 0; i < obj.length; i++) { 114 var value = obj[i].Value; 115 // prefillArray.push([Date.parse(obj[i].EffectiveDate), +value.toFixed(document.getElementById('rounding').value)]); 116 prefillArray.push([Date.parse(obj[i].EffectiveDate), +value.toFixed(4)]); 117 } 118 prefillArray.sort() 119 yourOwnChart.series[0].setData(prefillArray) 120 // yourOwnChartSOFR.redraw(); 121 }); 122 makechart(RateInfo, false); 123 124 // ------------------------------------------------------------------------------------------------ 125 // Update upon clicking button 126 // ------------------------------------------------------------------------------------------------ 127 function updateChart() { 128 129 // Retrieve user data -------------------------------------------------------------------- 130 var lenPeriod = document.getElementById('lenPeriod').value; 131 var dpy = document.getElementById('dpy').value; 132 var symbol = document.getElementById('symbol').value; 133 var rounding = document.getElementById('rounding').value; 134 var dia = document.getElementById('DIA').checked; 135 136 // update rate information --------------------------------------------------------------- 137 // retrieve first publication date 138 const found = Object.values(firstPublications).find(element => element.Symbol == symbol); 139 RateInfo.firstPublication = found.FirstDate.slice(0,10); 140 // Increase initial date according to observation period 141 dateInit = addDays(RateInfo.firstPublication, lenPeriod).slice(0,10); 142 // Check which Index should be displayed 143 if(dia) { 144 RateInfo.name = symbol + lenPeriod + '_by_DIA'; 145 RateInfo.url = 'http://localhost:8081/v1/compoundedAvgDIA/' + symbol + '/' + lenPeriod + '/' + dpy + '?dateInit=' + dateInit + '&dateFinal=' + today; 146 } else { 147 RateInfo.name = symbol + lenPeriod; 148 RateInfo.url = 'https://api.diadata.org/v1/compoundedAvg/' + symbol + '/' + lenPeriod + '/' + dpy + '?dateInit=' + dateInit + '&dateFinal=' + today; 149 } 150 151 // update rate information --------------------------------------------------------------- 152 getData(RateInfo.url, function(obj) { 153 154 var prefillArray = []; 155 for(i = 0; i < obj.length; i++) { 156 var value = obj[i].Value; 157 prefillArray.push([Date.parse(obj[i].EffectiveDate), +value.toFixed(rounding)]); 158 // prefillArray.push([Date.parse(obj[i].EffectiveDate), parseFloat(value.toFixed(rounding))]); 159 } 160 yourOwnChart.series[0].setData(prefillArray); 161 yourOwnChart.hideLoading(); 162 }); 163 164 makechart(RateInfo, true); 165 166 };