2017-08-30 12:35:25 +01:00
import { Component , OnInit , Input , Output , EventEmitter } from '@angular/core' ;
2017-09-07 13:32:42 +01:00
import { CurrencyPipe } from '@angular/common' ;
2017-08-30 12:35:25 +01:00
import { OrgGraphsService } from '../providers/org-graphs.service' ;
2017-09-07 13:32:42 +01:00
import { DataType } from '../shared/data-types.enum' ;
2017-08-30 12:35:25 +01:00
interface ChartData {
data : Array < number > ;
label : string ;
}
@Component ( {
selector : 'widget-graph' ,
templateUrl : 'graph-widget.component.html' ,
} )
export class GraphWidget implements OnInit {
@Input ( ) public graphName : string ;
@Input ( ) public graphTitle = 'Graph' ;
@Input ( ) public graphIcon = 'icon-graph' ;
2017-09-07 13:32:42 +01:00
@Input ( ) public dataType : DataType = DataType . number ;
2017-08-30 12:35:25 +01:00
@Output ( ) public graphHover = new EventEmitter ( ) ;
@Output ( ) public graphClick = new EventEmitter ( ) ;
public graphSum : Number = 0 ;
2017-09-07 13:32:42 +01:00
public availableDataTypes = DataType ;
2017-08-30 12:35:25 +01:00
public lineChartData : Array < ChartData > = [
{
data : [ ] ,
label : 'Series A'
}
] ;
public lineChartLabels : Array < string > ;
public lineChartOptions : any = {
maintainAspectRatio : false ,
scales : {
xAxes : [ {
gridLines : {
color : 'transparent' ,
zeroLineColor : 'transparent'
} ,
ticks : {
fontSize : 2 ,
fontColor : 'transparent' ,
}
} ] ,
yAxes : [ {
display : false ,
ticks : {
display : false ,
}
} ] ,
} ,
elements : {
line : {
borderWidth : 1
} ,
point : {
radius : 4 ,
hitRadius : 10 ,
hoverRadius : 4 ,
} ,
} ,
legend : {
display : false
2017-09-07 13:32:42 +01:00
} ,
tooltips : {
callbacks : {
label : ( tooltip , data ) = > {
return this . tooltipLabelCallback ( tooltip , data ) ;
} ,
} ,
} ,
2017-08-30 12:35:25 +01:00
} ;
public lineChartColours : Array < any > = [
{
backgroundColor : '#20a8d8' ,
borderColor : 'rgba(255,255,255,.55)'
}
] ;
public lineChartLegend = false ;
public lineChartType = 'line' ;
2017-09-07 13:32:42 +01:00
constructor (
private graphService : OrgGraphsService ,
private currencyPipe : CurrencyPipe ,
) { }
2017-08-30 12:35:25 +01:00
ngOnInit ( ) : void {
if ( this . graphName == null ) {
throw new Error ( 'Attribute \'graphName\' is required on component \'widget-graph\'' ) ;
}
2017-09-07 13:32:42 +01:00
if ( this . dataType === undefined ) {
// Need to do this as it may be passed in a loop with an undefined value
this . dataType = DataType . number ;
}
if ( ! ( this . dataType in DataType ) ) {
console . warn ( 'Unknown DataType for graph \'' + this . graphName + '\' - defaulting to number' ) ;
}
2017-08-30 12:35:25 +01:00
this . graphService . getGraph ( this . graphName )
. subscribe ( result = > this . setData ( result . graph ) ) ;
}
private setData ( data : any ) {
2017-09-05 12:04:36 +01:00
this . setChartData ( data . data ) ;
this . setChartLabels ( data . labels ) ;
2017-08-30 12:35:25 +01:00
}
private setChartData ( data : Array < number > ) {
this . lineChartData [ 0 ] . data = data ;
this . graphSum = data . reduce ( ( a , b ) = > a + b , 0 ) ;
// Set point size based on data
if ( data . length < 15 ) {
this . lineChartOptions . elements . point . radius = 4 ;
this . lineChartOptions . elements . line . borderWidth = 1 ;
} else {
this . lineChartOptions . elements . point . radius = 2 ;
this . lineChartOptions . elements . line . borderWidth = 2 ;
}
}
private setChartLabels ( data : Array < string > ) {
this . lineChartLabels = data ;
}
// events
public chartClicked ( e : any ) : void {
console . log ( e ) ;
}
public chartHovered ( e : any ) : void {
console . log ( e ) ;
}
2017-09-07 13:32:42 +01:00
private tooltipLabelCallback ( tooltipItem : any , data : any ) {
const value = tooltipItem . yLabel ;
if ( this . dataType === DataType . currency ) {
2017-11-15 13:46:34 +00:00
return this . currencyPipe . transform ( value , 'GBP' , 'symbol' , '1.2-2' ) ;
2017-09-07 13:32:42 +01:00
}
return value || '0' ;
}
2017-08-30 12:35:25 +01:00
}