1
1
import { 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' ;
3
3
import { 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' ;
7
22
import { ChartjsComponent } from '@coreui/angular-chartjs' ;
8
23
import { 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' ;
10
27
import { WidgetsDropdownComponent } from '../widgets/widgets-dropdown/widgets-dropdown.component' ;
28
+ import { DashboardChartsData , IChartProps } from './dashboard-charts-data' ;
11
29
12
30
interface IUser {
13
31
name : string ;
@@ -24,10 +42,10 @@ interface IUser {
24
42
}
25
43
26
44
@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 ]
31
49
} )
32
50
export class DashboardComponent implements OnInit {
33
51
@@ -116,8 +134,14 @@ export class DashboardComponent implements OnInit {
116
134
color : 'dark'
117
135
}
118
136
] ;
119
- public mainChart : IChartProps = { } ;
137
+
138
+ public mainChart : IChartProps = { type : 'line' } ;
120
139
public mainChartRef : WritableSignal < any > = signal ( undefined ) ;
140
+ #mainChartRefEffect = effect ( ( ) => {
141
+ if ( this . mainChartRef ( ) ) {
142
+ this . setChartStyles ( ) ;
143
+ }
144
+ } ) ;
121
145
public chart : Array < IChartProps > = [ ] ;
122
146
public trafficRadioGroup = new FormGroup ( {
123
147
trafficRadio : new FormControl ( 'Month' )
@@ -146,23 +170,51 @@ export class DashboardComponent implements OnInit {
146
170
147
171
updateChartOnColorModeChange ( ) {
148
172
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 ( ) ;
163
174
} ) ;
175
+
164
176
this . #destroyRef. onDestroy ( ( ) => {
165
177
unListen ( ) ;
166
178
} ) ;
167
179
}
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
+ }
168
220
}
0 commit comments