import { Component , OnInit } from '@angular/core' ; import { DateService } from '../../../../services/date.service' ; import { UserService } from 'carey-user' ; import { Deal } from '../../models/deal' ; import { DealCriteria } from '../../models/deal-criteria' ; import { DealService } from '../../service/deal.service' ; @ Component ( { selector : 'app-accounts-ranked' , templateUrl : './accounts-ranked.component.html' , styleUrls : [ './accounts-ranked.component.css' ] } ) export class AccountsRankedComponent implements OnInit { options : any ; map : Map < string , number > = new Map < string , number > ( ) ; pageTitle : string = 'Accounts Ranked' ; pageSubtitle : string = 'By Deal Values' ; dimensions : string [ ] = [ 'account' , 'value' ] ; dataset : Array < Array < string | number > > = [ ] ; constructor ( private dealService , private userService , private dateService ) { } ngOnInit ( ) { this . loadDeals ( ) ; } private getDealCriteria ( ) { let criteria = new DealCriteria ( ) ; criteria . userId = "6014081e221e1b534a8aa432" ; return criteria ; } private loadDeals ( ) { let criteria = this . getDealCriteria ( ) ; this . dealService . fetchDealsByCriteria ( criteria ) . subscribe ( ( deals ) => this . handleDealsResponse ( deals ) , ( err ) => this . handleError ( err ) ) ; } private handleDealsResponse ( deals ) { deals . forEach ( deal => { this . addDealValue ( deal ) ; } ) ; this . setOptions ( ) ; } private addDealValue ( deal ) { let account = deal . contact . account . name ; let value = this . dealService . getAmount ( deal ) ; let foundAccount = this . dataset . find ( data => data [ 0 ] === account ) ; if ( foundAccount ) { let currentVal = + foundAccount [ 1 ] ; let newVal = currentVal + value ; foundAccount [ 1 ] = newVal ; } else { let accountData = [ account , value ] ; this . dataset . push ( accountData ) ; } } private setOptions ( ) { this . options = { grid : { left : '15%' } , dataset : [ { dimensions : [ 'account' , 'value' ] , source : this . dataset } , { transform : { type : 'sort' , config : { dimension : 'value' , order : 'desc' } } } ] , xAxis : { type : 'category' , axisLabel : { interval : 0 , rotate : 30 } , } , yAxis : { type : 'value' , axisLabel : { formatter : function ( value ) { return '$' + ( value / 1000 ) + 'K' ; } } } , series : { type : 'bar' , encode : { x : 'account' , y : 'value' } , datasetIndex : 1 } } ; } private handleError ( err ) { console . error ( err ) ; } }