Skip to content

Commit deb4b3d

Browse files
authored
add date formatting depend on period (#273)
1 parent a8a4370 commit deb4b3d

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/NuGetTrends.Web/Portal/src/app/packages/packages.component.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ export class PackagesComponent implements OnInit, OnDestroy {
120120
* Initializes the chart with the first added package
121121
*/
122122
private initializeChart(firstPackageData: IPackageDownloadHistory): void {
123+
let rawDates: Date[] = firstPackageData.downloads.map((download: IDownloadStats) => download.week);
123124
this.chartData.labels = firstPackageData.downloads.map((download: IDownloadStats) => {
124-
return this.datePipe.transform(download.week, 'MMM d')!;
125+
return this.getFormattedDate(download.week, this.packageInteractionService.searchPeriod);
125126
});
126127

127128
this.chartData.datasets!.push(this.parseDataSet(firstPackageData));
@@ -135,6 +136,11 @@ export class PackagesComponent implements OnInit, OnDestroy {
135136
},
136137
tooltips: {
137138
callbacks: {
139+
title: (tooltipItems: any[]) => {
140+
const tooltipItem = tooltipItems[0];
141+
const rawDate = rawDates[tooltipItem.index];
142+
return this.datePipe.transform(rawDate, 'dd MMM yyyy')!;
143+
},
138144
label: (tooltipItem: any, data: any) => {
139145
let label = data.datasets[tooltipItem.datasetIndex].label || 'NuGet Package';
140146
if (label) {
@@ -269,4 +275,19 @@ export class PackagesComponent implements OnInit, OnDestroy {
269275
queryParamsHandling: 'merge'
270276
});
271277
}
278+
279+
/**
280+
* Gets correctly formated date depending on the period
281+
*/
282+
private getFormattedDate(date: Date | string, period: number): string {
283+
if (period >= 3 && period <= 6) {
284+
return this.datePipe.transform(date, 'dd.MMM.yyyy')!;
285+
} else if (period >= 12 && period <= 24) {
286+
return this.datePipe.transform(date, 'MMM.yyyy')!;
287+
} else if (period >= 72 && period <= 120) {
288+
return this.datePipe.transform(date, 'yyyy')!;
289+
} else {
290+
return this.datePipe.transform(date, 'MMM yyyy')!;
291+
}
292+
}
272293
}

0 commit comments

Comments
 (0)