我有一点疑问ng-init
,实际上我有一些图表。
- 顶部趋势线
- 下面的饼图。
- 下面的 HTML 表。
在页面加载时我没有显示任何内容,但是一旦我第一次单击特定按钮,我的整个图表和表格就会显示为 http 请求接收到的数据。但之后,如果我单击另一个按钮并执行相同的 http 请求,则它会更新趋势线和表格,但不会更新饼图。我的饼图使用 ng-init 在下面的 div 中呈现,当我测试 ng-init="myFunction(index)" 方法时,它不会在随后的点击中更新。
<form>
<md-input-container>
<label for="myInput">Search Division</label>
<md-icon md-svg-icon="~/Content/myIcons/searchicon/ic_search_black_36px.svg"></md-icon>
<input type="text" id="myInput" ng-model="searchDiv" md-autofocus>
</md-input-container>
<md-content ng-repeat="prod in listofProd | filter: searchDiv">
<md-button class="md-whiteframe-1dp card" ng-click="generateChart(prod)" flex-sm="100" flex-gt-sm="100" flex-gt-md="100">
{{prod}}
</md-button>
</md-content>
</form>
<div layout="row" layout-wrap>
<div flex="25" style="width:1000px;height:300px; border: 1px solid skyblue;" ng-repeat="year in years track by $index" id="piechart{{$index}}" ng-init="myFunction($index)">
</div>
</div>
这是我的 ng-init 函数的 angularjs 代码。
(function () {
'use strict'
angular.module("GAiiNSApp").controller("ProdPerDash", ['$http', '$scope', '$log', function ($http, $scope, $log) {
$scope.isLoading = false;
$scope.hideme = true;
var currentdate = new Date();
var startdd = "01";
var startmonth = "00";
var lastyear = currentdate.getFullYear() - 3;
var enddd = "31";
var endmonth = "11";
var endyear = currentdate.getFullYear();
$scope.StartDate = new Date(lastyear, startmonth, startdd);
$scope.EndDate = new Date(endyear, endmonth, enddd);
$scope.Region = "Local";
$scope.Country = "";
$scope.ProductLineSelected = false;
$scope.ButtonText = "CUSTOMIZE";
$scope.clicked = false;
$scope.Yes = function () {
if (!$scope.clicked == true) {
$scope.ButtonText = "CANCEL";
$scope.clicked = true;
}
else {
$scope.ButtonText = "CUSTOMIZE";
$scope.clicked = false;
}
}
$scope.alert = function (message) {
$window.alert(message);
}
$scope.Regions = ["Export", "Local"];
$http.get('/General/API/GetProductDivision').then(function (res) {
$scope.listofProd = res.data;
});
$scope.selectedProduct = "";
$scope.generateChart = function (prodname) {
$scope.selectedProduct = prodname;
$scope.isLoading = true;
$http.get('/Marketing/MarketingAPI/ProdPerformance', {
params: {
StartDate: $scope.StartDate,
EndDate: $scope.EndDate,
ProductDivision: prodname,
Division: $scope.Region,
Area: $scope.Country
}
}).then(function (res) {
var resArray = res.data;
var trendarray = [];
$scope.TableRecords = resArray;
$scope.hideme = false;
var json = resArray;
// console.log("Array of Objects: " + JSON.stringify($scope.TableRecords));
var groupedData = {};
var result = [];
resArray.forEach(function (item) {
var year = item.SecuredYear;
var value = item.ValueInDhs;
if (groupedData.hasOwnProperty(year)) {
groupedData[year] += value;
} else {
groupedData[year] = value;
}
});
//Pie chart data starts here
var years = [];
json.forEach(function (obj) {
if (years.indexOf(obj.SecuredYear) == -1)
years.push(obj.SecuredYear);
});
//$scope.years = years;
$scope.years = angular.copy(years);
$scope.pichartsview = true;
$scope.myFunction = function (index) {
var data = [['Product', 'ValueInDhs']];
json.forEach(function (obj) {
if (obj.SecuredYear == years[index]) {
data.push([String(obj.GroupName).trim(), obj.ValueInDhs]);
}
});
$scope.displayChart(data, index);
}
$scope.displayChart = function (dataForChart, index) {
var chartData = dataForChart;
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChartPie);
function drawChartPie() {
var data = google.visualization.arrayToDataTable(chartData);
var options = {
chartArea: { left: 10, top: 20, width: "80%", height: "80%" },
legend: 'bottom',
is3D: true,
pieSliceText: 'percentage',
pieSliceTextStyle: {
fontSize: 8
},
title: $scope.selectedProduct + " - " + $scope.years[index]
};
var chart = new google.visualization.PieChart(document.getElementById('piechart' + index));
$scope.$apply(function () {
chart.draw(data, options);
});
}
}
//Pie chart data ends here
for (var year in groupedData) {
var tmp = {};
tmp[year] = groupedData[year];
result.push(tmp);
}
result.forEach(function (obj, index) {
var key = Object.keys(obj)[0];
trendarray.push([parseInt(key, 10), obj[key]]);
});
trendarray.splice(0, 0, [{ label: 'SecuredYear', type: 'number' }, { label: 'ValueInDhs', type: 'number' }]);
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
$scope.isLoading = false;
function drawChart() {
var data = google.visualization.arrayToDataTable(
trendarray
);
var options = {
legend: 'none',
title: 'Product Performance Trendline - ' + $scope.selectedProduct + " - " + $scope.Region,
hAxis: { title: 'Secured Year', format: '0' },
vAxis: { title: 'Secured Value in DHS' },
trendlines: {
0: {
lineWidth: 5,
type: 'polynomial',
visibleInLegend: true,
color: 'green',
}
}
};
var chart = new google.visualization.ScatterChart(document.getElementById('chart_div2'));
chart.draw(data, options);
}
})
$scope.ProductLineSelected = true;
};
}]); //this is ctrl closing
})(); //this is function closing