martinrusev
5/31/2014 - 12:46 PM

rickshaw-angular.js

 "use strict";
  var app = angular.module('RickshawApp', [])
  .factory('RickshawDataService', function($http){
       return {
         GetData: function() {
               return $http.get('http://192.168.174.1:8080/data.json/?callback=JSON_CALLBACK')
               .then(function(result) {
                   return result.data;
               });
           } // get_data
       }
  })
  .controller('RickshawTimer', function($scope, $timeout){
        $scope.when  = function( booleanExpr, trueValue, falseValue) {
              return booleanExpr ? trueValue : falseValue;
        };

        var isPaused  = false;
        $scope.pause = function() {
            isPaused = true;
        }
        $scope.resume = function() {
            isPaused = false;
            $scope.runCounter();
        }
        $scope.runCounter = function() {
            if (isPaused) return;

            console.log('test');
            $timeout($scope.runCounter, 1000);
        }
        $scope.toggleCounter = function() {
            isPaused = !isPaused;
            $scope.runCounter();
        };

        $scope.isPaused  = function() {
            return isPaused;
        };

        $scope.runCounter();

  })
  .directive('rickshawChart', function($window, $timeout) {
    return {
      restrict: 'EA',
      controller: ['$scope', '$http', 'RickshawDataService',
          function($scope, $http, RickshawDataService) {
            $scope.get_initial_data = function() {
                RickshawDataService.GetData().then(function(data){
                    $scope.data = data
                })
            }
    }],
    link: function(scope, element, attrs) {
        scope.data = [{x: 0, y:0 }];
        scope.get_initial_data();
        var w = angular.element($window);
        var el;

        scope.get_window_width = function () {
          return w[0].innerWidth
        }

        scope.$watch("data", function () {
             scope.render();
         });

       scope.$watch(scope.get_window_width, function () {
            scope.render();
        });

        w.bind('resize', function () {
             scope.$apply();
        });

        scope.render = function() {
          el = element[0]
          el.innerHTML = '';
          var graph = new Rickshaw.Graph( {
            element: el,
            renderer: 'bar',
            width: element.parent()[0].offsetWidth,
            height: el.clientHeight,
            series: [{
                name: 'test',
                color: '#ccc',
                data: scope.data
            }]
          });
              graph.render();

        }; // scope.render

      } // link

    }; // return
  });