How to add data to a graph from an API - livecode

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?

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

Related

Extracting trendline slope with VBA

I'm currently making a macro to extract raw data from multiple csvs, print it in different sheets, trim extraneous data, plot data from each sheet on a single graph with individual trendlines, and report some information for said trendlines and data. I've had success with everything except extracting the trendline slopes. I want to report the slope average of all the trendlines and I've written the code below to do this action:
For j = 1 To UBound(SelectedSheets)
TLEq = CompositeChartObject.Chart.SeriesCollection(j).Trendlines(1).DataLabel.Text
ReDim Preserve ModulusArr(j)
ModulusArr(j) = CDbl(Mid(TLEq, 4, Len(TLEq) - 4))
Next j
SelectedSheets is an array filled with the sheets the user selects to be analyzed from a multi-select userform listbox. TLEq is a string to temporarily hold the full text of the trendline equation (intercept set to 0 so simple y = mx equation). The CDbl(Mid()) is used to trim TLEq to just the slope and convert to a Double. In lines above these (not included), the series are added, trendlines inserted, and DisplayEquation is called for each trendline. There has been no issues with graphing, trendlines showing up, or trendline equations displaying.
The issue begins in line 2 where TLEq is inconsistently left blank during the loop; some iterations all the trendline equation are extracted, sometimes only some are, and other times no equations are extracted.. I have yet to find any pattern. Then, anytime TLEq is empty, line 4 errors because it cannot carry out the CDbl(Mid()) an empty TLEq. Any ideas?

Excel: Update Line plot based on dynamically changing column length

I am line plotting data in excel based on data on a column. However, the amount of data in this column may change (it's length may change) and I want the graph to update to only included values in the updated length of the column.
How can I accomplish this?
Supposing your data starts at ("A1"), you could do the following:
ActiveWorksheet.Range("A1", Range("A1").End(xldown))
You can read more about .End here
Furthermore, you can utilize .End to dynamically append new values to the existing list using .Offset(1, 0) where the first parameter = column; second = row.
For instance, say we want to add the Integer 5 to our existing list:
ActiveWorksheet.Range("A1").End(xldown).Offset(1, 0).Value = 5

How can I separate all the characters of a word into separate fields?

I have a random word generation script that works perfectly. I also have a few single character-width fields, exactly the same amount of them as the longest possible word that can be generated. Below each field I have a line that would resemble an underscore. Each field has a unique id, numbered from one like so: "letter1", "letter2".... and so on. Likewise, each line has the id of "line1", "line2", and so on.
Now that I've set the stage, here is what I want to happen. On the opening of the card, I want all of the letter fields to hide as I will have them show individually outside this subroutine. I then want all of the lines to hide, and then only the amount of lines that are required for the random word should show again. (The same number as length(randomword) from left to right.) Now, the random word should be looped through and the first character should be put into the field "letter1", and the second into "letter2", and so on. The word will change each time the card is opened, so it is important that this is not bruteforced (which unfortunately, myself having two weeks experience with LiveCode, is the only thing I know concretely how to do).
Could someone with more experience do me a solid and provide the code I would need to do this, and the location I should put the code in? (the card? the letter fields? I really don't know at this point).
I am happy to provide further description if I have not been articulate about my problem.
Try this (my LiveCode is a bit rusty):
on openCard
//Put your word generation script here
put 0 into amtOfLines
put 0 into amtOfLetters
repeat until amtOfLines is 6 //Replace 6 with the amount of lines
add 1 to amtOfLines
put "line" & amtOfLines into x
set the visible of graphic x to false
end repeat
repeat until amtOfLetters is 6 //Replace 6 with the amount of fields
add 1 to amtOfLetters
put "letter" & amtOfLetters into x
set the visible of field x to false
end repeat
put 0 into amtOfLines
put 0 into amtOfLetters
repeat until amtOfLines is length(randomword)
add 1 to amtOfLines
put "line" & amtOfLines into x
set the visible of graphic x to true
end repeat
repeat until amtOfLetters is length(randomword)
add 1 to amtOfLetters
put "letter" & amtOfLetters into x
set the visible of field x to true
end repeat
put 0 into amtOfChars
repeat until amtOfChars is length(randomword)
add 1 to amtOfChars
put "letter" & amtOfChars into x
set the text of field x to char amtOfChars of randomword
end repeat
end openCard
This would go in the card script.
EDIT: Fixed some errors.
This would go into the card script:
on setup pRandomWord
put 6 into tNumObjs -- or however many fields there are
put length(pRandomWord) into tLen
repeat with x = 1 to tNumObjs
hide fld ("letter"&x)
if x <= tLen then
put char x of pRandomWord into fld ("letter"&x)
show grc ("line"&x)
else
hide grc ("line" & x)
end if
end repeat
end setup
When you want to use it, put the random word into a variable and call the setup handler like this:
setup randomWord
This does everything in a single repeat loop. The random word is passed to the setup handler in the parameter "pRandomWord".

excel macros - line chart loses year axis values and chart title

I am recording a macro using Excel 2013 to create a line chart.
I have one column with years and an adjacent column with population values. The column headings are "Year" and "Population" respectively.
The line chart looks good.
When I run the macro, the chart title (Population) is striped out. Additionally the year values (x axis) are replaced with a number series, 1 . . . onwards
How do I have the macro retain the year values and the chart title (Population)?
Thanks!
This is my recorded Macro
Range("A1:B29").Select
ActiveWindow.WindowState = xlMaximized
ActiveSheet.Shapes.AddChart2(227, xlLineMarkers).Select
ActiveChart.SetSourceData Source:=Range("compilation!$A$1:$B$29")
This is an example of what I want the final graph to look like when I run the macro.
Add the following line to your last line of your macro
ActiveChart.FullSeriesCollection(1).XValues = "=compilation!$A$2:$A$29"
That should get you your year back along the bottom.
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Select
Selection.Caption = "=compilation!R1C2"
That will set your Title for you. However you still need your Primary Horizontal Title so why dont we throw in this tid bit of random code:
ActiveChart.Axes(xlCategory).Select
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
Selection.Caption = "=compilation!R1C1"
now the only outstanding issue is if you are generating 1 or 2 lines in your graph.
To get rid of the second line (technically the first since its really series 1) we have two choices.
Choice 1 is to select it with the code and delete it. I am going to opt for choice 2 though. Choice 2 is to never add it in the first place!
So to start with eliminate your first line of your Macro. When the graph is initially created, it will be created as a blank slate as there is no preselected data. So what we need to do is change 1 lettre in the line of your macro where you define the Source. (4th line of your posted code) and we are going to change the starting cell reference from $A$1 to $B$1. If the graph only has one column of data to work with for points then there can only be one series. So that line will look like the following in the end:
ActiveChart.SetSourceData Source:=Range("compilation!$B$1:$B$29")

Livecode repeat command

I have the bellow code and I want to complete the following.
I have a group with some fields.
I have 2 Variables. Variable 1 with names from fields and variable 2 with the values.
I want to set the text for the fields in the group to variable 2.
Line 1 from Var 1 = field name 1 -- Var 2 line 1 = value
Line 2 from Var 1 = field name 2 -- Var 2 Line 2 = value
repeat with x=1 to the number of flds of grp "flds_grp"
add 1 to counter
put the short name of fld x & cr after fldNames
end repeat
set the itemdel to comma
put 0 into myCounter
repeat for each line f in fldNames
repeat for each line t in tvalues
end repeat
end repeat
You have a variable counterbut it seems that you don't need this variable.
To set the text of field f to the value of variable t, use the following syntax:
set the text of field f to t
The right method have be answered from Sparkout in the Livecode forums.
repeat with x=1 to the number of flds of group "grp-A"
put line x of tValues into field x of group "grp-A"
end repeat

Resources