2011-08-15 21 views
10

¿Cómo puedo recuperar y usar un conjunto de datos para los gráficos de Google si fuera un archivo JSON separado? Intenté jQuery getJSON pero no pude hacerlo funcionar ... Google Viz debería usar el JSON para dibujar un gráfico de barras ¿Hay una forma nativa de API de Google? o puedo encontrar una forma de usar jQuery y cómo? Gracias dibujar gráficos de Google con JSON

 // Load the Visualization API and the piechart package. 
    google.load('visualization', '1.0', {'packages':['corechart']}); 

    // Set a callback to run when the Google Visualization API is loaded. 
    google.setOnLoadCallback(drawChart); 

    // Callback that creates and populates a data table, 
    // instantiates the pie chart, passes in the data and 
    // draws it. 
    function drawChart() { 

     // Create the data table. 
     var data = new google.visualization.DataTable(); 
     data.addColumn('string', 'Products'); 
     data.addColumn('number', 'Automated'); 
     data.addRows([ 
     ['Product 1', 85], 
     ['Product 2', 75], 
     ['Product 3', 90], 
     ['Product 4', 40], 
     ['Product 5', 40] 
     ]); 

     // Set chart options 
     var pie_options = {'title':'How Much Automated our Products are?', 
        'width':520,'height':300 
        }; 
     var bar_options ={'width': 620, 'height': 300, 
         'title': 'Products', 
         'hAxis': {'title': '% Automated', 'titleTextStyle': {'color': 'red', 'fontSize': 16}} 
         } 
     // Instantiate and draw our chart, passing in some options. 
     var chart = new google.visualization.PieChart(document.getElementById('piechart_div')); 
     chart.draw(data, pie_options); 

    var chart = new google.visualization.ColumnChart(document.getElementById('barchart_div')); 
    chart.draw(data, bar_options); 
} 
+0

¿Le publicar su código tanto como sea posible para que podamos ayudar con eficacia? –

+0

@Taesung Shin seguro – neverlate

Respuesta

17

new google.visualization.DataTable(json) obras.

Busque en la salida de dataTable.toJSON() la estructura correcta para usar.

Por lo tanto, si usted tiene un script getjson.php en su servidor que devuelve JSON correctamente formateado, se puede hacer eso:

$.getJSON('/getjson.php', function(json) { 
    var dataTable = new google.visualization.DataTable(json); 
}); 
+0

Gracias ,, todavía no tengo una secuencia de comandos php, pero puedo intentar esto llamando a un archivo json válido – neverlate

Cuestiones relacionadas