Web Programming examples

Google Maps,AngularJS

Google Chart by AngularJS table’s row click,display modal

  • AngularJS : 1.6.5
  • Bootstrap : 3.x
  • UI Bootstrap : 2.5.0
  <head>
//
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  </head>
  <body>
  <div ng-controller="MyCtrl">
    <script type="text/ng-template" id="myModalContent.html">
    <div class="modal-header">
      <h3 class="modal-title">{{ title }}'s Detail data</h3>
    </div>
    <div class="modal-body">
      <div ng-repeat="item in items">
        <div class="radio">
          <label>
            <input type="radio" ng-model="$parent.selected.item" value="{{item}}" required />{{item}}
          </label>
        </div>
      </div>
    </div>
    <div class="modal-footer">
      <button class="btn btn-primary" ng-click="ok()">OK</button>
      <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
    </div>
    </script>
    <div id="chart_div"></div>
      <button ng-click="getDataAsync()" class="btn btn-xs btn-primary">Back to Site list</button>
    </div>
  </div>
  </body>
google.charts.load('current', {packages: ['table','corechart']});

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

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

myApp.controller('MyCtrl', function($scope,$http,$uibModal,$log) {
  $scope.items = ['Line Chart', 'Detail Table'];
  $scope.open = function (site_name) {
    var modalInstance = $uibModal.open({
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      size: 'sm',
      resolve: {
        items: function () {
          return $scope.items;
        },
        title: function () {
          return site_name;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      switch (selectedItem) {
        case 'Line Chart':
          $scope.grphDisplay(site_name);
          break;
        case 'Detail Table':
          $scope.getDetail(site_name);
          break;
      }
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };
  $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_div'));
      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_div'));
      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.open(data1.getValue(row, 0))
      });
    });
  };
  $scope.getDetail = function (site_name) {
    var url = '../pdo-webana3.php?site_name=' + site_name ;
    $http.get(url).then(function(response) {
      var data1 = new google.visualization.DataTable();
      data1.addColumn('string', 'page_name');
      data1.addColumn('number', 'page_view');
      for(i = 0 ; i < response.data.length;i++) {
        data1.addRow([
          response.data[i]["page_name"],
          parseInt(response.data[i]["page_view"])
        ]);
      };
      var options = {
        'title':'Sample Title',
        'height':300
      };
      var chart = new google.visualization.Table(document.getElementById('chart_div'));
      var formatter = new google.visualization.NumberFormat(
        {fractionDigits: 0}
      );
      formatter.format(data1, 1);
      chart.draw(data1, options);
    });
  };
  $scope.getDataAsync();
});

myApp.controller("ModalInstanceCtrl", function($scope, $uibModalInstance, items, title) {
  $scope.title = title;
  $scope.items = items;
  $scope.selected = {
    item: $scope.items[0]
  };
  $scope.ok = function () {
    $uibModalInstance.close($scope.selected.item);
  };

  $scope.cancel = function () {
    $uibModalInstance.dismiss('cancel');
  };
});
(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);

(pdp-webana3.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 page_name, page_view from eng_webana where site_name = :site_name order by page_name');

$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 *

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