Normy Haddad
Normy Haddad

Reputation: 181

How to add data to a graph from an API

I want to make a stock app, where the stock values of the past 7 days are shown in a graph. This is the code for extracting the data from the API:

on mouseUp
   put url("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=YPMBUVQ8AJXX1HQP&datatype=csv") into myData
   put myData into field "Data"

   set the itemdel to ","
   put 1 into x
   repeat until x > 8
      add 1 to x
      put items 1 to 5 of line 5 of myData & return after gData
   end repeat
   set the graphData of widget "graph" to gData
end mouseUp

The first item will be the x axis, and the rest will all be on the y axis. But when I run this code, it only puts a single line into the graphData of the graph, and nothing shows up on the graph except for the 2 axis. What am I doing wrong here?

Upvotes: 0

Views: 144

Answers (1)

Scott Rossi
Scott Rossi

Reputation: 885

I tried the following variation that appears to work. One issue is your data contains a volume amount at the end of each line that's super high compared to the trading values, so I delete that value from each of the lines used for the chart.

on mouseUp
   put url("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=YPMBUVQ8AJXX1HQP&datatype=csv") into temp
   delete line 1 of temp -- REMOVE THE COLUMN LABLES
   delete line 8 to -1 of temp -- LIMIT DATA TO 7 DAYS
   repeat for each line theLine in temp
      delete last item of theLine -- IGNORE VOLUME (NUMBER IS TOO LARGE COMPARED TO TRADING DATA)
      put theLine & return after myData
   end repeat
   set the graphData of widget "graph" to myData
end mouseUp

Upvotes: 1

Related Questions