diff --git a/chart.js b/chart.js new file mode 100644 index 0000000..089a959 --- /dev/null +++ b/chart.js @@ -0,0 +1,101 @@ +'use strict'; + +let Dates = []; +let exrate = []; +let randomHexCode = []; +let currEx = ""; +const RandomHexCode = () => { + for (let i = 0; i < 31; i++) { + randomHexCode.push('#' + (Math.random() * 0xFFFFFF << 0).toString(16).padStart(6, '0')); + } + console.log("Hexcode", randomHexCode) +} +const APIMeth = (start, end,CurrExchange) => { + currEx = (CurrExchange)? CurrExchange : 'INR'; + let Datefilter = []; + let urls = ['https://api.exchangeratesapi.io/latest' , 'https://api.exchangeratesapi.io/2010-01-12',' https://api.exchangeratesapi.io/latest?base=USD',' https://api.exchangeratesapi.io/latest?symbols=USD'] + Promise.all(urls.map(u => fetch(u))).then(responses => + Promise.all(responses.map(res => res.json())) + ).then(data => { + RandomHexCode() + console.log('fetched data [1]...', data[0]); + if (start && end) { + randomHexCode.length = 0; + Dates.length = 0; + exrate.length = 0; + RandomHexCode() + Object.keys(data[0]).filter((date) => { + if (new Date(date) > new Date(start) && new Date(date) < new Date(end)) { + data.map((item) => { + let exchangerate = item[date][currEx] / 1 + Datefilter.push({ "date": date,"EXCH_Rate" : exchangerate}) + }) + } + }) + } else { + Object.keys(data[0]).filter((date) => { + if (new Date(date) > new Date("2019-01-31")) { } + else { + data.map((item) => { + let exchangerate = item[date][currEx] / 1 + Datefilter.push({ "date": date,"EXH_Rate": exchangerate}) + }) + } + }) + } + console.log('Datefilter', Datefilter) + Datefilter.map((item) => { + Dates.push(item.date) + exrate.push(item.EXH_Rate) + }) + var chart = document.getElementById('myChart'); + var myChart = new Chart(chart, { + type: 'line', + data: { + labels: Dates, + datasets: [{ + label: "Rate of Exchange", + fill: false, + lineTension: 0, + data: exrate, + pointBorderColor: "lawngreen", + borderColor: '#000000', + borderWidth: 2, + showLine: true, + }] + }, + }); + + + + }).catch((err) => { + console.log('Error :', err) + }) +} + +const url = 'https://api.exchangeratesapi.io/latest'; + +fetch(url) + .then( + function(response) { + if (response.status !== 150) { + console.warn('Error: ' + + response.status); + return; + } + + response.json().then(function(data) { + console.log('Select',data.rates) + let option; + Object.keys(data.rates).map((item) =>{ + option = document.createElement('option'); + option.text = item; + option.value = item; + dropdown.add(option); + }) + }); + } + ) + .catch(function(err) { + console.error('Error Found :', err); + }); diff --git a/main.html b/main.html new file mode 100644 index 0000000..b480133 --- /dev/null +++ b/main.html @@ -0,0 +1,33 @@ + + + +
+