Skip to content

Commit aa3e7f0

Browse files
authored
Run event handlers in zone (#1365)
* Run event handlers in zone * Updated screenshots
1 parent a3367d4 commit aa3e7f0

File tree

10 files changed

+3683
-4192
lines changed

10 files changed

+3683
-4192
lines changed

angular.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"build": {
1818
"builder": "@angular-devkit/build-angular:browser",
1919
"options": {
20-
"preserveSymlinks": true,
2120
"outputPath": "dist/ng2-charts-demo",
2221
"index": "src/index.html",
2322
"main": "src/main.ts",
-1.13 KB
Loading
-5.17 KB
Loading

package-lock.json

Lines changed: 3621 additions & 4162 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
},
2323
"private": true,
2424
"dependencies": {
25-
"@angular/animations": "^13.0.3",
26-
"@angular/cdk": "^13.0.3",
27-
"@angular/common": "^13.0.3",
28-
"@angular/compiler": "^13.0.3",
29-
"@angular/core": "^13.0.3",
30-
"@angular/forms": "^13.0.3",
31-
"@angular/material": "^13.0.3",
32-
"@angular/platform-browser": "^13.0.3",
33-
"@angular/platform-browser-dynamic": "^13.0.3",
34-
"@angular/router": "^13.0.3",
25+
"@angular/animations": "^13.1.1",
26+
"@angular/cdk": "^13.1.1",
27+
"@angular/common": "^13.1.1",
28+
"@angular/compiler": "^13.1.1",
29+
"@angular/core": "^13.1.1",
30+
"@angular/forms": "^13.1.1",
31+
"@angular/material": "^13.1.1",
32+
"@angular/platform-browser": "^13.1.1",
33+
"@angular/platform-browser-dynamic": "^13.1.1",
34+
"@angular/router": "^13.1.1",
3535
"chart.js": "^3.6.0",
3636
"chartjs-adapter-date-fns": "^2.0.0",
3737
"chartjs-chart-financial": "^0.1.0",
@@ -48,18 +48,18 @@
4848
"zone.js": "~0.11.4"
4949
},
5050
"devDependencies": {
51-
"@angular-devkit/build-angular": "^13.0.4",
52-
"@angular-devkit/core": "^13.0.4",
53-
"@angular-devkit/schematics": "^13.0.4",
51+
"@angular-devkit/build-angular": "^13.1.2",
52+
"@angular-devkit/core": "^13.1.2",
53+
"@angular-devkit/schematics": "^13.1.2",
5454
"@angular-eslint/builder": "^13.0.1",
5555
"@angular-eslint/eslint-plugin": "^13.0.1",
5656
"@angular-eslint/eslint-plugin-template": "^13.0.1",
5757
"@angular-eslint/schematics": "^13.0.1",
5858
"@angular-eslint/template-parser": "^13.0.1",
59-
"@angular/cli": "^13.0.4",
60-
"@angular/compiler-cli": "^13.0.3",
61-
"@angular/language-service": "^13.0.3",
62-
"@nrwl/cypress": "^13.2.3",
59+
"@angular/cli": "^13.1.2",
60+
"@angular/compiler-cli": "^13.1.1",
61+
"@angular/language-service": "^13.1.1",
62+
"@nrwl/cypress": "^13.4.3",
6363
"@schematics/angular": "^13.0.3",
6464
"@types/jasmine": "~3.8.0",
6565
"@types/jasminewd2": "^2.0.10",
@@ -68,6 +68,7 @@
6868
"@types/node": "^14.17.32",
6969
"@typescript-eslint/eslint-plugin": "^5.5.0",
7070
"@typescript-eslint/parser": "^5.5.0",
71+
"chartjs-test-utils": "^0.3.1",
7172
"cypress": "^9.1.0",
7273
"cypress-image-snapshot": "^4.0.1",
7374
"eslint": "^8.2.0",
@@ -88,4 +89,4 @@
8889
"tslib": "^2.3.1",
8990
"typescript": "~4.4.4"
9091
}
91-
}
92+
}

projects/ng2-charts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng2-charts",
3-
"version": "3.0.5",
3+
"version": "3.0.6",
44
"publishConfig": {
55
"registry": "https://registry.npmjs.org/"
66
},

projects/ng2-charts/src/lib/base-chart.directive.spec.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import { BaseChartDirective } from './base-chart.directive';
22
import { By } from '@angular/platform-browser';
3-
import { ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
44
import { Component } from '@angular/core';
55
import { Chart, registerables } from "chart.js";
66

77
@Component({
88
template: '<canvas baseChart' +
99
' [data]="data"' +
1010
' [datasets]="datasets"' +
11-
' [labels]="labels"></canvas>'
11+
' [labels]="labels"' +
12+
' (chartClick)="click()"></canvas>'
1213
})
1314
class TestComponent {
1415
public data?: any
1516
public datasets?: any[]
1617
public labels?: string[]
18+
public click? = jasmine.createSpy('chartClick')
19+
public hover? = jasmine.createSpy('chartHover')
1720
}
1821

1922
describe('BaseChartDirective', () => {
@@ -79,7 +82,7 @@ describe('BaseChartDirective', () => {
7982
expect(directive.chart?.data.labels).toEqual(element.data.labels);
8083

8184
element.data = {
82-
labels: [ 'Life', 'Universe', 'Everything'],
85+
labels: [ 'Life', 'Universe', 'Everything' ],
8386
datasets: []
8487
}
8588

@@ -89,4 +92,26 @@ describe('BaseChartDirective', () => {
8992
expect(directive.chart?.data?.labels && directive.chart?.data?.labels[0]).not.toEqual('Answers');
9093
});
9194

95+
it('should emit when the chart is clicked', fakeAsync(() => {
96+
97+
fixture.detectChanges();
98+
99+
let canvas = fixture.nativeElement.querySelector('canvas');
100+
101+
canvas.dispatchEvent(
102+
new MouseEvent(
103+
"click",
104+
{
105+
clientX: canvas.getBoundingClientRect().left + 50,
106+
clientY: canvas.getBoundingClientRect().top + 50,
107+
bubbles: true
108+
}
109+
)
110+
);
111+
112+
tick(25);
113+
114+
expect(element.click).toHaveBeenCalled();
115+
}));
116+
92117
});

projects/ng2-charts/src/lib/base-chart.directive.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,18 @@ export class BaseChartDirective<TType extends ChartType = ChartType,
121121
private getChartOptions(): ChartConfiguration<TType, TData, TLabel>['options'] {
122122
return merge({
123123
onHover: (event: ChartEvent, active: {}[]) => {
124-
if (active && !active.length) {
124+
if (this.chartHover.observed && active && !active.length) {
125125
return;
126126
}
127-
this.chartHover.emit({ event, active });
127+
128+
this.zone.run(() => this.chartHover.emit({ event, active }));
128129
},
129130
onClick: (event?: ChartEvent, active?: {}[]) => {
130-
this.chartClick.emit({ event, active });
131+
if(!this.chartClick.observed){
132+
return;
133+
}
134+
135+
this.zone.run(() => this.chartClick.emit({ event, active }));
131136
}
132137
},
133138
this.themeOverrides,

src/app/bar-chart/bar-chart.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
[data]="barChartData"
66
[options]="barChartOptions"
77
[plugins]="barChartPlugins"
8-
[type]="barChartType">
8+
[type]="barChartType"
9+
(chartHover)="chartHovered($event)"
10+
(chartClick)="chartClicked($event)">
911
</canvas>
1012
</div>
1113
<button mat-button mat-raised-button color="primary" (click)="randomize()">Update</button>

src/app/bar-chart/bar-chart.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, OnInit, ViewChild } from '@angular/core';
2-
import { ChartConfiguration, ChartData, ChartType } from 'chart.js';
2+
import { ChartConfiguration, ChartData, ChartEvent, ChartType } from 'chart.js';
33
import { BaseChartDirective } from 'ng2-charts';
44

55
import DataLabelsPlugin from 'chartjs-plugin-datalabels';
@@ -45,11 +45,11 @@ export class BarChartComponent {
4545
};
4646

4747
// events
48-
public chartClicked({ event, active }: { event: MouseEvent, active: {}[] }): void {
48+
public chartClicked({ event, active }: { event?: ChartEvent, active?: {}[] }): void {
4949
console.log(event, active);
5050
}
5151

52-
public chartHovered({ event, active }: { event: MouseEvent, active: {}[] }): void {
52+
public chartHovered({ event, active }: { event?: ChartEvent, active?: {}[] }): void {
5353
console.log(event, active);
5454
}
5555

0 commit comments

Comments
 (0)