Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draw google charts with JSON

How can I retrieve and use a dataset for google charts if it was a separate JSON file?? I tried jQuery getJSON but couldn't get it worked.. Google Viz should use the JSON to draw a bar chart Is there a native google API way? or can I find a way using jQuery and how? Thanks

      // 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);
}
like image 677
neverlate Avatar asked Aug 15 '11 17:08

neverlate


People also ask

Is Google Charts API free?

Google chart tools are powerful, simple to use, and free. Try out our rich gallery of interactive charts and data tools.

Can you draw on a Google chart?

Every chart supports a draw() method that takes two values: a DataTable (or a DataView ) object that holds your data, and an optional chart options object.

Is Google Charts deprecated?

This article relies excessively on references to primary sources. Please improve this by adding secondary or tertiary sources. The Google Chart API is an interactive Web service (now deprecated) that creates graphical charts from user-supplied data.


1 Answers

new google.visualization.DataTable(json) works.

Look the output of dataTable.toJSON() for the correct structure to use.

So, if you have a getjson.php script on your server that returns correctly formatted json, you could do that:

$.getJSON('/getjson.php', function(json) {
    var dataTable = new google.visualization.DataTable(json);
});
like image 110
Arnaud Le Blanc Avatar answered Oct 29 '22 11:10

Arnaud Le Blanc