1- import { Component , type OnInit , inject } from '@angular/core' ;
1+ import { type AfterViewInit , Component , type OnInit , inject } from '@angular/core' ;
22import { FormBuilder , type FormGroup , ReactiveFormsModule , Validators } from '@angular/forms' ;
33import { MatButtonModule } from '@angular/material/button' ;
44import { MatCardModule } from '@angular/material/card' ;
55import { MatFormFieldModule } from '@angular/material/form-field' ;
66import { MatInputModule } from '@angular/material/input' ;
77import { MatProgressBarModule } from '@angular/material/progress-bar' ;
88import { MatTableDataSource , MatTableModule } from '@angular/material/table' ;
9+ import { Chart , type ChartConfiguration , registerables } from 'chart.js' ;
910import type { AppResult } from '../../core/app-result' ;
1011import { DavengoService } from '../../core/davengo.service' ;
1112
13+ Chart . register ( ...registerables ) ;
14+
1215@Component ( {
1316 imports : [ MatButtonModule , MatCardModule , MatFormFieldModule , MatInputModule , MatProgressBarModule , MatTableModule , ReactiveFormsModule ] ,
1417 selector : 'dr-dashboard' ,
1518 templateUrl : './dashboard.component.html' ,
1619 styleUrls : [ './dashboard.component.css' ] ,
1720} )
18- export class DashboardComponent implements OnInit {
21+ export class DashboardComponent implements OnInit , AfterViewInit {
1922 private formBuilder = inject ( FormBuilder ) ;
2023 private davengoService = inject ( DavengoService ) ;
2124
@@ -26,6 +29,34 @@ export class DashboardComponent implements OnInit {
2629 dataSource = new MatTableDataSource < AppResult > ( ) ;
2730 displayedColumns : string [ ] = [ 'year' , 'name' , 'teamName' , 'rank' , 'nettoTime' ] ;
2831
32+ lineChart : any ;
33+
34+ lineChartConfig : ChartConfiguration = {
35+ type : 'line' ,
36+ data : {
37+ datasets : [
38+ {
39+ data : [ ] ,
40+ label : 'Davengo data' ,
41+ tension : 0.5
42+ } ,
43+ ] ,
44+ labels : [ ] ,
45+ } ,
46+ options : {
47+ scales : {
48+ y : {
49+ ticks : {
50+ callback : function ( val , index ) {
51+ // Hide every 2nd tick label
52+ return `${ ( Number ( val ) / 60 ) . toFixed ( 2 ) } min` ;
53+ } ,
54+ } ,
55+ } ,
56+ } ,
57+ } ,
58+ } ;
59+
2960 ngOnInit ( ) : void {
3061 this . form = this . formBuilder . group ( {
3162 firstName : [ '' , Validators . required ] ,
@@ -34,6 +65,10 @@ export class DashboardComponent implements OnInit {
3465 } ) ;
3566 }
3667
68+ ngAfterViewInit ( ) : void {
69+ this . lineChart = new Chart ( 'lineChart' , this . lineChartConfig ) ;
70+ }
71+
3772 onSubmit ( ) : void {
3873 this . initialSearch = true ;
3974 this . loading = true ;
@@ -44,6 +79,14 @@ export class DashboardComponent implements OnInit {
4479 . subscribe ( ( appResult ) => {
4580 this . dataSource = new MatTableDataSource ( appResult ) ;
4681 this . loading = false ;
82+ this . lineChartConfig . data . labels = appResult . map ( ( result ) => result . year ) ;
83+ this . lineChartConfig . data . datasets [ 0 ] . data = appResult . map ( ( result ) => this . #convertTimeToTimestamp( result . nettoTime ) ) ;
84+ this . lineChart . update ( ) ;
4785 } ) ;
4886 }
87+
88+ #convertTimeToTimestamp( timeString : string ) : number {
89+ const [ hours , minutes , seconds ] = timeString . split ( ':' ) . map ( Number ) ;
90+ return hours * 3600 + minutes * 60 + seconds ;
91+ }
4992}
0 commit comments