import { Component, ElementRef, Input, OnChanges, ViewChild } from '@angular/core'; import { CurrencyService } from '@core/services/currency.service'; import { ApplicantTopLevelStats } from '@core/typings/applicant.typing'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { ChartService } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { ChartData, ChartOptions, ChartType, TooltipItem } from 'chart.js'; @Component({ selector: 'gc-applicant-top-level-pie', templateUrl: './applicant-top-level-pie.component.html', styleUrls: ['./applicant-top-level-pie.component.scss'] }) export class ApplicantTopLevelPieComponent implements OnChanges { @Input() applicantTopLevelStats: ApplicantTopLevelStats; @ViewChild('pieChart') pieChart: ElementRef; data: number[]; hidePie = false; branding = this.clientSettingsService.get('clientBranding'); backgroundColors: string[] = []; legendData: { label: string; color: string; }[]; labels = [ this.i18n.translate( 'common:textPaymentTotal', {}, 'Payment total' ), this.i18n.translate( 'common:textAvailableToPay', {}, 'Available to pay' ) ]; chartOptions: ChartOptions = { responsive: true, plugins: { legend: { display: false } }, tooltips: { callbacks: { label: (tooltipItem: TooltipItem, data: ChartData) => { return this.chartService.getLabelForTooltip( tooltipItem, data ); }, afterLabel: (tooltipItem: TooltipItem, data: ChartData) => { const amount = this.chartService.getAmountForTooltip( tooltipItem, data ); return this.currencyService.formatMoney( amount ); } } } } as ChartOptions; constructor ( private i18n: I18nService, private clientSettingsService: ClientSettingsService, private chartService: ChartService, private currencyService: CurrencyService ) { } ngOnChanges () { this.pieChartSetup(); } pieChartSetup () { this.data = [ this.applicantTopLevelStats.paymentsTotal, this.applicantTopLevelStats.awardsTotal - this.applicantTopLevelStats.paymentsTotal ]; this.backgroundColors = this.chartService.getFixedAmountOfColors( this.data.length, [ this.branding.brandPrimary, this.branding.brandSecondary, this.branding.brandUtility ] ); this.legendData = this.chartService.generateLegendData( this.data, this.labels, this.backgroundColors ); const legitData = this.data.filter((data) => !!data); this.hidePie = legitData.length === 0; } }