2019-07-15 12:38:06 +01:00
import { Component , OnInit , Input , Output , EventEmitter , ViewChild , ElementRef } from '@angular/core' ;
2019-07-15 01:17:01 +01:00
import { ApiService } from '../providers/api-service' ;
2019-07-15 12:38:06 +01:00
import { BaseChartDirective , Color } 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-08-15 14:22:33 +01:00
import { NgModel } from '@angular/forms' ;
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-08-14 12:07:05 +01:00
bubbleChartBegin : any ;
bubbleChartEnd : any ;
2019-08-14 14:18:53 +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-08-14 12:07:05 +01:00
this . bubbleChartBegin = moment ( ) . format ( 'YYYY-MM-DD' ) ;
this . bubbleChartEnd = moment ( ) . format ( 'YYYY-MM-DD' ) ;
2019-07-15 01:17:01 +01:00
}
2019-07-10 13:04:07 +01:00
ngOnInit ( ) : void {
2019-07-15 12:38:06 +01:00
this . loadYearSpend ( ) ;
2019-08-15 14:33:56 +01:00
this . loadSupplierBubble ( false , ( 'January 1, 2018' ) , ( 'January 1, 2019' ) ) ; // pass start and end date ranges to this as Date()s
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 ;
public sampleColours : Color [ ] = [
{
backgroundColor : [
'red' ,
'green' ,
'blue' ,
'purple' ,
'yellow' ,
'brown' ,
'magenta' ,
'cyan' ,
'orange' ,
'pink'
]
}
] ;
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-08-15 12:09:46 +01:00
private loadSupplierBubble ( useRange : boolean , start_range : string , end_range : string ) {
2019-08-15 14:22:33 +01:00
console . log ( "Fetching data for bubble chart... this will take a while. custom range = " + useRange ) ;
2019-08-14 16:27:28 +01:00
2019-07-15 12:38:06 +01:00
this . api . loadMiscUrl ( 'organisation/external/supplier_count' ) . subscribe (
result = > {
let graph_data = [ ] ;
2019-08-15 12:09:46 +01:00
if ( useRange == true ) {
console . log ( "using range " + start_range + " : " + end_range ) ;
result . data . map ( item = > {
2019-08-15 12:37:49 +01:00
let is_item_in_range = ( new Date ( item . date ) >= new Date ( start_range ) && new Date ( item . date ) <= new Date ( end_range ) ) ;
2019-08-15 14:51:10 +01:00
// there are a lot of `new Date(blah)` but that is what works for some reason.
2019-08-15 12:09:46 +01:00
2019-08-15 14:22:33 +01:00
// IT WORKS!!!!!!!!!
2019-08-15 12:37:49 +01:00
console . log ( "item.date : " + new Date ( item . date ) ) ;
2019-08-15 14:51:10 +01:00
console . log ( "start_range input box: " + start_range ) ;
2019-08-15 12:37:49 +01:00
console . log ( "start_range : " + new Date ( start_range ) ) ;
2019-08-15 14:51:10 +01:00
console . log ( "end_range input box: " + end_range ) ;
2019-08-15 12:37:49 +01:00
console . log ( "end_range : " + new Date ( end_range ) ) ;
console . log ( "item.date >= start_range: " + ( new Date ( item . date ) >= new Date ( start_range ) ) ) ;
console . log ( "item.date <= end_range: " + ( new Date ( item . date ) <= new Date ( end_range ) ) ) ;
console . log ( "is_item_in_range: " + is_item_in_range ) ;
console . log ( "----------------------" ) ;
2019-08-15 14:22:33 +01:00
2019-08-15 12:37:49 +01:00
if ( is_item_in_range ) {
2019-08-15 12:09:46 +01:00
graph_data . push ( {
t : item.date ,
r : item.value > 1000000 ? ( item . value / 1000000 ) + 10 : ( item . value / 100000 ) + 5 ,
supplier : item.seller ,
y : item.count ,
value : item.value ,
count : item.count ,
} ) ;
}
2019-07-15 12:38:06 +01:00
} ) ;
2019-08-15 12:09:46 +01:00
this . supplierBubbleChartData [ 0 ] . data = graph_data ;
} else {
result . data . map ( item = > {
graph_data . push ( {
t : item.date ,
r : item.value > 1000000 ? ( item . value / 1000000 ) + 10 : ( item . value / 100000 ) + 5 ,
supplier : item.seller ,
y : item.count ,
value : item.value ,
count : item.count ,
} ) ;
} ) ;
2019-08-15 14:22:33 +01:00
this . supplierBubbleChartData [ 0 ] . data = graph_data ;
2019-08-15 12:09:46 +01:00
}
2019-07-15 12:38:06 +01:00
this . supplierBubbleChartData [ 0 ] . data = graph_data ;
2019-08-15 14:22:33 +01:00
console . log ( "Graph fetched with " + graph_data . length + " items." ) ;
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-06 16:51:44 +01:00
hoverBackgroundColor : 'blue' ,
hoverBorderColor : 'blue' ,
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
} ,
scaleLabel : {
display :true ,
labelString : 'Date'
2019-07-15 01:17:01 +01:00
}
} ] ,
2019-08-07 16:21:40 +01:00
yAxes : [ {
scaleLabel : {
display :true ,
labelString : 'Number of purchases'
}
} ]
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 ` ;
}
private loadYearSpend() {
this . api . loadMiscUrl ( 'organisation/external/year_spend' ) . subscribe (
result = > {
let value_data = [ ] ;
let count_data = [ ] ;
2019-08-15 14:22:33 +01:00
console . log ( "Graph being fetched." ) ;
2019-07-15 12:38:06 +01:00
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-08-14 12:07:05 +01:00
bubbleChartUpdate() {
2019-08-15 14:51:10 +01:00
console . log ( "start_range input box: " + this . bubbleChartBegin ) ;
console . log ( "start_range : " + new Date ( this . bubbleChartBegin ) ) ;
console . log ( "end_range input box: " + this . bubbleChartEnd ) ;
console . log ( "end_range : " + new Date ( this . bubbleChartEnd ) ) ;
2019-08-15 14:22:33 +01:00
// this is called when daterange is changed
this . loadSupplierBubble ( true , ( this . bubbleChartBegin ) , ( this . bubbleChartEnd ) ) ;
console . log ( "Bubble chart updating..." ) ;
/ *
bubbleChartBegin : any ;
bubbleChartEnd : any ;
* /
2019-08-14 12:07:05 +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 = {
responsive : true ,
scales : {
xAxes : [ {
type : 'time' ,
time : {
unit : 'month'
2019-08-07 16:21:40 +01:00
} ,
scaleLabel : {
display :true ,
labelString : 'Date'
2019-07-15 01:17:01 +01:00
}
} ] ,
yAxes : [
{ id : 'y-value' , position : 'left' , beginAtZero : true } ,
{ id : 'y-count' , position : 'right' , beginAtZero : true } ,
]
} ,
} ;
public yearSpendChartColors : Color [ ] = [
2019-07-10 12:49:36 +01:00
{
backgroundColor : [
2019-08-06 16:51:44 +01:00
'#ffa1b5' ,
'#52afed'
2019-07-10 12:49:36 +01:00
]
}
] ;
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-07-15 12:38:06 +01:00
@ViewChild ( 'supplierChart' , { read : BaseChartDirective , static : false } ) supplierChart : BaseChartDirective ;
private loadSupplierHistory() {
this . api . loadMiscUrl ( 'organisation/external/supplier_history' ) . subscribe (
result = > {
let labels = [ ] ;
let year = [ ] ;
let half = [ ] ;
let quarter = [ ] ;
result . data . map ( item = > {
labels . push ( item . name ) ;
year . push ( item . year_total ) ;
half . push ( item . half_total ) ;
quarter . push ( item . quarter_total ) ;
} ) ;
2019-08-06 16:51:44 +01:00
this . supplierMonthChartData [ 0 ] . data = quarter . slice ( 0 , 15 ) ;
this . supplierMonthChartData [ 1 ] . data = half . slice ( 0 , 15 ) ;
this . supplierMonthChartData [ 2 ] . data = year . slice ( 0 , 15 ) ;
this . supplierMonthChartLabels = labels . slice ( 0 , 15 ) ;
2019-07-15 12:38:06 +01:00
}
)
}
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 : {
display :true ,
labelString : 'Spend amount £'
}
} ] ,
yAxes : [ {
scaleLabel : {
display :true ,
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
}