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-16 16:58:35 +01:00
lineChartBegin : any ;
lineChartEnd : any ;
2019-08-14 12:07:05 +01:00
bubbleChartBegin : any ;
bubbleChartEnd : any ;
2019-08-16 10:27:49 +01:00
cached_graph_data : any ;
2019-08-16 15:23:18 +01:00
isBubbleChartLoaded = 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-08-14 12:07:05 +01:00
this . bubbleChartBegin = moment ( ) . format ( 'YYYY-MM-DD' ) ;
this . bubbleChartEnd = moment ( ) . format ( 'YYYY-MM-DD' ) ;
2019-08-16 12:27:05 +01:00
this . lineChartBegin = moment ( ) . format ( 'YYYY-MM-DD' ) ;
this . lineChartEnd = moment ( ) . format ( 'YYYY-MM-DD' ) ;
2019-07-15 01:17:01 +01:00
}
2019-07-10 13:04:07 +01:00
ngOnInit ( ) : void {
2019-08-16 16:58:35 +01:00
this . loadYearSpend ( ) ;
2019-08-16 15:23:18 +01:00
this . loadSupplierBubble ( false , ( '0' ) , ( '0' ) ) ;
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' ,
2019-08-20 16:47:20 +01:00
'#52afed' ,
2019-07-15 01:17:01 +01:00
'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-16 10:27:49 +01:00
private formatGraphData ( passed_graph_data : any , useRange : boolean , start_range : string , end_range : string ) : any [ ] {
let graph_data = [ ] ;
if ( useRange == true ) {
2019-08-16 10:51:47 +01:00
// console.log("using range " + start_range + " : " + end_range);
2019-08-16 10:27:49 +01:00
passed_graph_data . data . map ( item = > {
2019-08-16 11:32:45 +01:00
let is_item_in_range = ( new Date ( item . date . substring ( 0 , 10 ) ) >= new Date ( start_range ) && new Date ( item . date . substring ( 0 , 10 ) ) <= new Date ( end_range ) ) ;
2019-08-16 10:27:49 +01:00
// there are a lot of `new Date(blah)` but that is what works for some reason.
2019-08-20 16:47:20 +01:00
2019-08-16 15:24:49 +01:00
// console.log("item.date : " + (item.date));
// console.log("Date(item.date) : " + new Date(item.date));
// console.log("Date(item.date.substring(0, 10)) : " + new Date(item.date.substring(0, 10)));
2019-08-16 10:51:47 +01:00
// console.log("start_range input box: " + start_range);
// console.log("start_range : " + new Date(start_range));
// console.log("end_range input box: " + end_range);
// 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);
2019-08-16 15:24:49 +01:00
// console.log("----------------------");
2019-08-20 16:47:20 +01:00
2019-08-16 10:27:49 +01:00
if ( is_item_in_range ) {
graph_data . push ( {
2019-08-16 11:32:45 +01:00
t : new Date ( item . date . substring ( 0 , 10 ) ) ,
2019-08-22 16:59:51 +01:00
r : item.value > 1000000 ? ( item . value / 200000 ) : ( item . value / 100000 ) + 5 ,
2019-08-16 10:27:49 +01:00
supplier : item.seller ,
y : item.count ,
value : item.value ,
count : item.count ,
2019-07-15 12:38:06 +01:00
} ) ;
2019-08-16 10:27:49 +01:00
}
} ) ;
return graph_data ;
} else {
passed_graph_data . data . map ( item = > {
graph_data . push ( {
t : item.date ,
2019-08-20 16:47:20 +01:00
r : item.value > 1000000 ? ( item . value / 200000 ) : ( item . value / 100000 ) + 5 ,
2019-08-16 10:27:49 +01:00
supplier : item.seller ,
y : item.count ,
value : item.value ,
count : item.count ,
} ) ;
} ) ;
2019-08-15 12:09:46 +01:00
2019-08-16 10:27:49 +01:00
return graph_data ;
}
}
private loadSupplierBubble ( useRange : boolean , start_range : string , end_range : string ) {
2019-08-16 15:23:18 +01:00
this . isBubbleChartLoaded = false ;
// console.log("Fetching data for bubble chart... this will take a while. custom range = " + useRange);
2019-08-15 14:22:33 +01:00
2019-08-16 10:51:47 +01:00
var is_cached = false ;
try {
2019-08-16 12:13:43 +01:00
if ( this . cached_graph_data ) {
2019-08-16 10:51:47 +01:00
is_cached = true ;
}
} catch {
// not cached
}
if ( is_cached ) {
2019-08-16 15:24:49 +01:00
// console.log("Using cached data of " + this.cached_graph_data.length + " items.");
2019-08-16 10:27:49 +01:00
this . supplierBubbleChartData [ 0 ] . data = this . formatGraphData ( this . cached_graph_data , useRange , start_range , end_range ) ;
2019-08-16 15:23:18 +01:00
this . isBubbleChartLoaded = true ;
2019-08-16 15:24:49 +01:00
// console.log("variable \"this.isBubbleChartLoaded\": " + this.isBubbleChartLoaded);
2019-08-16 10:27:49 +01:00
}
else {
2019-08-16 15:24:49 +01:00
// console.log("Not using cached data.");
2019-08-16 10:27:49 +01:00
this . api . loadMiscUrl ( 'organisation/external/supplier_count' ) . subscribe (
result = > {
this . cached_graph_data = result ;
2019-08-16 16:58:35 +01:00
2019-08-16 10:51:47 +01:00
this . supplierBubbleChartData [ 0 ] . data = this . formatGraphData ( result , useRange , start_range , end_range ) ;
2019-08-16 15:24:49 +01:00
// console.log("Graph fetched with " + this.supplierBubbleChartData[0].data.length + " items.");
2019-08-16 15:23:18 +01:00
this . isBubbleChartLoaded = true ;
2019-08-16 15:24:49 +01:00
// console.log("variable \"this.isBubbleChartLoaded\": " + this.isBubbleChartLoaded);
2019-08-15 12:09:46 +01:00
}
2019-08-16 10:27:49 +01:00
)
}
2019-08-15 12:09:46 +01:00
2019-08-16 15:24:49 +01:00
// console.log("variable \"this.isBubbleChartLoaded\": " + this.isBubbleChartLoaded);
2019-07-15 12:38:06 +01:00
}
2019-08-14 15:34:08 +01:00
private bubbleChartUpdate() {
console . log ( "test change" ) ;
}
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
} ,
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-16 15:24:49 +01:00
console . log ( "The server is UP" ) ;
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-16 15:24:49 +01:00
// console.log("start_range input box: " + this.bubbleChartBegin);
// console.log("end_range input box: " + this.bubbleChartEnd);
2019-08-15 14:51:10 +01:00
2019-08-15 14:22:33 +01:00
// this is called when daterange is changed
this . loadSupplierBubble ( true , ( this . bubbleChartBegin ) , ( this . bubbleChartEnd ) ) ;
2019-08-16 15:23:18 +01:00
2019-08-16 15:24:49 +01:00
// console.log("variable \"this.isBubbleChartLoaded\": " + this.isBubbleChartLoaded);
2019-08-15 14:22:33 +01:00
/ *
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-08-16 12:27:05 +01:00
lineChartUpdate() {
2019-08-22 16:59:51 +01:00
this . loadYearSpend ( true , ( this . lineChartBegin ) , ( this . lineChartEnd ) ) ;
2019-08-16 12:27:05 +01:00
}
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-08-22 16:59:51 +01:00
supplierChartUpdate() {
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 ) ;
} ) ;
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 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
}