开发者

Inserting JSON in DataTable in Google Visualisation

开发者 https://www.devze.com 2023-01-19 05:47 出处:网络
I read that JSON can be inserted into a datatable. However, its done in JS on the HTML page (if memory serves right). I am using the GWT-

I read that JSON can be inserted into a datatable. However, its done in JS on the HTML page (if memory serves right). I am using the GWT- visualisation driver as given here, and the only methods to add a row are not designed with JSON in mind. Is there any work-able solution? I am trying开发者_StackOverflow社区 to make a Stacked Column Chart. Thanks.


I also struggled with this using geomap and data table combined. In my workaround I used the evalJSON() in prototype javascript framework to make the json string an object.

var country_obj = country_data.evalJSON(true);

Then I loop over the object's length :

var data = new google.visualization.DataTable();
data.addRows(country_obj.country.length);
data.addColumn('string', 'Country');
data.addColumn('number', map_context); 

var j = country_obj.country.length;
for ( i = 0; i < j; i++ )
{
    var num = new Number(country_obj.country[i].geo_mstat_reads);

    data.setValue(i, 0, country_obj.country[i].country_name);
    data.setValue(i, 1, num.valueOf());
}

// and then the table.draw() with the data

This worked very well for me because it scales to the exact size of the dataset you are trying to show.

Also you can do a whole whack of operations on an object which makes your life and coding easier to read and write.

I hope this helps you with your problem.


same pb, and don't know if it's feasible :

public native DataTable createDataTable(String obj) /*-{
    console.log("create Datatable :" + obj);
    var dt = new $wnd.google.visualization.DataTable(obj);
    console.log("Datatable created");
    return dt;
}-*/;

This create an empty dataTable whereas the JSON input is well formatted...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号