import { Component, OnInit } from '@angular/core'; import {Color, Label} from 'ng2-charts'; import {ChartDataSets, ChartOptions, ChartType} from 'chart.js'; import {LocationService, UserService} from '@app/_services'; import { ArraySum } from '@app/_helpers'; @Component({ selector: 'app-pages', templateUrl: './pages.component.html', styleUrls: ['./pages.component.scss'] }) export class PagesComponent implements OnInit { disbursements: number = 0; users: any; locations: any; transactions: number = 0; public lineChartData: ChartDataSets[] = [ { data: [65, 59, 80, 81, 56, 55, 40], label: 'User Registration'}, { data: [28, 48, 40, 19, 86, 27, 90], label: 'Transaction Volumes'}, { data: [180, 480, 770, 90, 1000, 270, 400], label: 'Token Disbursements', yAxisID: 'y-axis-1'} ]; public lineChartLabels: Label[] = ['January', 'February', 'March', 'April', 'May', 'June', 'July']; 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'; public barChartOptions: ChartOptions = { responsive: true, // We use these empty structures as placeholders for dynamic theming. scales: { xAxes: [{}], yAxes: [{}] }, plugins: { datalabels: { anchor: 'end', align: 'end', } } }; public barChartLabels: Label[] = ['January', 'February', 'March', 'April', 'May', 'June', 'July']; public barChartType: ChartType = 'horizontalBar'; public barChartLegend = true; public barChartData: ChartDataSets[] = [ { data: [65, 59, 80, 81, 56, 55, 40], label: 'New Users'}, { data: [28, 48, 40, 19, 86, 27, 90], label: 'Recurrent Users'} ]; public transferUsagesChartLabels: Label[] = ['Food/Water', 'Fuel/Energy', 'Education', 'Health', 'Shop', 'Environment', 'Transport', 'Farming/Labour', 'Savings Group', 'Savings Group']; public transferUsagesChartData: number[] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; public transferUsagesChartType: ChartType = 'pie'; public transferUsagesChartOptions: ChartOptions = { responsive: true, legend: { position: 'top', }, plugins: { datalabels: { formatter: (value, ctx) => { const label = ctx.chart.data.labels[ctx.dataIndex]; return label; }, }, } }; public transferUsagesChartLegend = true; public transferUsagesChartColors = [ { backgroundColor: [ 'rgba(0,0,255,0.3)', 'rgba(255,0,0,0.3)', 'rgba(0,255,0,0.3)', 'rgba(255,0,255,0.3)', 'rgba(255,255,0,0.3)', 'rgba(0,255,255,0.3)', 'rgba(255,255,255,0.3)', 'rgba(255,100,0,0.3)', 'rgba(0,255,100,0.3)', 'rgba(100,0,255,0.3)'], }, ]; constructor( private userService: UserService, private locationService: LocationService ) { this.locationService.getLocations(); this.locationService.locationsSubject.subscribe(locations => { this.locations = locations; }); } ngOnInit(): void { this.newDataPoint([50, 80], 'August'); this.transferUsagesChartData = [18, 12, 10, 8, 6, 5, 5, 3, 2, 1]; this.disbursements = ArraySum.arraySum(this.lineChartData.find(data => data.label === 'Token Disbursements').data); this.users = ArraySum.arraySum(this.barChartData.find(data => data.label === 'New Users').data); this.transactions = ArraySum.arraySum(this.lineChartData.find(data => data.label === 'Transaction Volumes').data); } newDataPoint(dataArr: any[], label: string): void { this.barChartData.forEach((dataset, index) => { this.barChartData[index] = Object.assign({}, this.barChartData[index], { data: [...this.barChartData[index].data, dataArr[index]] }); }); this.barChartLabels = [...this.barChartLabels, label]; } public chartClicked({ event, active}: { event: MouseEvent, active: {}[] }): void { console.log(event, active); } public chartHovered({ event, active }: { event: MouseEvent, active: {}[] }): void { console.log(event, active); } }