Javascript AngularJS和Google图表(尝试动态更改图形数据)
我正在做一个政治测验,根据你问题的答案,图表中点的位置会发生变化 正如您在示例中看到的: 有两个变量(“经济变量”和“社会变量”),其值根据用户在测验中给出的答案而变化。这些值保存在两个作用域中:$scope.contadoreco和$scope.contadorsoc 如果我想让点在图上的位置随答案的变化而变化,我该怎么办 代码如下:Javascript AngularJS和Google图表(尝试动态更改图形数据),javascript,angularjs,scope,Javascript,Angularjs,Scope,我正在做一个政治测验,根据你问题的答案,图表中点的位置会发生变化 正如您在示例中看到的: 有两个变量(“经济变量”和“社会变量”),其值根据用户在测验中给出的答案而变化。这些值保存在两个作用域中:$scope.contadoreco和$scope.contadorsoc 如果我想让点在图上的位置随答案的变化而变化,我该怎么办 代码如下: var aritmetica = angular.module('aritmetica', ['ngAnimate']); aritmetica.facto
var aritmetica = angular.module('aritmetica', ['ngAnimate']);
aritmetica.factory("contadoreco",function(){
return {};
});
aritmetica.factory("contadorsoc",function(){
return {};
});
aritmetica.controller('encuestaController', ['$scope', 'contadoreco', 'contadorsoc', function($scope,contadoreco,contadorsoc) {
$scope.contadoreco= 0;
$scope.contadorsoc= 0;
$scope.pageClass = 'encuesta';
$scope.sumareco = function(cantidad) { $scope.contadoreco += cantidad};
$scope.restareco = function(cantidad) { $scope.contadoreco -= cantidad};
$scope.sumarsoc = function(cantidad) { $scope.contadorsoc += cantidad};
$scope.restarsoc = function(cantidad) { $scope.contadorsoc -= cantidad};
}]);
aritmetica.controller('MyCtrl', ['$scope', 'contadoreco', 'contadorsoc', function($scope,contadoreco,contadorsoc) {
$scope.name = "Name";
}]);
aritmetica.directive('chart', function() {
return {
restrict: 'A',
link: function($scope, $elm, $attr) {
// Create the data table.
var data = google.visualization.arrayToDataTable([
['Económico', 'Social'],
[$scope.contadoreco, $scope.contadorsoc]]);
var options = {
title: 'Age of sugar maples vs. trunk diameter, in inches',
hAxis: {title: 'Económico', minValue: -130, maxValue: 130},
vAxis: {title: 'Social', minValue: -130, maxValue: 130},
legend: 'none',
width: 400,
height:400,
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.ScatterChart($elm[0]);
chart.draw(data, options);
}
}
});
google.setOnLoadCallback(function() {
angular.bootstrap(document.body, ['aritmetica']);
});
google.load('visualization', '1', {packages: ['corechart']});
在您的范围内的指令上添加$watch,以监视需要更新的变量。然后将更改应用到google图表:非常感谢,尽管我只能用CSS制作图表,用AngularJS操作变量