Array with mixed types

Just starting out? Need help? Post your questions and find answers here.
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Array with mixed types

Post 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?
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

Re: Array with mixed types

Post 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
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Re: Array with mixed types

Post 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..?
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

Re: Array with mixed types

Post 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.
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Re: Array with mixed types

Post 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
Post Reply