如何将json字符串转换为google.visualization.DataTable?

时间:2022-09-16 12:54:04

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

我从响应中获取了一个json字符串。如何从中创建数据表?

e.g.

例如

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

var data = new google.visualization.DataTable(jasonString);

3 个解决方案

#1


12  

You can use the function arrayToDataTable

您可以使用函数arrayToDataTable

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

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

// use dataTableData to build dataTable

#2


2  

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

根据这个页面,它说你可以直接把JSON响应放到google.visualization.DataTable中

var data = new google.visualization.DataTable(jsonData);

#3


0  

You can do:

你可以做:

google.charts.load('current', { 'packages': ['line'] });
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
    var json = $.ajax({
        url: "GetFaturamentoMes",
        dataType: "json",
        success: function (jsonData) {
            var data = new google.visualization.DataTable();
            data.addColumn('number', 'Mês');
            data.addColumn('number', 'Faturamento Por Mês');

            for (var i = 0; i < jsonData.length; i++) {
                mes = jsonData[i].Mes;
                total = jsonData[i].Total;
                data.addRow([mes, total]);
            }
            var options = {
                chart: {
                    title: 'Gráfico de Faturamento Mensal',
                    subtitle: 'Moeda (R$)'
                },
                width: 600,
                height: 300,
                axes: {
                    x: {
                        10: { side: 'top' }
                    }
                }
            };
            var chart = new google.charts.Line(document.getElementById('line_top_x'));
            chart.draw(data, google.charts.Line.convertOptions(options));
        }
    });
}

#1


12  

You can use the function arrayToDataTable

您可以使用函数arrayToDataTable

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

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

// use dataTableData to build dataTable

#2


2  

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

根据这个页面,它说你可以直接把JSON响应放到google.visualization.DataTable中

var data = new google.visualization.DataTable(jsonData);

#3


0  

You can do:

你可以做:

google.charts.load('current', { 'packages': ['line'] });
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
    var json = $.ajax({
        url: "GetFaturamentoMes",
        dataType: "json",
        success: function (jsonData) {
            var data = new google.visualization.DataTable();
            data.addColumn('number', 'Mês');
            data.addColumn('number', 'Faturamento Por Mês');

            for (var i = 0; i < jsonData.length; i++) {
                mes = jsonData[i].Mes;
                total = jsonData[i].Total;
                data.addRow([mes, total]);
            }
            var options = {
                chart: {
                    title: 'Gráfico de Faturamento Mensal',
                    subtitle: 'Moeda (R$)'
                },
                width: 600,
                height: 300,
                axes: {
                    x: {
                        10: { side: 'top' }
                    }
                }
            };
            var chart = new google.charts.Line(document.getElementById('line_top_x'));
            chart.draw(data, google.charts.Line.convertOptions(options));
        }
    });
}