Skip to content

Commit b87bb92

Browse files
committed
fix(dashboard): missing custom tooltips on first render, refactor main chart scales
1 parent 54089e7 commit b87bb92

File tree

2 files changed

+48
-63
lines changed

2 files changed

+48
-63
lines changed

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

+45-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { Injectable } from '@angular/core';
2-
import { ChartData, ChartDataset, ChartOptions, ChartType, PluginOptionsByType, TooltipLabelStyle } from 'chart.js';
2+
import {
3+
ChartData,
4+
ChartDataset,
5+
ChartOptions,
6+
ChartType,
7+
PluginOptionsByType,
8+
ScaleOptions,
9+
TooltipLabelStyle
10+
} from 'chart.js';
311
import { DeepPartial } from 'chart.js/dist/types/utils';
412
import { getStyle, hexToRgba } from '@coreui/utils';
513

@@ -19,7 +27,7 @@ export interface IChartProps {
1927
})
2028
export class DashboardChartsData {
2129
constructor() {
22-
this.initMainChart();
30+
this.initMainChart();
2331
}
2432

2533
public mainChart: IChartProps = { type: 'line' };
@@ -131,38 +139,12 @@ export class DashboardChartsData {
131139
}
132140
};
133141

134-
const colorBorderTranslucent = getStyle('--cui-border-color-translucent');
135-
const colorBody = getStyle('--cui-body-color');
142+
const scales = this.getScales();
136143

137144
const options: ChartOptions = {
138145
maintainAspectRatio: false,
139146
plugins,
140-
scales: {
141-
x: {
142-
grid: {
143-
color: colorBorderTranslucent,
144-
drawOnChartArea: false
145-
},
146-
ticks: {
147-
color: colorBody
148-
}
149-
},
150-
y: {
151-
border: {
152-
color: colorBorderTranslucent
153-
},
154-
grid: {
155-
color: colorBorderTranslucent
156-
},
157-
max: 250,
158-
beginAtZero: true,
159-
ticks: {
160-
color: colorBody,
161-
maxTicksLimit: 5,
162-
stepSize: Math.ceil(250 / 5)
163-
}
164-
}
165-
},
147+
scales,
166148
elements: {
167149
line: {
168150
tension: 0.4
@@ -183,4 +165,37 @@ export class DashboardChartsData {
183165
labels
184166
};
185167
}
168+
169+
getScales() {
170+
const colorBorderTranslucent = getStyle('--cui-border-color-translucent');
171+
const colorBody = getStyle('--cui-body-color');
172+
173+
const scales: ScaleOptions<any> = {
174+
x: {
175+
grid: {
176+
color: colorBorderTranslucent,
177+
drawOnChartArea: false
178+
},
179+
ticks: {
180+
color: colorBody
181+
}
182+
},
183+
y: {
184+
border: {
185+
color: colorBorderTranslucent
186+
},
187+
grid: {
188+
color: colorBorderTranslucent
189+
},
190+
max: 250,
191+
beginAtZero: true,
192+
ticks: {
193+
color: colorBody,
194+
maxTicksLimit: 5,
195+
stepSize: Math.ceil(250 / 5)
196+
}
197+
}
198+
};
199+
return scales;
200+
}
186201
}

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

+3-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DOCUMENT, NgStyle } from '@angular/common';
22
import { Component, DestroyRef, effect, inject, OnInit, Renderer2, signal, WritableSignal } from '@angular/core';
33
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
4-
import { ChartOptions, ScaleOptions } from 'chart.js';
4+
import { ChartOptions } from 'chart.js';
55
import {
66
AvatarComponent,
77
ButtonDirective,
@@ -21,7 +21,6 @@ import {
2121
} from '@coreui/angular';
2222
import { ChartjsComponent } from '@coreui/angular-chartjs';
2323
import { IconDirective } from '@coreui/icons-angular';
24-
import { getStyle } from '@coreui/utils';
2524

2625
import { WidgetsBrandComponent } from '../widgets/widgets-brand/widgets-brand.component';
2726
import { WidgetsDropdownComponent } from '../widgets/widgets-dropdown/widgets-dropdown.component';
@@ -181,38 +180,9 @@ export class DashboardComponent implements OnInit {
181180
setChartStyles() {
182181
if (this.mainChartRef()) {
183182
setTimeout(() => {
184-
185183
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 } };
184+
const scales = this.#chartsData.getScales();
185+
this.mainChartRef().options.scales = { ...options.scales, ...scales };
216186
this.mainChartRef().update();
217187
});
218188
}

0 commit comments

Comments
 (0)