diff --git a/src/app/widgets/customers.component.ts b/src/app/widgets/customers.component.ts deleted file mode 100644 index 7ccc692..0000000 --- a/src/app/widgets/customers.component.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { Component, OnInit } from '@angular/core'; -import { OrgGraphsService } from '../providers/org-graphs.service'; - -interface ChartData { - data: Array; - label: string; -} - -@Component({ - selector: 'widget-customers-last-7-days', - templateUrl: 'customers.component.html', -}) -export class Customer7DayWidget implements OnInit { - - public lineChartData: Array = [ - { - data: [], - label: 'Series A' - } - ]; - public lineChartLabels: Array; - 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 - } - }; - public lineChartColours: Array = [ - { // grey - backgroundColor: '#20a8d8', - borderColor: 'rgba(255,255,255,.55)' - } - ]; - public lineChartLegend = false; - public lineChartType = 'line'; - - public customerSum: Number = 0; - - constructor(private graphService: OrgGraphsService) { } - - ngOnInit(): void { - this.graphService.getGraph('customers_last_7_days') - .subscribe( - result => { - console.log(result); - this.lineChartData[0].data = result.graph.count; - this.lineChartLabels = result.graph.day; - this.customerSum = this.lineChartData[0].data.reduce((a, b) => a + b, 0); - } - ); - } - - // events - public chartClicked(e: any): void { - console.log(e); - } - - public chartHovered(e: any): void { - console.log(e); - } -} diff --git a/src/app/widgets/customers.component.html b/src/app/widgets/graph-widget.component.html similarity index 50% rename from src/app/widgets/customers.component.html rename to src/app/widgets/graph-widget.component.html index 2eb47af..9c008e3 100644 --- a/src/app/widgets/customers.component.html +++ b/src/app/widgets/graph-widget.component.html @@ -1,17 +1,10 @@
- -

{{ customerSum }}

-

Customers this week

+ +

{{ graphSum }}

+

{{ graphTitle }}

; + 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'; + + @Output() public graphHover = new EventEmitter(); + @Output() public graphClick = new EventEmitter(); + + public graphSum: Number = 0; + + public lineChartData: Array = [ + { + data: [], + label: 'Series A' + } + ]; + public lineChartLabels: Array; + 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 + } + }; + public lineChartColours: Array = [ + { + backgroundColor: '#20a8d8', + borderColor: 'rgba(255,255,255,.55)' + } + ]; + public lineChartLegend = false; + public lineChartType = 'line'; + + + constructor(private graphService: OrgGraphsService) { } + + ngOnInit(): void { + if ( this.graphName == null ) { + throw new Error('Attribute \'graphName\' is required on component \'widget-graph\''); + } + this.graphService.getGraph(this.graphName) + .subscribe( result => this.setData(result.graph) ); + } + + private setData(data: any) { + this.setChartData(data.count); + this.setChartLabels(data.day); + } + + private setChartData(data: Array) { + 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) { + this.lineChartLabels = data; + } + + // events + public chartClicked(e: any): void { + console.log(e); + } + + public chartHovered(e: any): void { + console.log(e); + } +}