Page 1 of 1

Array with mixed types

Posted: Fri May 10, 2019 1:37 pm
by Dirk Geppert
How can I create such an array with spiderbasic? I need to transfer values from a list to the array..

Code: Select all
Define ChartData

! var v_chartdata= [
!   ['Task', 'Hours per Day'],
!   ['Work',     11],
!   ['Eat',      2],
!   ['Commute',  2],
!   ['Watch TV', 2],
!   ['Sleep',    7]
! ];


Anyone canhelp?

Re: Array with mixed types

Posted: Sat May 11, 2019 8:00 am
by the.weavster
Code: Select all
If CreateJSON(0)
  sub1 = SetJSONArray(JSONValue(0))
 
  sub2 = SetJSONArray(AddJSONElement(sub1))
  SetJSONString(AddJSONElement(sub2),"Task")
  SetJSONString(AddJSONElement(sub2),"Hours per Day")
 
  sub2 = SetJSONArray(AddJSONElement(sub1))
  SetJSONString(AddJSONElement(sub2),"Work")
  SetJSONInteger(AddJSONElement(sub2),11)
 
  ;etc...
 
  Debug ComposeJSON(0)
EndIf


When it comes to JSON I'd stick to inline JS :D

Re: Array with mixed types

Posted: Mon May 13, 2019 3:11 pm
by Dirk Geppert
Thx the.weavster for your help.

But JSON seems not the right type. I get the error then: the table has no columns.

This is a part of GoogleChartDemo.sb from https://github.com/spiderbytes/GoogleChart
Code: Select all

[..]
     Protected ChartData
    
     ! v_chartdata = [
     !   ['Topping', 'Slices'],
     !   ['Mushrooms', 3],
     !   ['Onions', 1],
     !   ['Olives', 1],
     !   ['Zucchini', 1],
     !   ['Pepperoni', 2]
     ! ];
    
     GoogleChart::SetChartGadgetAttribute(#PieChart, GoogleChart::#ChartData, ChartData) 


How can I fill ChartData with SpideBasic Commands, without InlineJS..?

Re: Array with mixed types

Posted: Tue May 14, 2019 2:54 pm
by the.weavster
Hi Dirk,

Seeing as JSON is a way of serializing/deserializing JS objects I would guess we have a JS object under the hood even though the command names refer to JSON. It's the ComposeJSON() command that does the serializing (similar to JSON.stringify() in JS). Quite how you grab hold of the object in SB though I'm not quite sure.

You could try this:
Code: Select all
jsRef = CreateJSON(#PB_Any)
If jsRef
  sub1 = SetJSONArray(JSONValue(jsRef))
 
  sub2 = SetJSONArray(AddJSONElement(sub1))
  SetJSONString(AddJSONElement(sub2),"Task")
  SetJSONString(AddJSONElement(sub2),"Hours per Day")
 
  sub2 = SetJSONArray(AddJSONElement(sub1))
  SetJSONString(AddJSONElement(sub2),"Work")
  SetJSONInteger(AddJSONElement(sub2),11)
 
  ;etc...
 
  Debug ComposeJSON(jsRef)
EndIf

And then try passing either jsRef, JSONValue(jsRef) or v_jsref to Google charts and see what happens.

Re: Array with mixed types

Posted: Thu May 16, 2019 11:00 am
by Dirk Geppert
I took a closer look at the include and noticed that the data is explicitly passed as an array, although Google Chart can also do this as a JSON.
But I didn't find a solution how to pass the Values as JSON. Then I found another function addRows(). With this one, I'm able to fill the "ChartDataTable"array now combined with SpiderBasic.

Code: Select all
Procedure AddToDataTable( ChartDataTable, txt.s, value.i )
   ! v_chartdatatable.addRows([[v_txt, v_value]]);
EndProcedure