Tips on passing data structures from Go to QML to be used by JS functions (e.g. Chart.js)

143 views
Skip to first unread message

Tim Pedersen

unread,
Feb 11, 2015, 6:57:17 AM2/11/15
to go-...@googlegroups.com
Hi all,

(Disclosure: QML & go-qml n00b!  :) ) 

I'd appreciate a bit of guidance on how to pass data structures from Go to QML so that they can be used by QML-side JavaScript code. 

I'm using QMLChartJS (which wraps Charts.js) (see https://github.com/shuirna/QMLChartJs). I have the demo working OK via go-qml.

The Chart.js code expects JavaScript objects as graph data arguments, e.g. for a pie chart you would use a JS object like this:

(NB: JavaScript)
var ChartPieData = [
{
value: 300,
color:"#F7464A",
highlight: "#FF5A5E",
label: "Red"
},
{
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
} ]
Other Chart.js graph types use more complicated objects as graph data parameter.

Any tips about how I should code this up as a data structure in Go, and how I should pass the data into QML (e.g. via Object.SetVar(), Object.Call(), etc) so that the Chart.js JavaScript code can understand it?

I've tried various combinations of Go structs, slices, etc., but these don't seem to be 'understood' by the Chart.js code. I read elsewhere in this group about marshalling/unmarshalling via JSON strings - but is there a better way?

Thanks,

Tim.

Gustavo Niemeyer

unread,
Feb 11, 2015, 7:06:35 AM2/11/15
to Tim Pedersen, Go QML
Hi Tim,

You shouldn't have to marshal it. Just handing off a Go struct value into the QML scene should work fine. See this example for instance:


This is one way to do it, and there are others, all based on just handing off or calling Go methods as necessary.

The only wart to have in mind is the lowercasing of the fields. This is a bit unfortunate, but a requirement for QML to accept those values at all.




--
You received this message because you are subscribed to the Google Groups "go-qml" group.
To unsubscribe from this group and stop receiving emails from it, send an email to go-qml+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages