Skip to content

Commit e92aefb

Browse files
committed
refactor(dashboard): main chart data - typings and theme switching fix
1 parent 27b63fb commit e92aefb

File tree

3 files changed

+98
-46
lines changed

3 files changed

+98
-46
lines changed

Diff for: src/app/views/dashboard/dashboard-charts-data.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Injectable } from '@angular/core';
2+
import { ChartData, ChartDataset, ChartOptions, ChartType, PluginOptionsByType, TooltipLabelStyle } from 'chart.js';
3+
import { DeepPartial } from 'chart.js/dist/types/utils';
24
import { getStyle, hexToRgba } from '@coreui/utils';
35

46
export interface IChartProps {
5-
data?: any;
7+
data?: ChartData;
68
labels?: any;
7-
options?: any;
9+
options?: ChartOptions;
810
colors?: any;
9-
type?: any;
11+
type: ChartType;
1012
legend?: any;
1113

1214
[propName: string]: any;
@@ -17,10 +19,10 @@ export interface IChartProps {
1719
})
1820
export class DashboardChartsData {
1921
constructor() {
20-
this.initMainChart();
22+
this.initMainChart();
2123
}
2224

23-
public mainChart: IChartProps = {};
25+
public mainChart: IChartProps = { type: 'line' };
2426

2527
public random(min: number, max: number) {
2628
return Math.floor(Math.random() * (max - min + 1) + min);
@@ -29,7 +31,7 @@ export class DashboardChartsData {
2931
initMainChart(period: string = 'Month') {
3032
const brandSuccess = getStyle('--cui-success') ?? '#4dbd74';
3133
const brandInfo = getStyle('--cui-info') ?? '#20a8d8';
32-
const brandInfoBg = hexToRgba(getStyle('--cui-info') ?? '#20a8d8', 10) ;
34+
const brandInfoBg = hexToRgba(getStyle('--cui-info') ?? '#20a8d8', 10);
3335
const brandDanger = getStyle('--cui-danger') ?? '#f86c6b';
3436

3537
// mainChart
@@ -100,7 +102,7 @@ export class DashboardChartsData {
100102
}
101103
];
102104

103-
const datasets = [
105+
const datasets: ChartDataset[] = [
104106
{
105107
data: this.mainChart['Data1'],
106108
label: 'Current',
@@ -118,45 +120,44 @@ export class DashboardChartsData {
118120
}
119121
];
120122

121-
const plugins = {
123+
const plugins: DeepPartial<PluginOptionsByType<any>> = {
122124
legend: {
123125
display: false
124126
},
125127
tooltip: {
126128
callbacks: {
127-
labelColor: function(context: any) {
128-
return {
129-
backgroundColor: context.dataset.borderColor
130-
};
131-
}
129+
labelColor: (context) => ({ backgroundColor: context.dataset.borderColor } as TooltipLabelStyle)
132130
}
133131
}
134132
};
135133

136-
const options = {
134+
const colorBorderTranslucent = getStyle('--cui-border-color-translucent');
135+
const colorBody = getStyle('--cui-body-color');
136+
137+
const options: ChartOptions = {
137138
maintainAspectRatio: false,
138139
plugins,
139140
scales: {
140141
x: {
141142
grid: {
142-
color: getStyle('--cui-border-color-translucent'),
143+
color: colorBorderTranslucent,
143144
drawOnChartArea: false
144145
},
145146
ticks: {
146-
color: getStyle('--cui-body-color'),
147-
},
147+
color: colorBody
148+
}
148149
},
149150
y: {
150151
border: {
151-
color: getStyle('--cui-border-color-translucent'),
152+
color: colorBorderTranslucent
152153
},
153154
grid: {
154-
color: getStyle('--cui-border-color-translucent'),
155+
color: colorBorderTranslucent
155156
},
156157
max: 250,
157158
beginAtZero: true,
158159
ticks: {
159-
color: getStyle('--cui-body-color'),
160+
color: colorBody,
160161
maxTicksLimit: 5,
161162
stepSize: Math.ceil(250 / 5)
162163
}
@@ -182,5 +183,4 @@ export class DashboardChartsData {
182183
labels
183184
};
184185
}
185-
186186
}

Diff for: src/app/views/dashboard/dashboard.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ <h4 class="card-title mb-0" id="traffic">Traffic</h4>
339339
@for (user of users; track user.name; let i = $index) {
340340
<tr>
341341
<td class="text-center">
342-
<c-avatar size="md" src="{{ user.avatar }}" status="{{ user.status }}" />
342+
<c-avatar [size]="'md'" src="{{ user.avatar }}" status="{{ user.status }}" />
343343
</td>
344344
<td>
345345
<div>{{ user.name }}</div>

Diff for: src/app/views/dashboard/dashboard.component.ts

+76-24
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
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';
33
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';
722
import { ChartjsComponent } from '@coreui/angular-chartjs';
823
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';
1027
import { WidgetsDropdownComponent } from '../widgets/widgets-dropdown/widgets-dropdown.component';
28+
import { DashboardChartsData, IChartProps } from './dashboard-charts-data';
1129

1230
interface 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
})
3250
export 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

Comments
 (0)