Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const apiKey = '6H9KFYV6QB4V95H4'
  2. const fetchUrl = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MLT.AX&apikey=6H9KFYV6QB4V95H4'
  3. const button = document.querySelector('.btn')
  4. const myChart = document.getElementById('myChart').getContext('2d')
  5.  
  6. // *** No `data` variable here
  7.  
  8. // Get data from API
  9. async function getData() {
  10.     // You'd wrap the entire body in `try`/`catch` if you wanted to catch
  11.     // errors here as you do in your original, but I strongly recommend
  12.     // not suppressing errors until the outermost code, so that intervening
  13.     // code knows whether the operation worked
  14.     const response = await fetch(fetchUrl)
  15.     if (!response.ok) {
  16.         throw new Error("HTTP error " + response.status)
  17.     }
  18.     const object = await response.json()
  19.     const data = {}                     // *** Create the object we'll return
  20.     data.rawData = object
  21.     data.xlabels = Object.keys(object['Time Series (Daily)'])
  22.  
  23.     data.price = new Object
  24.  
  25.     data.price.open = new Array
  26.     data.price.high = new Array
  27.     data.price.low = new Array
  28.     data.price.close = new Array
  29.     data.price.adjustedClose = new Array
  30.     data.price.volume = new Array
  31.     data.price.dividendAmount = new Array
  32.     data.price.splitCoefficient = new Array
  33.  
  34.     Object.entries(object["Time Series (Daily)"]).forEach(function (day) { data.price.open.push(day[1]['1. open']) })
  35.     Object.entries(object["Time Series (Daily)"]).forEach(function (day) { data.price.high.push(day[1]['2. high']) })
  36.     Object.entries(object["Time Series (Daily)"]).forEach(function (day) { data.price.low.push(day[1]['3. low']) })
  37.     Object.entries(object["Time Series (Daily)"]).forEach(function (day) { data.price.close.push(day[1]['4. close']) })
  38.     Object.entries(object["Time Series (Daily)"]).forEach(function (day) { data.price.adjustedClose.push(day[1]['5. adjusted close']) })
  39.     Object.entries(object["Time Series (Daily)"]).forEach(function (day) { data.price.volume.push(day[1]['6. volume']) })
  40.     Object.entries(object["Time Series (Daily)"]).forEach(function (day) { data.price.dividendAmount.push(day[1]['7. dividend amount']) })
  41.     Object.entries(object["Time Series (Daily)"]).forEach(function (day) { data.price.splitCoefficient.push(day[1]['8. split coefficient']) })
  42.  
  43.     data.price.open.reverse()
  44.     data.price.high.reverse()
  45.     data.price.low.reverse()
  46.     data.price.close.reverse()
  47.     data.price.adjustedClose.reverse()
  48.     data.price.volume.reverse()
  49.     data.price.dividendAmount.reverse()
  50.     data.price.splitCoefficient.reverse()
  51.     data.xlabels.reverse()
  52.  
  53.     // *** Return the data
  54.     return data
  55. }
  56.  
  57. // Draw chart
  58. async function chartIt() {
  59.     // *** Use the returned data
  60.     const priceData = await getData()
  61.     console.log(priceData)
  62.  
  63.     const priceChart = new Chart(myChart, {
  64.         type: 'bar',
  65.         data: {
  66.             labels: data.xlabels,
  67.             datasets: [{
  68.                 label: 'Price',
  69.                 data: data.price.close,
  70.             }]
  71.         },
  72.         options: {}
  73.     })
  74. }
  75.  
  76. chartIt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement