Web Programming examples

Google Maps,AngularJS

Google Chart by AngularJS Server side DB 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: ['corechart']});

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

angular.module('myApp', []).
  controller('MyCtrl', ['$scope','$http',
    function($scope,$http) {
      $http.get('../ng1/json.php').then(function(response) {
        var data1 = new google.visualization.DataTable();
        data1.addColumn('string', 'year/mon');
        data1.addColumn('number', 'avarage');
        for(i = 0 ; i < response.data.length;i++) {
          var ym = response.data[i]["updateday"].substr(0,4) + "/" + response.data[i]["updateday"].substr(5,2);
          data1.addRow([ym, parseFloat(response.data[i]["sum(price)\/count(price)"])]);
        };
        var options = {
          'title':'Sample Title',
          'height':400
        };
        var chart = new google.visualization.LineChart(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) from products GROUP BY updateday");
echo json_encode($st->fetchAll(PDO::FETCH_ASSOC));

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.