2019-09-09 19:52:38 +01:00
import { Component , OnInit , Input , Output , EventEmitter , ViewChild } from '@angular/core' ;
2019-07-15 01:17:01 +01:00
import { ApiService } from '../providers/api-service' ;
2019-09-09 19:52:38 +01:00
import { BaseChartDirective } from 'ng2-charts' ;
2019-07-15 01:17:01 +01:00
import { CurrencyPipe } from '@angular/common' ;
2019-07-15 12:38:06 +01:00
import { ChartType } from "chart.js" ;
2019-08-14 12:07:05 +01:00
import * as moment from 'moment' ;
2019-07-09 14:22:19 +01:00
@Component ( {
templateUrl : 'more-graphs-and-tables.component.html' ,
} )
2019-07-11 15:18:33 +01:00
export class MoreStuffComponent implements OnInit {
2019-07-09 14:22:19 +01:00
@Output ( ) public onClick = new EventEmitter ( ) ;
@Input ( ) public categories : any ;
2019-09-09 19:52:38 +01:00
// Global Filter Setup
filterFrom : any ;
filterTo : any ;
isBubbleChartLoaded : boolean = false ;
isSupplierChartLoaded : boolean = false ;
2019-09-04 18:21:21 +01:00
wardList : any ;
wardListAvailable = false ;
2019-09-09 16:13:34 +01:00
metaTypeList : any ;
metaTypeListAvailable = false ;
2019-08-16 10:27:49 +01:00
2019-07-10 13:04:07 +01:00
constructor (
private api : ApiService ,
private currencyPipe : CurrencyPipe ,
2019-07-15 01:17:01 +01:00
) {
2019-09-09 19:52:38 +01:00
let now = moment ( ) ;
this . filterTo = now . format ( 'YYYY-MM-DD' ) ;
now . subtract ( 1 , 'months' ) ;
this . filterFrom = now . format ( 'YYYY-MM-DD' ) ;
2019-09-04 18:21:21 +01:00
this . tableSummary ( ) ;
2019-07-15 01:17:01 +01:00
}
2019-07-10 13:04:07 +01:00
ngOnInit ( ) : void {
2019-09-09 19:52:38 +01:00
this . loadData ( ) ;
}
2019-09-09 20:45:24 +01:00
public loadData() {
2019-09-09 19:52:38 +01:00
this . tableSummary ( ) ;
2019-08-30 16:50:19 +01:00
this . loadYearSpend ( ) ;
2019-09-09 19:52:38 +01:00
this . loadSupplierBubble ( ) ;
2019-07-15 12:38:06 +01:00
this . loadSupplierHistory ( ) ;
2019-07-10 13:04:07 +01:00
}
2019-07-15 01:17:01 +01:00
public showLegend = true ;
2019-07-10 13:04:07 +01:00
2019-07-15 01:17:01 +01:00
/ *
* Supplier Bubble Chart Setup
* /
2019-07-15 12:38:06 +01:00
2019-09-09 19:52:38 +01:00
private formatGraphData ( data : any ) : any [ ] {
2019-08-16 10:27:49 +01:00
let graph_data = [ ] ;
2019-09-09 19:52:38 +01:00
data . data . map ( item = > {
graph_data . push ( {
t : item.date ,
r : item.value > 1000000 ? ( item . value / 200000 ) : ( item . value / 100000 ) + 5 ,
supplier : item.seller ,
y : item.count ,
value : item.value ,
count : item.count ,
2019-08-16 10:27:49 +01:00
} ) ;
2019-09-09 19:52:38 +01:00
} ) ;
2019-08-15 12:09:46 +01:00
2019-09-09 19:52:38 +01:00
return graph_data ;
2019-08-16 10:27:49 +01:00
}
2019-09-09 19:52:38 +01:00
private loadSupplierBubble() {
this . api . loadMiscUrl ( 'organisation/external/supplier_count' , {
from : this . filterFrom ,
to : this.filterTo ,
} ) . subscribe (
result = > {
this . supplierBubbleChartData [ 0 ] . data = this . formatGraphData ( result ) ;
this . isBubbleChartLoaded = true ;
2019-08-16 10:51:47 +01:00
}
2019-09-09 19:52:38 +01:00
)
2019-07-15 12:38:06 +01:00
}
2019-09-09 19:52:38 +01:00
2019-07-15 12:38:06 +01:00
public supplierBubbleChartType : ChartType = 'bubble' ;
2019-07-15 01:17:01 +01:00
public supplierBubbleChartData : any [ ] = [
2019-07-11 15:18:33 +01:00
{
2019-07-15 12:38:06 +01:00
data : [ ] ,
label : [ "Spend" ] ,
2019-07-15 01:17:01 +01:00
borderColor : 'blue' ,
2019-08-22 16:59:51 +01:00
hoverBorderColor : 'black' ,
2019-08-14 09:49:18 +01:00
radius : 5 ,
2019-07-15 01:17:01 +01:00
} ,
2019-07-11 15:18:33 +01:00
] ;
2019-07-15 01:17:01 +01:00
public supplierBubbleChartLabels : string [ ] = [ ] ;
public supplierBubbleChartOptions : any = {
2019-07-11 15:18:33 +01:00
responsive : true ,
scales : {
2019-07-15 01:17:01 +01:00
xAxes : [ {
type : 'time' ,
time : {
unit : 'month'
2019-08-07 16:21:40 +01:00
} ,
2019-09-09 19:52:38 +01:00
scaleLabel : {
display : true ,
labelString : 'Date'
2019-07-15 01:17:01 +01:00
}
} ] ,
2019-08-07 16:21:40 +01:00
yAxes : [ {
scaleLabel : {
2019-09-09 19:52:38 +01:00
display : true ,
labelString : 'Number of purchases'
2019-08-07 16:21:40 +01:00
}
} ]
2019-07-15 01:17:01 +01:00
} ,
2019-07-10 13:04:07 +01:00
tooltips : {
callbacks : {
label : ( tooltip , data ) = > {
2019-07-15 01:17:01 +01:00
return this . bubbleTooltipCallback ( tooltip , data ) ;
2019-07-10 13:04:07 +01:00
} ,
} ,
} ,
2019-07-15 01:17:01 +01:00
} ;
2019-07-10 13:04:07 +01:00
2019-07-15 01:17:01 +01:00
private bubbleTooltipCallback ( tooltipItem : any , data : any ) {
let dataset = data . datasets [ tooltipItem . datasetIndex ] ;
let value = dataset . data [ tooltipItem . index ] ;
2019-07-15 12:38:06 +01:00
return ` ${ value . supplier } : ${ this . currencyPipe . transform ( value . value , 'GBP' , 'symbol' , '1.2-2' ) } over ${ value . count } purchases ` ;
}
2019-09-04 18:21:21 +01:00
private tableSummary() {
2019-09-09 19:52:38 +01:00
this . api . loadMiscUrl ( 'organisation/external/lcc_tables' , {
from : this . filterFrom ,
to : this.filterTo ,
} ) . subscribe (
2019-09-04 18:21:21 +01:00
result = > {
this . wardList = result . wards ;
2019-09-09 16:13:34 +01:00
this . metaTypeList = Object . keys ( result . types ) . map ( key = > result . types [ key ] ) ;
2019-09-04 18:21:21 +01:00
if ( this . wardList ) {
this . wardListAvailable = true ;
}
2019-09-09 16:13:34 +01:00
if ( this . metaTypeList ) {
this . metaTypeListAvailable = true ;
}
2019-09-04 18:21:21 +01:00
} ,
error = > {
console . log ( 'Retrieval Error' ) ;
2019-09-09 19:52:38 +01:00
console . log ( error . _body ) ;
2019-09-04 18:21:21 +01:00
}
)
}
2019-07-15 12:38:06 +01:00
private loadYearSpend() {
2019-09-09 19:52:38 +01:00
this . api . loadMiscUrl ( 'organisation/external/year_spend' , {
from : this . filterFrom ,
to : this.filterTo ,
} ) . subscribe (
2019-07-15 12:38:06 +01:00
result = > {
let value_data = [ ] ;
let count_data = [ ] ;
result . data . map ( item = > {
value_data . push ( {
t : item.date ,
y : item.value ,
} ) ;
count_data . push ( {
t : item.date ,
y : item.count ,
} ) ;
} ) ;
this . yearSpendChartData [ 0 ] . data = value_data ;
this . yearSpendChartData [ 1 ] . data = count_data ;
}
)
2019-07-10 13:04:07 +01:00
}
2019-07-15 01:17:01 +01:00
public yearSpendChartData : any [ ] = [
{
2019-07-15 12:38:06 +01:00
data : [ ] ,
2019-08-07 16:21:40 +01:00
label : [ "Value £" ] ,
2019-07-15 01:17:01 +01:00
fill : false ,
borderColor : 'red' ,
2019-08-06 16:51:44 +01:00
hoverBackgroundColor : '#ffa1b5' ,
2019-07-15 01:17:01 +01:00
hoverBorderColor : 'red' ,
yAxisID : 'y-value' ,
} ,
{
2019-07-15 12:38:06 +01:00
data : [ ] ,
2019-07-15 01:17:01 +01:00
label : [ "Count" ] ,
fill : false ,
borderColor : 'blue' ,
2019-08-06 16:51:44 +01:00
hoverBackgroundColor : '#52afed' ,
2019-07-15 01:17:01 +01:00
hoverBorderColor : 'blue' ,
yAxisID : 'y-count' ,
} ,
] ;
public yearSpendChartOptions : any = {
2019-09-09 19:52:38 +01:00
elements : { line : { tension : 0 } } ,
2019-07-15 01:17:01 +01:00
responsive : true ,
scales : {
xAxes : [ {
type : 'time' ,
time : {
unit : 'month'
2019-08-07 16:21:40 +01:00
} ,
scaleLabel : {
2019-09-09 19:52:38 +01:00
display : true ,
2019-08-07 16:21:40 +01:00
labelString : 'Date'
2019-07-15 01:17:01 +01:00
}
} ] ,
yAxes : [
2019-09-09 19:52:38 +01:00
{ id : 'y-value' , position : 'left' , beginAtZero : true , type : 'linear' } ,
{ id : 'y-count' , position : 'right' , beginAtZero : true , type : 'linear' } ,
2019-07-15 01:17:01 +01:00
]
} ,
} ;
public yearSpendChartLabels : string [ ] = [ ] ;
2019-07-15 12:38:06 +01:00
public yearSpendChartType : ChartType = 'line' ;
2019-07-15 01:17:01 +01:00
randomData() {
return Math . random ( ) ;
}
2019-08-16 12:27:05 +01:00
lineChartUpdate() {
2019-08-30 16:50:19 +01:00
this . loadYearSpend ( ) ;
2019-08-16 12:27:05 +01:00
}
2020-07-29 13:31:46 +01:00
@ViewChild ( 'supplierChart' , { read : BaseChartDirective } ) supplierChart : BaseChartDirective ;
2019-07-15 12:38:06 +01:00
private loadSupplierHistory() {
this . api . loadMiscUrl ( 'organisation/external/supplier_history' ) . subscribe (
result = > {
2019-09-09 19:52:38 +01:00
this . _supplierHistoryData = result . data ;
this . _supplierHistoryPage = 1 ;
this . _supplierHistoryPages = Math . ceil ( this . _supplierHistoryData . length / this . _supplierHistoryPerPage ) ;
this . updateSupplierHistoryData ( ) ;
this . isSupplierChartLoaded = true ;
2019-07-15 12:38:06 +01:00
}
2019-09-09 19:52:38 +01:00
) ;
2019-07-15 12:38:06 +01:00
}
2019-08-30 16:50:19 +01:00
2019-09-09 19:52:38 +01:00
private updateSupplierHistoryData() {
const lastResult = this . _supplierHistoryPerPage * this . _supplierHistoryPage ;
console . log ( this . _supplierHistoryPage ) ;
const firstResult = lastResult - this . _supplierHistoryPerPage ;
const pageData = this . _supplierHistoryData . slice ( firstResult , lastResult ) ;
console . log ( pageData ) ;
let labels = [ ] ;
let year = [ ] ;
let half = [ ] ;
let quarter = [ ] ;
pageData . map ( item = > {
labels . push ( item . name ) ;
year . push ( item . year_total ) ;
half . push ( item . half_total ) ;
quarter . push ( item . quarter_total ) ;
} ) ;
this . supplierMonthChartData [ 0 ] . data = quarter ;
this . supplierMonthChartData [ 1 ] . data = half ;
this . supplierMonthChartData [ 2 ] . data = year ;
this . supplierMonthChartLabels = labels ;
}
public nextSupplierHistoryPage() {
if ( this . _supplierHistoryPage < this . _supplierHistoryPages ) {
this . _supplierHistoryPage ++ ;
2019-08-30 16:50:19 +01:00
}
2019-09-09 19:52:38 +01:00
this . updateSupplierHistoryData ( ) ;
2019-08-30 16:50:19 +01:00
}
2019-09-09 19:52:38 +01:00
public previousSupplierHistoryPage() {
if ( this . _supplierHistoryPage > 1 ) {
this . _supplierHistoryPage -- ;
2019-08-30 16:50:19 +01:00
}
2019-09-09 19:52:38 +01:00
this . updateSupplierHistoryData ( ) ;
2019-08-22 16:59:51 +01:00
}
2019-08-30 16:50:19 +01:00
2019-09-09 19:52:38 +01:00
private _supplierHistoryData : any [ ] ;
private _supplierHistoryPerPage : number = 15 ;
2019-09-09 20:45:24 +01:00
public _supplierHistoryPage : number = 1 ;
public _supplierHistoryPages : number = 1 ;
2019-07-15 01:17:01 +01:00
public supplierMonthChartData : any [ ] = [
{
2019-07-15 12:38:06 +01:00
data : [ ] ,
2019-07-15 01:17:01 +01:00
label : [ "3 Month" ] ,
fill : false ,
borderColor : 'red' ,
hoverBorderColor : 'red' ,
hoverBackgroundColor : 'red' ,
} ,
{
2019-07-15 12:38:06 +01:00
data : [ ] ,
2019-07-15 01:17:01 +01:00
label : [ "6 Month" ] ,
fill : false ,
2019-08-06 16:51:44 +01:00
borderColor : 'blue' ,
hoverBorderColor : 'blue' ,
hoverBackgroundColor : 'blue' ,
2019-07-15 01:17:01 +01:00
} ,
{
2019-07-15 12:38:06 +01:00
data : [ ] ,
2019-07-15 01:17:01 +01:00
label : [ "12 Month" ] ,
fill : false ,
2019-08-06 16:51:44 +01:00
borderColor : 'orange' ,
hoverBorderColor : 'orange' ,
hoverBackgroundColor : 'orange' ,
2019-07-15 01:17:01 +01:00
} ,
] ;
public supplierMonthChartOptions : any = {
2019-07-15 12:38:06 +01:00
//maintainAspectRatio: false,
2019-07-15 01:17:01 +01:00
responsive : true ,
scales : {
2019-08-07 16:21:40 +01:00
xAxes : [ {
scaleLabel : {
2019-09-09 19:52:38 +01:00
display : true ,
2019-08-07 16:21:40 +01:00
labelString : 'Spend amount £'
}
} ] ,
yAxes : [ {
scaleLabel : {
2019-09-09 19:52:38 +01:00
display : true ,
2019-08-07 16:21:40 +01:00
labelString : 'Supplier Names'
}
} ]
2019-07-15 01:17:01 +01:00
} ,
} ;
2019-07-15 12:38:06 +01:00
public supplierMonthChartLabels : string [ ] = [ ] ;
public supplierMonthChartType : ChartType = 'horizontalBar' ;
2019-07-09 14:22:19 +01:00
}