Skip to content

Commit 93b9340

Browse files
committed
fix(angular-chartjs): chart canvas incorrect width on first render
1 parent 331201d commit 93b9340

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

projects/coreui-angular-chartjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coreui/angular-chartjs",
3-
"version": "4.2.0",
3+
"version": "4.2.2",
44
"description": "Angular wrapper component for Chart.js",
55
"copyright": "Copyright 2022 creativeLabs Łukasz Holeczek",
66
"license": "MIT",

projects/coreui-angular-chartjs/src/lib/chartjs.component.spec.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
22

33
import { ChartjsComponent } from './chartjs.component';
44
import { Chart, registerables } from 'chart.js';
@@ -42,30 +42,33 @@ describe('ChartjsComponent', () => {
4242
component.type = 'line';
4343
});
4444

45-
it('chart should create', () => {
45+
it('chart should create', fakeAsync(() => {
4646
fixture.detectChanges();
47+
tick();
4748
expect(component).toBeTruthy();
4849
expect(component.chart).toBeDefined();
49-
});
50+
}));
5051

51-
it('chart should receive data', () => {
52+
it('chart should receive data', fakeAsync(() => {
5253
component.data = { ...data };
5354
fixture.detectChanges();
55+
tick();
5456
expect(component.chart?.config.data.labels?.length).toBe(7);
5557
expect(component.chart?.config.data.labels).toEqual(labels);
5658
expect(component.chart?.config.data.datasets[0]?.data.length).toBe(7);
57-
});
59+
}));
5860

59-
it('chart to Base64Image', () => {
61+
it('chart to Base64Image', fakeAsync(() => {
6062
component.data = { ...data };
6163
fixture.detectChanges();
64+
tick();
6265
const image = component.chartToBase64Image();
6366
expect(image).toBeDefined();
6467
expect(typeof image).toBe('string');
6568
expect(image).toContain('data:image/png;base64,');
66-
});
69+
}));
6770

68-
it('chart should update on data change', () => {
71+
it('chart should update on data change', fakeAsync(() => {
6972
component.data = {
7073
...data,
7174
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
@@ -75,7 +78,8 @@ describe('ChartjsComponent', () => {
7578
]
7679
};
7780
fixture.detectChanges();
78-
expect(component.chart.config?.data.labels?.length).toBe(5);
79-
expect(component.chart.config?.data.datasets[1]?.data.length).toBe(5);
80-
});
81+
tick();
82+
expect(component.chart?.config?.data.labels?.length).toBe(5);
83+
expect(component.chart?.config?.data.datasets[1]?.data.length).toBe(5);
84+
}));
8185
});

projects/coreui-angular-chartjs/src/lib/chartjs.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ export class ChartjsComponent<TType extends ChartType = ChartType, TData = Defau
143143
this.ngZone.runOutsideAngular(() => {
144144
const config = this.chartConfig();
145145
if (config) {
146-
this.chart = new Chart(ctx, config);
147146
setTimeout(() => {
147+
this.chart = new Chart(ctx, config);
148148
this.renderer.setStyle(this.canvasElement.nativeElement, 'display', 'block');
149149
});
150150
}

0 commit comments

Comments
 (0)