Web Programming examples

Google Maps,AngularJS

Google Chart by AngularJS dual y-axis

  • 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('../json1.php').then(function(response) {
        var data1 = new google.visualization.DataTable();
        data1.addColumn('string', 'date');
        data1.addColumn('number', 'avarage');
        data1.addColumn('number', 'count');
        for(i = 0 ; i < response.data.length;i++) {
          var ym = response.data[i]["updateday"].substr(2,2) + "/" + response.data[i]["updateday"].substr(5,2);
          data1.addRow([
            ym,
            parseFloat(response.data[i]["sum(price)\/count(price)"]),
            parseInt(response.data[i]["count(price)"])
          ]);
        };
        var options = {
           title: 'Sample Title',
           height: 300,
           bar: { groupWidth: 10 },
           hAxis: {title: "year/mon"},
           series: {
             0: {targetAxisIndex:0,type: "line",pointSize: 5},
             1: {targetAxisIndex:1,type: "bars",dataOpacity: 0.2}
           },
           vAxes: [
             { title: 'avg' },
             { title: 'count'}
           ]
        };
        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),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 *