Picker Wheel Values vs Index

Hi again Forum people.  Quick question

I have a picker wheel with two columns that have specific values and I want to then pass the specific index number for the chosen item in the list rather than its value?

My issue is that I am parsing an HTML URL and it needs the two variables for x and x

blah.blah/graph_export/graphs/graph_x_x.png

The first column on the picker seems to pass the index number rather than its value but the issue is the second column.  It seems to pass the value rather than the index number.

Example

Column1 Value   Index

Temps                  1        

Electric Usage      2

Column2 Value    Index

1hour                     1

2hour                     2

3hour                     3

So the way its currently working if I wanted to display the ‘Temps’ for the last ‘3hours’ would parse the URL as

blah.blah/graph_export/graphs/graph_1_3hour.png

whereas I want to parse

blah.blah/graph_export/graphs/graph_1_3.png

Thanks in anticipation

Si

Here’s the code

[lua]

local storyboard = require “storyboard”
local scene = storyboard.newScene()
local widget = require “widget”
local url = require(“socket.url”)

function scene:createScene( event )

    local loading
    local view = self.view
    local graph
    local newGraphbtn = { x = 0, y = 0, width = 150, height = 40 }
    – Position graph under the newGraphBtn
    local graphBounds = { x = 0,
                          y = newGraphbtn.y + newGraphbtn.height,
                          width = display.viewableContentWidth,
                          height = display.viewableContentHeight -
                          newGraphbtn.height - widget.theme.segmentedControl.height }

    – Show something whilst network traffic is happening.
    local loading = display.newGroup()
    local r = display.newRect(graphBounds.x, graphBounds.y, graphBounds.width, graphBounds.height)
    r:setFillColor(128,128,128)    
    loading:insert®
    local spinner = widget.newSpinner{left = display.contentCenterX - widget.theme.spinner.width/2,
                                      top = display.contentCenterY - widget.theme.spinner.height/2
                                         }    
    loading:insert(spinner)        
    loading.isVisible = false
    view:insert(loading)
    
    local function graphload(datastream, pickerGroup)
        loading.isVisible = true
        spinner:start()
        local file = system.pathForFile( “CactiChart.png”, system.TemporaryDirectory )
        os.remove( file )
        local datastreamNumber = datastream[1].index

local httpaddress= “x.x.x.x/cacti/graph_”…datastreamNumber…"_"…datastream[2].index…".png"

        display.loadRemoteImage( httpaddress, “GET”,
                            function(event)
                            spinner:stop()
                            loading.isVisible = false
                            if event.isError == false then
                                pickerGroup.isVisible = false                                        
                                graph = event.target
                                view:insert(graph)                                        
                                end
                            end,
                            “CactiChart.png”, system.TemporaryDirectory,
                            graphBounds.x, graphBounds.y )
    end

    local function pickerWheelPopup()
    
        local grp = display.newGroup()
        local columnData =     {
                               {
                                  align = “left”,
                                  startIndex = 1,
                                  width=180,
                                  labels =
{
            “Memory Usage”, “Load Average”, “Logged In Users”,
            “Processes”, “Ping1”, “Ping2”, “Ping3”, “LOS”,
            “CRC”, “ES”, “FEC”, “Line Attenuation”,
            “DSL Speed”, “LOF”, “SES”, “DSL SNR”,
            “”, “”, “”, “”, “”, “”, “Electricity Usage”, “1Wire”, “Traffic”,
            “”, “”, “”, “Temp”
},
                               },
                               {
                                  align = “left”,
                                  startIndex = 5,
                                  width=display.contentWidth-180,
                                  labels =
    {
         “Daily”, “Weekly”, “Monthly”, “Yearly”, “4hrs”
    },
                            },
                    }

        myPicker = widget.newPickerWheel
        {
            left = 0, top = 0,    
            font = native.systemFontBold,
            fontSize = 17,
            columns = columnData
        }        
        grp:insert(myPicker)
        
        local btnWidth = 100
        selectButton = widget.newButton
            {
                left = display.contentCenterX-btnWidth/2, top = myPicker.height,
                width= btnWidth, height=40,        
                font=“Arial”, fontSize=18,
                label=“Select”, labelAlign=“center”,
                labelColor =  { default = {0,0,0}, over = {255,255,255} },
                onPress = function()
                            grp.isVisible = false
                            graphload(myPicker:getValues(), grp)
                        end
            }
        grp:insert(selectButton)
        grp.isVisible = false – we initialize hidden.
        return grp
    end

    – Build the UI    
    local pickerGroup = pickerWheelPopup()
    view:insert(widget.newButton
            {
                left = newGraphbtn.x, top= newGraphbtn.y,
                width= newGraphbtn.width, height=newGraphbtn.height,        
                font=“Arial”, fontSize=18,
                label=“New Graph”, labelAlign=“center”,
                labelColor =  { default = {0,0,0}, over = {255,255,255} },
                onEvent = function()
                            – If there was a graph being displayed remove it.
                            if graph then
                                view:remove(graph)
                                graph = nil
                            end
                            pickerGroup.isVisible = true
                          end
            }
    )    
    view:insert(pickerGroup)
end

scene:addEventListener( “createScene”, scene )

return scene

[/lua]
 

Hello Si,

Can I see your code for setting up this picker wheel?

Best regards,

Brent

Brent, I managed to figure it out!

I was using sending the .value rather than the .index

On this line

graph_"…datastreamNumber…"_"…datastream[2].index…".png"

To be honest I am just trying to adapt this code to show my Cacti Graphs.

You may also notice in the code that there are gaps in the 1st columns range of values

{
“Memory Usage”, “Load Average”, “Logged In Users”,
“Processes”, “Ping1”, “Ping2”, “Ping3”, “LOS”,
“CRC”, “ES”, “FEC”, “Line Attenuation”,
“DSL Speed”, “LOF”, “SES”, “DSL SNR”,
“”, “”, “”, “”, “”, “”, “Electricity Usage”, “1Wire”, “Traffic”,
“”, “”, “”, “Temp”
},

This is due to my Cacti graphs having non sequential ID’s after value 17, so I had to pad out the values so that the index number corresponded correctly to values 23,24,25 and 29

I’m sure there is there a way of creating the list without the gaps but just unsure how to.

I also need to re-size and center the .png files on import

Brent really appreciate any help as I am only just new to all this app building stuff.

Si

Hi Si,

If you iterate (via pairs) through the table gathered by the picker wheel “:getValues” function, you’ll notice that it has both an “index” and “value” property. So, just use the index property and you’ll be all set. :slight_smile:

[lua]

local values = pickerWheel:getValues()

for i,v in pairs( values[1] ) do print(i,v) end

[/lua]

This is, of course, for the first column as indicated by the [1]. [2] will get the second column, as expected.

Hi again  Brent, I am unsure where in the code that needs to go?

I understand I need to add it twice somewhere to get the values for both columns.

Do I need to replace the line

graphload(myPicker:getValues(), grp)

As you can see I really am new to all this

Hi Si,
Basically, just pull the values from the wheel into a variable. The logical place is where you indicated… on the button press. So, it might look like this:

[lua]
local values = myPicker:getValues()

local column1Index = values[1].index
print( column1Index )

local column2Index = values[2].index
print( column2Index )
[/lua]

Yep that works okay thank you Brent.  Another quick question.  Is there a way of manually assigning index numbers to values?

As above I have the picker wheel showing the values below and when one is picked it correctly parses the index number

{
“Memory Usage”, “Load Average”, “Logged In Users”,
“Processes”, “Ping1”, “Ping2”, “Ping3”, “LOS”,
“CRC”, “ES”, “FEC”, “Line Attenuation”,
“DSL Speed”, “LOF”, “SES”, “DSL SNR”,
“”, “”, “”, “”, “”, “”, “Electricity Usage”, “1Wire”, “Traffic”,
“”, “”, “”, “Temp”
},

However I am having to insert blank values so that the index corresponds to the correct graph i.e ‘Memory Usage’ is graph 1 etc

But you may notice that there are blank inserted values “”, “”, “”, etc so that the index for ‘Electricity Usage’ is correctly 23 and not 17.

So is there a way of not having to pad out the values in order to get the correct index.  Some sort of way of doing this

if value[1] = “Electricity Usage” then [1]index = 23

This is due to my Cacti graph ID’s not being completely sequential.  I cannot change the ID’s without deleting all the RRD graph data on my Cacti installation and I don’t want to do that.

I also need to shrink the imported graph to fit horizontally within the app window.

Thanks again

Si

Hello Si,

I think in this case, a good approach would be to create some kind of relational “index” to associate the picker’s internal index values with the modified index values you need. For example:

[lua]

local indexRelation = {

   3, 6, 7, 16  --etc.

}

print(indexRelation[1])

[/lua]

So basically, you compare the index value from the picker to this table, and you know that index 1 is actually 3 for your purposes, index 2 is actually 6, and so forth. This table can then be modified and expanded outward as necessary.

Brent

Hi again Brent.  I have been trying and trying to attempt to re-index using your technique above but have come to a brick wall as I am not sure how to actually code in order to get the first value and reindex against the second set of values.

Would it be a for loop?

Si

Hi Si,

If you examine line 5 in my example, you’ll see a standard Lua table, where each item is an index in that table. Unlike some languages, the first item is actually #1 versus #0.

So given another example:

[lua]

local indexRelation = {

   “cat”, “dog”, “bird”

}

[/lua]

If you say this:

[lua]

print( indexRelation[2] )

[/lua]

It will print “dog” (without quotes) in the Terminal. But in your case, since you don’t have just one value (2) every time, you should use the value coming from the picker wheel, like this:

[lua]

print( indexRelation[values[1].index] )  --compare column 1 against indexRelation table

–or

print( indexRelation[values[2].index] )  --compare column 2 against indexRelation table

[/lua]

Brent, thank you for that extra bit of info above.  It pointed me in the right direction and I have managed to get it working as expected.

Apologies for the naive questioning.  I’ve only recently started to code and really just need to know more of the basics.  What I have realised though is that I need to read more and ask less ; )

Thanks again Brent

Si

Hello Si,

Can I see your code for setting up this picker wheel?

Best regards,

Brent

Brent, I managed to figure it out!

I was using sending the .value rather than the .index

On this line

graph_"…datastreamNumber…"_"…datastream[2].index…".png"

To be honest I am just trying to adapt this code to show my Cacti Graphs.

You may also notice in the code that there are gaps in the 1st columns range of values

{
“Memory Usage”, “Load Average”, “Logged In Users”,
“Processes”, “Ping1”, “Ping2”, “Ping3”, “LOS”,
“CRC”, “ES”, “FEC”, “Line Attenuation”,
“DSL Speed”, “LOF”, “SES”, “DSL SNR”,
“”, “”, “”, “”, “”, “”, “Electricity Usage”, “1Wire”, “Traffic”,
“”, “”, “”, “Temp”
},

This is due to my Cacti graphs having non sequential ID’s after value 17, so I had to pad out the values so that the index number corresponded correctly to values 23,24,25 and 29

I’m sure there is there a way of creating the list without the gaps but just unsure how to.

I also need to re-size and center the .png files on import

Brent really appreciate any help as I am only just new to all this app building stuff.

Si

Hi Si,

If you iterate (via pairs) through the table gathered by the picker wheel “:getValues” function, you’ll notice that it has both an “index” and “value” property. So, just use the index property and you’ll be all set. :slight_smile:

[lua]

local values = pickerWheel:getValues()

for i,v in pairs( values[1] ) do print(i,v) end

[/lua]

This is, of course, for the first column as indicated by the [1]. [2] will get the second column, as expected.

Hi again  Brent, I am unsure where in the code that needs to go?

I understand I need to add it twice somewhere to get the values for both columns.

Do I need to replace the line

graphload(myPicker:getValues(), grp)

As you can see I really am new to all this

Hi Si,
Basically, just pull the values from the wheel into a variable. The logical place is where you indicated… on the button press. So, it might look like this:

[lua]
local values = myPicker:getValues()

local column1Index = values[1].index
print( column1Index )

local column2Index = values[2].index
print( column2Index )
[/lua]

Yep that works okay thank you Brent.  Another quick question.  Is there a way of manually assigning index numbers to values?

As above I have the picker wheel showing the values below and when one is picked it correctly parses the index number

{
“Memory Usage”, “Load Average”, “Logged In Users”,
“Processes”, “Ping1”, “Ping2”, “Ping3”, “LOS”,
“CRC”, “ES”, “FEC”, “Line Attenuation”,
“DSL Speed”, “LOF”, “SES”, “DSL SNR”,
“”, “”, “”, “”, “”, “”, “Electricity Usage”, “1Wire”, “Traffic”,
“”, “”, “”, “Temp”
},

However I am having to insert blank values so that the index corresponds to the correct graph i.e ‘Memory Usage’ is graph 1 etc

But you may notice that there are blank inserted values “”, “”, “”, etc so that the index for ‘Electricity Usage’ is correctly 23 and not 17.

So is there a way of not having to pad out the values in order to get the correct index.  Some sort of way of doing this

if value[1] = “Electricity Usage” then [1]index = 23

This is due to my Cacti graph ID’s not being completely sequential.  I cannot change the ID’s without deleting all the RRD graph data on my Cacti installation and I don’t want to do that.

I also need to shrink the imported graph to fit horizontally within the app window.

Thanks again

Si

Hello Si,

I think in this case, a good approach would be to create some kind of relational “index” to associate the picker’s internal index values with the modified index values you need. For example:

[lua]

local indexRelation = {

   3, 6, 7, 16  --etc.

}

print(indexRelation[1])

[/lua]

So basically, you compare the index value from the picker to this table, and you know that index 1 is actually 3 for your purposes, index 2 is actually 6, and so forth. This table can then be modified and expanded outward as necessary.

Brent

Hi again Brent.  I have been trying and trying to attempt to re-index using your technique above but have come to a brick wall as I am not sure how to actually code in order to get the first value and reindex against the second set of values.

Would it be a for loop?

Si

Hi Si,

If you examine line 5 in my example, you’ll see a standard Lua table, where each item is an index in that table. Unlike some languages, the first item is actually #1 versus #0.

So given another example:

[lua]

local indexRelation = {

   “cat”, “dog”, “bird”

}

[/lua]

If you say this:

[lua]

print( indexRelation[2] )

[/lua]

It will print “dog” (without quotes) in the Terminal. But in your case, since you don’t have just one value (2) every time, you should use the value coming from the picker wheel, like this:

[lua]

print( indexRelation[values[1].index] )  --compare column 1 against indexRelation table

–or

print( indexRelation[values[2].index] )  --compare column 2 against indexRelation table

[/lua]