Skip to content Skip to sidebar Skip to footer

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(jasonStri

Solution 1:

You can use the function arrayToDataTable

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

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

// use dataTableData to build dataTable

Solution 2:

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

Solution 3:

You can do:

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

functiondrawChart() {
    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));
        }
    });
}

Post a Comment for "How Can I Convert Json String To Google.visualization.datatable?"