Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert json string to google.visualization.DataTable?

I am getting a json string from the response. How can I create a data table from that?

e.g.

var jasonString = "..........";

var data = new google.visualization.DataTable(jasonString);
like image 388
Njax3SmmM2x2a0Zf7Hpd Avatar asked Jun 03 '13 11:06

Njax3SmmM2x2a0Zf7Hpd


People also ask

Why do I see a JavaScript literal or JSON version of DataTable?

In some cases, you might see a JavaScript literal or JSON version of a DataTable used, for instance when data is sent over the Internet by a Chart Tools Datasource, or as a possible input value for a ChartWrapper. A DataTable is used to create the original data table.

How do I display a JSON response in a DataTable?

This is only for a very special case of JSON string. If you have a random JSON object and you'd like to show it in a DataTable, then convert to an array first. See stackoverflow.com/questions/20881213/… Show activity on this post. According to this page it says you can just put the JSON response directly into google.visualization.DataTable

What happens if I change the value in a DataTable in visualization?

If you change values in a DataTable after it is passed into a visualization's draw () method, the changes will not immediately change the chart. You must call draw () again to reflect any changes. Note: Google Charts does not perform any validation on datatables.

What is a DataTable object?

The DataTable object is used to hold the data passed into a visualization. A DataTable is a basic two-dimensional table. All data in each column must have the same data type.


2 Answers

You can use the function arrayToDataTable

var jsonString = ".........."; // json string of array
var array  = JSON.parse(jsonString);

var dataTableData = google.visualization.arrayToDataTable(array);

// use dataTableData to build dataTable
like image 137
Diode Avatar answered Sep 20 '22 18:09

Diode


According to this page it says you can just put the JSON response directly into google.visualization.DataTable

var data = new google.visualization.DataTable(jsonData);
like image 36
Jeff Baldwin Avatar answered Sep 21 '22 18:09

Jeff Baldwin