Web Programming examples

Google Maps,AngularJS

Google Chart by AngularJS Table(Server Data)

  • AngularJS : 1.6.5
  • Bootstrap : 3.x
  <head>
//
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  </head>
  <body>
    <div ng-controller="MyCtrl" id="chart_div"></div>
    <script type='text/javascript' src='js/angular.min.js'></script>
    <script type="text/javascript" src="js/app.js"></script>
  </body>
google.charts.load('current', {packages: ['table']});

google.charts.setOnLoadCallback(function() {
  angular.bootstrap(document.body, ['myApp']);
});

angular.module('myApp', []).
  controller('MyCtrl', ['$scope','$http',
    function($scope,$http) {
      $http.get('../ng1/json1.php').then(function(response) {
        var data1 = new google.visualization.DataTable();
        data1.addColumn('string', 'year/mon');
        data1.addColumn('number', 'avarage');
        data1.addColumn('number', 'count');
        for(i = 0 ; i < response.data.length;i++) {
          data1.addRow([
            response.data[i]["updateday"],
            parseFloat(response.data[i]["sum(yachin)\/count(yachin)"]),
            parseInt(response.data[i]["count(yachin)"])
          ]);
        };
        var options = {
          'title':'Sample Title',
          'height':300
        };
        var chart = new google.visualization.Table(document.getElementById('chart_div'));
        var formatter = new google.visualization.NumberFormat(
          {fractionDigits: 2}
        );
        formatter.format(data1, 1);
        chart.draw(data1, options);
      });
    }
  ]);
require("dbinfo.php");
$dsn = 'mysql:host=localhost;dbname='.$database.';charset=utf8';

try {
  $dbh = new PDO($dsn, $username, $password,array(PDO::ATTR_EMULATE_PREPARES =>false,PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`"));
} catch (PDOException $e) {
  exit('DB connect error'.$e->getMessage());
}
$st = $dbh->query("select updateday,sum(price)/count(price),count(price)  from products GROUP BY updateday");
echo json_encode($st->fetchAll(PDO::FETCH_ASSOC));

One thought on “Google Chart by AngularJS Table(Server Data)”

  1. I totally understand everything you have stated. Actually, I browsed through your various other articles and I do think that you’re certainly correct. Best wishes with this particular site.

Leave a Reply

Your email address will not be published. Required fields are marked *