11import { DOCUMENT , NgStyle } from '@angular/common' ;
2- import { Component , DestroyRef , inject , OnInit , Renderer2 , signal , WritableSignal } from '@angular/core' ;
2+ import { Component , DestroyRef , effect , inject , OnInit , Renderer2 , signal , WritableSignal } from '@angular/core' ;
33import { FormControl , FormGroup , ReactiveFormsModule } from '@angular/forms' ;
4- import { getStyle } from '@coreui/utils' ;
5- import { DashboardChartsData , IChartProps } from './dashboard-charts-data' ;
6- import { WidgetsBrandComponent } from '../widgets/widgets-brand/widgets-brand.component' ;
4+ import { ChartOptions , ScaleOptions } from 'chart.js' ;
5+ import {
6+ AvatarComponent ,
7+ ButtonDirective ,
8+ ButtonGroupComponent ,
9+ CardBodyComponent ,
10+ CardComponent ,
11+ CardFooterComponent ,
12+ CardHeaderComponent ,
13+ ColComponent ,
14+ FormCheckLabelDirective ,
15+ GutterDirective ,
16+ ProgressBarDirective ,
17+ ProgressComponent ,
18+ RowComponent ,
19+ TableDirective ,
20+ TextColorDirective
21+ } from '@coreui/angular' ;
722import { ChartjsComponent } from '@coreui/angular-chartjs' ;
823import { IconDirective } from '@coreui/icons-angular' ;
9- import { TextColorDirective , CardComponent , CardBodyComponent , RowComponent , ColComponent , ButtonDirective , ButtonGroupComponent , FormCheckLabelDirective , CardFooterComponent , GutterDirective , ProgressBarDirective , ProgressComponent , CardHeaderComponent , TableDirective , AvatarComponent } from '@coreui/angular' ;
24+ import { getStyle } from '@coreui/utils' ;
25+
26+ import { WidgetsBrandComponent } from '../widgets/widgets-brand/widgets-brand.component' ;
1027import { WidgetsDropdownComponent } from '../widgets/widgets-dropdown/widgets-dropdown.component' ;
28+ import { DashboardChartsData , IChartProps } from './dashboard-charts-data' ;
1129
1230interface IUser {
1331 name : string ;
@@ -24,10 +42,10 @@ interface IUser {
2442}
2543
2644@Component ( {
27- templateUrl : 'dashboard.component.html' ,
28- styleUrls : [ 'dashboard.component.scss' ] ,
29- standalone : true ,
30- imports : [ WidgetsDropdownComponent , TextColorDirective , CardComponent , CardBodyComponent , RowComponent , ColComponent , ButtonDirective , IconDirective , ReactiveFormsModule , ButtonGroupComponent , FormCheckLabelDirective , ChartjsComponent , NgStyle , CardFooterComponent , GutterDirective , ProgressBarDirective , ProgressComponent , WidgetsBrandComponent , CardHeaderComponent , TableDirective , AvatarComponent ]
45+ templateUrl : 'dashboard.component.html' ,
46+ styleUrls : [ 'dashboard.component.scss' ] ,
47+ standalone : true ,
48+ imports : [ WidgetsDropdownComponent , TextColorDirective , CardComponent , CardBodyComponent , RowComponent , ColComponent , ButtonDirective , IconDirective , ReactiveFormsModule , ButtonGroupComponent , FormCheckLabelDirective , ChartjsComponent , NgStyle , CardFooterComponent , GutterDirective , ProgressBarDirective , ProgressComponent , WidgetsBrandComponent , CardHeaderComponent , TableDirective , AvatarComponent ]
3149} )
3250export class DashboardComponent implements OnInit {
3351
@@ -116,8 +134,14 @@ export class DashboardComponent implements OnInit {
116134 color : 'dark'
117135 }
118136 ] ;
119- public mainChart : IChartProps = { } ;
137+
138+ public mainChart : IChartProps = { type : 'line' } ;
120139 public mainChartRef : WritableSignal < any > = signal ( undefined ) ;
140+ #mainChartRefEffect = effect ( ( ) => {
141+ if ( this . mainChartRef ( ) ) {
142+ this . setChartStyles ( ) ;
143+ }
144+ } ) ;
121145 public chart : Array < IChartProps > = [ ] ;
122146 public trafficRadioGroup = new FormGroup ( {
123147 trafficRadio : new FormControl ( 'Month' )
@@ -146,23 +170,51 @@ export class DashboardComponent implements OnInit {
146170
147171 updateChartOnColorModeChange ( ) {
148172 const unListen = this . #renderer. listen ( this . #document. documentElement , 'ColorSchemeChange' , ( ) => {
149- if ( this . mainChartRef ( ) ) {
150- setTimeout ( ( ) => {
151- const scales = { ...this . mainChart . options . scales } ;
152- scales . x . grid . borderColor = getStyle ( '--cui-border-color-translucent' ) ;
153- scales . x . grid . color = getStyle ( '--cui-border-color-translucent' ) ;
154- scales . x . ticks . color = getStyle ( '--cui-body-color' ) ;
155- scales . y . border . color = getStyle ( '--cui-border-color-translucent' ) ;
156- scales . y . grid . borderColor = getStyle ( '--cui-border-color-translucent' ) ;
157- scales . y . grid . color = getStyle ( '--cui-border-color-translucent' ) ;
158- scales . y . ticks . color = getStyle ( '--cui-body-color' ) ;
159- this . mainChartRef ( ) . options . scales = { ...scales } ;
160- this . mainChartRef ( ) . update ( ) ;
161- } ) ;
162- }
173+ this . setChartStyles ( ) ;
163174 } ) ;
175+
164176 this . #destroyRef. onDestroy ( ( ) => {
165177 unListen ( ) ;
166178 } ) ;
167179 }
180+
181+ setChartStyles ( ) {
182+ if ( this . mainChartRef ( ) ) {
183+ setTimeout ( ( ) => {
184+
185+ const options : ChartOptions = { ...this . mainChart . options } ;
186+ const colorBorderTranslucent = getStyle ( '--cui-border-color-translucent' ) ;
187+ const colorBody = getStyle ( '--cui-body-color' ) ;
188+
189+ const scales : ScaleOptions < any > = {
190+ x : {
191+ grid : {
192+ color : colorBorderTranslucent ,
193+ drawOnChartArea : false
194+ } ,
195+ ticks : {
196+ color : colorBody
197+ }
198+ } ,
199+ y : {
200+ border : {
201+ color : colorBorderTranslucent
202+ } ,
203+ grid : {
204+ color : colorBorderTranslucent
205+ } ,
206+ max : 250 ,
207+ beginAtZero : true ,
208+ ticks : {
209+ color : colorBody ,
210+ maxTicksLimit : 5 ,
211+ stepSize : Math . ceil ( 250 / 5 )
212+ }
213+ }
214+ } ;
215+ this . mainChartRef ( ) . options = { ...options , scales : { ...options . scales , ...scales } } ;
216+ this . mainChartRef ( ) . update ( ) ;
217+ } ) ;
218+ }
219+ }
168220}
0 commit comments