I have a small doubt about ng-init
, actually I have some charts.
- Trendline on top
- Piecharts below.
- HTML Table below.
on page load I am not displaying anything, but once I click on particular button for the first time then my entire charts and table is getting displayed for the data received by the http request. but afterwards if I click on the other button and executing same http request then its updating the trendline and table but not the piecharts. My piecharts are being rendered inside the below div with the ng-init, and when I am testing that ng-init="myFunction(index)" method its not updating on the subsequent click.
<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>
And this is my angularjs code for the ng-init function.
(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