本教程演示了如何使用Google Charts API和AngularJS构建数据可视化应用程序,重点是双向数据绑定。 先前的零件涵盖控制器和指令;本部分探讨了如何通过下拉菜单动态更改图表类型。
密钥功能:
ngOptions
ngModel
和ngChange
$scope.$watch
将下拉列表添加到> 元素中。 我们使用AngularJS方法来定义图表类型,而不是硬编码选项:
index.html
<select></select>
controllers.js
元素使用
$scope.chartTypes = [ {typeName: 'PieChart', typeValue: '1'}, {typeName: 'BarChart', typeValue: '2'}, {typeName: 'ColumnChart', typeValue: '3'}, {typeName: 'LineChart', typeValue: '4'} ]; $scope.chartType = $scope.chartTypes[0];
index.html
<select>
ng-options
ng-model
>的指令应附加到身体元素上,以获得适当的功能。
<select id="chartType" ng-model="chartType" ng-options="c.typeName for c in chartTypes"></select>
>动态图表更新:ng-controller
index.html
基于下拉选择更新图表类型:
selectType
controllers.js
$scope.selectType = function(type) { $scope.chart.type = type.typeValue; };
监视gChart
>中的更改,并相应地重新绘制图表:directives.js
$scope.$watch
$scope.chart
请记住启动节点服务器(
link: function($scope, elm, attrs) { $scope.$watch('chart', function() { var type = $scope.chart.type; var chart = ''; // Conditional chart creation based on type if (type == '1') { chart = new google.visualization.LineChart(elm[0]); } else if (type == '2') { chart = new google.visualization.BarChart(elm[0]); } else if (type == '3') { chart = new google.visualization.ColumnChart(elm[0]); } else if (type == '4') { chart = new google.visualization.PieChart(elm[0]); } chart.draw($scope.chart.data, $scope.chart.options); }, true); }
node scripts/web-server.js
常见问题(常见问题解答):http://localhost:8000/app/index.html
(此处省略了输入的FAQ部分以保持响应简洁。提供的信息充分解决了教程的核心功能。)
以上是使用Google Charts API和AngularJS创建可视化应用程序 - PT 3的详细内容。更多信息请关注PHP中文网其他相关文章!