2017-12-13 12:37:11 +00:00
import { Component , OnInit , Input , Output , EventEmitter } from '@angular/core' ;
2018-03-26 15:47:21 +01:00
import { ApiService } from '../providers/api-service' ;
2017-12-14 16:44:23 +00:00
import { CustPiesService } from '../providers/cust-pies.service' ;
2017-12-13 12:37:11 +00:00
import { DataType } from '../shared/data-types.enum' ;
import { ChartData } from '../_interfaces/chart-data' ;
2018-03-26 15:47:21 +01:00
import * as moment from 'moment' ;
import 'rxjs/add/operator/map' ;
2017-12-13 12:37:11 +00:00
@Component ( {
selector : 'panel-pie' ,
templateUrl : 'pie-panel.component.html' ,
} )
export class PiePanel implements OnInit {
public chartType = 'doughnut' ;
public chartLegend = true ;
2018-03-26 15:47:21 +01:00
public doughnutChartDataLocal : number [ ] = [ ] ;
public doughnutChartLabelsLocal : string [ ] = [ ] ;
public doughnutChartDataCategory : number [ ] = [ ] ;
public doughnutChartLabelsCategory : string [ ] = [ ] ;
2017-12-13 12:37:11 +00:00
2018-03-26 15:47:21 +01:00
dayList : any [ ] = [ ] ;
valueList : number [ ] = [ ] ;
myWeek1 : any ;
myWeek2 : any ;
myWeek3 : any ;
myWeek4 : any ;
2017-12-13 12:37:11 +00:00
2018-04-11 19:10:03 +01:00
weekList1 = [ ] ;
2018-04-09 19:19:06 +01:00
public purchaseNotEssential : number ;
public purchaseEssential : number ;
2018-04-11 19:10:03 +01:00
public showEssentialBarChart = false ;
public showCategoryBarChart = false ;
2018-04-09 19:19:06 +01:00
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' ;
2018-04-11 19:10:03 +01:00
public barChartOptionsCategory :any = {
scaleShowVerticalLines : false ,
responsive : true
} ;
public barChartTypeCategory :string = 'bar' ;
public barChartLegendCategory :boolean = false ;
public barChartDataCategory :any [ ] = [
{ data : 0 , label : 'Series A' } ,
] ;
public barChartLabelsCategory :string [ ] = [ ] ;
2017-12-14 16:44:23 +00:00
//Old
2017-12-13 12:37:11 +00:00
// public mainChartElements = 7;
constructor (
2018-03-26 15:47:21 +01:00
private api : ApiService ,
2017-12-14 16:44:23 +00:00
private pieService : CustPiesService ,
2018-03-26 15:47:21 +01:00
) {
this . setDate ( ) ;
this . pieService . getPie ( ) . subscribe (
result = > {
this . setWeekData ( result ) ;
2018-04-11 19:10:03 +01:00
this . setChartData ( result . data . local_all , result . data . cat_total ) ;
2018-04-09 19:19:06 +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' } ,
] ;
2018-04-11 19:10:03 +01:00
this . showEssentialBarChart = true ;
2018-03-26 15:47:21 +01:00
} ,
error = > {
console . log ( 'Retrieval Error' ) ;
console . log ( error . _body ) ;
}
) ;
}
2017-12-13 12:37:11 +00:00
public ngOnInit ( ) : void {
2018-03-26 15:47:21 +01:00
2017-12-13 12:37:11 +00:00
}
2018-04-11 19:10:03 +01:00
private setChartData ( dataLocal : any , dataCat : any ) {
console . log ( dataLocal , dataCat ) ;
this . doughnutChartDataLocal = Object . keys ( dataLocal ) . map ( key = > dataLocal [ key ] ) ;
this . barChartLabelsCategory = Object . keys ( dataCat ) ;
this . barChartDataCategory = Object . keys ( dataCat ) . map ( key = > dataCat [ key ] ) ;
this . doughnutChartDataCategory = this . weekList1 . map ( function ( a ) { return a . value ; } ) ;
2017-12-14 16:44:23 +00:00
// setTimeout is currently a workaround for ng2-charts labels
2018-04-11 19:10:03 +01:00
setTimeout ( ( ) = > this . doughnutChartLabelsLocal = Object . keys ( dataLocal ) , 0 ) ;
setTimeout ( ( ) = > this . doughnutChartLabelsCategory = this . weekList1 . map ( function ( a ) { return a . category ; } ) , 0 ) ;
console . log ( this . barChartDataCategory ) ;
console . log ( this . barChartLabelsCategory ) ;
this . showCategoryBarChart = true ;
2018-03-26 15:47:21 +01:00
}
private setDate ( ) {
this . myWeek1 = moment ( ) . startOf ( 'isoWeek' ) . format ( 'YYYY-MM-DD' ) ;
this . myWeek2 = moment ( this . myWeek1 ) . subtract ( 1 , 'weeks' ) . format ( 'YYYY-MM-DD' ) ;
this . myWeek3 = moment ( this . myWeek2 ) . subtract ( 1 , 'weeks' ) . format ( 'YYYY-MM-DD' ) ;
this . myWeek4 = moment ( this . myWeek3 ) . subtract ( 1 , 'weeks' ) . format ( 'YYYY-MM-DD' ) ;
console . log ( this . myWeek1 , this . myWeek2 , this . myWeek3 , this . myWeek4 )
}
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 16:44:23 +00:00
}
2017-12-13 12:37:11 +00:00
// convert Hex to RGBA
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 ;
}
// events
public chartClicked ( e : any ) : void {
console . log ( e ) ;
}
public chartHovered ( e : any ) : void {
console . log ( e ) ;
}
2018-04-11 19:10:03 +01:00
private prop < T , K extends keyof T > ( obj : T , key : K ) {
return obj [ key ] ;
}
2017-12-13 12:37:11 +00:00
}