github.com/diadata-org/diadata@v1.4.593/frontend/interestRates/createCharts/ownChart_v3.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 function makechart2(rate1, rate2, loading) { 99 100 yourOwnChart = Highcharts.stockChart(rate1.container, { 101 rangeSelector: { 102 buttonTheme: { 103 width: 20, 104 }, 105 inputBoxWidth: 75, 106 }, 107 chart: { 108 type: 'spline', 109 }, 110 credits: { 111 text: 'DIADATA', 112 href: 'https://diadata.org' 113 }, 114 title: { 115 text: rate1.name, 116 style: { 117 fontSize: '20px', 118 }, 119 }, 120 xAxis: { 121 tickPixelInterval: 150, 122 maxZoom: 20 * 1000, 123 title: { 124 margin: 10, 125 } 126 }, 127 yAxis: { 128 minPadding: 0.2, 129 maxPadding: 0.2, 130 title: { 131 text: 'Index value', 132 margin: 80 133 } 134 }, 135 legend: { 136 enabled: true 137 }, 138 series: [ 139 { 140 name: rate2.name, 141 data: [], 142 color: '#707070', 143 lineWidth: 1 144 // dashStyle: 'DashDot', 145 }, 146 { 147 name: rate1.name, 148 data: [], 149 }, 150 ] 151 }); 152 if(loading) { 153 yourOwnChart.showLoading(); 154 } 155 } 156 157 // ------------------------------------------------------------------------------------------------ 158 // First fill of chart when loading the page 159 // ------------------------------------------------------------------------------------------------ 160 161 // Rate info for the first fill 162 var RateInfo = { 163 name: 'SOFR30', 164 container: 'yourOwnContainer', 165 firstPublication: "2018-04-03", 166 url: 'https://api.diadata.org/v1/compoundedAvg/SOFR/30/360?dateInit=2018-05-14&dateFinal=' + today, 167 }; 168 169 // Initial fill 170 getData(RateInfo.url, function(obj) { 171 prefillArray = [] 172 for(i = 0; i < obj.length; i++) { 173 var value = obj[i].Value; 174 // prefillArray.push([Date.parse(obj[i].EffectiveDate), +value.toFixed(document.getElementById('rounding').value)]); 175 prefillArray.push([Date.parse(obj[i].EffectiveDate), +value.toFixed(4)]); 176 } 177 prefillArray.sort() 178 yourOwnChart.series[0].setData(prefillArray) 179 // yourOwnChartSOFR.redraw(); 180 }); 181 makechart(RateInfo, false); 182 183 // ------------------------------------------------------------------------------------------------ 184 // Update upon clicking button 185 // ------------------------------------------------------------------------------------------------ 186 function updateChart() { 187 188 // Retrieve user data -------------------------------------------------------------------- 189 var lenPeriod = document.getElementById('lenPeriod').value; 190 var dpy = document.getElementById('dpy').value; 191 var symbol = document.getElementById('symbol').value; 192 var rounding = document.getElementById('rounding').value; 193 var dia = document.getElementById('DIA').checked; 194 var compare = document.getElementById('compare').checked; 195 196 // update rate information --------------------------------------------------------------- 197 // retrieve first publication date 198 const found = Object.values(firstPublications).find(element => element.Symbol == symbol); 199 RateInfo.firstPublication = found.FirstDate.slice(0,10); 200 // Increase initial date according to observation period 201 dateInit = addDays(RateInfo.firstPublication, lenPeriod).slice(0,10); 202 // Check which Index should be displayed 203 if(dia) { 204 RateInfo.name = symbol + lenPeriod + '_by_DIA'; 205 RateInfo.url = 'https://api.diadata.org/v1/compoundedAvgDIA/' + symbol + '/' + lenPeriod + '/' + dpy + '?dateInit=' + dateInit + '&dateFinal=' + today; 206 } else { 207 RateInfo.name = symbol + lenPeriod; 208 RateInfo.url = 'https://api.diadata.org/v1/compoundedAvg/' + symbol + '/' + lenPeriod + '/' + dpy + '?dateInit=' + dateInit + '&dateFinal=' + today; 209 } 210 211 if(compare){ 212 // Plot the original rate along with the custom compounded rate 213 214 var RateInfoOriginal = { 215 name: symbol, 216 container: 'yourOwnContainer', 217 firstPublication: RateInfo.firstPublication, 218 url: 'https://api.diadata.org/v1/compoundedAvg/' + symbol + '/1/' + dpy + '?dateInit=' + dateInit + '&dateFinal=' + today, 219 }; 220 221 getData(RateInfoOriginal.url, function(obj) { 222 var prefillArray = []; 223 for(i = 0; i < obj.length; i++) { 224 var value = obj[i].Value; 225 prefillArray.push([Date.parse(obj[i].EffectiveDate), +value.toFixed(rounding)]); 226 // prefillArray.push([Date.parse(obj[i].EffectiveDate), parseFloat(value.toFixed(rounding))]); 227 } 228 yourOwnChart.series[0].setData(prefillArray); 229 yourOwnChart.hideLoading(); 230 }); 231 232 getData(RateInfo.url, function(obj) { 233 var prefillArray = []; 234 for(i = 0; i < obj.length; i++) { 235 var value = obj[i].Value; 236 prefillArray.push([Date.parse(obj[i].EffectiveDate), +value.toFixed(rounding)]); 237 // prefillArray.push([Date.parse(obj[i].EffectiveDate), parseFloat(value.toFixed(rounding))]); 238 } 239 yourOwnChart.series[1].setData(prefillArray); 240 yourOwnChart.hideLoading(); 241 }); 242 243 makechart2(RateInfo, RateInfoOriginal, true); 244 245 246 } else { 247 248 getData(RateInfo.url, function(obj) { 249 250 var prefillArray = []; 251 for(i = 0; i < obj.length; i++) { 252 var value = obj[i].Value; 253 prefillArray.push([Date.parse(obj[i].EffectiveDate), +value.toFixed(rounding)]); 254 // prefillArray.push([Date.parse(obj[i].EffectiveDate), parseFloat(value.toFixed(rounding))]); 255 } 256 yourOwnChart.series[0].setData(prefillArray); 257 yourOwnChart.hideLoading(); 258 }); 259 makechart(RateInfo, true); 260 } 261 262 };