Roles certaintity and populating DataTable with JSON

349 views
Skip to first unread message

Bassa Safa

unread,
Sep 6, 2012, 10:37:12 AM9/6/12
to google-visua...@googlegroups.com
I get a All series on a given axis must be of the same data type× when adding Roles Certaintity. Probably something really simple that I have missed... but 4 hours of trying with different possibilities im lost. It works when I populate DataTable with "regular" data as in the playground. But when populating with JSON it fails.

As a related question? How do one debug "google chart" code?

regards
bassa


function drawVisualization() {
  // Create and populate the data table.

  
var grundJsonDataResultatrakning = {cols:[
                                          {type:'number',role:'domain'},
                                          {type:'number',role:'data'},
                                          {type:'boolean',role:'certainty'}],
                                    rows:[
                                          {c:[{v:1},{v:7},{v:false}]},
                                          {c:[{v:2},{v:8},{v:false}]}]};
  var dataResultatrakning = new google.visualization.DataTable(grundJsonDataResultatrakning);
  
  // Create and draw the visualization.
  new google.visualization.LineChart(document.getElementById('visualization')).
      draw(dataResultatrakning , {curveType: "function",
                  width: 500, height: 400,
                  vAxis: {maxValue: 10}}
          );
}

asgallant

unread,
Sep 6, 2012, 11:02:42 AM9/6/12
to google-visua...@googlegroups.com
The correct way to specify column roles in JSON is in the column properties: "p: {role: "certainty"}", so your JSON should look like this:

var grundJsonDataResultatrakning {
    cols[
        {type'number'p{role'domain'}},
        {type'number'p{role'data'}},
        {type'boolean'p{role'certainty'}}

    ],
    rows[
        {c[{v1}{v7}{vfalse}]},
        {c[{v2}{v8}{vfalse}]}
    ]
};

Technically, if you are returning that JSON from an AJAX request, all of the properties and all of the string should be enclosed with double-quotes to make it valid JSON, but it works fine as is for a test case.

asgallant

unread,
Sep 6, 2012, 11:04:59 AM9/6/12
to google-visua...@googlegroups.com
As far as debugging goes, there isn't any "good" way that I have found, other than familiarity with the API.

Bassa Safa

unread,
Sep 6, 2012, 11:46:08 AM9/6/12
to google-visua...@googlegroups.com
Hurray! =) changing to p {role; 'domain'} did the trick! =)

Also thank you for your point about correct JSON syntax. Im using GSON to build my JSON string up so it does actually does print double-quotations... it was me who removed them as a part of testing if that was wrong. And I just forgot to readd them before posting example here. But it was the p {role; 'domain'} that made it work! Now I also see that there were a special part in the documentation for Roles that handled JSON literatal notation.

Im as happy as a child on xmas!
regards
bassa

asgallant

unread,
Sep 6, 2012, 12:00:21 PM9/6/12
to google-visua...@googlegroups.com
The way you structured it is fine if you're working strictly in javascript, I was just pointing out that it wouldn't parse as proper JSON if you stringified it.
Reply all
Reply to author
Forward
0 new messages