2017-08-31 16:06:47 +01:00
import { Directive , Component , OnInit } from '@angular/core' ;
2018-04-13 16:07:26 +01:00
import { CurrencyPipe } from '@angular/common' ;
2017-08-31 16:06:47 +01:00
import { ApiService } from '../providers/api-service' ;
import { Router } from '@angular/router' ;
2019-07-12 17:12:01 +01:00
import { ChartOptions , ChartType , ChartDataSets } from 'chart.js' ;
2017-08-31 16:06:47 +01:00
import { GraphWidget } from '../widgets/graph-widget.component' ;
2019-07-12 17:12:01 +01:00
import { Color , Label } from 'ng2-charts' ;
2017-12-12 15:10:44 +00:00
import { CustBarSnippetComponent } from '../snippets/cust-snippet-bar.component' ;
2017-12-13 12:37:11 +00:00
import { PiePanel } from '../panels/pie-panel.component' ;
2017-12-12 15:10:44 +00:00
import { DataType } from '../shared/data-types.enum' ;
2018-04-13 16:37:26 +01:00
import * as moment from 'moment' ;
2019-07-04 16:36:06 +01:00
import { SuppliersComponent } from '../dashboard/suppliers.component' ;
2019-07-09 14:24:59 +01:00
import { MoreStuffComponent } from '../dashboard/more-graphs-and-tables.component' ;
2019-07-10 12:22:26 +01:00
// import { StackedBarChartComponent } from '../panels/stacked-bar.component';
2018-06-04 15:23:16 +01:00
2019-07-12 17:45:43 +01:00
interface SuppliersComponent {
2019-07-10 12:22:26 +01:00
name : string ;
}
2017-08-31 16:06:47 +01:00
@Component ( {
2017-08-31 18:44:17 +01:00
templateUrl : 'dashboard-customer.component.html'
2017-08-31 16:06:47 +01:00
} )
2017-08-31 18:44:17 +01:00
export class DashboardCustomerComponent implements OnInit {
/* Setting up dashboard's main variables*/
name : any ;
2017-09-19 15:04:49 +01:00
email : any ;
2017-08-31 18:44:17 +01:00
myPearPoints : any ;
trends : any ;
myRank : any ;
username : any ;
2017-12-15 17:44:50 +00:00
maxPurchase : number = 0 ;
2017-08-31 18:44:17 +01:00
2018-05-22 12:28:09 +01:00
disableCategoryButton : boolean = false ;
2017-12-19 18:01:29 +00:00
2018-06-18 12:08:50 +01:00
public bootstrapColours : string [ ] = [ 'bg-primary' , 'bg-secondary' , 'bg-success' ,
'bg-danger' , 'bg-warning' , 'bg-info' ] ;
2019-07-09 12:41:17 +01:00
public chartType = 'doughnut' ;
2018-04-13 16:07:26 +01:00
public chartLegend = true ;
2018-04-13 16:37:26 +01:00
public doughnutChartDataCategory : any [ ] = [ ] ;
2018-04-13 16:07:26 +01:00
public doughnutChartLabelsCategory : string [ ] = [ ] ;
2018-05-01 13:23:47 +01:00
public doughnutChartOptionsCategory :any = {
2018-04-13 16:07:26 +01:00
tooltips : {
callbacks : {
label : ( tooltip , data ) = > {
return this . tooltipLabelCallback ( tooltip , data ) ;
} ,
} ,
} ,
}
myWeek1 : any ;
weekList1 = [ ] ;
public purchaseNotEssential : number ;
public purchaseEssential : number ;
public showEssentialBarChart = false ;
public showCategoryBarChart = false ;
public showCategoryDoughnutChart = false ;
public barChartDataEssential :any [ ] = [
{ data : 0 , label : 'Essential' , stack : '1' } ,
{ data : 0 , label : 'Non-Essential' , stack : '1' } ,
] ;
public barChartLabelsEssential :string [ ] = [ 'All Purchases' ] ;
public barChartOptionsEssential :any = {
responsive : true ,
scales : {
xAxes : [ {
stacked :true
} ] ,
yAxes : [ {
stacked :true
} ]
}
} ;
public barChartTypeEssential :string = 'horizontalBar' ;
public barChartOptionsCategory :any = {
scaleShowVerticalLines : false ,
responsive : true ,
scales : {
yAxes : [ {
ticks : {
beginAtZero : true ,
callback : label = > ` £ ${ label } `
}
} ]
} ,
tooltips : {
callbacks : {
label : ( tooltip , data ) = > {
return this . tooltipLabelCallback ( tooltip , data ) ;
} ,
} ,
} ,
} ;
public barChartTypeCategory :string = 'bar' ;
public barChartLegendCategory :boolean = false ;
public barChartDataCategory :any [ ] = [ ] ;
public barChartLabelsCategory :string [ ] = [ ] ;
2019-07-11 15:17:50 +01:00
2017-12-14 20:14:47 +00:00
weekPurchaseList = {
2017-12-19 18:01:29 +00:00
first : 0 ,
second : 0 ,
max : 0 ,
sum : 0 ,
count : 0 ,
2017-12-14 20:14:47 +00:00
} ;
2017-12-13 12:37:11 +00:00
2018-05-22 12:57:25 +01:00
showTotalCategoryList : boolean = false ;
2018-05-22 12:28:09 +01:00
totalCategoryLimit : number = 10 ;
2018-05-22 13:25:21 +01:00
totalCategoryList : any [ ] = [ ] ;
2017-12-14 20:14:47 +00:00
2019-07-11 15:18:33 +01:00
public lineChartData : ChartDataSets [ ] = [
{ data : [ 65 , 59 , 80 , 81 , 56 , 55 , 40 ] , label : 'Series A' } ,
{ data : [ 28 , 48 , 40 , 19 , 86 , 27 , 90 ] , label : 'Series B' } ,
{ data : [ 180 , 480 , 770 , 90 , 1000 , 270 , 400 ] , label : 'Series C' , yAxisID : 'y-axis-1' }
] ;
public lineChartLabels : Label [ ] = [ 'January' , 'February' , 'March' , 'April' , 'May' , 'June' , 'July' , 'August' , 'September' , 'October' , 'November' , 'December' ] ;
public lineChartOptions : ( ChartOptions & { annotation : any } ) = {
responsive : true ,
scales : {
// We use this empty structure as a placeholder for dynamic theming.
xAxes : [ { } ] ,
yAxes : [
{
id : 'y-axis-0' ,
position : 'left' ,
} ,
{
id : 'y-axis-1' ,
position : 'right' ,
gridLines : {
color : 'rgba(255,0,0,0.3)' ,
} ,
ticks : {
fontColor : 'red' ,
}
}
]
} ,
annotation : {
annotations : [
{
type : 'line' ,
mode : 'vertical' ,
scaleID : 'x-axis-0' ,
value : 'March' ,
borderColor : 'orange' ,
borderWidth : 2 ,
label : {
enabled : true ,
fontColor : 'orange' ,
content : 'LineAnno'
}
} ,
] ,
} ,
} ;
public lineChartColors : Color [ ] = [
{ // grey
backgroundColor : 'rgba(148,159,177,0.2)' ,
borderColor : 'rgba(148,159,177,1)' ,
pointBackgroundColor : 'rgba(148,159,177,1)' ,
pointBorderColor : '#fff' ,
pointHoverBackgroundColor : '#fff' ,
pointHoverBorderColor : 'rgba(148,159,177,0.8)'
} ,
{ // dark grey
backgroundColor : 'rgba(77,83,96,0.2)' ,
borderColor : 'rgba(77,83,96,1)' ,
pointBackgroundColor : 'rgba(77,83,96,1)' ,
pointBorderColor : '#fff' ,
pointHoverBackgroundColor : '#fff' ,
pointHoverBorderColor : 'rgba(77,83,96,1)'
} ,
{ // red
backgroundColor : 'rgba(255,0,0,0.3)' ,
borderColor : 'red' ,
pointBackgroundColor : 'rgba(148,159,177,1)' ,
pointBorderColor : '#fff' ,
pointHoverBackgroundColor : '#fff' ,
pointHoverBorderColor : 'rgba(148,159,177,0.8)'
}
] ;
public lineChartLegend = true ;
public lineChartType = 'line' ;
2017-12-14 20:14:47 +00:00
// Graph widgets
2017-12-12 15:10:44 +00:00
public widgetList = [
{
type : 'graph' ,
name : 'total_last_week' ,
icon : 'icon-diamond' ,
title : 'Last Week Total' ,
dataType : DataType.currency ,
} ,
{
type : 'graph' ,
name : 'avg_spend_last_week' ,
icon : 'icon-diamond' ,
title : 'Last Week Avg. Spend' ,
dataType : DataType.currency ,
} ,
{
type : 'graph' ,
name : 'total_last_month' ,
title : 'Last Month Total' ,
dataType : DataType.currency ,
} ,
{
type : 'graph' ,
name : 'avg_spend_last_month' ,
title : 'Last Month Avg. Spend' ,
dataType : DataType.currency ,
} ,
] ;
2017-08-31 16:06:47 +01:00
constructor (
private api : ApiService ,
2018-04-13 16:07:26 +01:00
private currencyPipe : CurrencyPipe ,
2017-08-31 16:06:47 +01:00
) {
2018-04-13 16:07:26 +01:00
this . setDate ( ) ;
2017-12-18 12:57:40 +00:00
this . api . customerStats ( ) . subscribe (
2017-12-14 20:14:47 +00:00
result = > {
2017-12-15 17:20:18 +00:00
this . setWeekPurchaseList ( result . weeks ) ;
2018-04-13 16:07:26 +01:00
this . setWeekData ( result ) ;
this . setChartData ( result . data . cat_total ) ;
2018-05-22 12:28:09 +01:00
this . totalCategoryList = result . data . cat_list ;
2019-07-09 12:41:17 +01:00
if ( this . totalCategoryList ) {
this . showTotalCategoryList = true ;
}
2018-04-13 16:07:26 +01:00
this . purchaseEssential = result . data . essentials . purchase_no_essential_total ;
this . purchaseNotEssential = result . data . essentials . purchase_no_total - this . purchaseEssential ;
this . barChartDataEssential = [
{ data : [ this . purchaseEssential ] , label : 'Essential' , stack : '1' } ,
{ data : [ this . purchaseNotEssential ] , label : 'Non-Essential' , stack : '1' } ,
] ;
this . showEssentialBarChart = true ;
2017-12-14 20:14:47 +00:00
} ,
error = > {
console . log ( 'Retrieval Error' ) ;
console . log ( error . _body ) ;
}
) ;
}
2018-04-13 16:07:26 +01:00
private setChartData ( dataCat : any ) {
this . barChartLabelsCategory = Object . keys ( dataCat ) ;
let barChartDataCategoryInitial = Object . keys ( dataCat ) . map ( key = > dataCat [ key ] ) ;
this . barChartDataCategory = [
{ data : barChartDataCategoryInitial , label : 'Series A' } ,
] ;
this . showCategoryBarChart = true ;
2018-04-16 15:41:45 +01:00
if ( this . weekList1 ) {
let doughnutChartDataCategoryInitial = this . weekList1 . map ( function ( a ) { return a . value ; } ) ;
this . doughnutChartDataCategory = [
{ data : doughnutChartDataCategoryInitial , label : 'Series A' } ,
] ;
// setTimeout is currently a workaround for ng2-charts labels
setTimeout ( ( ) = > this . doughnutChartLabelsCategory = this . weekList1 . map ( function ( a ) { return a . category ; } ) , 0 ) ;
this . showCategoryDoughnutChart = true ;
}
2018-04-13 16:07:26 +01:00
}
private setDate ( ) {
this . myWeek1 = moment ( ) . startOf ( 'isoWeek' ) . format ( 'YYYY-MM-DD' ) ;
}
private setWeekData ( data : any ) {
function prop < T , K extends keyof T > ( obj : T , key : K ) {
return obj [ key ] ;
}
this . weekList1 = prop ( data . data . categories , this . myWeek1 ) ;
}
2017-12-14 20:14:47 +00:00
public setWeekPurchaseList ( data : any ) {
this . weekPurchaseList = {
2017-12-19 18:01:29 +00:00
first : data.first ,
second : data.second ,
max : data.max ,
sum : data.sum ,
count : data.count ,
2017-12-14 20:14:47 +00:00
} ;
2017-12-15 17:20:18 +00:00
}
2018-05-22 12:28:09 +01:00
private categoryLoadMore ( ) {
this . disableCategoryButton = true ;
this . totalCategoryLimit = 30 ;
2017-12-19 18:01:29 +00:00
}
2018-06-18 12:08:50 +01:00
public getBootstrapColour ( index : number ) {
return this . bootstrapColours [ index % this . bootstrapColours . length ] ;
}
2018-04-13 16:07:26 +01:00
public convertHex ( hex : string , opacity : number ) {
hex = hex . replace ( '#' , '' ) ;
const r = parseInt ( hex . substring ( 0 , 2 ) , 16 ) ;
const g = parseInt ( hex . substring ( 2 , 4 ) , 16 ) ;
const b = parseInt ( hex . substring ( 4 , 6 ) , 16 ) ;
const rgba = 'rgba(' + r + ', ' + g + ', ' + b + ', ' + opacity / 100 + ')' ;
return rgba ;
}
private tooltipLabelCallback ( tooltipItem : any , data : any ) {
var dataset = data . datasets [ tooltipItem . datasetIndex ] ;
var value = dataset . data [ tooltipItem . index ] ;
return this . currencyPipe . transform ( value , 'GBP' , 'symbol' , '1.2-2' ) ;
}
// events
public chartClicked ( e : any ) : void {
}
public chartHovered ( e : any ) : void {
}
2017-08-31 16:06:47 +01:00
ngOnInit ( ) : void {
}
}