Web Programming examples

Google Maps,AngularJS

Google Chart by AngularJS table’s row click,display line chart

  • 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">
      <div id="chart_div1"></div>
      <div id="chart_div2"></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','corechart']});

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

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope,$http) {
  $scope.grphdisplay = function (site_name) {
    var url = '../pdo-webana2.php?site_name=' + site_name ;
    $http.get(url).then(function(response) {
      var data1 = new google.visualization.DataTable();
      data1.addColumn('string', 'year/mon');
      data1.addColumn('number', 'page_view');
      for(i = 0 ; i < response.data.length;i++) {
        var year_mon = response.data[i]["year_mon"].substr(2,5);
        data1.addRow([year_mon, parseInt(response.data[i]["page_view"])]);
      };
      var title = site_name + "\'s page_view";
      var options = {
        'title': title,
        'height': 300
      };
      var chart = new google.visualization.LineChart(document.getElementById('chart_div2'));
      chart.draw(data1, options);
    });
  };
  $scope.getDataAsync = function () {
    $http.get('../pdo-webana1.php').then(function(response) {
      var data1 = new google.visualization.DataTable();
      data1.addColumn('string', 'site_name');
      data1.addColumn('number', 'page_view');
      for(i = 0 ; i < response.data.length;i++) {
        data1.addRow([
          response.data[i]["site_name"],
          parseInt(response.data[i]["page_view"])
        ]);
      };
      var options = {
        'title': 'page_view',
        'height':300
      };
      var chart = new google.visualization.Table(document.getElementById('chart_div1'));
      var formatter = new google.visualization.NumberFormat(
        {fractionDigits: 0}
      );
      formatter.format(data1, 1);
      chart.draw(data1, options);
      google.visualization.events.addListener(chart, 'select', function() {
        var row = chart.getSelection()[0].row;
        $scope.grphdisplay(data1.getValue(row, 0))
      });
    });
  };
  $scope.getDataAsync();
});
(pdo-webana1.php)
require("db_info.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 NAMES 'utf8'"
    )
  );
} catch (PDOException $e) {
  exit('データベース接続失敗。'.$e->getMessage());
}

date_default_timezone_set('Asia/Tokyo');
$result = array();

$st = $dbh->prepare('select year_mon,site_name,sum(page_view) as page_view from wp_sample where group by site_name');

$st->execute();
$result = $st->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($result);


(pdo-webana2.php)
require("db_info.php");

$site_name = filter_input(INPUT_GET, 'site_name');
$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 NAMES 'utf8'"
    )
  );
} catch (PDOException $e) {
  exit('データベース接続失敗。'.$e->getMessage());
}

date_default_timezone_set('Asia/Tokyo');
$result = array();

$st = $dbh->prepare('select year_mon, sum(page_view) as page_view from wp_sample where site_name = :site_name group by year_mon order by year_mon');

$st->bindValue(':site_name', $site_name , PDO::PARAM_STR);
$st->execute();
$result = $st->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($result);

Leave a Reply

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