'use strict'; angular.module('acufuel') .controller('analyticsController', ['$scope', 'analyticsService', function($scope, analyticsService) { //Implemented drill down chart and Now used this on 07 Sep 2018 -KD /* TODO: - Check data labels after drilling. Label rank? New positions? */ $scope.getAllRegionalPapDrilldown = function() { $scope.flag = true; //for spinner var data = Highcharts.geojson(Highcharts.maps['countries/us/us-all']); var separators = Highcharts.geojson(Highcharts.maps['countries/us/us-all'], 'mapline'); analyticsService.getAllRPP().then(function(result) { for (var i = 0; i < data.length; i++) { for (var j = 0; j < result.length; j++) { if (data[i].name === result[j].region) { data[i].value = result[j].averagePAP.toFixed(2); } } } $.each(data, function(i) { this.drilldown = this.properties['hc-key']; this.value = this.value; }); $scope.flag = false; // Instantiate the map Highcharts.mapChart('container', { chart: { events: { drilldown: function(e) { if (!e.seriesOptions) { var chart = this, mapKey = 'countries/us/' + e.point.drilldown + '-all', // Handle error, the timeout is cleared on success fail = setTimeout(function() { if (!Highcharts.maps[mapKey]) { // chart.showLoading(' Failed loading ' + e.point.name); fail = setTimeout(function() { chart.hideLoading(); }, 10000); } }, 3000); // Show the spinner chart.showLoading(''); // Font Awesome spinner // Load the drilldown map var region = e.point.name; analyticsService.getRPP(region).then(function(result) { $.getScript('https://code.highcharts.com/mapdata/' + mapKey + '.js', function() { //called the get api region wise data = Highcharts.geojson(Highcharts.maps[mapKey]); for (var i = 0; i < data.length; i++) { for (var j = 0; j < result.length; j++) { //delete the County word from api result to match with map data result[j].region = result[j].region.replace("County", ""); if (data[i].name.trim() === result[j].region.trim()) { data[i].value = result[j].averagePAP.toFixed(2); } } } // Set a non-random bogus value $.each(data, function(i) { this.value = this.value; }); // Hide loading and add series chart.hideLoading(); clearTimeout(fail); chart.addSeriesAsDrilldown(e.point, { name: e.point.name, data: data, dataLabels: { enabled: true, format: '{point.name}' } }); }); }) } this.setTitle(null, { text: e.point.name }); }, drillup: function() { this.setTitle(null, { text: '' }); } } }, title: { text: 'USA Map-Drilldown for Regional Pricing' }, subtitle: { text: '', floating: true, align: 'right', y: 50, style: { fontSize: '16px' } }, mapNavigation: { enabled: true, buttonOptions: { verticalAlign: 'bottom' } }, plotOptions: { map: { states: { hover: { color: '#EEDD66' } } } }, series: [{ data: data, name: 'USA', dataLabels: { enabled: true, format: '{point.properties.postal-code}' } }, { type: 'mapline', data: separators, color: 'silver', enableMouseTracking: false, animation: { duration: 500 } }], drilldown: { activeDataLabelStyle: { color: '#FFFFFF', textDecoration: 'none', textOutline: '1px #000000' }, drillUpButton: { relativeTo: 'spacingBox', position: { x: 0, y: 60 } } } }); }) } $scope.getAllRegionalPapDrilldown(); $scope.cancelStatus = function() { $('#popup1').css('display', 'none'); } $scope.ordersCost = 0; $scope.ordersCount = 0; $scope.orderCostAndCount = function() { analyticsService.orderCostAndCount().then(function(result) { $scope.ordersCost = result.cost; $scope.ordersCount = result.count; }) } $scope.drf = {}; $scope.hfp = {}; $scope.cs = {}; $scope.mfsValues = []; $scope.msfarray = []; $scope.getMFS = function() { //var makeId = makeId; analyticsService.getMFS().then(function(result) { $scope.mfsValues = result; $scope.optionsmfs = { chart: { type: 'multiBarChart', height: 450, margin: { top: 45, right: 20, bottom: 45, left: 45 }, clipEdge: true, //staggerLabels: true, duration: 500, stacked: false, color: ["#FEDFC3", "#990000", "#000099", "#009900"], xAxis: { axisLabel: 'Months', showMaxMin: false, }, yAxis: { axisLabel: 'Orders', axisLabelDistance: -20, tickFormat: function(d) { return d3.format(',')(d); } } } }; $scope.datamfs = $scope.mfsValues; }) } $scope.orderCostAndCount(); $scope.getMFS(); $scope.csValues = []; $scope.getCS = function() { //var makeId = makeId; analyticsService.getCS().then(function(result) { $scope.csValues = result; console.log("cs",result) $scope.optionscs = { chart: { type: 'pieChart', height: 500, x: function(d) { return d.key; }, y: function(d) { return d.y; }, valueFormat: d3.format(".0f"), showLabels: false, segmentShowStroke: false, animationSteps: 20, animationEasing: "linear", animateScale: true, duration: 500, labelThreshold: 0.01, labelSunbeamLayout: true, legend: { margin: { top: 5, right: 35, bottom: 5, left: 0 } } } }; $scope.datacs = $scope.csValues; }) } $scope.getCS(); $scope.hfpValuesOne = {}; $scope.hfpValuesOne.key = "JET-A Full Service"; $scope.hfpValuesOne.values = []; $scope.hfpValuesTwo = {}; $scope.hfpValuesTwo.key = "100LL Full Service"; $scope.hfpValuesTwo.values = []; $scope.hfpFinalData = []; $scope.getHfpChartData = function() { analyticsService.gethfpChart().then(function(result) { for(var i=0; i0){return d[0]}}) //adjusting, 100% is 1.00, not 100 as it is in the data .y(function(d) { return d[1]}) .color(d3.scale.category10().range()) .useInteractiveGuideline(true) ; chart.xAxis .tickFormat(function(d) { return d3.time.format('%x')(new Date(d)) }); chart.yAxis .axisLabel("PaP / Retail Price") .axisLabelDistance(-5) .tickFormat(d3.format(',.2f')).showMaxMin(false); d3.select('#chart svg') .datum($scope.hfpRangeData) .transition().duration(500) .call(chart) ; nv.utils.windowResize(chart.update); return chart; }) } else if ($scope.hfp.fromDate != undefined && $scope.hfp.toDate != undefined) { $scope.fillForm = false; //from date format $scope.dat1 = $scope.hfp.fromDate; $scope.fd1 = $scope.dat1.split("/").reverse(); $scope.tmp = $scope.fd1[2]; $scope.fd1[2] = $scope.fd1[1]; $scope.fd1[1] = $scope.tmp; $scope.fd = $scope.fd1.join("-"); //to date format $scope.dat2 = $scope.hfp.toDate; $scope.td1 = $scope.dat2.split("/").reverse(); $scope.tmp1 = $scope.td1[2]; $scope.td1[2] = $scope.td1[1]; $scope.td1[1] = $scope.tmp1; $scope.td = $scope.td1.join("-") + " " + '23:59:59'; // console.log("===both dates==",$scope.td) analyticsService.getDRFHfpChart($scope.fd, $scope.td).then(function(result) { //console.log("===date range22===",result); for(var i=0; i0){return d[0] }}) //adjusting, 100% is 1.00, not 100 as it is in the data .y(function(d) { return d[1]}) .color(d3.scale.category10().range()) .useInteractiveGuideline(true) ; chart.xAxis .tickFormat(function(d) { return d3.time.format('%x')(new Date(d)) }); chart.yAxis .axisLabel("PaP / Retail Price") .axisLabelDistance(-5) .tickFormat(d3.format(',.2f')).showMaxMin(false); d3.select('#chart svg') .datum($scope.hfpRangeData) .transition().duration(500) .call(chart) ; nv.utils.windowResize(chart.update); return chart; }) } else { $scope.fillForm = true; } } /*Date Range Filter for Monthly fuel Sales*/ $scope.submitDate = function() { if ($scope.drf.fromDate === undefined) { $scope.fillForm = true; } else if ($scope.drf.fromDate != undefined && $scope.drf.toDate === undefined || $scope.drf.toDate === '') { $scope.fillForm = false; var today = new Date(); var DefaultToDate = today.getFullYear() + '-' + ("0" + (today.getMonth() + 1)).slice(-2) + '-' + ("0" + today.getDate()).slice(-2); //from date format $scope.dat1 = $scope.drf.fromDate; $scope.fd1 = $scope.dat1.split("/").reverse(); $scope.tmp = $scope.fd1[2]; $scope.fd1[2] = $scope.fd1[1]; $scope.fd1[1] = $scope.tmp; $scope.fd = $scope.fd1.join("-"); analyticsService.getDRFChart($scope.fd, DefaultToDate).then(function(result) { $scope.datamfs = []; $scope.datamfs = result; }) analyticsService.orderCostAndCountByDate($scope.fd, DefaultToDate).then(function(result) { $scope.ordersCost = result.cost; $scope.ordersCount = result.count; }) } else if ($scope.drf.fromDate != undefined && $scope.drf.toDate != undefined) { $scope.fillForm = false; //from date format $scope.dat1 = $scope.drf.fromDate; $scope.fd1 = $scope.dat1.split("/").reverse(); $scope.tmp = $scope.fd1[2]; $scope.fd1[2] = $scope.fd1[1]; $scope.fd1[1] = $scope.tmp; $scope.fd = $scope.fd1.join("-"); $scope.dat2 = $scope.drf.toDate; $scope.td1 = $scope.dat2.split("/").reverse(); $scope.tmp1 = $scope.td1[2]; $scope.td1[2] = $scope.td1[1]; $scope.td1[1] = $scope.tmp1; $scope.td = $scope.td1.join("-"); analyticsService.getDRFChart($scope.fd, $scope.td).then(function(result) { $scope.datamfs = []; $scope.datamfs = result; }) analyticsService.orderCostAndCountByDate($scope.fd, $scope.td).then(function(result) { $scope.ordersCost = result.cost; $scope.ordersCount = result.count; }) } else { $scope.fillForm = true; } } /*Date Range Filter for Customer Sources*/ $scope.submitDateCs = function() { if ($scope.cs.fromDate === undefined) { $scope.fillForm = true; } else if ($scope.cs.fromDate != undefined && $scope.cs.toDate === undefined || $scope.cs.toDate === '') { $scope.fillForm = false; var today = new Date(); var DefaultToDate = today.getFullYear() + '-' + ("0" + (today.getMonth() + 1)).slice(-2) + '-' + ("0" + today.getDate()).slice(-2); //from date format $scope.dat1 = $scope.cs.fromDate; $scope.fd1 = $scope.dat1.split("/").reverse(); $scope.tmp = $scope.fd1[2]; $scope.fd1[2] = $scope.fd1[1]; $scope.fd1[1] = $scope.tmp; $scope.fd = $scope.fd1.join("-"); analyticsService.getDRFCSChart($scope.fd, DefaultToDate).then(function(result) { $scope.csValues = result; $scope.optionscs = { chart: { type: 'pieChart', height: 500, // color: ["#FEDFC3","#D89700","#FFE8B3","FFD980","#FF7F0E","#fff2e6","#ffcc99","#ffa64d","#ff8c1a","#B37D00","#f5f5f0","#e0e0d1","#ccccb3","#adad85","#999966","#7a7a52"], x: function(d) { return d.key; }, y: function(d) { return d.y; }, showLabels: false, segmentShowStroke: false, animationSteps: 20, animationEasing: "linear", animateScale: true, duration: 500, labelThreshold: 0.01, labelSunbeamLayout: true, legend: { margin: { top: 5, right: 35, bottom: 5, left: 0 } } } }; $scope.datacs = $scope.csValues; }) } else if ($scope.cs.fromDate != undefined && $scope.cs.toDate != undefined) { $scope.fillForm = false; //from date format $scope.dat1 = $scope.cs.fromDate; $scope.fd1 = $scope.dat1.split("/").reverse(); $scope.tmp = $scope.fd1[2]; $scope.fd1[2] = $scope.fd1[1]; $scope.fd1[1] = $scope.tmp; $scope.fd = $scope.fd1.join("-"); //to date format $scope.dat2 = $scope.cs.toDate; $scope.td1 = $scope.dat2.split("/").reverse(); $scope.tmp1 = $scope.td1[2]; $scope.td1[2] = $scope.td1[1]; $scope.td1[1] = $scope.tmp1; $scope.td = $scope.td1.join("-"); analyticsService.getDRFCSChart($scope.fd, $scope.td).then(function(result) { $scope.csValues = result; $scope.optionscs = { chart: { type: 'pieChart', height: 500, x: function(d) { return d.key; }, y: function(d) { return d.y; }, showLabels: false, segmentShowStroke: false, animationSteps: 20, animationEasing: "linear", animateScale: true, duration: 500, labelThreshold: 0.01, labelSunbeamLayout: true, legend: { margin: { top: 5, right: 35, bottom: 5, left: 0 } } } }; $scope.datacs = $scope.csValues; }) } else { $scope.fillForm = true; } } }]);