简体   繁体   中英

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?

eg

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

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

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

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

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

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));
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM